wp-mail-smtp
Last commit date
assets
7 years ago
languages
7 years ago
src
7 years ago
vendor
7 years ago
class-wpms-am-notification.php
7 years ago
readme.txt
7 years ago
uninstall.php
7 years ago
wp-mail-smtp.php
7 years ago
wp_mail_smtp.php
7 years ago
class-wpms-am-notification.php
456 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Awesome Motive Notifications. |
| 5 | * |
| 6 | * This creates a custom post type (if it doesn't exist) and calls the API to |
| 7 | * retrieve notifications for this product. |
| 8 | * |
| 9 | * @package AwesomeMotive |
| 10 | * @author AwesomeMotive Team |
| 11 | * @license GPL-2.0+ |
| 12 | * @copyright Copyright (c) 2018, Awesome Motive LLC |
| 13 | * @version 1.0.7 |
| 14 | */ |
| 15 | class WPMS_AM_Notification { |
| 16 | |
| 17 | /** |
| 18 | * The api url we are calling. |
| 19 | * |
| 20 | * @since 1.0.0 |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | public $api_url = 'https://api.awesomemotive.com/v1/notification/'; |
| 25 | |
| 26 | /** |
| 27 | * A unique slug for this plugin. |
| 28 | * (Not the WordPress plugin slug) |
| 29 | * |
| 30 | * @since 1.0.0 |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | public $plugin; |
| 35 | |
| 36 | /** |
| 37 | * The current plugin version. |
| 38 | * |
| 39 | * @since 1.0.0 |
| 40 | * |
| 41 | * @var string |
| 42 | */ |
| 43 | public $plugin_version; |
| 44 | |
| 45 | /** |
| 46 | * Flag if a notice has been registered. |
| 47 | * |
| 48 | * @since 1.0.0 |
| 49 | * |
| 50 | * @var bool |
| 51 | */ |
| 52 | public static $registered = false; |
| 53 | |
| 54 | /** |
| 55 | * Construct. |
| 56 | * |
| 57 | * @since 1.0.0 |
| 58 | * |
| 59 | * @param string $plugin The plugin slug. |
| 60 | * @param mixed $version The version of the plugin. |
| 61 | */ |
| 62 | public function __construct( $plugin = '', $version = 0 ) { |
| 63 | $this->plugin = $plugin; |
| 64 | $this->plugin_version = $version; |
| 65 | |
| 66 | add_action( 'init', array( $this, 'custom_post_type' ) ); |
| 67 | add_action( 'admin_init', array( $this, 'get_remote_notifications' ), 100 ); |
| 68 | add_action( 'admin_notices', array( $this, 'display_notifications' ) ); |
| 69 | add_action( 'wp_ajax_am_notification_dismiss', array( $this, 'dismiss_notification' ) ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Registers a custom post type. |
| 74 | * |
| 75 | * @since 1.0.0 |
| 76 | */ |
| 77 | public function custom_post_type() { |
| 78 | register_post_type( 'amn_' . $this->plugin, array( |
| 79 | 'label' => $this->plugin . ' Announcements', |
| 80 | 'can_export' => false, |
| 81 | 'supports' => false, |
| 82 | 'capability_type' => 'manage_options', |
| 83 | ) ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Retrieve the remote notifications if the time has expired. |
| 88 | * |
| 89 | * @since 1.0.0 |
| 90 | */ |
| 91 | public function get_remote_notifications() { |
| 92 | if ( ! apply_filters( 'am_notifications_display', is_super_admin() ) ) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | $last_checked = get_option( '_amn_' . $this->plugin . '_last_checked', strtotime( '-1 week' ) ); |
| 97 | |
| 98 | if ( $last_checked < strtotime( 'today midnight' ) ) { |
| 99 | $plugin_notifications = $this->get_plugin_notifications( 1 ); |
| 100 | $notification_id = null; |
| 101 | |
| 102 | if ( ! empty( $plugin_notifications ) ) { |
| 103 | // Unset it from the array. |
| 104 | $notification = $plugin_notifications[0]; |
| 105 | $notification_id = get_post_meta( $notification->ID, 'notification_id', true ); |
| 106 | } |
| 107 | |
| 108 | $response = wp_remote_retrieve_body( wp_remote_post( $this->api_url, array( |
| 109 | 'body' => array( |
| 110 | 'slug' => $this->plugin, |
| 111 | 'version' => $this->plugin_version, |
| 112 | 'last_notification' => $notification_id, |
| 113 | ), |
| 114 | ) ) ); |
| 115 | |
| 116 | $data = json_decode( $response ); |
| 117 | |
| 118 | if ( ! empty( $data->id ) ) { |
| 119 | $notifications = array(); |
| 120 | |
| 121 | foreach ( (array) $data->slugs as $slug ) { |
| 122 | $notifications = array_merge( |
| 123 | $notifications, |
| 124 | (array) get_posts( |
| 125 | array( |
| 126 | 'post_type' => 'amn_' . $slug, |
| 127 | 'post_status' => 'all', |
| 128 | 'meta_key' => 'notification_id', |
| 129 | 'meta_value' => $data->id, |
| 130 | ) |
| 131 | ) |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | if ( empty( $notifications ) ) { |
| 136 | $new_notification_id = wp_insert_post( |
| 137 | array( |
| 138 | 'post_content' => wp_kses_post( $data->content ), |
| 139 | 'post_type' => 'amn_' . $this->plugin, |
| 140 | ) |
| 141 | ); |
| 142 | |
| 143 | update_post_meta( $new_notification_id, 'notification_id', absint( $data->id ) ); |
| 144 | update_post_meta( $new_notification_id, 'type', sanitize_text_field( trim( $data->type ) ) ); |
| 145 | update_post_meta( $new_notification_id, 'dismissable', (bool) $data->dismissible ? 1 : 0 ); |
| 146 | update_post_meta( $new_notification_id, 'location', function_exists( 'wp_json_encode' ) ? wp_json_encode( $data->location ) : json_encode( $data->location ) ); |
| 147 | update_post_meta( $new_notification_id, 'version', sanitize_text_field( trim( $data->version ) ) ); |
| 148 | update_post_meta( $new_notification_id, 'viewed', 0 ); |
| 149 | update_post_meta( $new_notification_id, 'expiration', $data->expiration ? absint( $data->expiration ) : false ); |
| 150 | update_post_meta( $new_notification_id, 'plans', function_exists( 'wp_json_encode' ) ? wp_json_encode( $data->plans ) : json_encode( $data->plans ) ); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // Possibly revoke notifications. |
| 155 | if ( ! empty( $data->revoked ) ) { |
| 156 | $this->revoke_notifications( $data->revoked ); |
| 157 | } |
| 158 | |
| 159 | // Set the option now so we can't run this again until after 24 hours. |
| 160 | update_option( '_amn_' . $this->plugin . '_last_checked', strtotime( 'today midnight' ) ); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Get local plugin notifications that have already been set. |
| 166 | * |
| 167 | * @since 1.0.0 |
| 168 | * |
| 169 | * @param integer $limit Set the limit for how many posts to retrieve. |
| 170 | * @param array $args Any top-level arguments to add to the array. |
| 171 | * |
| 172 | * @return WP_Post[] WP_Post that match the query. |
| 173 | */ |
| 174 | public function get_plugin_notifications( $limit = - 1, $args = array() ) { |
| 175 | return get_posts( |
| 176 | array( |
| 177 | 'posts_per_page' => $limit, |
| 178 | 'post_type' => 'amn_' . $this->plugin, |
| 179 | ) + $args |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Display any notifications that should be displayed. |
| 185 | * |
| 186 | * @since 1.0.0 |
| 187 | */ |
| 188 | public function display_notifications() { |
| 189 | if ( ! apply_filters( 'am_notifications_display', is_super_admin() ) ) { |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | $plugin_notifications = $this->get_plugin_notifications( - 1, array( |
| 194 | 'post_status' => 'all', |
| 195 | 'meta_key' => 'viewed', |
| 196 | 'meta_value' => '0', |
| 197 | ) ); |
| 198 | |
| 199 | $plugin_notifications = $this->validate_notifications( $plugin_notifications ); |
| 200 | |
| 201 | if ( ! empty( $plugin_notifications ) && ! self::$registered ) { |
| 202 | foreach ( $plugin_notifications as $notification ) { |
| 203 | $dismissable = get_post_meta( $notification->ID, 'dismissable', true ); |
| 204 | $type = get_post_meta( $notification->ID, 'type', true ); |
| 205 | ?> |
| 206 | <div class="am-notification am-notification-<?php echo absint( $notification->ID ); ?> notice notice-<?php echo esc_attr( $type ); ?><?php echo $dismissable ? ' is-dismissible' : ''; ?>"> |
| 207 | <?php echo wp_kses_post( $notification->post_content ); ?> |
| 208 | </div> |
| 209 | <script type="text/javascript"> |
| 210 | jQuery( document ).ready( function ( $ ) { |
| 211 | $( document ).on( 'click', '.am-notification-<?php echo absint( $notification->ID ); ?> button.notice-dismiss', function ( event ) { |
| 212 | $.post( ajaxurl, { |
| 213 | action: 'am_notification_dismiss', |
| 214 | notification_id: '<?php echo absint( $notification->ID ); ?>' |
| 215 | } ); |
| 216 | } ); |
| 217 | } ); |
| 218 | </script> |
| 219 | <?php |
| 220 | } |
| 221 | |
| 222 | self::$registered = true; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Validate the notifications before displaying them. |
| 228 | * |
| 229 | * @since 1.0.0 |
| 230 | * |
| 231 | * @param array $plugin_notifications An array of plugin notifications. |
| 232 | * |
| 233 | * @return array A filtered array of plugin notifications. |
| 234 | */ |
| 235 | public function validate_notifications( $plugin_notifications ) { |
| 236 | global $pagenow; |
| 237 | |
| 238 | foreach ( $plugin_notifications as $key => $notification ) { |
| 239 | // Location validation. |
| 240 | $location = (array) json_decode( get_post_meta( $notification->ID, 'location', true ) ); |
| 241 | $continue = false; |
| 242 | if ( ! in_array( 'everywhere', $location, true ) ) { |
| 243 | if ( in_array( 'index.php', $location, true ) && 'index.php' === $pagenow ) { |
| 244 | $continue = true; |
| 245 | } |
| 246 | |
| 247 | if ( in_array( 'plugins.php', $location, true ) && 'plugins.php' === $pagenow ) { |
| 248 | $continue = true; |
| 249 | } |
| 250 | |
| 251 | if ( ! $continue ) { |
| 252 | unset( $plugin_notifications[ $key ] ); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | // Plugin validation (OR conditional). |
| 257 | $plugins = (array) json_decode( get_post_meta( $notification->ID, 'plugins', true ) ); |
| 258 | $continue = false; |
| 259 | if ( ! empty( $plugins ) ) { |
| 260 | foreach ( $plugins as $plugin ) { |
| 261 | if ( is_plugin_active( $plugin ) ) { |
| 262 | $continue = true; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if ( ! $continue ) { |
| 267 | unset( $plugin_notifications[ $key ] ); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // Theme validation. |
| 272 | $theme = get_post_meta( $notification->ID, 'theme', true ); |
| 273 | $continue = (string) wp_get_theme() === $theme; |
| 274 | |
| 275 | if ( ! empty( $theme ) && ! $continue ) { |
| 276 | unset( $plugin_notifications[ $key ] ); |
| 277 | } |
| 278 | |
| 279 | // Version validation. |
| 280 | $version = get_post_meta( $notification->ID, 'version', true ); |
| 281 | $continue = false; |
| 282 | if ( ! empty( $version ) ) { |
| 283 | if ( version_compare( $this->plugin_version, $version, '<=' ) ) { |
| 284 | $continue = true; |
| 285 | } |
| 286 | |
| 287 | if ( ! $continue ) { |
| 288 | unset( $plugin_notifications[ $key ] ); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | // Expiration validation. |
| 293 | $expiration = get_post_meta( $notification->ID, 'expiration', true ); |
| 294 | $continue = false; |
| 295 | if ( ! empty( $expiration ) ) { |
| 296 | if ( $expiration > time() ) { |
| 297 | $continue = true; |
| 298 | } |
| 299 | |
| 300 | if ( ! $continue ) { |
| 301 | unset( $plugin_notifications[ $key ] ); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | // Plan validation. |
| 306 | $plans = (array) json_decode( get_post_meta( $notification->ID, 'plans', true ) ); |
| 307 | $continue = false; |
| 308 | if ( ! empty( $plans ) ) { |
| 309 | $level = $this->get_plan_level(); |
| 310 | if ( in_array( $level, $plans, true ) ) { |
| 311 | $continue = true; |
| 312 | } |
| 313 | |
| 314 | if ( ! $continue ) { |
| 315 | unset( $plugin_notifications[ $key ] ); |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | return $plugin_notifications; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Grab the current plan level. |
| 325 | * |
| 326 | * @since 1.0.0 |
| 327 | * |
| 328 | * @return string The current plan level. |
| 329 | */ |
| 330 | public function get_plan_level() { |
| 331 | // Prepare variables. |
| 332 | $key = ''; |
| 333 | $level = ''; |
| 334 | |
| 335 | switch ( $this->plugin ) { |
| 336 | case 'wpforms': |
| 337 | $option = get_option( 'wpforms_license' ); |
| 338 | $key = is_array( $option ) && isset( $option['key'] ) ? $option['key'] : ''; |
| 339 | $level = is_array( $option ) && isset( $option['type'] ) ? $option['type'] : ''; |
| 340 | |
| 341 | // Possibly check for a constant. |
| 342 | if ( empty( $key ) && defined( 'WPFORMS_LICENSE_KEY' ) ) { |
| 343 | $key = WPFORMS_LICENSE_KEY; |
| 344 | } |
| 345 | break; |
| 346 | case 'mi-lite': |
| 347 | case 'mi': |
| 348 | if ( version_compare( MONSTERINSIGHTS_VERSION, '6.9.0', '>=' ) ) { |
| 349 | if ( MonsterInsights()->license->get_site_license_type() ) { |
| 350 | $key = MonsterInsights()->license->get_site_license_key(); |
| 351 | $type = MonsterInsights()->license->get_site_license_type(); |
| 352 | } else if ( MonsterInsights()->license->get_network_license_type() ) { |
| 353 | $key = MonsterInsights()->license->get_network_license_key(); |
| 354 | $type = MonsterInsights()->license->get_network_license_type(); |
| 355 | } |
| 356 | |
| 357 | // Check key fallbacks |
| 358 | if ( empty( $key ) ) { |
| 359 | $key = MonsterInsights()->license->get_license_key(); |
| 360 | } |
| 361 | } else { |
| 362 | $option = get_option( 'monsterinsights_license' ); |
| 363 | $key = is_array( $option ) && isset( $option['key'] ) ? $option['key'] : ''; |
| 364 | $level = is_array( $option ) && isset( $option['type'] ) ? $option['type'] : ''; |
| 365 | |
| 366 | // Possibly check for a constant. |
| 367 | if ( empty( $key ) && defined( 'MONSTERINSIGHTS_LICENSE_KEY' ) && is_string( MONSTERINSIGHTS_LICENSE_KEY ) && strlen( MONSTERINSIGHTS_LICENSE_KEY ) > 10 ) { |
| 368 | $key = MONSTERINSIGHTS_LICENSE_KEY; |
| 369 | } |
| 370 | } |
| 371 | break; |
| 372 | case 'om': |
| 373 | $option = get_option( 'optin_monster_api' ); |
| 374 | $key = is_array( $option ) && isset( $option['api']['apikey'] ) ? $option['api']['apikey'] : ''; |
| 375 | |
| 376 | // Possibly check for a constant. |
| 377 | if ( empty( $key ) && defined( 'OPTINMONSTER_REST_API_LICENSE_KEY' ) ) { |
| 378 | $key = OPTINMONSTER_REST_API_LICENSE_KEY; |
| 379 | } |
| 380 | |
| 381 | // If the key is still empty, check for the old legacy key. |
| 382 | if ( empty( $key ) ) { |
| 383 | $key = is_array( $option ) && isset( $option['api']['key'] ) ? $option['api']['key'] : ''; |
| 384 | } |
| 385 | break; |
| 386 | } |
| 387 | |
| 388 | // Possibly set the level to 'none' if the key is empty and no level has been set. |
| 389 | if ( empty( $key ) && empty( $level ) ) { |
| 390 | $level = 'none'; |
| 391 | } |
| 392 | |
| 393 | // Possibly set the level to 'unknown' if a key is entered, but no level can be determined (such as manually entered key) |
| 394 | if ( ! empty( $key ) && empty( $level ) ) { |
| 395 | $level = 'unknown'; |
| 396 | } |
| 397 | |
| 398 | // Normalize the level. |
| 399 | switch ( $level ) { |
| 400 | case 'bronze': |
| 401 | case 'personal': |
| 402 | $level = 'basic'; |
| 403 | break; |
| 404 | case 'silver': |
| 405 | case 'multi': |
| 406 | $level = 'plus'; |
| 407 | break; |
| 408 | case 'gold': |
| 409 | case 'developer': |
| 410 | $level = 'pro'; |
| 411 | break; |
| 412 | case 'platinum': |
| 413 | case 'master': |
| 414 | $level = 'ultimate'; |
| 415 | break; |
| 416 | } |
| 417 | |
| 418 | // Return the plan level. |
| 419 | return $level; |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Dismiss the notification via AJAX. |
| 424 | * |
| 425 | * @since 1.0.0 |
| 426 | */ |
| 427 | public function dismiss_notification() { |
| 428 | if ( ! apply_filters( 'am_notifications_display', is_super_admin() ) ) { |
| 429 | die; |
| 430 | } |
| 431 | |
| 432 | $notification_id = intval( $_POST['notification_id'] ); |
| 433 | update_post_meta( $notification_id, 'viewed', 1 ); |
| 434 | die; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Revokes notifications. |
| 439 | * |
| 440 | * @since 1.0.0 |
| 441 | * |
| 442 | * @param array $ids An array of notification IDs to revoke. |
| 443 | */ |
| 444 | public function revoke_notifications( $ids ) { |
| 445 | // Loop through each of the IDs and find the post that has it as meta. |
| 446 | foreach ( (array) $ids as $id ) { |
| 447 | $notifications = $this->get_plugin_notifications( - 1, array( 'post_status' => 'all', 'meta_key' => 'notification_id', 'meta_value' => $id ) ); |
| 448 | if ( $notifications ) { |
| 449 | foreach ( $notifications as $notification ) { |
| 450 | update_post_meta( $notification->ID, 'viewed', 1 ); |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 |