class-vc-roles.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. die( '-1' );
  4. }
  5. /**
  6. * Manage role.
  7. * @since 4.8
  8. *
  9. * Class Vc_Roles
  10. */
  11. class Vc_Roles {
  12. protected $post_types = false;
  13. protected $vc_excluded_post_types = false;
  14. protected $parts = array(
  15. 'post_types',
  16. 'backend_editor',
  17. 'frontend_editor',
  18. 'post_settings',
  19. 'settings',
  20. 'templates',
  21. 'shortcodes',
  22. 'grid_builder',
  23. 'presets',
  24. 'dragndrop',
  25. );
  26. /**
  27. * Get list of parts
  28. * @return mixed
  29. */
  30. public function getParts() {
  31. return apply_filters( 'vc_roles_parts_list', $this->parts );
  32. }
  33. /**
  34. * Check required capability for this role to have user access.
  35. *
  36. * @param $part
  37. *
  38. * @return array|string
  39. */
  40. public function getPartCapability( $part ) {
  41. return 'settings' !== $part ? array(
  42. 'edit_posts',
  43. 'edit_pages',
  44. ) : 'manage_options';
  45. }
  46. /**
  47. * @param $role
  48. * @param $caps
  49. * @return bool
  50. */
  51. public function hasRoleCapability( $role, $caps ) {
  52. $has = false;
  53. $wp_role = get_role( $role );
  54. if ( is_string( $caps ) ) {
  55. $has = $wp_role->has_cap( $caps );
  56. } elseif ( is_array( $caps ) ) {
  57. $i = 0;
  58. $count = count( $caps );
  59. while ( false === $has && $i < $count ) {
  60. $has = $this->hasRoleCapability( $role, $caps[ $i ++ ] );
  61. }
  62. }
  63. return $has;
  64. }
  65. /**
  66. * @return \WP_Roles
  67. */
  68. public function getWpRoles() {
  69. global $wp_roles;
  70. if ( function_exists( 'wp_roles' ) ) {
  71. return $wp_roles;
  72. } else {
  73. if ( ! isset( $wp_roles ) ) {
  74. // @codingStandardsIgnoreLine
  75. $wp_roles = new WP_Roles();
  76. }
  77. }
  78. return $wp_roles;
  79. }
  80. /**
  81. * @param array $params
  82. * @return array
  83. * @throws \Exception
  84. */
  85. public function save( $params = array() ) {
  86. $data = array( 'message' => '' );
  87. $roles = $this->getWpRoles();
  88. $editable_roles = get_editable_roles();
  89. foreach ( $params as $role => $parts ) {
  90. if ( is_string( $parts ) ) {
  91. $parts = json_decode( stripslashes( $parts ), true );
  92. }
  93. if ( isset( $editable_roles[ $role ] ) ) {
  94. foreach ( $parts as $part => $settings ) {
  95. $part_key = vc_role_access()->who( $role )->part( $part )->getStateKey();
  96. $stateValue = '0';
  97. $roles->use_db = false; // Disable saving in DB on every cap change
  98. foreach ( $settings as $key => $value ) {
  99. if ( '_state' === $key ) {
  100. $stateValue = in_array( $value, array(
  101. '0',
  102. '1',
  103. ), true ) ? (bool) $value : $value;
  104. } else {
  105. if ( empty( $value ) ) {
  106. $roles->remove_cap( $role, $part_key . '/' . $key );
  107. } else {
  108. $roles->add_cap( $role, $part_key . '/' . $key, true );
  109. }
  110. }
  111. }
  112. $roles->use_db = true; // Enable for the lat change in cap of role to store data in DB
  113. $roles->add_cap( $role, $part_key, $stateValue );
  114. }
  115. }
  116. }
  117. $data['message'] = esc_html__( 'Roles settings successfully saved.', 'js_composer' );
  118. return $data;
  119. }
  120. /**
  121. * @return array|bool
  122. */
  123. public function getPostTypes() {
  124. if ( false === $this->post_types ) {
  125. $this->post_types = array();
  126. $exclude = $this->getExcludePostTypes();
  127. foreach ( get_post_types( array( 'public' => true ) ) as $post_type ) {
  128. if ( ! in_array( $post_type, $exclude, true ) ) {
  129. $this->post_types[] = array(
  130. $post_type,
  131. $post_type,
  132. );
  133. }
  134. }
  135. }
  136. return $this->post_types;
  137. }
  138. /**
  139. * @return bool|mixed|void
  140. */
  141. public function getExcludePostTypes() {
  142. if ( false === $this->vc_excluded_post_types ) {
  143. $this->vc_excluded_post_types = apply_filters( 'vc_settings_exclude_post_type', array(
  144. 'attachment',
  145. 'revision',
  146. 'nav_menu_item',
  147. 'mediapage',
  148. ) );
  149. }
  150. return $this->vc_excluded_post_types;
  151. }
  152. }