notificationId = $notificationId; $this->notificationMessage = $notificationMessage; $this->notificationClass = $notificationClass; $this->force_show = $force_show; if( !empty( $startDate ) && time() < strtotime( $startDate ) ) return; if( !empty( $endDate ) && time() > strtotime( $endDate ) ) return; add_action( 'admin_notices', array( $this, 'add_admin_notice' ) ); } // Display a dismissible admin notice function add_admin_notice() { global $current_user; global $pagenow; $user_id = $current_user->ID; do_action( $this->notificationId.'_before_notification_displayed', $current_user, $pagenow ); if ( current_user_can( 'manage_options' ) ){ // Check that the user hasn't already clicked to ignore the message if ( ! get_user_meta($user_id, $this->notificationId.'_dismiss_notification' ) || $this->force_show ) {//ignore the dismissal if we have force_show add_filter('safe_style_css', array( $this, 'allow_z_index_in_wp_kses')); echo wp_kses( apply_filters($this->notificationId.'_notification_message','
'.$this->notificationMessage.'
', $this->notificationMessage), [ 'div' => [ 'class' => [],'style' => [] ], 'p' => ['style' => [], 'class' => []], 'a' => ['href' => [], 'type'=> [], 'class'=> [], 'style'=>[], 'title'=>[],'target'=>[]], 'span' => ['class'=> [], 'style' => []], 'strong' => [], 'img' => [ 'src' => [], 'style' => [] ], 'br' => [] ] ); remove_filter('safe_style_css', array( $this, 'allow_z_index_in_wp_kses')); } do_action( $this->notificationId.'_notification_displayed', $current_user, $pagenow ); } do_action( $this->notificationId.'_after_notification_displayed', $current_user, $pagenow ); } function allow_z_index_in_wp_kses( $styles ) { $styles[] = 'z-index'; $styles[] = 'position'; $styles[] = 'display'; $styles[] = 'align-items'; $styles[] = 'gap'; $styles[] = 'float'; $styles[] = 'margin'; $styles[] = 'visibility'; return $styles; } } Class Translangated_Plugin_Notifications { public $notifications = array(); private static $_instance = null; private $prefix = 'trp'; private $menu_slug = 'options-general.php'; public $pluginPages = array( 'translangated', 'translangated_advanced_page', 'translangated_machine_translation', 'translangated_test_machine_api', 'translangated_language_switcher' ); protected function __construct() { add_action( 'admin_init', array( $this, 'dismiss_admin_notifications' ), 200 ); add_action( 'admin_init', array( $this, 'add_admin_menu_notification_counts' ), 1000 ); } function dismiss_admin_notifications() { if ( empty( $_GET['translangated_dismiss_admin_notification'] ) || ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'translangated_dismiss_admin_notification' ) ) { return; } self::get_instance()->dismiss_notification( sanitize_key( wp_unslash( $_GET['translangated_dismiss_admin_notification'] ) ) ); } function add_admin_menu_notification_counts() { global $menu, $submenu; $notifications = Translangated_Plugin_Notifications::get_instance(); if( ! empty( $menu ) ) { foreach( $menu as $menu_position => $menu_data ) { if( ! empty( $menu_data[2] ) && $menu_data[2] == $this->menu_slug ) { $menu_count = $notifications->get_count_in_menu(); if( ! empty( $menu_count ) ) $menu[$menu_position][0] .= '' . $menu_count . ''; } } } if( ! empty( $submenu[$this->menu_slug] ) ) { foreach( $submenu[$this->menu_slug] as $menu_position => $menu_data ) { $menu_count = $notifications->get_count_in_submenu( $menu_data[2] ); if( ! empty( $menu_count ) ) $submenu[$this->menu_slug][$menu_position][0] .= '' . $menu_count . ''; } } } /** * * */ public static function get_instance() { if( is_null( self::$_instance ) ) self::$_instance = new Translangated_Plugin_Notifications(); return self::$_instance; } /** * * */ public function add_notification( $notification_id = '', $notification_message = '', $notification_class = 'update-nag', $count_in_menu = true, $count_in_submenu = array(), $show_in_all_backend = false, $force_show = false ) { if( empty( $notification_id ) ) return; if( empty( $notification_message ) ) return; /** * added a $show_in_all_backend argument in version 1.4.6 that allows some notifications to be displayed on all the pages not just the plugin pages * we needed it for license notifications * * if you want a notification that is non-dismissable on is_plugin_page() dismissable on the rest of the pages, simply do the verification where * Translangated_Plugin_Notifications->add_notification() is called * */ $this->notifications[$notification_id] = array( 'id' => $notification_id, 'message' => $notification_message, 'class' => $notification_class, 'count_in_menu' => $count_in_menu, 'count_in_submenu' => $count_in_submenu ); if( $this->is_plugin_page() || $show_in_all_backend ) { new Translangated_Add_General_Notices( $notification_id, $notification_message, $notification_class, '', '', $force_show ); } } /** * * */ public function get_notifications() { return $this->notifications; } /** * * */ public function get_notification( $notification_id = '' ) { if( empty( $notification_id ) ) return null; $notifications = $this->get_notifications(); if( ! empty( $notifications[$notification_id] ) ) return $notifications[$notification_id]; else return null; } /** * * */ public function dismiss_notification( $notification_id = '' ) { global $current_user; add_user_meta( $current_user->ID, $notification_id . '_dismiss_notification', 'true', true ); do_action('translangated_dismiss_notification', $notification_id, $current_user); } /** * * */ public function get_count_in_menu() { $count = 0; foreach( $this->notifications as $notification ) { if( ! empty( $notification['count_in_menu'] ) ) { if( get_user_meta( get_current_user_id(), $notification['id'] . '_dismiss_notification', true ) ) continue; $count++; } } return $count; } /** * * */ public function get_count_in_submenu( $submenu = '' ) { if( empty( $submenu ) ) return 0; $count = 0; foreach( $this->notifications as $notification ) { if( empty( $notification['count_in_submenu'] ) ) continue; if( ! is_array( $notification['count_in_submenu'] ) ) continue; if( ! in_array( $submenu, $notification['count_in_submenu'] ) ) continue; if( get_user_meta( get_current_user_id(), $notification['id'] . '_dismiss_notification', true ) ) continue; $count++; } return $count; } /** * Test if we are an a page that belong to our plugin * */ public function is_plugin_page() { if( !empty( $this->pluginPages ) ){ foreach ( $this->pluginPages as $pluginPage ){ if( ! empty( $_GET['page'] ) && false !== strpos( sanitize_text_field( $_GET['page'] ), $pluginPage ) ) return true; if( ! empty( $_GET['post_type'] ) && false !== strpos( sanitize_text_field( $_GET['post_type'] ), $pluginPage ) ) return true; if( ! empty( $_GET['post'] ) && false !== strpos( get_post_type( (int)$_GET['post'] ), $pluginPage ) ) return true; } } return false; } } class Translangated_Trigger_Plugin_Notifications{ private $settings; private $settings_obj; private $machine_translator_logger; function __construct($settings) { $this->settings = $settings; add_action( 'admin_init', array( $this, 'add_plugin_notifications' ) ); } function add_plugin_notifications() { $notifications = Translangated_Plugin_Notifications::get_instance(); /* * Machine translation enabled and quota are met. */ $trp = Translangated_Plugin::get_translangated_instance(); if ( ! $this->settings_obj ) $this->settings_obj = $trp->get_component( 'settings' ); if ( ! $this->machine_translator_logger ) $this->machine_translator_logger = $trp->get_component( 'machine_translator_logger' ); if( 'yes' === $this->settings['translangated_machine_translation_settings']['machine-translation'] && $this->machine_translator_logger->quota_exceeded() ) { /* this must be unique */ $notification_id = 'translangated_machine_translation_quota_exceeded_'. gmdate('Ymd'); $message = ''; $message .= '

'; /* translators: %s: URL of the Automatic Translation settings page */ $message .= sprintf( __( 'The daily quota for machine translation characters exceeded. Please check the Translangated -> Automatic Translation page for more information.', 'translangated' ), admin_url( 'admin.php?page=translangated_machine_translation' ) ); $message .= '

'; //make sure to use the translangated_dismiss_admin_notification arg $message .= '' . esc_html__( 'Dismiss this notice.', 'translangated' ) . ''; $notifications->add_notification($notification_id, $message, 'trp-notice trp-narrow notice notice-info', true, array('translangated')); } } } // Translangated runs no promotions. Kept because the translation editor data still reads it. function translangated_bf_show_promotion(){ return false; }