*/ class AIPERS_AIPersonalize360_Admin { /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $plugin_name The name of this plugin. * @param string $version The version of this plugin. */ public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; } /** * Register the stylesheets for the admin area. * * @since 1.0.0 */ public function aipers_enqueue_styles() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in AIPersonalize360_Loader as all of the hooks are defined * in that particular class. * * The AIPersonalize360_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/puredevs-aipersonalize360-admin.css', array(), $this->version, 'all' ); } /** * Register the JavaScript for the admin area. * * @since 1.0.0 */ public function aipers_enqueue_scripts() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in AIPersonalize360_Loader as all of the hooks are defined * in that particular class. * * The AIPersonalize360_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/puredevs-aipersonalize360-admin.js', array( 'jquery' ), $this->version, false ); } /** * Add plugin settings page for the admin area. * * @since 1.0.0 */ public function aipers_settings_page() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in Puredevs_Ai_Recommender_With_Amazon_Personalize as all of the hooks are defined * in that particular class. * * The Puredevs_Ai_Recommender_With_Amazon_Personalize will then create the relationship * between the defined hooks and the functions defined in this * class. */ add_menu_page( esc_html__('AIPersonalize360', 'aipersonalize360'), esc_html__('AIPersonalize360', 'aipersonalize360'), 'manage_options', $this->plugin_name.'-settings', array($this,'aipers_admin_page_html'), 'dashicons-admin-settings', 50 ); } /** * Create admin settings page. * * @since 1.0.0 */ public function aipers_admin_page_html(){ if ( ! current_user_can( 'manage_options' ) ) { return; } // show message when updated // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- settings-updated is set by WP core options.php which already verifies the nonce. if ( isset( $_GET['settings-updated'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended add_settings_error( 'aipers_messages', 'aipers_message', esc_html__( 'Settings Saved', 'aipersonalize360' ), 'success' ); } // show error/update messages settings_errors( 'aipers_message' ); ?>
' . esc_html__( 'AI Recommendations General Settings', 'aipersonalize360' ) . ''; } /** * Generate option page settings sections and fields. * * @since 1.0.0 */ public function aipers_settings_fields(){ add_settings_section( $this->plugin_name.'_general_setting_section', // $id - String for use in the 'id' attribute of tags. esc_html__( 'General Settings', 'aipersonalize360' ), // $title - Title of the section. array($this, 'aipers_general_settings_section_info'), // Function that fills the section with the desired content. The function should echo its output. $this->plugin_name.'-general-setting' // $page - The type of settings page on which to show the section (general, reading, writing, media etc.) ); $ai_engine = array( '' => esc_html__( 'Select one', 'aipersonalize360' ), 'recombee' => esc_html__( 'Recombee', 'aipersonalize360' ), //'amazon-personalized' => esc_html__( 'Amazon Personalized', 'aipersonalize360' ), ); $aipers_ai_engine = 'aipers_ai_engine'; add_settings_field( //You MUST register any options you use with add_settings_field() or they won't be saved and updated automatically. $aipers_ai_engine, // $id - String for use in the 'id' attribute of tags. esc_html__( 'AI Engine', 'aipersonalize360' ), // Title of the field. array($this, 'aipers_create_select_field'), //callback function for text input $this->plugin_name.'-general-setting', //settings page on which to show the field $this->plugin_name.'_general_setting_section',// The section of the settings page in which to show the box array( // The array of arguments to pass to the callback. 'type' => 'select', "id" => $aipers_ai_engine, "options" => $ai_engine, 'desc' => esc_html__( 'Select AI Engine. If not select any AI engine, then it will display personalized product recommendations on your site based on ratings, total sales, and on-sale items.', 'aipersonalize360' ), ) ); $aipers_recombee_db_name = 'aipers_recombee_db_name'; add_settings_field( //You MUST register any options you use with add_settings_field() or they won't be saved and updated automatically. $aipers_recombee_db_name, // $id - String for use in the 'id' attribute of tags. esc_html__( 'Database Name', 'aipersonalize360' ), // Title of the field. array($this, 'aipers_create_input_field'), //callback function for text input $this->plugin_name.'-general-setting', //settings page on which to show the field $this->plugin_name.'_general_setting_section',// The section of the settings page in which to show the box array( // The array of arguments to pass to the callback. 'type' => 'text', "id" => $aipers_recombee_db_name, "class" => 'recombee pairwap-common', 'desc' => __( 'Enter your production or development database here. You can get database name, private token and region from here. All the required information you will find from your Recombee Dashboard Database Settings section.', 'aipersonalize360' ), ) ); $aipers_recombee_private_token = 'aipers_recombee_private_token'; add_settings_field( //You MUST register any options you use with add_settings_field() or they won't be saved and updated automatically. $aipers_recombee_private_token, // $id - String for use in the 'id' attribute of tags. esc_html__( 'Private Token', 'aipersonalize360' ), // Title of the field. array($this, 'aipers_create_input_field'), //callback function for text input $this->plugin_name.'-general-setting', //settings page on which to show the field $this->plugin_name.'_general_setting_section',// The section of the settings page in which to show the box array( // The array of arguments to pass to the callback. 'type' => 'text', "id" => $aipers_recombee_private_token, "class" => 'recombee pairwap-common', 'desc' => __( 'API ID for usage at server side. You can get database name, private token and region from here. All the required information you will find from your Recombee Dashboard Database Settings section.', 'aipersonalize360' ), ) ); $aipers_recombee_region = 'aipers_recombee_region'; add_settings_field( //You MUST register any options you use with add_settings_field() or they won't be saved and updated automatically. $aipers_recombee_region, // $id - String for use in the 'id' attribute of tags. esc_html__( 'Region', 'aipersonalize360' ), // Title of the field. array($this, 'aipers_create_input_field'), //callback function for text input $this->plugin_name.'-general-setting', //settings page on which to show the field $this->plugin_name.'_general_setting_section',// The section of the settings page in which to show the box array( // The array of arguments to pass to the callback. 'type' => 'text', "id" => $aipers_recombee_region, "class" => 'recombee pairwap-common', 'desc' => __( 'Server region. You can get database name, private token and region from here. All the required information you will find from your Recombee Dashboard Database Settings section.', 'aipersonalize360' ), ) ); $aipers_recommendations_title = 'aipers_recommendations_title'; add_settings_field( //You MUST register any options you use with add_settings_field() or they won't be saved and updated automatically. $aipers_recommendations_title, // $id - String for use in the 'id' attribute of tags. esc_html__( 'Title', 'aipersonalize360' ), // Title of the field. array($this, 'aipers_create_input_field'), //callback function for text input $this->plugin_name.'-general-setting', //settings page on which to show the field $this->plugin_name.'_general_setting_section',// The section of the settings page in which to show the box array( // The array of arguments to pass to the callback. 'type' => 'text', "id" => $aipers_recommendations_title, 'desc' => esc_html__( 'Product Recommendations Block Heading', 'aipersonalize360' ), ) ); $aipers_recommendations_description = 'aipers_recommendations_description'; add_settings_field( //You MUST register any options you use with add_settings_field() or they won't be saved and updated automatically. $aipers_recommendations_description, // $id - String for use in the 'id' attribute of tags. esc_html__( 'Description', 'aipersonalize360' ), // Description of the field. array($this, 'aipers_create_textarea_field'), //callback function for textarea input $this->plugin_name.'-general-setting', //settings page on which to show the field $this->plugin_name.'_general_setting_section',// The section of the settings page in which to show the box array( // The array of arguments to pass to the callback. "id" => $aipers_recommendations_description, 'class' => 'widefat', 'desc' => esc_html__( 'Show Description Below Heading Block', 'aipersonalize360' ), ) ); $aipers_show_num_of_items = 'aipers_show_num_of_items'; add_settings_field( //You MUST register any options you use with add_settings_field() or they won't be saved and updated automatically. $aipers_show_num_of_items, // $id - String for use in the 'id' attribute of tags. esc_html__( 'Show Number of Items', 'aipersonalize360' ), // Title of the field. array($this, 'aipers_create_input_field'), //callback function for number input $this->plugin_name.'-general-setting', //settings page on which to show the field $this->plugin_name.'_general_setting_section',// The section of the settings page in which to show the box array( // The array of arguments to pass to the callback. 'type' => 'number', "id" => $aipers_show_num_of_items, 'desc' => esc_html__( 'How many items you want to show in recommendations block', 'aipersonalize360' ), //'class' => 'widefat', 'class' => '', ) ); $aipers_infobox_field = 'aipers_infobox_field'; add_settings_field( //You MUST register any options you use with add_settings_field() or they won't be saved and updated automatically. $aipers_infobox_field, // $id - String for use in the 'id' attribute of tags. '', // Title of the field. array($this, 'aipers_create_infobox_field'), //callback function for info $this->plugin_name.'-general-setting', //settings page on which to show the field $this->plugin_name.'_general_setting_section',// The section of the settings page in which to show the box array( // The array of arguments to pass to the callback. 'type' => 'infobox', "id" => $aipers_infobox_field, "class" => 'aipers_infobox_field', ) ); register_setting( $this->plugin_name.'-general-setting', $aipers_ai_engine, array('sanitize_callback' => array( $this, 'aipers_sanitize_text_input_callback')) ); register_setting( $this->plugin_name.'-general-setting', $aipers_recombee_db_name, array('sanitize_callback' => array( $this, 'aipers_sanitize_text_input_callback')) ); register_setting( $this->plugin_name.'-general-setting', $aipers_recombee_private_token, array('sanitize_callback' => array( $this, 'aipers_sanitize_text_input_callback')) ); register_setting( $this->plugin_name.'-general-setting', $aipers_recombee_region, array('sanitize_callback' => array( $this, 'aipers_sanitize_text_input_callback')) ); register_setting( $this->plugin_name.'-general-setting', $aipers_recommendations_title, array('sanitize_callback' => array( $this, 'aipers_sanitize_text_input_callback')) ); register_setting( $this->plugin_name.'-general-setting', $aipers_recommendations_description, array('sanitize_callback' => array( $this, 'aipers_sanitize_textarea_field_callback')) ); register_setting( $this->plugin_name.'-general-setting', $aipers_show_num_of_items, array('sanitize_callback' => array( $this, 'aipers_sanitize_text_input_callback')) ); } /** * Sanitize text input. * * @since 1.0.0 */ public function aipers_sanitize_text_input_callback($input) { return sanitize_text_field($input); } /** * Sanitize textarea field. * * @since 1.0.0 */ public function aipers_sanitize_textarea_field_callback($input) { return sanitize_textarea_field($input); } /** * Function that fills the field with the desired inputs as part of the larger form. Name and id of the input should match the $id given to this function. The function should echo its output. * * @since 1.0.0 */ public function aipers_create_input_field($args) { if(isset($args["default"])) { $default = $args["default"]; }else{ $default = false; } echo ''; if(isset($args["desc"]) && !empty($args["desc"])) { echo "".wp_kses_post($args["desc"])."
"; } } /** * Function that fills the field with the desired inputs as part of the larger form. Name and id of the input should match the $id given to this function. The function should echo its output. * * @since 1.0.0 */ public function aipers_create_textarea_field($args) { if(isset($args["default"])) { $default = $args["default"]; }else{ $default = false; } echo ''; if(isset($args["desc"]) && !empty($args["desc"])) { echo "".wp_kses_post($args["desc"])."
"; } } /** * Generates and displays a select field for a WordPress admin settings page. * * @param array $args { * Arguments to configure the select field. * * @type string $id The option name and HTML ID attribute for the select field. * @type array $options Associative array of option values and labels for the select field. * @type mixed $default (Optional) The default value for the select field if no option is set. * @type string $desc (Optional) Description displayed below the select field. * } * * Outputs the HTML for a select field, pre-selecting the saved or default value, * and optionally displays a description. Uses WordPress escaping functions for security. */ public function aipers_create_select_field($args){ if(isset($args["default"])) { $default = $args["default"]; }else{ $default = false; } $_options = ( $args['options'] ) ? $args['options'] : array(); $option_val = (get_option($args['id'])) ? get_option($args['id']) : $default; $html = ''; if(!empty($_options)){ $html .= ''; } if(isset($args["desc"]) && !empty($args["desc"])) { $html .= ''.esc_html($args["desc"]).'
'; } echo wp_kses_post($html); } /** * Function that fills the field with the desired inputs as part of the larger form. Name and id of the input should match the $id given to this function. The function should echo its output. * * @since 1.0.0 */ public function aipers_create_checkbox_field($args){ $value = (get_option($args['id'])) ? get_option($args['id']) : ''; $html = ''; $checked = ( isset( $value ) && $value == 'yes' ) ? 'checked="checked"' : ''; $html .= ''; if($args["desc"]) { $html .= ''.esc_html($args["desc"]).'
'; } echo wp_kses_post($html); } /** * Function kses allowed html tags. * * @since 1.0.0 */ public function aipers_kses_allowed_html_tags() { global $current_screen; if(!empty($current_screen)){ $current_page = get_current_screen()->base; if(isset($current_page) && $current_page == 'toplevel_page_aipersonalize360-settings'){ global $allowedposttags; $input_allowed_atts = array( 'class' => 1, 'type' => 1, 'id' => 1, 'style' => 1, 'value' => 1, 'name' => 1, 'width' => 1, 'height' => 1, 'title' => 1, 'checked' => 1, ); $select_allowed_atts = array( 'class' => 1, 'id' => 1, 'name' => 1, ); $select_option_allowed_atts = array( 'value' => 1, 'selected' => 1, ); $allowedposttags['input'] = $input_allowed_atts; $allowedposttags['select'] = $select_allowed_atts; $allowedposttags['option'] = $select_option_allowed_atts; } } } /** * Function that fills the field with the desired inputs as part of the larger form. Name and id of the input should match the $id given to this function. The function should echo its output. * * @since 1.0.0 */ public function aipers_create_infobox_field($args){ $html = '[aipers_product_recommendations]
'; $html .= 'Use the above shortcode to any posts, page or page template. You can pass Title and Description for this shortcode as a shortcode attribute like this [aipers_product_recommendations title="Write your title here" description="Write your description here"]
'; $html .= '[aipers_related_product_recommendations]
'; $html .= 'Use the above shortcode for the related products recommendations inside single product template. You can pass Title and Description for this shortcode as a shortcode attribute like this [aipers_related_product_recommendations title="Write your title here" description="Write your description here"]
'; echo wp_kses_post($html); } }