PluginProbe
Elementor Website Builder – more than just a page builder / 3.27.3
Elementor Website Builder – more than just a page builder v3.27.3
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / notifications / api.php

api.php in Elementor Website Builder – more than just a page builder 3.27.3, at modules/notifications/api.php

180 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 return $notifications;
42 }
43
44 private static function fetch_data() : array {
45 $response = wp_remote_get( self::NOTIFICATIONS_URL );
46
47 if ( is_wp_error( $response ) ) {
48 return [];
49 }
50
51 $data = json_decode( wp_remote_retrieve_body( $response ), true );
52
53 if ( empty( $data['notifications'] ) || ! is_array( $data['notifications'] ) ) {
54 return [];
55 }
56
57 return $data['notifications'];
58 }
59
60 private static function add_to_array( $filtered_notifications, $notification ) {
61 foreach ( $filtered_notifications as $filtered_notification ) {
62 if ( $filtered_notification['id'] === $notification['id'] ) {
63 return $filtered_notifications;
64 }
65 }
66
67 $filtered_notifications[] = $notification;
68
69 return $filtered_notifications;
70 }
71
72 private static function check_conditions( $groups ) {
73 foreach ( $groups as $group ) {
74 if ( static::check_group( $group ) ) {
75 return true;
76 }
77 }
78
79 return false;
80 }
81
82 private static function check_group( $group ) {
83 $is_or_relation = ! empty( $group['relation'] ) && 'OR' === $group['relation'];
84 unset( $group['relation'] );
85 $result = false;
86
87 foreach ( $group as $condition ) {
88 // Reset results for each condition.
89 $result = false;
90 switch ( $condition['type'] ) {
91 case 'wordpress': // phpcs:ignore WordPress.WP.CapitalPDangit.Misspelled
92 // include an unmodified $wp_version
93 include ABSPATH . WPINC . '/version.php';
94 $result = version_compare( $wp_version, $condition['version'], $condition['operator'] );
95 break;
96 case 'multisite':
97 $result = is_multisite() === $condition['multisite'];
98 break;
99 case 'language':
100 $in_array = in_array( get_locale(), $condition['languages'], true );
101 $result = 'in' === $condition['operator'] ? $in_array : ! $in_array;
102 break;
103 case 'plugin':
104 if ( ! function_exists( 'is_plugin_active' ) ) {
105 require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
106 }
107
108 $is_plugin_active = is_plugin_active( $condition['plugin'] );
109
110 if ( empty( $condition['operator'] ) ) {
111 $condition['operator'] = '==';
112 }
113
114 $result = '==' === $condition['operator'] ? $is_plugin_active : ! $is_plugin_active;
115 break;
116 case 'theme':
117 $theme = wp_get_theme();
118 if ( wp_get_theme()->parent() ) {
119 $theme = wp_get_theme()->parent();
120 }
121
122 if ( $theme->get_template() === $condition['theme'] ) {
123 $version = $theme->version;
124 } else {
125 $version = '';
126 }
127
128 $result = version_compare( $version, $condition['version'], $condition['operator'] );
129 break;
130 case 'introduction_meta':
131 $result = User::get_introduction_meta( $condition['meta'] );
132 break;
133
134 default:
135 /**
136 * Filters the notification condition, whether to check the group or not.
137 *
138 * The dynamic portion of the hook name, `$condition['type']`, refers to the condition type.
139 *
140 * @since 3.19.0
141 *
142 * @param bool $result Whether to check the group.
143 * @param array $condition Notification condition.
144 */
145 $result = apply_filters( "elementor/notifications/condition/{$condition['type']}", $result, $condition );
146 break;
147 }
148
149 if ( ( $is_or_relation && $result ) || ( ! $is_or_relation && ! $result ) ) {
150 return $result;
151 }
152 }
153
154 return $result;
155 }
156
157 private static function get_transient( $cache_key ) {
158 $cache = get_option( $cache_key );
159
160 if ( empty( $cache['timeout'] ) ) {
161 return false;
162 }
163
164 if ( current_time( 'timestamp' ) > $cache['timeout'] ) {
165 return false;
166 }
167
168 return json_decode( $cache['value'], true );
169 }
170
171 private static function set_transient( $cache_key, $value, $expiration = '+12 hours' ) {
172 $data = [
173 'timeout' => strtotime( $expiration, current_time( 'timestamp' ) ),
174 'value' => json_encode( $value ),
175 ];
176
177 return update_option( $cache_key, $data, false );
178 }
179 }
180