query("UPDATE `mesbr_coupons` SET inco_id = $inco_id WHERE code = $code"); ldc_web_mesbr_get_product_info($code, true); } $coupons = $wpdb->get_results("SELECT * FROM mesbr_coupons"); $page = '
'; $page .= '

LDC Web - MesBR Admin

'; // Tableau $page .= ""; $page .= ""; $page .= ""; $page .= ""; $page .= ""; $page .= ""; $page .= ""; $page .= ""; foreach ($coupons as $coupon) { $page .= ""; $page .= ""; $page .= ""; $page .= ""; $page .= ""; $page .= ""; $page .= ""; } $page .= "
CodeNomValeurImageID Inco
$coupon->code$coupon->name$coupon->value €"; $page .= "
"; $page .= ""; $page .= ""; $page .= ""; $page .= "
"; $page .= "
"; $page .= '
'; echo $page; } function ldc_web_mesbr_get_product_info($coupon_code, $force_update = false) { global $wpdb; $cache = false; if (class_exists('Memcached')) { $cache = new \Memcached(); $cache->addServer("127.0.0.1", 11211); if (!$force_update) { $fromCache = $cache->get('mesbr_product_info_coupon_' . $coupon_code); if ($fromCache) { return $fromCache; } } } if ($cache && $force_update) { $cache->delete('mesbr_product_info_coupon_' . $coupon_code); } $coupon = $wpdb->get_row("SELECT * FROM mesbr_coupons WHERE code = $coupon_code"); if (is_null($coupon)) return false; if (is_null($coupon->inco_id)) return false; $post_id = $wpdb->get_var("SELECT post_id FROM " . $wpdb->prefix . "postmeta WHERE meta_key = 'reference_produit' AND meta_value = " . $coupon->inco_id); if (is_null($post_id)) return false; $inco_img = $wpdb->get_var("SELECT guid FROM " . $wpdb->prefix . "posts WHERE id = (SELECT meta_value FROM " . $wpdb->prefix . "postmeta WHERE meta_key = 'image' AND post_id = $post_id)"); if (is_null($inco_img)) return false; $result = ['post_id' => $post_id, 'inco_img' => $inco_img]; if ($cache) { $cache->set('mesbr_product_info_coupon_' . $coupon_code, $result, 3600); // Cache pour 1h } return $result; } add_action('ldc_web_mesbr_get_product_info', 'ldc_web_mesbr_get_product_info'); /** * Fonction à appeler dans un CRON */ function update() { global $wpdb; $wpdb->query("CREATE TABLE IF NOT EXISTS `mesbr_coupons` ( `code` VARCHAR(50) NOT NULL DEFAULT '0', `name` VARCHAR(255) NOT NULL DEFAULT '0', `value` FLOAT NOT NULL DEFAULT '0', `image` VARCHAR(255) NOT NULL DEFAULT '0', `inco_id` BIGINT(20) NULL DEFAULT NULL, PRIMARY KEY (`code`) ) COLLATE='utf8_general_ci' ENGINE=InnoDB;"); if (class_exists('Memcached')) { $cache = new \Memcached(); $cache->addServer("127.0.0.1", 11211); $cache->flush(); } $mur = getApiMESBR()->getMur(MESBR_REF_CAMP); if ($mur->status === 'OK') { $receivedCodes = array_map(function ($o) { return "$o->code"; }, $mur->coupons); $receivedCodes = implode("', '", $receivedCodes); $wpdb->query("DELETE FROM `mesbr_coupons` WHERE code NOT IN('$receivedCodes')"); foreach ($mur->coupons as $coupon) { $dbCoupon = $wpdb->get_row("SELECT * FROM mesbr_coupons WHERE code = " . $coupon->code); if (is_null($dbCoupon)) { $wpdb->insert( 'mesbr_coupons', array( 'code' => $coupon->code, 'name' => $coupon->name, 'value' => floatval($coupon->value), 'image' => $coupon->desktop_image_available ), array( '%s', '%s', '%f', '%s', ) ); } else { $wpdb->update( 'mesbr_coupons', array( 'name' => $coupon->name, 'value' => floatval($coupon->value), 'image' => $coupon->desktop_image_available ), array( 'code' => $coupon->code, ) ); } } } }