* @return void */ public function __construct() { // Create the page object parent::__construct( __( 'Installation wizard', 'affilizz' ), 'affilizz-wizard', 'admin-generic', AFFILIZZ_DIRECTORY . 'templates/administration/page/wizard', true ); // Hook to AJAX actions add_action( 'wp_ajax_affilizz_check_api_key', array( $this, 'check_api_key' ) ); add_action( 'wp_ajax_affilizz_get_entities', array( $this, 'get_entities' ) ); add_action( 'wp_ajax_affilizz_get_media', array( $this, 'get_media' ) ); add_action( 'wp_ajax_affilizz_get_channels', array( $this, 'get_channels' ) ); add_action( 'wp_ajax_affilizz_save_params', array( $this, 'save_params' ) ); // Common object $this->api = new \Affilizz\API(); } /** * Enqueues styles and scripts needed for the wizard page. * @author Affilizz * @return void */ public function display() { wp_register_style( 'affilizz-wizard', AFFILIZZ_URL . 'assets/dist/css/admin.css', array(), AFFILIZZ_VERSION, true ); wp_register_script( 'affilizz-wizard', AFFILIZZ_URL . 'assets/dist/js/wizard.js', array( 'jquery' ), AFFILIZZ_VERSION, true ); wp_enqueue_style( 'affilizz-wizard' ); wp_enqueue_script( 'affilizz-wizard' ); parent::display(); } /** * Checks if an API key provided through AJAX is valid. * @author Affilizz * @return void */ public function check_api_key() { if ( check_ajax_referer( 'affilizz-api-key', '_wpnonce_key' ) === false ) { return; } $api_key = sanitize_text_field( wp_unslash( $_POST['key'] ?? '' ) ); if ( ! empty( $api_key ) ) { $valid = $this->api->verify_key( $api_key ); if ( true === $valid ) { update_option( 'affilizz_api_key', $api_key ); wp_send_json( array( 'status' => 'success' ) ); } elseif ( is_wp_error( $valid ) ) { $this->send_api_error( $valid, 'key' ); } else { wp_send_json( array( 'status' => 'error', 'message' => __( 'This API key is invalid', 'affilizz' ), ) ); } } else { wp_send_json( array( 'status' => 'error', 'message' => __( 'Please provide an API key, the field seems empty.', 'affilizz' ), ) ); } exit; } /** * Retrieve entities of this account. * @author Affilizz * @return void */ public function get_entities() { $entities = $this->api->get_entities( get_option( 'affilizz_api_key' ) ); if ( is_wp_error( $entities ) ) { $this->send_api_error( $entities, 'key' ); } if ( $entities ) { wp_send_json( $entities ); } wp_send_json( array( 'status' => 'error', 'message' => __( 'You don\'t have any organization on your account. Please create your first organization before installing this plugin.', 'affilizz' ), ) ); } /** * Retrieves media for a specific entity. * @author Affilizz * @return void */ public function get_media() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Authenticated admin AJAX endpoint reads a selector value; installation save is nonce-protected. $organization = sanitize_text_field( wp_unslash( $_GET['organizationId'] ?? '' ) ); $organization = isset( $organization ) ? trim( $organization ) : null; if ( ! empty( $organization ) ) { $media = $this->api->get_media( get_option( 'affilizz_api_key' ), $organization ); if ( is_wp_error( $media ) ) { $this->send_api_error( $media, 'selection' ); } if ( $media ) { wp_send_json( $media ); } } wp_send_json( array( 'status' => 'error', 'message' => __( 'No media found for this organization. Please choose another organization or create your first media for this organization before installing this plugin.', 'affilizz' ), ) ); } /** * Gets the channels of given organization and media. * @author Affilizz * @return void */ public function get_channels() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Authenticated admin AJAX endpoint reads a selector value; installation save is nonce-protected. $media = sanitize_text_field( wp_unslash( $_GET['mediaId'] ?? '' ) ); $media = isset( $media ) ? trim( $media ) : null; if ( ! empty( $media ) ) { $channels = $this->api->get_channels( '', $media ); if ( is_wp_error( $channels ) ) { $this->send_api_error( $channels, 'selection' ); } if ( $channels ) { wp_send_json( $channels ); } } wp_send_json( array( 'status' => 'error', 'message' => __( 'No channels found for this media. Please choose another media or create your first channel for this media before installing this plugin.', 'affilizz' ), ) ); } /** * Sends the wizard why a call to the User API failed, so that it tells a refused API key from Affilizz being unavailable. * @author Affilizz * @param $error \WP_Error The failure returned by \Affilizz\API. * @param $refused String What a 401/403 refuses: the "key", or the "selection" (organization or media). * @return void */ private function send_api_error( \WP_Error $error, string $refused ) { wp_send_json( array( 'status' => 'error', 'code' => $error->get_error_code(), 'message' => self::get_api_error_message( $error, $refused ), ) ); } /** * Words why a call to the User API failed, for the wizard and for the API key of the settings. * \Affilizz\API words a 401/403 and a 404 for the editor's publications: here a refusal is about the API key (users/me), * or about the organization or media asked for (expands answers 403 for one deleted or out of reach of the key). * Affilizz being unavailable or unreachable keeps the words of \Affilizz\API. * @author Affilizz * @since 1.19.1 * @param $error \WP_Error The failure returned by \Affilizz\API. * @param $refused String What a 401/403 refuses: the "key", or the "selection" (organization or media). * @return String The message. */ public static function get_api_error_message( \WP_Error $error, string $refused ) { $data = $error->get_error_data(); $status = (int) ( $data['status'] ?? 0 ); if ( 'affilizz_api_forbidden' === $error->get_error_code() && 'key' === $refused ) { $message = sprintf( /* translators: %d: HTTP status code returned by the Affilizz API (401 or 403). */ __( 'Affilizz refused this API key (error %d). Check that it is the WordPress API key of your Affilizz user account, not an access token; if it is, contact Affilizz support.', 'affilizz' ), $status ); } elseif ( 'affilizz_api_forbidden' === $error->get_error_code() ) { $message = sprintf( /* translators: %d: HTTP status code returned by the Affilizz API (401 or 403). */ __( 'Affilizz refused access to this organization or media (error %d): it may have been deleted, or your API key may no longer give access to it.', 'affilizz' ), $status ); } elseif ( 'affilizz_api_not_found' === $error->get_error_code() ) { $message = sprintf( /* translators: %d: HTTP status code returned by the Affilizz API. */ __( 'Affilizz could not process the request (error %d). If the problem persists, contact Affilizz support.', 'affilizz' ), $status ); } else { $message = $error->get_error_message(); } return $message; } /** * Saves the information at the end of the wizard process. * @author Affilizz * @return void */ public function save_params() { // Check the nonce before we save data if ( check_ajax_referer( 'affilizz-save-wizard-data' ) === false ) { return; } $organization = trim( sanitize_text_field( wp_unslash( $_POST['organization'] ?? '' ) ) ); $media = trim( sanitize_text_field( wp_unslash( $_POST['media'] ?? '' ) ) ); $channel = trim( sanitize_text_field( wp_unslash( $_POST['channel'] ?? '' ) ) ); $organization_label = trim( sanitize_text_field( wp_unslash( $_POST['organization-label'] ?? '' ) ) ); $media_label = trim( sanitize_text_field( wp_unslash( $_POST['media-label'] ?? '' ) ) ); $channel_label = trim( sanitize_text_field( wp_unslash( $_POST['channel-label'] ?? '' ) ) ); if ( ! empty( $organization ) && ! empty( $media ) && ! empty( $channel ) ) { update_option( 'affilizz_organization', $organization ); update_option( 'affilizz_media', $media ); update_option( 'affilizz_channel', $channel ); update_option( 'affilizz_organization_label', $organization_label, false ); update_option( 'affilizz_media_label', $media_label, false ); update_option( 'affilizz_channel_label', $channel_label, false ); // The wizard has just listed them: no longer warn that they are missing \Affilizz\Util\Cron::forget_missing_media(); wp_send_json( array( 'status' => 'success' ) ); } wp_send_json( array( 'status' => 'error', 'message' => __( 'You must pick an organization, a media and a channel to continue', 'affilizz' ), ), 400 ); } }