init(); } return self::$instance; } /** * Instantiate the plugin * * @since 1.3.0 * @return void */ private function init() { // Make sure the WordPress version is recent enough if ( !self::$instance->is_version_compatible() ) { return; } // Make sure we have a version of PHP that's not too old if ( !self::$instance->is_php_version_enough() ) { return; } // Call the dismiss method before testing for Ajax if ( isset( $_GET['rn'] ) && isset( $_GET['notification'] ) ) { // add_action( 'plugins_loaded', array( self::$instance, 'dismiss' ) ); } if ( !defined( 'DOING_AJAX' ) || !DOING_AJAX ) { add_action( 'admin_notices', array( self::$instance, 'show_notices' ) ); add_action( 'admin_print_styles', array( self::$instance, 'wpi_admin_inline_css' ) ); add_action( 'admin_enqueue_scripts', array( self::$instance, 'scripts' ) ); } add_action( 'wp_ajax_rdn_fetch_notifications', array( $this, 'remote_get_notice_ajax' ) ); add_action( 'wp_ajax_dismissnotice', array( $this, 'dismiss_notice' ) ); add_filter( 'heartbeat_received', array( self::$instance, 'heartbeat' ), 10, 2 ); } /** * Throw error on object clone * * The whole idea of the singleton design pattern is that there is a single * object therefore, we don't want the object to be cloned. * * @since 3.2.5 * @return void */ public function __clone() { // Cloning instances of the class is forbidden _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'wpi-remote-notice' ), '3.2.5' ); } /** * Disable unserializing of the class * * @since 3.2.5 * @return void */ public function __wakeup() { // Unserializing instances of the class is forbidden _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'wpi-remote-notice' ), '3.2.5' ); } /** * Check if the core version is compatible with this addon. * * @since 1.3.0 * @return boolean */ private function is_version_compatible() { if ( empty( self::$instance->wordpress_version_required ) ) { return true; } if ( version_compare( get_bloginfo( 'version' ), self::$instance->wordpress_version_required, '<' ) ) { return false; } return true; } /** * Check if the version of PHP is compatible with this addon. * * @since 1.3.0 * @return boolean */ private function is_php_version_enough() { /** * No version set, we assume everything is fine. */ if ( empty( self::$instance->php_version_required ) ) { return true; } if ( version_compare( phpversion(), self::$instance->php_version_required, '<' ) ) { return false; } return true; } /** * Register a new remote notification * * @since 1.3.0 * * @param int $channel_id Channel ID on the remote server * @param string $channel_key Channel key for authentication with the server * @param string $server Notification server URL * @param int $cache Cache lifetime (in hours) * * @return bool|string */ public function add_notification( $channel_id, $channel_key, $server, $cache = 6 ) { $notification = array( 'channel_id' => (int) $channel_id, 'channel_key' => $channel_key, 'server_url' => esc_url( $server ), 'cache_lifetime' => apply_filters( 'rn_notice_caching_time', $cache ), ); // Generate the notice unique ID $notification['notice_id'] = $notification['channel_id'] . substr( $channel_key, 0, 5 ); // Double check that the required info is here if ( '' === ( $notification['channel_id'] || $notification['channel_key'] || $notification['server_url'] ) ) { return false; } // Check that there is no notification with the same ID if ( array_key_exists( $notification['notice_id'], $this->notifications ) ) { return false; } $this->notifications[$notification['notice_id']] = $notification; return $notification['notice_id']; } /** * Remove a registered notification * * @since 1.3.0 * * @param string $notice_id ID of the notice to remove * * @return void */ public function remove_notification( $notice_id ) { if ( array_key_exists( $notice_id, $this->notifications ) ) { unset( $this->notifications[$notice_id] ); } } /** * Get all registered notifications * * @since 1.3.0 * @return array */ public function get_notifications() { return $this->notifications; } /** * Get a specific notification * * @since 1.3.0 * * @param string $notice_id ID of the notice to retrieve * * @return bool|array */ public function get_notification( $notice_id ) { if ( !array_key_exists( $notice_id, $this->notifications ) ) { return false; } return $this->notifications[$notice_id]; } /** * Display all the registered and available notifications * * @since 1.3.0 * @return void */ public function show_notices() { foreach ( $this->notifications as $id => $notification ) { $rn = $this->get_remote_notification( $notification ); if ( empty( $rn ) || is_wp_error( $rn ) ) { continue; } if ( $this->is_notification_error( $rn ) ) { continue; } if ( $this->is_notice_dismissed( $rn->slug ) ) { continue; } if ( $this->is_post_type_restricted( $rn ) ) { continue; } if ( !$this->is_notification_started( $rn ) ) { continue; } if ( $this->has_notification_ended( $rn ) ) { continue; } // Output the admin notice $this->create_admin_notice( $rn->message, $this->get_notice_class( isset( $rn->style ) ? $rn->style : 'notice notice-success' ), $rn->slug ); } } /** * Check if the notification has been dismissed * * @since 1.2.0 * * @param string $slug Slug of the notice to check * * @return bool */ protected function is_notice_dismissed( $slug ) { global $current_user; $dismissed = array_filter( (array) get_user_meta( $current_user->ID, '_rn_dismissed', true ) ); if ( is_array( $dismissed ) && in_array( $slug, $dismissed ) ) { return true; } return false; } /** * Check if the notification can be displayed for the current post type * * @since 1.2.0 * * @param stdClass $notification The notification object * * @return bool */ protected function is_post_type_restricted( $notification ) { /* If the type array isn't empty we have a limitation */ if ( isset( $notification->type ) && is_array( $notification->type ) && !empty( $notification->type ) ) { /* Get current post type */ $pt = get_post_type(); /** * If the current post type can't be retrieved * or if it's not in the allowed post types, * then we don't display the admin notice. */ if ( false === $pt || !in_array( $pt, $notification->type ) ) { return true; } } return false; } /** * Check if the notification has started yet * * @since 1.2.0 * * @param stdClass $notification The notification object * * @return bool */ protected function is_notification_started( $notification ) { if ( !isset( $notification->date_start ) ) { return true; } if ( empty( $notification->date_start ) || strtotime( $notification->date_start ) < time() ) { return true; } return false; } /** * Check if the notification has expired * * @since 1.2.0 * * @param stdClass $notification The notification object * * @return bool */ protected function has_notification_ended( $notification ) { if ( !isset( $notification->date_end ) ) { return false; } if ( empty( $notification->date_end ) || strtotime( $notification->date_end ) > time() ) { return false; } return true; } /** * Get the remote notification object * * @since 1.3.0 * * @param array $notification The notification data array * * @return object|false */ protected function get_remote_notification( $notification ) { $content = get_transient( 'rn_last_notification_' . $notification['notice_id'] ); if ( false === $content ) { add_option( 'rdn_fetch_' . $notification['notice_id'], 'fetch' ); } return $content; } /** * Get the admin notice class attribute * * @since 1.3.0 * * @param string $style Notification style * * @return string */ protected function get_notice_class( $style ) { switch ( $style ) { case 'updated': $class = "notice notice-success is-dismissible"; break; case 'error': $class = "notice notice-$style is-dismissible"; break; default: $class = "notice notice-$style is-dismissible"; } return $class; } /** * Create the actual admin notice * * @since 1.3.0 * * @param string $contents Notice contents * @param string $class Wrapper class * @param string $dismiss Dismissal link * * @return void */ protected function create_admin_notice( $contents, $class, $dismiss ) {?>