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