MS_Rule_MemberRoles_Model

Membership Member Roles Rule class.

Defined (1)

The class is defined in the following location(s).

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