MS_Rule_MemberCaps_Model

Membership Member Capabilities Rule class.

Defined (1)

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

/app/rule/membercaps/class-ms-rule-membercaps-model.php  
  1. class MS_Rule_MemberCaps_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_MemberCaps::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. * Returns the active flag for a specific rule. 
  25. * State depends on Add-on 
  26. * @since 1.0.0 
  27. * @return bool 
  28. */ 
  29. static public function is_active() { 
  30. $def = MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_MEMBERCAPS ); 
  31. $adv = MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_MEMBERCAPS_ADV ); 
  32. return $def && $adv; 
  33.  
  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. $caps = $this->rule_value; 
  93.  
  94. if ( null === self::$real_caps ) { 
  95. // First get a list of the users default capabilities. 
  96. self::$real_caps = $allcaps; 
  97.  
  98. // Use the permissions of the first rule without checking. 
  99. foreach ( $caps as $key => $value ) { 
  100. self::$real_caps[$key] = $value; 
  101. } else { 
  102. // Only add additional capabilities from now on... 
  103. foreach ( $caps as $key => $value ) { 
  104. if ( $value ) { self::$real_caps[$key] = 1; } 
  105.  
  106. return $allcaps; 
  107.  
  108. /** 
  109. * Modify the users capabilities. 
  110. * Relevant Action Hooks: 
  111. * - user_has_cap 
  112. * @since 1.0.0 
  113. * @param array $allcaps An array of all the role's capabilities. 
  114. * @param array $caps Actual capabilities for meta capability. 
  115. * @param array $args Optional parameters passed to has_cap(), typically object ID. 
  116. */ 
  117. public function modify_caps( $allcaps, $caps, $args, $user ) { 
  118. if ( ! isset( self::$real_caps[$user->ID] ) ) { 
  119. self::$real_caps[$user->ID] = $allcaps; 
  120.  
  121. return apply_filters( 
  122. 'ms_rule_membercaps_model_modify_caps',  
  123. self::$real_caps[$user->ID],  
  124. $caps,  
  125. $args,  
  126. $user,  
  127. $this 
  128. ); 
  129.  
  130. /** 
  131. * Get a simple array of capabilties (e.g. for display in select lists) 
  132. * @since 1.0.0 
  133. * @global array $menu 
  134. * @return array { 
  135. * @type string $id The id. 
  136. * @type string $name The name. 
  137. * } 
  138. */ 
  139. public function get_capabilities( $args = null ) { 
  140. if ( null === $this->_content_array ) { 
  141. $this->_content_array = array(); 
  142. $member = MS_Model_Member::get_current_member(); 
  143. $capslist = $member->wp_user->allcaps; 
  144.  
  145. $ignored_caps = array( 
  146. 'level_10' => 1,  
  147. 'level_9' => 1,  
  148. 'level_8' => 1,  
  149. 'level_7' => 1,  
  150. 'level_6' => 1,  
  151. 'level_5' => 1,  
  152. 'level_4' => 1,  
  153. 'level_3' => 1,  
  154. 'level_2' => 1,  
  155. 'level_1' => 1,  
  156. 'level_0' => 1,  
  157. 'administrator' => 1,  
  158. ); 
  159.  
  160. $capslist = array_diff_assoc( $capslist, $ignored_caps ); 
  161. $capslist = array_keys( $capslist ); 
  162.  
  163. /** 
  164. * Exclude certain capabilities for security reasons. 
  165. * @since 1.0.0 
  166. * @var array 
  167. */ 
  168. $exclude = apply_filters( 
  169. 'ms_rule_membercaps_model_exclude',  
  170. array( 
  171. MS_Plugin::instance()->controller->capability,  
  172. 'edit_plugins',  
  173. 'delete_plugins',  
  174. 'edit_files',  
  175. 'edit_users',  
  176. 'delete_users',  
  177. 'remove_users',  
  178. 'promote_users',  
  179. 'list_users',  
  180. ); 
  181.  
  182. $capslist = array_diff( $capslist, $exclude ); 
  183. $this->_content_array = array_combine( $capslist, $capslist ); 
  184.  
  185. // Make sure the rule_value only contains valid items. 
  186. $rule_value = array_intersect_key( 
  187. $this->rule_value,  
  188. $this->_content_array 
  189. ); 
  190. $this->rule_value = lib3()->array->get( $rule_value ); 
  191.  
  192. // If not visitor membership, just show Membership2 
  193. if ( ! $this->get_membership()->is_base() ) { 
  194. $this->_content_array = array_intersect_key( 
  195. $this->_content_array,  
  196. $this->rule_value 
  197. ); 
  198.  
  199. $this->_content_array = apply_filters( 
  200. 'ms_rule_membercaps_model_get_content_array',  
  201. $this->_content_array,  
  202. $this 
  203. ); 
  204.  
  205. $contents = $this->_content_array; 
  206.  
  207. // Search the shortcode-tag... 
  208. if ( ! empty( $args['s'] ) ) { 
  209. foreach ( $contents as $key => $name ) { 
  210. if ( false === stripos( $name, $args['s'] ) ) { 
  211. unset( $contents[$key] ); 
  212.  
  213. $filter = self::get_exclude_include( $args ); 
  214. if ( is_array( $filter->include ) ) { 
  215. $contents = array_intersect( $contents, $filter->include ); 
  216. } elseif ( is_array( $filter->exclude ) ) { 
  217. $contents = array_diff( $contents, $filter->exclude ); 
  218.  
  219. // Pagination 
  220. if ( ! empty( $args['posts_per_page'] ) ) { 
  221. $total = $args['posts_per_page']; 
  222. $offset = ! empty( $args['offset'] ) ? $args['offset'] : 0; 
  223. $contents = array_slice( $contents, $offset, $total ); 
  224.  
  225. return $contents; 
  226.  
  227. /** 
  228. * Get content to protect. An array of objects is returned. 
  229. * @since 1.0.0 
  230. * @param $args The query post args 
  231. * @return array The contents array. 
  232. */ 
  233. public function get_contents( $args = null ) { 
  234. $contents = array(); 
  235. $caps = $this->get_capabilities( $args ); 
  236.  
  237. foreach ( $caps as $key => $item ) { 
  238. $content = (object) array(); 
  239.  
  240. $content->id = $item; 
  241. $content->title = $item; 
  242. $content->name = $item; 
  243. $content->post_title = $item; 
  244. $content->type = MS_Rule_MemberCaps::RULE_ID; 
  245. $content->access = $this->get_rule_value( $key ); 
  246.  
  247. $contents[ $key ] = $content; 
  248.  
  249. return apply_filters( 
  250. 'ms_rule_membercaps_model_get_contents',  
  251. $contents,  
  252. $args,  
  253. $this 
  254. ); 
  255.  
  256. /** 
  257. * Get the total content count. 
  258. * Used in Dashboard to display how many special pages are protected. 
  259. * @since 1.0.0 
  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_membercaps_model_get_content_count',  
  271. $count,  
  272. $args 
  273. ); 
  274.