apps
8 months ago
assets
1 year ago
icons
1 year ago
scripts
8 months ago
class-wpchill-about-us.php
1 year ago
class-wpchill-notifications.php
8 months ago
class-wpchill-rest-api.php
1 year ago
class-wpchill-upsells.php
1 month ago
class-wpchill-notifications.php
263 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if ( ! class_exists( 'WPChill_Notifications' ) ) { |
| 8 | //phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound |
| 9 | class WPChill_Notifications { |
| 10 | |
| 11 | public static $instance; |
| 12 | |
| 13 | public static $notification_prefix = 'wpchill_notification_'; |
| 14 | public static $blocked_notifications = 'wpchill_blocked_notifications'; |
| 15 | private $hook_name = 'wpchill_notifications_remote'; |
| 16 | |
| 17 | public function __construct() { |
| 18 | |
| 19 | if ( ! wp_next_scheduled( $this->hook_name ) ) { |
| 20 | wp_schedule_event( time(), 'daily', $this->hook_name ); |
| 21 | } |
| 22 | |
| 23 | add_action( $this->hook_name, array( $this, 'get_remote_notices' ) ); |
| 24 | |
| 25 | if ( ! class_exists( 'WPChill_Rest_Api' ) ) { |
| 26 | require_once plugin_dir_path( __FILE__ ) . 'class-wpchill-rest-api.php'; |
| 27 | } |
| 28 | |
| 29 | add_action( 'admin_enqueue_scripts', array( $this, 'notification_system_scripts' ) ); |
| 30 | |
| 31 | new WPChill_Rest_Api(); |
| 32 | } |
| 33 | |
| 34 | public static function get_instance() { |
| 35 | |
| 36 | if ( ! isset( self::$instance ) && ! ( self::$instance instanceof WPChill_Notifications ) ) { |
| 37 | self::$instance = new WPChill_Notifications(); |
| 38 | } |
| 39 | |
| 40 | return self::$instance; |
| 41 | } |
| 42 | |
| 43 | public static function add_notification( $key, $notification ) { |
| 44 | $blocked = get_option( self::$blocked_notifications, array() ); |
| 45 | if ( in_array( $key, $blocked, true ) ) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | // Set the timestamp but only if not previously set. |
| 50 | if ( ! isset( $notification['timestamp'] ) ) { |
| 51 | $notification['timestamp'] = strtotime( current_time( 'mysql' ) ); |
| 52 | } |
| 53 | |
| 54 | update_option( self::$notification_prefix . $key, $notification ); |
| 55 | } |
| 56 | |
| 57 | public static function remove_notification( $key ) { |
| 58 | delete_option( self::$notification_prefix . $key ); |
| 59 | } |
| 60 | |
| 61 | public function get_notifications() { |
| 62 | |
| 63 | $notifications = array( |
| 64 | 'error' => array(), |
| 65 | 'warning' => array(), |
| 66 | 'success' => array(), |
| 67 | 'info' => array(), |
| 68 | ); |
| 69 | |
| 70 | $options = $this->_get_options_wildcard( self::$notification_prefix . '%' ); |
| 71 | |
| 72 | if ( ! function_exists( 'is_plugin_active' ) ) { |
| 73 | include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 74 | } |
| 75 | |
| 76 | $plugin_map = array( |
| 77 | 'modula' => 'modula-best-grid-gallery/Modula.php', |
| 78 | 'download-monitor' => 'download-monitor/download-monitor.php', |
| 79 | 'strong-testimonials' => 'strong-testimonials/strong-testimonials.php', |
| 80 | ); |
| 81 | |
| 82 | foreach ( $options as $option ) { |
| 83 | $id = explode( '_', $option['option_name'] ); |
| 84 | $id = end( $id ); |
| 85 | |
| 86 | if ( ! isset( $option['option_value'] ) ) { |
| 87 | continue; |
| 88 | } |
| 89 | |
| 90 | $current_notifications = maybe_unserialize( $option['option_value'] ); |
| 91 | |
| 92 | if ( empty( $current_notifications ) || empty( $current_notifications['message'] ) ) { |
| 93 | continue; |
| 94 | } |
| 95 | |
| 96 | $status = isset( $current_notifications['status'] ) ? $current_notifications['status'] : 'info'; |
| 97 | |
| 98 | if ( isset( $current_notifications['source']['slug'] ) ) { |
| 99 | $slug = sanitize_text_field( $current_notifications['source']['slug'] ); |
| 100 | |
| 101 | if ( isset( $plugin_map[ $slug ] ) ) { |
| 102 | $plugin_file = $plugin_map[ $slug ]; |
| 103 | } else { |
| 104 | $plugin_file = $slug . '/' . $slug . '.php'; |
| 105 | } |
| 106 | |
| 107 | if ( ! is_plugin_active( $plugin_file ) ) { |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | $current_notifications['source']['icon'] = apply_filters( |
| 112 | 'wpchill_notification_icon', |
| 113 | plugin_dir_url( __FILE__ ) . 'icons/' . $slug . '.svg', |
| 114 | $current_notifications |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | $time_ago = $current_notifications['timestamp'] |
| 119 | ? human_time_diff( $current_notifications['timestamp'], strtotime( current_time( 'mysql' ) ) ) |
| 120 | : false; |
| 121 | |
| 122 | $notifications[ $status ][] = array( |
| 123 | 'id' => $id, |
| 124 | 'title' => isset( $current_notifications['title'] ) ? $current_notifications['title'] : 'Notification', |
| 125 | 'message' => $current_notifications['message'], |
| 126 | 'dismissible' => isset( $current_notifications['dismissible'] ) ? $current_notifications['dismissible'] : true, |
| 127 | 'actions' => isset( $current_notifications['actions'] ) ? $current_notifications['actions'] : array(), |
| 128 | 'timed' => isset( $current_notifications['timed'] ) ? $current_notifications['timed'] : false, |
| 129 | 'source' => isset( $current_notifications['source'] ) ? $current_notifications['source'] : array(), |
| 130 | // Translators: %s represents the time elapsed (e.g., "5 minutes", "2 hours", "1 day"). |
| 131 | 'time_ago' => $time_ago ? sprintf( esc_html__( '%s ago', 'strong-testimonials' ), $time_ago ) : false, |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | //phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 136 | $notifications = apply_filters( 'wpchill_notifications', $notifications ); |
| 137 | |
| 138 | return $notifications; |
| 139 | } |
| 140 | |
| 141 | private function _get_options_wildcard( $option_pattern ) { |
| 142 | global $wpdb; |
| 143 | |
| 144 | $options = $wpdb->get_results( |
| 145 | $wpdb->prepare( |
| 146 | "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE %s", |
| 147 | $option_pattern |
| 148 | ), |
| 149 | ARRAY_A |
| 150 | ); |
| 151 | |
| 152 | return $options; |
| 153 | } |
| 154 | |
| 155 | public function clear_notification( $key, $permanent = false ) { |
| 156 | if ( $permanent ) { |
| 157 | $blocked = get_option( self::$blocked_notifications, array() ); |
| 158 | $blocked[] = $key; |
| 159 | update_option( self::$blocked_notifications, $blocked ); |
| 160 | } |
| 161 | |
| 162 | delete_option( self::$notification_prefix . $key ); |
| 163 | } |
| 164 | |
| 165 | public function clear_notifications( $prefix = false ) { |
| 166 | $slug = $prefix ? $prefix : self::$notification_prefix; |
| 167 | $options = $this->_get_options_wildcard( $slug . '%' ); |
| 168 | |
| 169 | foreach ( $options as $option ) { |
| 170 | if ( isset( $option['option_name'] ) ) { |
| 171 | delete_option( $option['option_name'] ); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | public function get_remote_notices() { |
| 177 | $response = wp_remote_get( |
| 178 | 'https://wp-modula.com/wp-json/notification/v1/get', |
| 179 | array( |
| 180 | 'sslverify' => false, |
| 181 | 'timeout' => 15, |
| 182 | ) |
| 183 | ); |
| 184 | |
| 185 | if ( is_wp_error( $response ) ) { |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | $status_code = wp_remote_retrieve_response_code( $response ); |
| 190 | if ( 200 !== $status_code ) { |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | $body = wp_remote_retrieve_body( $response ); |
| 195 | $notifications = json_decode( $body, true ); |
| 196 | |
| 197 | if ( json_last_error() !== JSON_ERROR_NONE ) { |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | foreach ( $notifications as $key => $notification ) { |
| 202 | $this->add_notification( $key, $notification ); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | public function notification_system_scripts() { |
| 207 | |
| 208 | if ( ! $this->is_wpchill_admin_page() || ! current_user_can( 'manage_options' ) ) { |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | $wpchill_path = plugin_dir_path( __FILE__ ); |
| 213 | $wpchill_url = plugin_dir_url( __FILE__ ); |
| 214 | |
| 215 | $asset_file = require $wpchill_path . 'scripts/notification-system/notification-system.asset.php'; |
| 216 | $enqueue = array( |
| 217 | 'handle' => 'wpchill-notification-system', |
| 218 | 'dependencies' => $asset_file['dependencies'], |
| 219 | 'version' => $asset_file['version'], |
| 220 | 'script' => $wpchill_url . 'scripts/notification-system/notification-system.js', |
| 221 | 'style' => $wpchill_url . 'scripts/notification-system/notification-system.css', |
| 222 | ); |
| 223 | |
| 224 | wp_enqueue_script( |
| 225 | $enqueue['handle'], |
| 226 | $enqueue['script'], |
| 227 | $enqueue['dependencies'], |
| 228 | $enqueue['version'], |
| 229 | true |
| 230 | ); |
| 231 | |
| 232 | wp_enqueue_style( |
| 233 | $enqueue['handle'], |
| 234 | $enqueue['style'], |
| 235 | array( 'wp-components' ), |
| 236 | $enqueue['version'] |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Check if we are on a wpchill plugin admin page |
| 242 | * |
| 243 | * @return bool |
| 244 | * |
| 245 | */ |
| 246 | private function is_wpchill_admin_page() { |
| 247 | $screen = get_current_screen(); |
| 248 | |
| 249 | //phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 250 | $allowed_screens = apply_filters( 'wpchill_notifications_allowed_screens', array( 'modula-gallery', 'modula-albums', 'dlm_download', 'wpm-testimonial' ) ); |
| 251 | |
| 252 | foreach ( $allowed_screens as $allowed_screen ) { |
| 253 | if ( false !== strpos( $screen->id, $allowed_screen ) ) { |
| 254 | return true; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return false; |
| 259 | } |
| 260 | } |
| 261 | WPChill_Notifications::get_instance(); |
| 262 | } |
| 263 |