/app/rule/memberroles/class-ms-rule-memberroles-model.php

  1. <?php 
  2. /** 
  3. * Membership Member Roles Rule class. 
  4. * 
  5. * Persisted by Membership class. 
  6. * 
  7. * @since 1.0.0 
  8. * 
  9. * @package Membership2 
  10. * @subpackage Model 
  11. */ 
  12. class MS_Rule_MemberRoles_Model extends MS_Rule { 
  13.  
  14. /** 
  15. * Rule type. 
  16. * 
  17. * @since 1.0.0 
  18. * 
  19. * @var string $rule_type 
  20. */ 
  21. protected $rule_type = MS_Rule_MemberRoles::RULE_ID; 
  22.  
  23. /** 
  24. * List of capabilities that are effectively used for the current user 
  25. * 
  26. * @since 1.0.0 
  27. * 
  28. * @var array 
  29. */ 
  30. static protected $real_caps = array(); 
  31.  
  32. /** 
  33. * Caches the get_content_array output 
  34. * 
  35. * @var array 
  36. */ 
  37. protected $_content_array = null; 
  38.  
  39.  
  40. /** 
  41. * Returns the active flag for a specific rule. 
  42. * State depends on Add-on 
  43. * 
  44. * @since 1.0.0 
  45. * @return bool 
  46. */ 
  47. static public function is_active() { 
  48. $def = MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_MEMBERCAPS ); 
  49. $adv = MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_MEMBERCAPS_ADV ); 
  50. return $def && ! $adv; 
  51.  
  52. /** 
  53. * Initializes the object as early as possible 
  54. * 
  55. * @since 1.0.0 
  56. */ 
  57. public function prepare_obj() { 
  58. $this->_content_array = null; 
  59.  
  60. /** 
  61. * Set initial protection. 
  62. * 
  63. * @since 1.0.0 
  64. */ 
  65. public function protect_content() { 
  66. parent::protect_content(); 
  67.  
  68. $this->add_filter( 'user_has_cap', 'prepare_caps', 1, 4 ); 
  69. $this->add_filter( 'user_has_cap', 'modify_caps', 10, 4 ); 
  70.  
  71. /** 
  72. * Set initial protection. 
  73. * 
  74. * @since 1.0.0 
  75. */ 
  76. public function protect_admin_content() { 
  77. parent::protect_admin_content(); 
  78.  
  79. $this->add_filter( 'user_has_cap', 'prepare_caps', 1, 4 ); 
  80. $this->add_filter( 'user_has_cap', 'modify_caps', 10, 4 ); 
  81.  
  82. /** 
  83. * Verify access to the current content. 
  84. * 
  85. * Always returns null since this rule modifies the capabilities of the 
  86. * current user and does not directly block access to any page. 
  87. * 
  88. * @since 1.0.0 
  89. * 
  90. * @return bool|null True if has access, false otherwise. 
  91. * Null means: Rule not relevant for current page. 
  92. */ 
  93. public function has_access( $id, $admin_has_access = true ) { 
  94. return null; 
  95.  
  96. /** 
  97. * Prepares the list of effective capabilities to use 
  98. * 
  99. * Relevant Action Hooks: 
  100. * - user_has_cap 
  101. * 
  102. * @since 1.0.0 
  103. * 
  104. * @param array $allcaps An array of all the role's capabilities. 
  105. * @param array $caps Actual capabilities for meta capability. 
  106. * @param array $args Optional parameters passed to has_cap(), typically object ID. 
  107. */ 
  108. public function prepare_caps( $allcaps, $caps, $args, $user ) { 
  109. global $wp_roles; 
  110.  
  111. if ( isset( self::$real_caps[$user->ID] ) ) { 
  112. // Only run the init code once for each user-ID. 
  113. return $allcaps; 
  114. } else { 
  115. // First get a list of the users default capabilities. 
  116. self::$real_caps[$user->ID] = $allcaps; 
  117.  
  118. $all_roles = $wp_roles->roles; 
  119.  
  120. foreach ( $this->rule_value as $role => $state ) { 
  121. if ( ! $state ) { continue; } 
  122.  
  123. if ( isset( $all_roles[ $role ] ) 
  124. && is_array( $all_roles[ $role ]['capabilities'] ) 
  125. ) { 
  126. $caps = $all_roles[ $role ]['capabilities']; 
  127. $caps = lib3()->array->get( $caps ); 
  128.  
  129. // Only add additional capabilities from now on... 
  130. foreach ( $caps as $key => $value ) { 
  131. if ( $value ) { self::$real_caps[$user->ID][$key] = 1; } 
  132.  
  133. return $allcaps; 
  134.  
  135. /** 
  136. * Modify the users capabilities. 
  137. * 
  138. * Relevant Action Hooks: 
  139. * - user_has_cap 
  140. * 
  141. * @since 1.0.0 
  142. * 
  143. * @param array $allcaps An array of all the role's capabilities. 
  144. * @param array $caps Actual capabilities for meta capability. 
  145. * @param array $args Optional parameters passed to has_cap(), typically object ID. 
  146. */ 
  147. public function modify_caps( $allcaps, $caps, $args, $user ) { 
  148. if ( ! isset( self::$real_caps[$user->ID] ) ) { 
  149. self::$real_caps[$user->ID] = $allcaps; 
  150.  
  151. return apply_filters( 
  152. 'ms_rule_memberroles_model_modify_caps',  
  153. self::$real_caps[$user->ID],  
  154. $caps,  
  155. $args,  
  156. $user,  
  157. $this 
  158. ); 
  159.  
  160. /** 
  161. * Get a simple array of capabilties (e.g. for display in select lists) 
  162. * 
  163. * @since 1.0.0 
  164. * @global array $menu 
  165. * 
  166. * @return array { 
  167. * @type string $id The id. 
  168. * @type string $name The name. 
  169. * } 
  170. */ 
  171. public function get_roles( $args = null ) { 
  172. global $wp_roles; 
  173.  
  174. if ( null === $this->_content_array ) { 
  175. // User-Roles are only available in Accessible Content tab, so always display all roles. 
  176. $this->_content_array = array(); 
  177.  
  178. $exclude = apply_filters( 
  179. 'ms_rule_memberroles_model_exclude',  
  180. array( 'administrator' ) 
  181. ); 
  182.  
  183. $all_roles = $wp_roles->roles; 
  184.  
  185. // Make sure the rule_value only contains valid items. 
  186. $rule_value = array_intersect_key( 
  187. $this->rule_value,  
  188. $all_roles 
  189. ); 
  190. $this->rule_value = lib3()->array->get( $rule_value ); 
  191.  
  192. foreach ( $all_roles as $key => $role ) { 
  193. if ( in_array( $key, $exclude ) ) { continue; } 
  194. $this->_content_array[$key] = $role['name']; 
  195.  
  196. $this->_content_array = apply_filters( 
  197. 'ms_rule_memberroles_model_get_content_array',  
  198. $this->_content_array,  
  199. $this 
  200. ); 
  201.  
  202. $contents = $this->_content_array; 
  203.  
  204. // Search the shortcode-tag... 
  205. if ( ! empty( $args['s'] ) ) { 
  206. foreach ( $contents as $key => $name ) { 
  207. if ( false === stripos( $name, $args['s'] ) ) { 
  208. unset( $contents[$key] ); 
  209.  
  210. $filter = self::get_exclude_include( $args ); 
  211. if ( is_array( $filter->include ) ) { 
  212. $contents = array_intersect_key( $contents, array_flip( $filter->include ) ); 
  213. } elseif ( is_array( $filter->exclude ) ) { 
  214. $contents = array_diff_key( $contents, array_flip( $filter->exclude ) ); 
  215.  
  216. // Pagination 
  217. if ( ! empty( $args['posts_per_page'] ) ) { 
  218. $total = $args['posts_per_page']; 
  219. $offset = ! empty( $args['offset'] ) ? $args['offset'] : 0; 
  220. $contents = array_slice( $contents, $offset, $total ); 
  221.  
  222. return $contents; 
  223.  
  224. /** 
  225. * Get content to protect. An array of objects is returned. 
  226. * 
  227. * @since 1.0.0 
  228. * @param $args The query post args 
  229. * @return array The contents array. 
  230. */ 
  231. public function get_contents( $args = null ) { 
  232. $contents = array(); 
  233. $roles = $this->get_roles( $args ); 
  234.  
  235. foreach ( $roles as $key => $rolename ) { 
  236. $content = (object) array(); 
  237.  
  238. $content->id = $key; 
  239. $content->title = $rolename; 
  240. $content->name = $rolename; 
  241. $content->post_title = $rolename; 
  242. $content->type = MS_Rule_MemberRoles::RULE_ID; 
  243. $content->access = $this->get_rule_value( $key ); 
  244.  
  245. $contents[ $key ] = $content; 
  246.  
  247. return apply_filters( 
  248. 'ms_rule_memberroles_model_get_contents',  
  249. $contents,  
  250. $args,  
  251. $this 
  252. ); 
  253.  
  254. /** 
  255. * Get the total content count. 
  256. * Used in Dashboard to display how many special pages are protected. 
  257. * 
  258. * @since 1.0.0 
  259. * 
  260. * @param $args The query post args 
  261. * @see @link http://codex.wordpress.org/Class_Reference/WP_Query 
  262. * @return int The total content count. 
  263. */ 
  264. public function get_content_count( $args = null ) { 
  265. $args['posts_per_page'] = 0; 
  266. $args['offset'] = false; 
  267. $count = count( $this->get_contents( $args ) ); 
  268.  
  269. return apply_filters( 
  270. 'ms_rule_memberroles_get_content_count',  
  271. $count,  
  272. $args 
  273. ); 
  274.  
.