PluginProbe
Elementor Website Builder – more than just a page builder / 3.18.2
Elementor Website Builder – more than just a page builder v3.18.2
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 / includes / user.php

user.php in Elementor Website Builder – more than just a page builder 3.18.2, at includes/user.php

342 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor;
3
4 use Elementor\Core\Common\Modules\Ajax\Module as Ajax;
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit; // Exit if accessed directly.
8 }
9
10 /**
11 * Elementor user.
12 *
13 * Elementor user handler class is responsible for checking if the user can edit
14 * with Elementor and displaying different admin notices.
15 *
16 * @since 1.0.0
17 */
18 class User {
19
20 /**
21 * The admin notices key.
22 */
23 const ADMIN_NOTICES_KEY = 'elementor_admin_notices';
24
25 const INTRODUCTION_KEY = 'elementor_introduction';
26
27 const BETA_TESTER_META_KEY = 'elementor_beta_tester';
28
29 /**
30 * API URL.
31 *
32 * Holds the URL of the Beta Tester Opt-in API.
33 *
34 * @since 1.0.0
35 * @access private
36 *
37 * @var string API URL.
38 */
39 const BETA_TESTER_API_URL = 'https://my.elementor.com/api/v1/beta_tester/';
40
41 /**
42 * Init.
43 *
44 * Initialize Elementor user.
45 *
46 * @since 1.0.0
47 * @access public
48 * @static
49 */
50 public static function init() {
51 add_action( 'wp_ajax_elementor_set_admin_notice_viewed', [ __CLASS__, 'ajax_set_admin_notice_viewed' ] );
52 add_action( 'admin_post_elementor_set_admin_notice_viewed', [ __CLASS__, 'ajax_set_admin_notice_viewed' ] );
53
54 add_action( 'elementor/ajax/register_actions', [ __CLASS__, 'register_ajax_actions' ] );
55 }
56
57 /**
58 * @since 2.1.0
59 * @access public
60 * @static
61 */
62 public static function register_ajax_actions( Ajax $ajax ) {
63 $ajax->register_ajax_action( 'introduction_viewed', [ __CLASS__, 'set_introduction_viewed' ] );
64 $ajax->register_ajax_action( 'beta_tester_signup', [ __CLASS__, 'register_as_beta_tester' ] );
65 }
66
67 /**
68 * Is current user can edit.
69 *
70 * Whether the current user can edit the post.
71 *
72 * @since 1.0.0
73 * @access public
74 * @static
75 *
76 * @param int $post_id Optional. The post ID. Default is `0`.
77 *
78 * @return bool Whether the current user can edit the post.
79 */
80 public static function is_current_user_can_edit( $post_id = 0 ) {
81 $post = get_post( $post_id );
82
83 if ( ! $post ) {
84 return false;
85 }
86
87 if ( 'trash' === get_post_status( $post->ID ) ) {
88 return false;
89 }
90
91 if ( ! self::is_current_user_can_edit_post_type( $post->post_type ) ) {
92 return false;
93 }
94
95 $post_type_object = get_post_type_object( $post->post_type );
96
97 if ( ! isset( $post_type_object->cap->edit_post ) ) {
98 return false;
99 }
100
101 $edit_cap = $post_type_object->cap->edit_post;
102 if ( ! current_user_can( $edit_cap, $post->ID ) ) {
103 return false;
104 }
105
106 if ( intval( get_option( 'page_for_posts' ) ) === $post->ID ) {
107 return false;
108 }
109
110 return true;
111 }
112
113 /**
114 * Is current user can access elementor.
115 *
116 * Whether the current user role is not excluded by Elementor Settings.
117 *
118 * @since 2.1.7
119 * @access public
120 * @static
121 *
122 * @return bool True if can access, False otherwise.
123 */
124 public static function is_current_user_in_editing_black_list() {
125 $user = wp_get_current_user();
126 $exclude_roles = get_option( 'elementor_exclude_user_roles', [] );
127
128 $compare_roles = array_intersect( $user->roles, $exclude_roles );
129 if ( ! empty( $compare_roles ) ) {
130 return false;
131 }
132
133 return true;
134 }
135
136 /**
137 * Is current user can edit post type.
138 *
139 * Whether the current user can edit the given post type.
140 *
141 * @since 1.9.0
142 * @access public
143 * @static
144 *
145 * @param string $post_type the post type slug to check.
146 *
147 * @return bool True if can edit, False otherwise.
148 */
149 public static function is_current_user_can_edit_post_type( $post_type ) {
150 if ( ! self::is_current_user_in_editing_black_list() ) {
151 return false;
152 }
153
154 if ( ! Utils::is_post_type_support( $post_type ) ) {
155 return false;
156 }
157
158 $post_type_object = get_post_type_object( $post_type );
159
160 if ( ! current_user_can( $post_type_object->cap->edit_posts ) ) {
161 return false;
162 }
163
164 return true;
165 }
166
167 /**
168 * Get user notices.
169 *
170 * Retrieve the list of notices for the current user.
171 *
172 * @since 2.0.0
173 * @access public
174 * @static
175 *
176 * @return array A list of user notices.
177 */
178 public static function get_user_notices() {
179 $notices = get_user_meta( get_current_user_id(), self::ADMIN_NOTICES_KEY, true );
180 return is_array( $notices ) ? $notices : [];
181 }
182
183 /**
184 * Is user notice viewed.
185 *
186 * Whether the notice was viewed by the user.
187 *
188 * @since 1.0.0
189 * @access public
190 * @static
191 *
192 * @param int $notice_id The notice ID.
193 *
194 * @return bool Whether the notice was viewed by the user.
195 */
196 public static function is_user_notice_viewed( $notice_id ) {
197 $notices = self::get_user_notices();
198
199 if ( empty( $notices[ $notice_id ] ) ) {
200 return false;
201 }
202
203 // BC: Handles old structure ( `[ 'notice_id' => 'true' ]` ).
204 if ( 'true' === $notices[ $notice_id ] ) {
205 return true;
206 }
207
208 return $notices[ $notice_id ]['is_viewed'] ?? false;
209 }
210
211 /**
212 * Set admin notice as viewed.
213 *
214 * Flag the user admin notice as viewed using an authenticated ajax request.
215 *
216 * Fired by `wp_ajax_elementor_set_admin_notice_viewed` action.
217 *
218 * @since 1.0.0
219 * @access public
220 * @static
221 */
222 public static function ajax_set_admin_notice_viewed() {
223 // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification
224 $notice_id = Utils::get_super_global_value( $_REQUEST, 'notice_id' );
225
226 if ( ! $notice_id ) {
227 wp_die();
228 }
229
230 self::set_user_notice( $notice_id );
231
232 if ( ! wp_doing_ajax() ) {
233 wp_safe_redirect( admin_url() );
234 die;
235 }
236
237 wp_die();
238 }
239
240 /**
241 * @param $notice_id
242 * @param $is_viewed
243 * @param $meta
244 *
245 * @return void
246 */
247 public static function set_user_notice( $notice_id, $is_viewed = true, $meta = null ) {
248 $notices = self::get_user_notices();
249
250 if ( ! is_array( $meta ) ) {
251 $meta = $notices[ $notice_id ]['meta'] ?? [];
252 }
253
254 $notices[ $notice_id ] = [
255 'is_viewed' => $is_viewed,
256 'meta' => $meta,
257 ];
258
259 update_user_meta( get_current_user_id(), self::ADMIN_NOTICES_KEY, $notices );
260 }
261
262 /**
263 * @since 2.1.0
264 * @access public
265 * @static
266 */
267 public static function set_introduction_viewed( array $data ) {
268 $user_introduction_meta = self::get_introduction_meta();
269
270 $user_introduction_meta[ $data['introductionKey'] ] = true;
271
272 update_user_meta( get_current_user_id(), self::INTRODUCTION_KEY, $user_introduction_meta );
273 }
274
275 /**
276 * @throws \Exception
277 */
278 public static function register_as_beta_tester( array $data ) {
279 if ( ! current_user_can( 'install_plugins' ) ) {
280 throw new \Exception( __( 'You do not have permissions to install plugins on this site.', 'elementor' ) );
281 }
282
283 update_user_meta( get_current_user_id(), self::BETA_TESTER_META_KEY, true );
284 $response = wp_safe_remote_post(
285 self::BETA_TESTER_API_URL,
286 [
287 'timeout' => 25,
288 'body' => [
289 'api_version' => ELEMENTOR_VERSION,
290 'site_lang' => get_bloginfo( 'language' ),
291 'beta_tester_email' => $data['betaTesterEmail'],
292 ],
293 ]
294 );
295
296 $response_code = (int) wp_remote_retrieve_response_code( $response );
297
298 if ( 200 === $response_code ) {
299 self::set_introduction_viewed( [
300 'introductionKey' => Beta_Testers::BETA_TESTER_SIGNUP,
301 ] );
302 }
303 }
304
305 /**
306 * @param string $key
307 *
308 * @return array|mixed|string
309 * @since 2.1.0
310 * @access public
311 * @static
312 */
313 public static function get_introduction_meta( $key = '' ) {
314 $user_introduction_meta = get_user_meta( get_current_user_id(), self::INTRODUCTION_KEY, true );
315
316 if ( ! $user_introduction_meta ) {
317 $user_introduction_meta = [];
318 }
319
320 if ( $key ) {
321 return empty( $user_introduction_meta[ $key ] ) ? '' : $user_introduction_meta[ $key ];
322 }
323
324 return $user_introduction_meta;
325 }
326
327 /**
328 * Get a user option with default value as fallback.
329 *
330 * @param string $option - Option key.
331 * @param int $user_id - User ID
332 * @param mixed $default - Default fallback value.
333 *
334 * @return mixed
335 */
336 public static function get_user_option_with_default( $option, $user_id, $default ) {
337 $value = get_user_option( $option, $user_id );
338
339 return ( false === $value ) ? $default : $value;
340 }
341 }
342