bc-access-rules-4.8.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. die( '-1' );
  4. }
  5. // Part BC: Post types
  6. // =========================
  7. /**
  8. * @param $state
  9. * @return bool|string
  10. */
  11. function vc_bc_access_rule_48_post_type_get_state( $state ) {
  12. if ( null === $state ) {
  13. $content_types = vc_settings()->get( 'content_types' );
  14. if ( empty( $content_types ) ) {
  15. $state = true;
  16. } else {
  17. $state = 'custom';
  18. }
  19. }
  20. return $state;
  21. }
  22. /**
  23. * @param $value
  24. * @param $role
  25. * @param $rule
  26. * @return bool
  27. * @throws \Exception
  28. */
  29. function vc_bc_access_rule_48_post_type_rule( $value, $role, $rule ) {
  30. if ( ! $role ) {
  31. return $value;
  32. }
  33. global $vc_bc_access_rule_48_editor_post_types;
  34. $part = vc_role_access()->who( $role->name )->part( 'post_types' );
  35. if ( ! isset( $part->getRole()->capabilities[ $part->getStateKey() ] ) ) {
  36. if ( is_null( $vc_bc_access_rule_48_editor_post_types ) ) {
  37. $pt_array = vc_settings()->get( 'content_types' );
  38. $vc_bc_access_rule_48_editor_post_types = $pt_array ? $pt_array : vc_default_editor_post_types();
  39. }
  40. return in_array( $rule, $vc_bc_access_rule_48_editor_post_types, true );
  41. }
  42. return $value;
  43. }
  44. // Part BC: shortcodes
  45. // =========================
  46. /**
  47. * @param $state
  48. * @param $role
  49. * @return bool|string
  50. */
  51. function vc_bc_access_rule_48_shortcodes_get_state( $state, $role ) {
  52. if ( ! $role ) {
  53. return $state;
  54. }
  55. if ( null === $state ) {
  56. $group_access_settings = vc_settings()->get( 'groups_access_rules' );
  57. if ( ! isset( $group_access_settings[ $role->name ]['shortcodes'] ) ) {
  58. $state = true;
  59. } else {
  60. $state = 'custom';
  61. }
  62. }
  63. return $state;
  64. }
  65. /**
  66. * @param $value
  67. * @param $role
  68. * @param $rule
  69. * @return bool
  70. * @throws \Exception
  71. */
  72. function vc_bc_access_rule_48_shortcodes_rule( $value, $role, $rule ) {
  73. if ( ! $role ) {
  74. return $value;
  75. }
  76. if ( ! vc_bc_access_get_shortcodes_state_is_set( $role ) ) {
  77. if ( preg_match( '/_edit$/', $rule ) ) {
  78. return false;
  79. }
  80. $group_access_settings = vc_settings()->get( 'groups_access_rules' );
  81. if ( isset( $group_access_settings[ $role->name ]['shortcodes'] ) && ! empty( $group_access_settings[ $role->name ]['shortcodes'] ) ) {
  82. $rule = preg_replace( '/_all$/', '', $rule );
  83. return 'vc_row' === $rule || ( isset( $group_access_settings[ $role->name ]['shortcodes'][ $rule ] ) && 1 === (int) $group_access_settings[ $role->name ]['shortcodes'][ $rule ] );
  84. } else {
  85. return true;
  86. }
  87. }
  88. return $value;
  89. }
  90. /**
  91. * Check is state set
  92. *
  93. * @param $role
  94. *
  95. * @return bool
  96. * @throws \Exception
  97. */
  98. function vc_bc_access_get_shortcodes_state_is_set( $role ) {
  99. if ( ! $role ) {
  100. return false;
  101. }
  102. $part = vc_role_access()->who( $role->name )->part( 'shortcodes' );
  103. return isset( $part->getRole()->capabilities[ $part->getStateKey() ] );
  104. }
  105. // Part BC: backened editor
  106. // ===========================
  107. /**
  108. * @param $state
  109. * @param $role
  110. * @return bool|string
  111. */
  112. function vc_bc_access_rule_48_backend_editor_get_state( $state, $role ) {
  113. if ( ! $role ) {
  114. return $state;
  115. }
  116. if ( null === $state ) {
  117. $group_access_settings = vc_settings()->get( 'groups_access_rules' );
  118. if ( ! isset( $group_access_settings[ $role->name ]['show'] ) || 'all' === $group_access_settings[ $role->name ]['show'] ) {
  119. $state = true;
  120. } elseif ( 'no' === $group_access_settings[ $role->name ]['show'] ) {
  121. $state = false;
  122. } else {
  123. $state = 'default';
  124. }
  125. }
  126. return $state;
  127. }
  128. /**
  129. * @param $state
  130. * @param $role
  131. * @return bool
  132. */
  133. function vc_bc_access_rule_48_frontend_editor_get_state( $state, $role ) {
  134. if ( ! $role ) {
  135. return $state;
  136. }
  137. if ( null === $state ) {
  138. $group_access_settings = vc_settings()->get( 'groups_access_rules' );
  139. if ( isset( $group_access_settings[ $role->name ]['show'] ) && 'no' === $group_access_settings[ $role->name ]['show'] ) {
  140. $state = false;
  141. } else {
  142. $state = true;
  143. }
  144. }
  145. return $state;
  146. }
  147. /**
  148. * @param $value
  149. * @param $role
  150. * @return bool
  151. * @throws \Exception
  152. */
  153. function vc_bc_access_rule_48_backend_editor_can_disabled_ce_editor_rule( $value, $role ) {
  154. if ( ! $role ) {
  155. return $value;
  156. }
  157. $part = vc_role_access()->who( $role->name )->part( 'backend_editor' );
  158. if ( ! isset( $part->getRole()->capabilities[ $part->getStateKey() ] ) ) {
  159. $group_access_settings = vc_settings()->get( 'groups_access_rules' );
  160. return isset( $group_access_settings[ $role->name ]['show'] ) && 'only' === $group_access_settings[ $role->name ]['show'];
  161. }
  162. return $value;
  163. }
  164. /**
  165. * @param $role
  166. * @return mixed
  167. * @throws \Exception
  168. */
  169. function vc_bc_access_rule_48_backend_editor_add_cap_disabled_ce_editor( $role ) {
  170. if ( ! $role ) {
  171. return $role;
  172. }
  173. $part = vc_role_access()->who( $role->name )->part( 'backend_editor' );
  174. if ( ! isset( $part->getRole()->capabilities[ $part->getStateKey() ] ) ) {
  175. $group_access_settings = vc_settings()->get( 'groups_access_rules' );
  176. if ( isset( $group_access_settings[ $role->name ]['show'] ) && 'only' === $group_access_settings[ $role->name ]['show'] ) {
  177. $role->capabilities[ $part->getStateKey() . '/disabled_ce_editor' ] = true;
  178. }
  179. }
  180. return $role;
  181. }
  182. function vc_bc_access_rule_48() {
  183. add_filter( 'vc_role_access_with_post_types_get_state', 'vc_bc_access_rule_48_post_type_get_state' );
  184. add_filter( 'vc_role_access_with_post_types_can', 'vc_bc_access_rule_48_post_type_rule', 10, 3 );
  185. add_filter( 'vc_role_access_with_shortcodes_get_state', 'vc_bc_access_rule_48_shortcodes_get_state', 10, 3 );
  186. add_filter( 'vc_role_access_with_shortcodes_can', 'vc_bc_access_rule_48_shortcodes_rule', 10, 3 );
  187. add_filter( 'vc_role_access_with_backend_editor_get_state', 'vc_bc_access_rule_48_backend_editor_get_state', 10, 3 );
  188. add_filter( 'vc_role_access_with_backend_editor_can_disabled_ce_editor', 'vc_bc_access_rule_48_backend_editor_can_disabled_ce_editor_rule', 10, 2 );
  189. add_filter( 'vc_role_access_with_frontend_editor_get_state', 'vc_bc_access_rule_48_frontend_editor_get_state', 10, 3 );
  190. add_filter( 'vc_role_access_all_caps_role', 'vc_bc_access_rule_48_backend_editor_add_cap_disabled_ce_editor' );
  191. }
  192. // BC function for shortcode
  193. add_action( 'vc_before_init', 'vc_bc_access_rule_48' );