aacgfe_show_error('id is required for integrating admin notice.'); return; } if ( array_key_exists( $notice['id'], $this->messages ) ) { return; // Notice already registered. } if ( isset($notice['review']) && true !== (bool)$notice['review'] && ( !isset($notice['message']) || empty($notice['message']) )) { $this->aacgfe_show_error('message can not be null. You must provide some text for message field'); return; } $message = ( ! empty( $notice['message'] ) ) ? wp_kses_post( $notice['message'] ) : null; $type = (isset($notice['type']) && !empty($notice['type'])) ? 'notice-' . sanitize_text_field( $notice['type'] ) : 'notice-success' ; $class = (isset($notice['class']) && !empty($notice['class'])) ? sanitize_text_field( $notice['class'] ): ''; $review = (bool)(isset($notice['review'] ) && !empty( $notice['review'] ) ) ? sanitize_text_field( $notice['review'] ) : false; $slug = (isset($notice['slug']) && !empty($notice['slug'])) ? sanitize_text_field( $notice['slug'] ): '' ; $plugin_name = (isset($notice['plugin_name']) && !empty($notice['plugin_name'])) ? sanitize_text_field( $notice['plugin_name'] ) : '' ; $review_url = (isset($notice['review_url']) && !empty($notice['review_url'])) ? esc_url( $notice['review_url'] ) : '' ; $review_interval = (isset($notice['review_interval']) && !empty($notice['review_interval'])) ? sanitize_text_field( $notice['review_interval'] ) : '3' ; if( $review === true && ( empty( $slug ) || empty( $plugin_name ) || empty( $review_url ) )){ $this->aacgfe_show_error( 'slug / plugin_name / review_url can not be empty if admin notice is set to review' ); return; } $this->messages[$notice['id']] = array( 'message' => $message, 'type' => $type, 'class' => $class, 'review' => $review, 'slug' => $slug, 'plugin_name' => $plugin_name, 'review_url' => $review_url, 'review_interval' => $review_interval ); add_action('admin_notices', array($this, 'aacgfe_show_notice')); add_action( 'admin_enqueue_scripts', array($this, 'aacgfe_load_script' ) ); add_action('wp_ajax_aacgfe_admin_notice', array($this, 'aacgfe_admin_notice_dismiss')); add_action('wp_ajax_aacgfe_admin_review_notice_dismiss', array($this, 'aacgfe_admin_review_notice_dismiss')); } /** * Load script to dismiss notices. * * @return void */ public function aacgfe_load_script() { wp_register_style( 'aacgfe-review-css', AACGFE_URL . 'assets/css/aacgfe-admin-notice.css', array(), AACGFE_VERSION, 'all' ); wp_register_script( 'aacfge-admin-notices-js', AACGFE_URL . 'admin/feedback/js/admin-review.js', array( 'jquery' ), AACGFE_VERSION, true ); if (!empty($this->messages)) { foreach ($this->messages as $id => $message) { $nonce = $message['review'] ? wp_create_nonce($id . '_review_nonce') : wp_create_nonce($id . '_notice_nonce'); $localized_data = array( 'id' => $id, 'ajax_url' => admin_url('admin-ajax.php'), 'wp_nonce' => $nonce, 'plugin_slug' => $message['slug'] ?? $id, 'review' => $message['review'], 'ajax_callback' => $message['review'] ? 'aacgfe_admin_review_notice_dismiss' : 'aacgfe_admin_notice_dismiss', ); $js_safe_id = str_replace('-', '_', $id); wp_localize_script('aacfge-admin-notices-js', 'AACGFENoticeData_' . $js_safe_id, $localized_data); } } } /** * Create simple admin notice */ public function aacgfe_show_notice() { if (count($this->messages) > 0) { foreach ($this->messages as $id => $message) { if( true === (bool) $message['review'] ){ $this->aacgfe_admin_notice_for_review( $id, $message); }else{ $this->aacgfe_simple_notice($id, $message ); } } } } /** * Due to the nature of private function. This must not be called directly * Create simple text/html admin notice and initialize required JS * @param array $message This is an array of message object */ private function aacgfe_simple_notice( $id, $message ) { if ( get_option( $id . '_remove_notice' ) ) { return; } $classes = 'notice ' . trim( $message['type'] ) . ' is-dismissible ' . trim( $message['class'] ); $nonce = wp_create_nonce( $id . '_notice_nonce' ); $ajax_url = admin_url( 'admin-ajax.php' ); // Build logo HTML $img_html = ''; if ( ! empty( $message['logo'] ) ) { $url = isset( $message['url'] ) ? esc_url( $message['url'] ) : '#'; $img_path = esc_url( $message['logo'] ); $img_html = sprintf( '
', $url, $img_path ); } // Only kses filter, no pre-escaping $message_text = wp_kses_post( $message['message'] ); $script = isset( $message['script'] ) ? wp_kses_post( $message['script'] ) : ''; printf( '
%5$s

%6$s

%7$s', esc_attr( $id ), esc_attr( $classes ), esc_url( $ajax_url ), esc_attr( $nonce ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped $img_html, // already safely built HTML // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped $message_text, // already wp_kses_post filtered // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped $script // already wp_kses_post filtered ); } /** * This function decides if its good to show the review notice or not * Review notice will only be displayed if $slug_activation_time is greater or equals to the 3 days */ private function aacgfe_admin_notice_for_review( $id, $messageObj ){ // Everyone should not be able see the review message if( !current_user_can( 'update_plugins' ) ){ return; } $slug = $messageObj['slug']; $days = $messageObj['review_interval']; if(get_option( 'aacgfe-installDate' )){ // get installation dates and rated settings //$installation_date =date( 'Y-m-d h:i:s', get_option( 'ect-installDate' )); $installation_date =gmdate( 'Y-m-d h:i:s', strtotime(get_option( 'aacgfe-installDate' )) ); }else{ $this->aacgfe_show_error('Review notice can not be integrated. aacgfe-installDate option is not set for the plugin'); return; } $alreadyRated =get_option( 'aacgfe-ratingDiv' )!=false?get_option( 'aacgfe-ratingDiv'):"no"; // check user already rated if( $alreadyRated ==="yes") { return; } // grab plugin installation date and compare it with current date $display_date = gmdate( 'Y-m-d h:i:s' ); $install_date= new DateTime( $installation_date ); $current_date = new DateTime( $display_date ); $difference = $install_date->diff($current_date); $diff_days= $difference->days; // check if installation days is greator then week if (isset($diff_days) && $diff_days>= $days ) { wp_enqueue_style( 'aacgfe-review-css'); wp_enqueue_script( 'aacfge-admin-notices-js' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $this->aacgfe_create_notice_content( $id, $messageObj ); } } /** * Generate review notice HTMl with all required css & js * * @param array $messageObj array of a message object **/ function aacgfe_create_notice_content( $id, $messageObj ){ $ajax_url=admin_url( 'admin-ajax.php' ); $ajax_callback = 'aacgfe_admin_review_notice_dismiss'; $wrap_cls="notice notice-info is-dismissible"; $slug = $messageObj['slug']; $plugin_name= $messageObj['plugin_name']; $like_it_text=__( 'Rate Now! ★★★★★', 'ai-auto-content-generator-for-elementor' ); $already_rated_text=__( ' Already Reviewed', 'ai-auto-content-generator-for-elementor' ); $not_like_it_text=__( 'Not Interested', 'ai-auto-content-generator-for-elementor' ); $plugin_link= $messageObj['review_url'] ; $review_nonce = wp_create_nonce( $id . '_review_nonce' ); $message = sprintf( 'Thanks for using %1$s - WordPress plugin. We hope you liked it !
Please give us a quick rating, it works as a boost for us to keep working on more Cool Plugins!
', esc_html( $plugin_name ) ); $html='
'; $html .='
%2$s
'; return sprintf( $html, esc_attr( $wrap_cls ), wp_kses_post( $message ), esc_url( $plugin_link ), esc_attr( $like_it_text ), esc_html( $already_rated_text ), esc_url( $ajax_url ), esc_attr( $ajax_callback ), esc_html( $not_like_it_text ), esc_attr( $slug ), esc_attr( $review_nonce ), esc_attr( $id ) ); } /** * This function will dismiss the review notice. * This is called by a wordpress ajax hook */ public function aacgfe_admin_review_notice_dismiss(){ if ( ! current_user_can( 'update_plugins' ) ) { wp_send_json_error( 'Unauthorized.', 403 ); } $id = isset( $_REQUEST['id'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['id'] ) ) : ''; $nonce_key = $id . '_review_nonce' ; if ( ! check_ajax_referer($nonce_key,'_nonce', false ) ) { wp_send_json_error( array( 'message' => 'nonce verification failed!' ), 403 ); }else{ update_option( 'aacgfe-ratingDiv','yes' ); wp_send_json_success( array( 'success' => true ) ); } } /************************************************************ * This function will dismiss the text/html admin notice * * This is called by a wordpress ajax hook * ************************************************************/ public function aacgfe_admin_notice_dismiss() { if ( ! current_user_can( 'update_plugins' ) ) { wp_send_json_error( 'Unauthorized.', 403 ); } $id = isset( $_REQUEST['id'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['id'] ) ) : ''; $wp_nonce = $id . '_notice_nonce'; if ( ! check_ajax_referer($wp_nonce,'_nonce', false ) ) { wp_send_json_error( array( 'message' => __( 'Nonce verification failed.', 'ai-auto-content-generator-for-elementor' ), ), 403 ); }else{ $us=update_option( $id . '_remove_notice','yes' ); wp_send_json_success( array( 'message' => __( 'Admin message removed.', 'ai-auto-content-generator-for-elementor' ), ) ); } } /************************************************************** * This function is used by the class for displaying error * * in case of wrong implementation of the class. * **************************************************************/ private function aacgfe_show_error( $error_text ) { $er = "
"; $er .= "Error: ".$error_text; $er .= "
"; echo wp_kses_post($er); } } // end of main class aacgfe_admin_notices; endif; /******************************************************************************** * A global function to create admin notice/review box using the above class. * * This function makes it easy to use above class * ********************************************************************************/ function aacgfe_create_admin_notice($notice) { // Do not initialize anything if it's not wordpress admin dashboard if (!is_admin()) { return; } $main_class = aacgfe_admin_notices::aacgfe_create_notice(); $main_class->aacgfe_add_message($notice); return $main_class; }