MS_Controller_Metabox

Creates the Membership access metabox.

Defined (1)

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

/app/controller/class-ms-controller-metabox.php  
  1. class MS_Controller_Metabox extends MS_Controller { 
  2.  
  3. /** 
  4. * AJAX action constants. 
  5. * @since 1.0.0 
  6. * @var string 
  7. */ 
  8. const AJAX_ACTION_TOGGLE_ACCESS = 'toggle_metabox_access'; 
  9.  
  10. /** 
  11. * The custom post type used with Memberships and access. 
  12. * @since 1.0.0 
  13. * @var array 
  14. */ 
  15. private $post_types; 
  16.  
  17. /** 
  18. * The metabox ID. 
  19. * @since 1.0.0 
  20. * @var string 
  21. */ 
  22. private $metabox_id = 'ms-membership-access'; 
  23.  
  24. /** 
  25. * The metabox title. 
  26. * @since 1.0.0 
  27. * @var string 
  28. */ 
  29. private $metabox_title; 
  30.  
  31. /** 
  32. * Context for showing the metabox. 
  33. * @since 1.0.0 
  34. * @var string 
  35. */ 
  36. private $context = 'side'; 
  37.  
  38. /** 
  39. * Metabox priority. 
  40. * Effects position in the metabox hierarchy. 
  41. * @since 1.0.0 
  42. * @var string 
  43. */ 
  44. private $priority = 'high'; 
  45.  
  46. /** 
  47. * Prepare the metabox. 
  48. * @since 1.0.0 
  49. */ 
  50. public function __construct() { 
  51. parent::__construct(); 
  52.  
  53. $this->metabox_title = __( 'Membership Access', 'membership2' ); 
  54.  
  55. $post_types = array_merge( 
  56. array( 'page', 'post', 'attachment' ),  
  57. array() // PRO VERSION PARAMETER 
  58. ); 
  59.  
  60. $this->post_types = apply_filters( 
  61. 'ms_controller_membership_metabox_add_meta_boxes_post_types',  
  62. $post_types 
  63. ); 
  64.  
  65. if ( ! MS_Plugin::is_enabled() ) { 
  66. return $this; 
  67.  
  68. $this->add_action( 
  69. 'add_meta_boxes',  
  70. 'add_meta_boxes',  
  71. 10 
  72. ); 
  73.  
  74. $this->add_action( 
  75. 'admin_enqueue_scripts',  
  76. 'admin_enqueue_scripts' 
  77. ); 
  78.  
  79. $this->add_ajax_action( 
  80. self::AJAX_ACTION_TOGGLE_ACCESS,  
  81. 'ajax_action_toggle_metabox_access' 
  82. ); 
  83.  
  84. // Populates the WP editor with default contents of a page 
  85. $this->add_action( 
  86. 'the_editor_content',  
  87. 'show_default_content' 
  88. ); 
  89.  
  90. /** 
  91. * Handle Ajax toggle action. 
  92. * Related Action Hooks: 
  93. * - wp_ajax_toggle_metabox_access 
  94. * @since 1.0.0 
  95. */ 
  96. public function ajax_action_toggle_metabox_access() { 
  97. $fields = array( 'membership_id', 'rule_type', 'post_id' ); 
  98.  
  99. if ( 
  100. $this->verify_nonce() 
  101. && self::validate_required( $fields ) 
  102. && $this->is_admin_user() 
  103. ) { 
  104. $this->toggle_membership_access( 
  105. $_POST['post_id'],  
  106. $_POST['rule_type'],  
  107. $_POST['membership_id'] 
  108. ); 
  109.  
  110. $post = get_post( $_POST['post_id'] ); 
  111.  
  112. // Return the updated Membership metabox html via ajax response. 
  113. $this->membership_metabox( $post ); 
  114.  
  115. do_action( 
  116. 'ms_controller_membership_metabox_ajax_action_toggle_metabox_access',  
  117. $post,  
  118. $this 
  119. ); 
  120.  
  121. exit; 
  122.  
  123. /** 
  124. * Add the metabox for defined post types. 
  125. * @since 1.0.0 
  126. */ 
  127. public function add_meta_boxes() { 
  128. foreach ( $this->post_types as $post_type ) { 
  129. if ( ! $this->is_read_only( $post_type ) ) { 
  130. add_meta_box( 
  131. $this->metabox_id,  
  132. $this->metabox_title,  
  133. array( $this, 'membership_metabox' ),  
  134. $post_type,  
  135. $this->context,  
  136. $this->priority 
  137. ); 
  138.  
  139. do_action( 
  140. 'ms_controller_membership_metabox_add_meta_boxes',  
  141. $this 
  142. ); 
  143.  
  144. /** 
  145. * Membership metabox callback function for displaying the UI. 
  146. * @since 1.0.0 
  147. * @param object $post The current post object. 
  148. */ 
  149. public function membership_metabox( $post ) { 
  150. $data = array(); 
  151.  
  152. if ( MS_Model_Pages::is_membership_page() ) { 
  153. $data['special_page'] = true; 
  154. } else { 
  155. $all_memberships = MS_Model_Membership::get_memberships(); 
  156. $base = MS_Model_Membership::get_base(); 
  157. $data['base_id'] = $base->id; 
  158.  
  159. // Find the post-type of the current post. 
  160. if ( 'attachment' == $post->post_type ) { 
  161. $parent_id = $post->post_parent; 
  162. $post_type = get_post_type( $parent_id ); 
  163. } else { 
  164. $post_type = $post->post_type; 
  165.  
  166. // Get the base protection rule and check if post is protected. 
  167. $rule = $this->get_rule( $base, $post_type ); 
  168. $data['is_protected'] = ! $rule->has_access( $post->ID, false ); 
  169. $data['rule_type'] = $rule->rule_type; 
  170.  
  171. // Check each membership to see if the post is protected. 
  172. foreach ( $all_memberships as $membership ) { 
  173. if ( $membership->is_base ) { continue; } 
  174.  
  175. $rule = $this->get_rule( $membership, $post_type ); 
  176. $data['access'][ $membership->id ]['has_access'] = $rule->get_rule_value( $post->ID ); 
  177. $data['access'][ $membership->id ]['name'] = $membership->name; 
  178. $data['post_id'] = $post->ID; 
  179. $data['read_only'] = $this->is_read_only( $post->post_type ); 
  180.  
  181. $view = MS_Factory::create( 'MS_View_Metabox' ); 
  182. $view->data = apply_filters( 
  183. 'ms_view_membership_metabox_data',  
  184. $data,  
  185. $this 
  186. ); 
  187. $view->render(); 
  188.  
  189. /** 
  190. * Get rule accordingly to post type. 
  191. * @since 1.0.0 
  192. * @param MS_Model_Membership The membership to get rule from. 
  193. * @param string $post_type The post_type name of the queried post object. 
  194. * @return MS_Rule The rule model. 
  195. */ 
  196. private function get_rule( $membership, $post_type ) { 
  197. $rule = null; 
  198.  
  199. switch ( $post_type ) { 
  200. case 'post': 
  201. $rule = $membership->get_rule( MS_Rule_Post::RULE_ID ); 
  202. break; 
  203.  
  204. case 'page': 
  205. $rule = $membership->get_rule( MS_Rule_Page::RULE_ID ); 
  206. break; 
  207.  
  208. case 'attachment': 
  209. $rule = $membership->get_rule( MS_Rule_Media::RULE_ID ); 
  210. break; 
  211.  
  212. default: 
  213. if ( in_array( $post_type, MS_Rule_CptGroup_Model::get_custom_post_types() ) ) { 
  214. if ( MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_CPT_POST_BY_POST ) ) { 
  215. $rule = $membership->get_rule( MS_Rule_CptItem::RULE_ID ); 
  216. } else { 
  217. $rule = $membership->get_rule( MS_Rule_CptGroup::RULE_ID ); 
  218. } else { 
  219. $rule = $membership->get_rule( $post_type ); 
  220. break; 
  221.  
  222. return apply_filters( 
  223. 'ms_controller_metabox_get_rule',  
  224. $rule,  
  225. $membership,  
  226. $post_type,  
  227. $this 
  228. ); 
  229.  
  230. /** 
  231. * Toggle membership access. 
  232. * @since 1.0.0 
  233. * @param int $post_id The post id or attachment id to save access to. 
  234. * @param string $rule_type The membership rule type. 
  235. * @param array $membership_id The membership id to toggle access 
  236. */ 
  237. public function toggle_membership_access( $post_id, $rule_type, $membership_id ) { 
  238. if ( $this->is_admin_user() ) { 
  239. $membership = MS_Factory::load( 'MS_Model_Membership', $membership_id ); 
  240. $rule = $membership->get_rule( $rule_type ); 
  241. $protected = ! $rule->get_rule_value( $post_id ); 
  242.  
  243. if ( $membership->is_base() ) { 
  244. /** 
  245. * If we just modified the protection for the whole post then we 
  246. * have to update every single membership with the new rule 
  247. * value before changing the base rule itself. 
  248. */ 
  249. $all_memberships = MS_Model_Membership::get_memberships(); 
  250.  
  251. foreach ( $all_memberships as $the_membership ) { 
  252. if ( $the_membership->is_base ) { continue; } 
  253.  
  254. $the_rule = $the_membership->get_rule( $rule_type ); 
  255. if ( $protected ) { 
  256. $the_rule->give_access( $post_id ); 
  257. } else { 
  258. $the_rule->remove_access( $post_id ); 
  259.  
  260. $the_membership->set_rule( $rule_type, $the_rule ); 
  261. $the_membership->save(); 
  262.  
  263. if ( $rule ) { 
  264. if ( $protected ) { 
  265. $rule->give_access( $post_id ); 
  266. } else { 
  267. $rule->remove_access( $post_id ); 
  268.  
  269. $membership->set_rule( $rule_type, $rule ); 
  270. $membership->save(); 
  271.  
  272. do_action( 
  273. 'ms_controller_membership_metabox_toggle_membership_access',  
  274. $post_id,  
  275. $rule_type,  
  276. $membership_id,  
  277. $this 
  278. ); 
  279.  
  280. /** 
  281. * Determine whether Membership access can be changed or is read-only. 
  282. * @since 1.0.0 
  283. * @param string $post_type The post type of the post. 
  284. * @return bool 
  285. */ 
  286. public function is_read_only( $post_type ) { 
  287. if ( 'post' == $post_type 
  288. && ! MS_Model_Addon::is_enabled( MS_Model_Addon::ADDON_POST_BY_POST ) 
  289. ) { 
  290. $read_only = true; 
  291. } elseif ( 'attachment' == $post_type ) { 
  292. $read_only = true; 
  293. } else { 
  294. $read_only = false; 
  295.  
  296. return apply_filters( 
  297. 'ms_controller_membership_metabox_is_read_only',  
  298. $read_only,  
  299. $post_type,  
  300. $this 
  301. ); 
  302.  
  303. /** 
  304. * Filter returns the default contents of a Membership Page if the URL param 
  305. * &ms-default=1 is set. 
  306. * Effectively this will display the default contents inside the Post-Editor 
  307. * without changing the page itself. Only after the user saves the content 
  308. * it will affect the Membership page 
  309. * @since 1.0.0 
  310. * @param string $content Default page content. 
  311. * @return string Modified page content. 
  312. */ 
  313. public function show_default_content( $content ) { 
  314. static $Message = false; 
  315. global $post, $post_type; 
  316.  
  317. if ( ! isset( $_GET['ms-default'] ) ) { return $content; } 
  318. if ( '1' != $_GET['ms-default'] ) { return $content; } 
  319. if ( 'page' != $post_type ) { return $content; } 
  320.  
  321. $ms_page = MS_Model_Pages::get_page_by( 'id', $post->ID ); 
  322.  
  323. if ( empty( $ms_page ) ) { return $content; } 
  324.  
  325. $type = MS_Model_Pages::get_page_type( $ms_page ); 
  326.  
  327. if ( ! $Message ) { 
  328. $Message = true; 
  329. lib3()->ui->admin_message( 
  330. __( 
  331. '<strong>Tipp</strong>:<br />' . 
  332. 'The page content is reset to the default content but is <em>not saved yet</em>!<br />' . 
  333. 'You can simply close this page to keep your current page contents.',  
  334. 'membership2' 
  335. ); 
  336.  
  337. return MS_Model_Pages::get_default_content( $type ); 
  338.  
  339. /** 
  340. * Load Membership Metabox specific scripts. 
  341. * @since 1.0.0 
  342. */ 
  343. public function admin_enqueue_scripts() { 
  344. global $post_type; 
  345.  
  346. if ( in_array( $post_type, $this->post_types ) 
  347. && ! $this->is_read_only( $post_type ) 
  348. ) { 
  349. lib3()->ui->data( 'ms_data', array( 'ms_init' => array( 'metabox' ) ) ); 
  350. wp_enqueue_script( 'ms-admin' ); 
  351.