'; }); // Game link widget add_shortcode('allkeyshop_link_offers', function ($attr) { if (!isset($attr['game'])) return null; $gameId = intval($attr['game']); $affiliateKey = get_option('allkeyshop_widget_affiliate_key', null); $locale = get_locale(); if (!in_array($locale, self::$allowedLocales)) { $locale = self::$allowedLocales[0]; } $hrefUrl = AKS_AFFILIATE_PLUGIN_CLICK_URL . 'comparator/' . $gameId . '?locale=' . $locale . '&apiKey=' . $affiliateKey; $textLink = $attr['text']; if (empty($attr['text'])) { $textLink = 'Link to the game offers'; } return '' . $textLink . ''; }); } /** * Load the global JS and CSS files */ public static function loadDefaultAssets() { wp_enqueue_script('aks-affiliate-plugin', AKS_AFFILIATE_PLUGIN_URL_ROOT . 'public/build/index.js'); wp_enqueue_style('aks-affiliate-plugin-autocomplete', AKS_AFFILIATE_PLUGIN_URL_ROOT .'public/assets/css/plugin/autocomplete.css'); wp_enqueue_style('aks-affiliate-plugin-tabs-manager', AKS_AFFILIATE_PLUGIN_URL_ROOT .'public/assets/css/plugin/tabs-manager.css'); self::loadcss('main'); } /** * Load a CSS file * @param string $fileName the CSS filename */ public static function loadCss($fileName) { $fileName .= '.css'; $fileName = sanitize_file_name($fileName); $file = AKS_AFFILIATE_PLUGIN_URL_ROOT . 'public/assets/css/' . $fileName; $filePath = self::$assetDir . 'css/' . $fileName; if (file_exists($filePath)) { wp_enqueue_style('aks-affiliate-plugin-' . $fileName, $file); } } /** * Load a view with the CSS and JS * @param string $viewName the name of the view * @param array $viewParams the view parameters */ public static function loadView($viewName, $viewParams = []) { self::loadCss($viewName); // On créer les variables qui vont être utilisé dans la vue foreach ($viewParams as $key => $value) { ${$key} = $value; } require_once(self::$viewsDir . $viewName . '.php'); } /** * Check if the user has access to the plugin */ public static function hasPluginAccess() { return self::$apiKey !== null; } /** * Save the api key and the affiliate key * @param string $apiKey */ protected static function _saveApiKey($apiKey, $affiliateKey) { update_option('allkeyshop_widget_key', $apiKey); update_option('allkeyshop_widget_affiliate_key', $affiliateKey); self::$apiKey = $apiKey; } /** * Main page */ public static function mainPage() { $successApiKey = null; if (isset($_POST['apiKey'])) { $apiKey = sanitize_text_field($_POST['apiKey']); $success = self::_testApiKey($apiKey); } $data = [ 'successApiKey' => $successApiKey, 'apiKey' => self::$apiKey, 'affiliateUrl' => self::$affiliateUrl, 'affiliateStatsUrl' => self::$affiliateUrl . '/api/affiliate-stats', 'getApiKeyUrl' => self::$affiliateUrl . '/get-api-key', 'affiliateKey' => get_option('allkeyshop_widget_affiliate_key') ]; self::loadView('index', $data); } /** * Game Page creation */ public static function createGamePage() { $data = ['error' => null]; if(isset($_POST['game']) && is_numeric($_POST['game'])) { $gameId = intval($_POST['game']); $template = (!empty($_POST['template'])) ? intval($_POST['template']) : null; if ($template === 0) { $template = null; } $title = (!empty($_POST['title'])) ? sanitize_text_field($_POST['title']) : null; $idPage = self::_createGamePage($gameId, ['template' => $template, 'title' => $title], $errors); $data['error'] = $errors; if ($idPage !== null) { $data['url'] = get_edit_post_link($idPage); } } self::loadView('createGamePage', $data); } /** * Create a game page * @param int $gameId * @param array $options array for some specific options : * - template : template of the widget * - title: Change the title of the page * @param array $errors allow to retrieve the errors when the game page creation fail */ protected static function _createGamePage($gameId, $options = [], &$errors = null) { ob_start(); require_once(self::$viewsDir . 'gamePageContent.php'); $postContent = ob_get_contents(); ob_end_clean(); $gameTitle = VaksApi::getGamesTitle($gameId); $gamePageData = [ 'post_title' => $gameTitle, 'post_status' => 'publish', 'post_type' => 'page', 'post_content' => $postContent, 'post_author' => get_current_user_id() ]; foreach($options as $optionName => $optionValue) { self::_manageGamePageOption($gamePageData, $optionName, $optionValue); } $return = wp_insert_post($gamePageData, true); if (is_wp_error($return)) { $errors = $return; return null; } return $return; } /** * Manage the options for the game page creation */ protected static function _manageGamePageOption(&$currentPageData, $optionName, $optionValue) { if ($optionName == 'slug' && $optionValue !== null) { $currentPageData['post_name'] = $optionValue; } elseif ($optionName == 'title' && $optionValue !== null) { $currentPageData['post_title'] = $optionValue; } } public static function testApiKey() { if (!isset($_POST['apiKey'])) { wp_send_json_error(); } $apiKey = sanitize_text_field($_POST['apiKey']); $successs = self::_testApiKey($apiKey); if ($success) { wp_send_json_success(); } wp_send_json_error(); } protected static function _testApiKey($apiKey) { $data = AffiliateApi::checkApiKey($apiKey); if ($data['success'] === true) { self::_saveApiKey($apiKey, $data['data']['affiliateKey']); return true; } return false; } public static function linkAccountPage() { $data = [ 'success' => null, 'access' => self::hasPluginAccess(), 'affiliateKey' => get_option('allkeyshop_widget_affiliate_key'), 'affiliateUrl' => self::$affiliateUrl ]; if(isset($_POST['apiKey'])) { $apiKey = sanitize_text_field($_POST['apiKey']); $data['success'] = self::_testApiKey($apiKey); } $data['apiKey'] = self::$apiKey; self::loadView('apiKey', $data); } }