Skip to content
Snippets Groups Projects
Select Git revision
  • b6b23a5ea2109851c1915056379bd0c12ac2820e
  • main default protected
  • revert-ffead0bd
  • feature/init
  • 1.1.8
  • 1.1.7
  • 1.1.6
  • 1.1.5
  • 1.1.4
  • 1.1.3
  • 1.1.2
  • 1.1.1
  • 1.1.0
  • 1.0.2
  • 1.0.2-beta3
  • 1.0.2-beta2
  • 1.0.2-beta1
  • 1.0.1
  • 1.0.1-beta4
  • 1.0.1-beta3
  • 1.0.1-beta2
  • 1.0.1-beta1
  • 1.0.0
  • 1.0.0-beta6
24 results

Reminder.php

Blame
  • AjaxHandler.php 3.02 KiB
    <?php
    
    namespace WPDesk\Notice;
    
    use WPDesk\PluginBuilder\Plugin\HookablePluginDependant;
    use WPDesk\PluginBuilder\Plugin\PluginAccess;
    
    /**
     * Class AjaxHandler
     *
     * AjaxHandler for dismissible notices.
     *
     * @package WPDesk\Notice
     */
    class AjaxHandler implements HookablePluginDependant {
    
        use PluginAccess;
    
        const POST_FIELD_NOTICE_NAME = 'notice_name';
        const POST_FIELD_SOURCE = 'source';
        const POST_FIELD_SECURITY = 'security';
    
        const SCRIPTS_VERSION = '4';
        const SCRIPT_HANDLE = 'wpdesk_notice';
    
        /**
         * @var string
         */
        private $assetsURL;
    
        /**
         * AjaxHandler constructor.
         *
         * @param string|null $assetsURL Assets URL.
         */
        public function __construct( $assetsURL = null ) {
            $this->assetsURL = $assetsURL;
        }
    
        /**
         * Hooks.
         */
        public function hooks() {
            if ( $this->assetsURL ) {
                add_action( 'admin_enqueue_scripts', [ $this, 'enqueueAdminScripts' ] );
            } else {
                add_action( 'admin_head', [ $this, 'addScriptToAdminHead' ] );
            }
            add_action( 'wp_ajax_wpdesk_notice_dismiss', [ $this, 'processAjaxNoticeDismiss' ] );
        }
    
        /**
         * Enqueue admin scripts.
         */
        public function enqueueAdminScripts() {
            wp_register_script(
                self::SCRIPT_HANDLE,
                trailingslashit( $this->assetsURL ) . 'js/notice.js',
                [ 'jquery' ],
                self::SCRIPTS_VERSION
            );
            wp_enqueue_script( self::SCRIPT_HANDLE );
        }
    
        /**
         * Add Java Script to admin header.
         */
        public function addScriptToAdminHead() {
            include __DIR__ . '/views/admin-head-js.php';
        }
    
        /**
         * Process AJAX notice dismiss.
         *
         * Updates corresponded WordPress option and fires wpdesk_notice_dismissed_notice action with notice name.
         */
        public function processAjaxNoticeDismiss() {
            if ( isset( $_POST[ self::POST_FIELD_NOTICE_NAME ] ) ) {
                $noticeName = sanitize_text_field( $_POST[ self::POST_FIELD_NOTICE_NAME ] );
    
                $option_name = PermanentDismissibleNotice::OPTION_NAME_PREFIX . $noticeName;
                ajax_check_referer( $option_name, self::POST_FIELD_SECURITY );
    
                if ( ! current_user_can( 'edit_posts' ) ) {
                    wp_send_json_error();
                }
    
                if ( isset( $_POST[ self::POST_FIELD_SOURCE ] ) ) {
                    $source = sanitize_text_field( $_POST[ self::POST_FIELD_SOURCE ] );
                } else {
                    $source = null;
                }
    
                $option_name = PermanentDismissibleNotice::OPTION_NAME_PREFIX . $noticeName;
    
                update_option(
                    $option_name,
                    PermanentDismissibleNotice::OPTION_VALUE_DISMISSED
                );
                do_action( 'wpdesk_notice_dismissed_notice', $noticeName, $source );
                if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
                    wp_send_json_success();
                }
            }
            if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
                wp_send_json_error();
            }
        }
    
    }