user.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace Elementor;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit; // Exit if accessed directly.
  5. }
  6. /**
  7. * Elementor user.
  8. *
  9. * Elementor user handler class is responsible for checking if the user can edit
  10. * with Elementor and displaying different admin notices.
  11. *
  12. * @since 1.0.0
  13. */
  14. class User {
  15. /**
  16. * The admin notices key.
  17. */
  18. const ADMIN_NOTICES_KEY = 'elementor_admin_notices';
  19. const INTRODUCTION_KEY = 'elementor_introduction';
  20. const INTRODUCTION_VERSION = 2;
  21. /**
  22. * Init.
  23. *
  24. * Initialize Elementor user.
  25. *
  26. * @since 1.0.0
  27. * @access public
  28. * @static
  29. */
  30. public static function init() {
  31. add_action( 'wp_ajax_elementor_set_admin_notice_viewed', [ __CLASS__, 'ajax_set_admin_notice_viewed' ] );
  32. add_action( 'elementor/ajax/register_actions', [ __CLASS__, 'register_ajax_actions' ] );
  33. }
  34. public static function register_ajax_actions() {
  35. Plugin::$instance->ajax->register_ajax_action( 'introduction_viewed', [ __CLASS__, 'set_introduction_viewed' ] );
  36. }
  37. /**
  38. * Is current user can edit.
  39. *
  40. * Whether the current user can edit the post.
  41. *
  42. * @since 1.0.0
  43. * @access public
  44. * @static
  45. *
  46. * @param int $post_id Optional. The post ID. Default is `0`.
  47. *
  48. * @return bool Whether the current user can edit the post.
  49. */
  50. public static function is_current_user_can_edit( $post_id = 0 ) {
  51. $post = get_post( $post_id );
  52. if ( ! $post ) {
  53. return false;
  54. }
  55. if ( 'trash' === get_post_status( $post_id ) ) {
  56. return false;
  57. }
  58. if ( ! self::is_current_user_can_edit_post_type( $post->post_type ) ) {
  59. return false;
  60. }
  61. $post_type_object = get_post_type_object( $post->post_type );
  62. if ( ! isset( $post_type_object->cap->edit_post ) ) {
  63. return false;
  64. }
  65. $edit_cap = $post_type_object->cap->edit_post;
  66. if ( ! current_user_can( $edit_cap, $post_id ) ) {
  67. return false;
  68. }
  69. if ( get_option( 'page_for_posts' ) === $post_id ) {
  70. return false;
  71. }
  72. return true;
  73. }
  74. /**
  75. * Is current user can access elementor.
  76. *
  77. * Whether the current user role is not excluded by Elementor Settings.
  78. *
  79. * @access public
  80. * @static
  81. *
  82. * @return bool True if can access, False otherwise.
  83. */
  84. public static function is_current_user_in_editing_black_list() {
  85. $user = wp_get_current_user();
  86. $exclude_roles = get_option( 'elementor_exclude_user_roles', [] );
  87. $compare_roles = array_intersect( $user->roles, $exclude_roles );
  88. if ( ! empty( $compare_roles ) ) {
  89. return false;
  90. }
  91. return true;
  92. }
  93. /**
  94. * Is current user can edit post type.
  95. *
  96. * Whether the current user can edit the given post type.
  97. *
  98. * @since 1.9.0
  99. * @access public
  100. * @static
  101. *
  102. * @param string $post_type the post type slug to check.
  103. *
  104. * @return bool True if can edit, False otherwise.
  105. */
  106. public static function is_current_user_can_edit_post_type( $post_type ) {
  107. if ( ! self::is_current_user_in_editing_black_list() ) {
  108. return false;
  109. }
  110. if ( ! Utils::is_post_type_support( $post_type ) ) {
  111. return false;
  112. }
  113. $post_type_object = get_post_type_object( $post_type );
  114. if ( ! current_user_can( $post_type_object->cap->edit_posts ) ) {
  115. return false;
  116. }
  117. return true;
  118. }
  119. /**
  120. * Get user notices.
  121. *
  122. * Retrieve the list of notices for the current user.
  123. *
  124. * @since 2.0.0
  125. * @access private
  126. * @static
  127. *
  128. * @return array A list of user notices.
  129. */
  130. private static function get_user_notices() {
  131. return get_user_meta( get_current_user_id(), self::ADMIN_NOTICES_KEY, true );
  132. }
  133. /**
  134. * Is user notice viewed.
  135. *
  136. * Whether the notice was viewed by the user.
  137. *
  138. * @since 1.0.0
  139. * @access public
  140. * @static
  141. *
  142. * @param int $notice_id The notice ID.
  143. *
  144. * @return bool Whether the notice was viewed by the user.
  145. */
  146. public static function is_user_notice_viewed( $notice_id ) {
  147. $notices = self::get_user_notices();
  148. if ( empty( $notices ) || empty( $notices[ $notice_id ] ) ) {
  149. return false;
  150. }
  151. return true;
  152. }
  153. /**
  154. * Set admin notice as viewed.
  155. *
  156. * Flag the user admin notice as viewed using an authenticated ajax request.
  157. *
  158. * Fired by `wp_ajax_elementor_set_admin_notice_viewed` action.
  159. *
  160. * @since 1.0.0
  161. * @access public
  162. * @static
  163. */
  164. public static function ajax_set_admin_notice_viewed() {
  165. if ( empty( $_POST['notice_id'] ) ) {
  166. die;
  167. }
  168. $notices = self::get_user_notices();
  169. if ( empty( $notices ) ) {
  170. $notices = [];
  171. }
  172. $notices[ $_POST['notice_id'] ] = 'true';
  173. update_user_meta( get_current_user_id(), self::ADMIN_NOTICES_KEY, $notices );
  174. die;
  175. }
  176. public static function set_introduction_viewed() {
  177. $user_introduction_meta = self::get_introduction_meta();
  178. if ( ! $user_introduction_meta ) {
  179. $user_introduction_meta = [];
  180. }
  181. $user_introduction_meta[ self::INTRODUCTION_VERSION ] = true;
  182. update_user_meta( get_current_user_id(), self::INTRODUCTION_KEY, $user_introduction_meta );
  183. }
  184. public static function is_should_view_introduction() {
  185. $user_introduction_meta = self::get_introduction_meta();
  186. return empty( $user_introduction_meta[ self::INTRODUCTION_VERSION ] );
  187. }
  188. private static function get_introduction_meta() {
  189. return get_user_meta( get_current_user_id(), self::INTRODUCTION_KEY, true );
  190. }
  191. }
  192. User::init();