| 1 |
<?php |
| 2 |
namespace Elementor\Modules\Notifications; |
| 3 |
|
| 4 |
use Elementor\Includes\EditorAssetsAPI; |
| 5 |
use Elementor\User; |
| 6 |
|
| 7 |
class API { |
| 8 |
|
| 9 |
const NOTIFICATIONS_URL = 'https://assets.elementor.com/notifications/v1/notifications.json'; |
| 10 |
|
| 11 |
public static function get_notifications_by_conditions( $force_request = false ) { |
| 12 |
$notifications = static::get_notifications( $force_request ); |
| 13 |
|
| 14 |
$filtered_notifications = []; |
| 15 |
|
| 16 |
foreach ( $notifications as $notification ) { |
| 17 |
if ( empty( $notification['conditions'] ) ) { |
| 18 |
$filtered_notifications = static::add_to_array( $filtered_notifications, $notification ); |
| 19 |
|
| 20 |
continue; |
| 21 |
} |
| 22 |
|
| 23 |
if ( ! static::check_conditions( $notification['conditions'] ) ) { |
| 24 |
continue; |
| 25 |
} |
| 26 |
|
| 27 |
$filtered_notifications = static::add_to_array( $filtered_notifications, $notification ); |
| 28 |
} |
| 29 |
|
| 30 |
return $filtered_notifications; |
| 31 |
} |
| 32 |
|
| 33 |
private static function get_notifications( $force_request = false ) { |
| 34 |
$editor_assets_api = new EditorAssetsAPI( [ |
| 35 |
EditorAssetsAPI::ASSETS_DATA_URL => self::NOTIFICATIONS_URL, |
| 36 |
EditorAssetsAPI::ASSETS_DATA_TRANSIENT_KEY => '_elementor_notifications_data', |
| 37 |
EditorAssetsAPI::ASSETS_DATA_KEY => 'notifications', |
| 38 |
EditorAssetsAPI::ASSETS_DATA_EXPIRATION => '+12 hours', |
| 39 |
] ); |
| 40 |
$notifications = $editor_assets_api->get_assets_data( $force_request ); |
| 41 |
$notifications = apply_filters( 'elementor/core/admin/notifications', $notifications ); |
| 42 |
|
| 43 |
return $notifications; |
| 44 |
} |
| 45 |
|
| 46 |
private static function add_to_array( $filtered_notifications, $notification ) { |
| 47 |
foreach ( $filtered_notifications as $filtered_notification ) { |
| 48 |
if ( $filtered_notification['id'] === $notification['id'] ) { |
| 49 |
return $filtered_notifications; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
$filtered_notifications[] = $notification; |
| 54 |
|
| 55 |
return $filtered_notifications; |
| 56 |
} |
| 57 |
|
| 58 |
private static function check_conditions( $groups ) { |
| 59 |
foreach ( $groups as $group ) { |
| 60 |
if ( static::check_group( $group ) ) { |
| 61 |
return true; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
private static function check_group( $group ) { |
| 69 |
$is_or_relation = ! empty( $group['relation'] ) && 'OR' === $group['relation']; |
| 70 |
unset( $group['relation'] ); |
| 71 |
$result = false; |
| 72 |
|
| 73 |
foreach ( $group as $condition ) { |
| 74 |
// Reset results for each condition. |
| 75 |
$result = false; |
| 76 |
switch ( $condition['type'] ) { |
| 77 |
case 'wordpress': // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText |
| 78 |
// include an unmodified $wp_version |
| 79 |
include ABSPATH . WPINC . '/version.php'; |
| 80 |
$result = version_compare( $wp_version, $condition['version'], $condition['operator'] ); |
| 81 |
break; |
| 82 |
case 'multisite': |
| 83 |
$result = is_multisite() === $condition['multisite']; |
| 84 |
break; |
| 85 |
case 'language': |
| 86 |
$in_array = in_array( get_locale(), $condition['languages'], true ); |
| 87 |
$result = 'in' === $condition['operator'] ? $in_array : ! $in_array; |
| 88 |
break; |
| 89 |
case 'plugin': |
| 90 |
if ( ! function_exists( 'is_plugin_active' ) ) { |
| 91 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 92 |
} |
| 93 |
|
| 94 |
$is_plugin_active = is_plugin_active( $condition['plugin'] ); |
| 95 |
|
| 96 |
if ( empty( $condition['operator'] ) ) { |
| 97 |
$condition['operator'] = '=='; |
| 98 |
} |
| 99 |
|
| 100 |
$result = '==' === $condition['operator'] ? $is_plugin_active : ! $is_plugin_active; |
| 101 |
break; |
| 102 |
case 'theme': |
| 103 |
$theme = wp_get_theme(); |
| 104 |
if ( wp_get_theme()->parent() ) { |
| 105 |
$theme = wp_get_theme()->parent(); |
| 106 |
} |
| 107 |
|
| 108 |
if ( $theme->get_template() === $condition['theme'] ) { |
| 109 |
$version = $theme->version; |
| 110 |
} else { |
| 111 |
$version = ''; |
| 112 |
} |
| 113 |
|
| 114 |
$result = version_compare( $version, $condition['version'], $condition['operator'] ); |
| 115 |
break; |
| 116 |
case 'introduction_meta': |
| 117 |
$result = User::get_introduction_meta( $condition['meta'] ); |
| 118 |
break; |
| 119 |
|
| 120 |
default: |
| 121 |
/** |
| 122 |
* Filters the notification condition, whether to check the group or not. |
| 123 |
* |
| 124 |
* The dynamic portion of the hook name, `$condition['type']`, refers to the condition type. |
| 125 |
* |
| 126 |
* @since 3.19.0 |
| 127 |
* |
| 128 |
* @param bool $result Whether to check the group. |
| 129 |
* @param array $condition Notification condition. |
| 130 |
*/ |
| 131 |
$result = apply_filters( "elementor/notifications/condition/{$condition['type']}", $result, $condition ); |
| 132 |
break; |
| 133 |
} |
| 134 |
|
| 135 |
if ( ( $is_or_relation && $result ) || ( ! $is_or_relation && ! $result ) ) { |
| 136 |
return $result; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
return $result; |
| 141 |
} |
| 142 |
} |
| 143 |
|