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