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