balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

class-redux-admin-notices.php (6718B)


      1 <?php
      2 /**
      3  * Redux Framework Admin Notice Class
      4  * Makes instantiating a Redux object an absolute piece of cake.
      5  *
      6  * @package     Redux_Framework
      7  * @author      Kevin Provance & Dovy Paukstys
      8  * @subpackage  Core
      9  */
     10 
     11 // Exit if accessed directly.
     12 defined( 'ABSPATH' ) || exit;
     13 
     14 // Don't duplicate me!
     15 if ( ! class_exists( 'Redux_Admin_Notices', false ) ) {
     16 
     17 	/**
     18 	 * Redux Admin Notices Class
     19 	 *
     20 	 * @since       3.0.0
     21 	 */
     22 	class Redux_Admin_Notices extends Redux_Class {
     23 
     24 		/**
     25 		 * WordPress admin notice array.
     26 		 *
     27 		 * @var array
     28 		 * @access private
     29 		 */
     30 		private static $notices = array();
     31 
     32 		/**
     33 		 * Redux_Admin_Notices constructor.
     34 		 *
     35 		 * @param array $parent ReduxFramework object.
     36 		 * @access public
     37 		 */
     38 		public function __construct( $parent ) {
     39 			parent::__construct( $parent );
     40 
     41 			add_action( 'wp_ajax_redux_hide_admin_notice', array( $this, 'ajax' ) );
     42 			add_action( 'admin_notices', array( $this, 'notices' ), 99 );
     43 			add_action( 'admin_init', array( $this, 'dismiss' ), 9 );
     44 		}
     45 
     46 		/**
     47 		 * Display nices stored in notices array.
     48 		 *
     49 		 * @access public
     50 		 */
     51 		public function notices() {
     52 			$this->admin_notices( self::$notices );
     53 		}
     54 
     55 		/**
     56 		 * Dismisses admin notice
     57 		 *
     58 		 * @access public
     59 		 */
     60 		public function dismiss() {
     61 			$this->dismiss_admin_notice();
     62 		}
     63 
     64 		/**
     65 		 * Sets an admin notice for display.
     66 		 *
     67 		 * @param array $data Notice data.
     68 		 */
     69 		public static function set_notice( array $data ) {
     70 			$type    = null;
     71 			$msg     = null;
     72 			$id      = null;
     73 			$dismiss = null;
     74 
     75 			// phpcs:ignore WordPress.PHP.DontExtract
     76 			extract( $data );
     77 
     78 			self::$notices[ $parent->args['page_slug'] ][] = array(
     79 				'type'    => $type,
     80 				'msg'     => $msg,
     81 				'id'      => $id . '_' . $parent->args['opt_name'],
     82 				'dismiss' => $dismiss,
     83 				'color'   => $color ?? '#00A2E3',
     84 			);
     85 		}
     86 
     87 		/**
     88 		 * Evaluates user dismiss option for displaying admin notices.
     89 		 *
     90 		 * @param array $notices Array of stored notices to display.
     91 		 *
     92 		 * @return      void
     93 		 * @since       3.2.0
     94 		 * @access      public
     95 		 */
     96 		public function admin_notices( array $notices = array() ) {
     97 			global $current_user, $pagenow;
     98 
     99 			$core = $this->core();
    100 			if ( isset( $_GET ) && isset( $_GET['page'] ) && $core->args['page_slug'] === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification
    101 				do_action( 'redux_admin_notices_run', $core->args );
    102 
    103 				// Check for an active admin notice array.
    104 				if ( ! empty( $notices ) ) {
    105 					if ( isset( $notices[ $core->args['page_slug'] ] ) ) {
    106 						// Enum admin notices.
    107 						foreach ( $notices[ $core->args['page_slug'] ] as $notice ) {
    108 
    109 							$add_style = '';
    110 							if ( strpos( $notice['type'], 'redux-message' ) !== false ) {
    111 								$add_style = 'style="border-left: 4px solid ' . esc_attr( $notice['color'] ) . '!important;"';
    112 							}
    113 
    114 							if ( true === $notice['dismiss'] ) {
    115 
    116 								// Get user ID.
    117 								$userid = $current_user->ID;
    118 
    119 								if ( ! get_user_meta( $userid, 'ignore_' . $notice['id'] ) ) {
    120 									global $wp_version;
    121 
    122 									// Print the notice with the dismiss link.
    123 									if ( version_compare( $wp_version, '4.2', '>' ) ) {
    124 										$css_id    = esc_attr( $notice['id'] );
    125 										$css_class = esc_attr( $notice['type'] ) . ' redux-notice notice is-dismissible redux-notice';
    126 
    127 										$nonce = wp_create_nonce( $notice['id'] . $userid . 'nonce' );
    128 
    129 										echo '<div ' . $add_style . ' id="' . esc_attr( $css_id ) . '" class="' . esc_attr( $css_class ) . '">'; // phpcs:ignore WordPress.Security.EscapeOutput
    130 										echo '<input type="hidden" class="dismiss_data" id="' . esc_attr( $css_id ) . '" value="' . esc_attr( $nonce ) . '">';
    131 										echo '<p>' . wp_kses_post( $notice['msg'] ) . '</p>';
    132 										echo '</div>';
    133 									} else {
    134 										echo '<div ' . esc_html( $add_style ) . ' class="' . esc_attr( $notice['type'] ) . ' notice is-dismissable"><p>' . wp_kses_post( $notice['msg'] ) . '&nbsp;&nbsp;<a href="?dismiss=true&amp;id=' . esc_attr( $css_id ) . '">' . esc_html__( 'Dismiss', 'redux-framework' ) . '</a>.</p></div>';
    135 									}
    136 								}
    137 							} else {
    138 								// Standard notice.
    139 								echo '<div ' . esc_html( $add_style ) . ' class="' . esc_attr( $notice['type'] ) . ' notice"><p>' . wp_kses_post( $notice['msg'] ) . '</a>.</p></div>';
    140 							}
    141 							?>
    142 							<script>
    143 								jQuery( document ).ready( function( $ ) {
    144 									$( document.body ).on(
    145 										'click', '.redux-notice.is-dismissible .notice-dismiss', function( e ) {
    146 											e.preventDefault();
    147 											var $data = $( this ).parent().find( '.dismiss_data' );
    148 											$.post(
    149 												ajaxurl, {
    150 													action: 'redux_hide_admin_notice',
    151 													id: $data.attr( 'id' ),
    152 													nonce: $data.val()
    153 												}
    154 											);
    155 										} );
    156 								} );
    157 							</script>
    158 							<?php
    159 
    160 						}
    161 					}
    162 				}
    163 				// Clear the admin notice array.
    164 				self::$notices[ $core->args['opt_name'] ] = array();
    165 			}
    166 		}
    167 
    168 		/**
    169 		 * Updates user meta to store dismiss notice preference.
    170 		 *
    171 		 * @since       3.2.0
    172 		 * @access      private
    173 		 * @return      void
    174 		 */
    175 		private function dismiss_admin_notice() {
    176 			global $current_user;
    177 
    178 			// Verify the dismiss and id parameters are present.
    179 			if ( isset( $_GET['dismiss'] ) && isset( $_GET['id'] ) ) {
    180 				if ( isset( $_GET['nonce'] ) && wp_verify_nonce( sanitize_key( wp_unslash( $_GET['nonce'] ) ), 'redux_hint_toggle' ) ) {
    181 					if ( 'true' === $_GET['dismiss'] || 'false' === $_GET['dismiss'] ) {
    182 
    183 						// Get the user id.
    184 						$userid = $current_user->ID;
    185 
    186 						// Get the notice id.
    187 						$id  = sanitize_text_field( wp_unslash( $_GET['id'] ) );
    188 						$val = sanitize_text_field( wp_unslash( $_GET['dismiss'] ) );
    189 
    190 						// Add the dismiss request to the user meta.
    191 						update_user_meta( $userid, 'ignore_' . $id, $val );
    192 					}
    193 				} else {
    194 					wp_nonce_ays( 'redux_hint_toggle' );
    195 				}
    196 			}
    197 		}
    198 
    199 		/**
    200 		 * Updates user meta to store dismiss notice preference
    201 		 *
    202 		 * @since       3.2.0
    203 		 * @access      public
    204 		 * @return      void
    205 		 */
    206 		public function ajax() {
    207 			global $current_user;
    208 
    209 			if ( isset( $_POST['id'] ) ) {
    210 				// Get the notice id.
    211 				$id = explode( '&', sanitize_text_field( wp_unslash( $_POST['id'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification
    212 				$id = $id[0];
    213 
    214 				// Get the user id.
    215 				$userid = $current_user->ID;
    216 
    217 				if ( ! isset( $_POST['nonce'] ) || ( isset( $_POST['nonce'] ) && ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['nonce'] ) ), $id . $userid . 'nonce' ) ) ) {
    218 					die( 0 );
    219 				} else {
    220 					// Add the dismiss request to the user meta.
    221 					update_user_meta( $userid, 'ignore_' . $id, true );
    222 				}
    223 			}
    224 		}
    225 	}
    226 }