/app/controller/class-ms-controller-adminbar.php

  1. <?php 
  2. /** 
  3. * Controller to add functionality to the admin toolbar. 
  4. * 
  5. * Used extensively for simulating memberships and content access. 
  6. * Adds ability for Membership users to test the behaviour for their end-users. 
  7. * 
  8. * @since 1.0.0 
  9. * 
  10. * @package Membership2 
  11. * @subpackage Controller 
  12. */ 
  13. class MS_Controller_Adminbar extends MS_Controller { 
  14.  
  15. /** 
  16. * Details on current simulation mode 
  17. * 
  18. * @since 1.0.0 
  19. * 
  20. * @var MS_Model_Simulate 
  21. */ 
  22. protected $simulate = null; 
  23.  
  24. /** 
  25. * Prepare the Admin Bar simulator. 
  26. * 
  27. * @since 1.0.0 
  28. */ 
  29. public function __construct() { 
  30. parent::__construct(); 
  31.  
  32. $this->run_action( 'init', 'init_adminbar', 1 ); 
  33.  
  34. /** 
  35. * Returns the URL to start/switch simulation of a specific membership. 
  36. * 
  37. * @since 1.0.0 
  38. * @param int $id Membership-ID 
  39. * @return string URL 
  40. */ 
  41. static public function get_simulation_url( $id ) { 
  42. $link_url = admin_url( 
  43. '?action=ms_simulate&membership_id=' . $id,  
  44. is_ssl() ? 'https' : 'http' 
  45. ); 
  46. $link_url = wp_nonce_url( 
  47. $link_url,  
  48. 'ms_simulate' 
  49. ); 
  50.  
  51. return $link_url; 
  52.  
  53. /** 
  54. * Returns the URL to end simulation. 
  55. * 
  56. * @since 1.0.0 
  57. * @return string URL 
  58. */ 
  59. static public function get_simulation_exit_url() { 
  60. return self::get_simulation_url( 0 ); 
  61.  
  62. /** 
  63. * Initialize the Admin-Bar after we have determined the current user. 
  64. * 
  65. * @since 1.0.0 
  66. */ 
  67. public function init_adminbar() { 
  68. $this->simulate = MS_Factory::load( 'MS_Model_Simulate' ); 
  69.  
  70. // Hide WP toolbar in front end to not admin users 
  71. if ( ! $this->is_admin_user() && MS_Plugin::instance()->settings->hide_admin_bar ) { 
  72. add_filter( 'show_admin_bar', '__return_false' ); 
  73. $this->add_action( 'wp_before_admin_bar_render', 'customize_toolbar_front', 999 ); 
  74.  
  75. // Customize WP toolbar for admin users 
  76. if ( $this->is_admin_user() ) { 
  77. $this->add_action( 'wp_before_admin_bar_render', 'customize_toolbar', 999 ); 
  78. $this->add_action( 'add_admin_bar_menus', 'admin_bar_manager' ); 
  79. $this->add_action( 'admin_enqueue_scripts', 'enqueue_scripts' ); 
  80. $this->add_action( 'wp_enqueue_scripts', 'enqueue_scripts' ); 
  81.  
  82. /** 
  83. * Customize the Admin Toolbar. 
  84. * 
  85. * Related Action Hooks: 
  86. * - wp_before_admin_bar_render 
  87. * 
  88. * @since 1.0.0 
  89. */ 
  90. public function customize_toolbar() { 
  91. if ( MS_Model_Member::is_admin_user() 
  92. && MS_Plugin::is_enabled() 
  93. && ! is_network_admin() 
  94. && MS_Model_Simulate::can_simulate() 
  95. ) { 
  96. if ( $this->simulate->is_simulating() ) { 
  97. $this->add_detail_nodes(); 
  98. } else { 
  99. $this->add_test_membership_node(); 
  100. } else if ( ! MS_Plugin::is_enabled() ) { 
  101. $this->add_unprotected_node(); 
  102.  
  103. /** 
  104. * Process GET and POST requests 
  105. * 
  106. * Related Action Hooks: 
  107. * - add_admin_bar_menus 
  108. * 
  109. * @since 1.0.0 
  110. */ 
  111. public function admin_bar_manager() { 
  112. $redirect = false; 
  113.  
  114. lib3()->array->equip_get( 'membership_id' ); 
  115.  
  116. if ( $this->verify_nonce( 'ms_simulate', 'any' ) ) { 
  117. /** 
  118. * Check for memberhship id simulation GET request. 
  119. * - Any valid Membership_id will simulate that membership. 
  120. * - An ID of "0" will exit simulation mode. 
  121. */ 
  122. $new_id = absint( $_REQUEST['membership_id'] ); 
  123.  
  124. if ( $new_id != $this->simulate->membership_id ) { 
  125. // Change the simulated membership. 
  126. $this->simulate->membership_id = $new_id; 
  127.  
  128. $target = wp_get_referer(); 
  129. if ( $this->simulate->is_simulating() 
  130. && false !== strpos( $target, 'wp-admin' ) 
  131. ) { 
  132. $redirect = admin_url(); 
  133.  
  134. if ( ! empty( $_POST['simulate_date'] ) ) { 
  135. // Change the simulation date. 
  136. $this->simulate->date = $_POST['simulate_date']; 
  137.  
  138. $this->simulate->save(); 
  139.  
  140. if ( ! $redirect ) { 
  141. if ( ! empty( $_GET['redirect_to'] ) ) { 
  142. $redirect = $_GET['redirect_to']; 
  143. } else { 
  144. $redirect = wp_get_referer(); 
  145.  
  146. if ( ! $redirect ) { 
  147. $redirect = lib3()->net->current_url(); 
  148.  
  149. if ( $redirect ) { 
  150. wp_safe_redirect( $redirect ); 
  151. exit; 
  152.  
  153. /** 
  154. * Remove all Admin Bar nodes. 
  155. * 
  156. * @since 1.0.0 
  157. * 
  158. * @param string[] $exclude The node IDs to exclude. 
  159. */ 
  160. private function remove_admin_bar_nodes( $exclude = array() ) { 
  161. global $wp_admin_bar; 
  162.  
  163. $nodes = $wp_admin_bar->get_nodes(); 
  164.  
  165. $exclude = apply_filters( 
  166. 'ms_controller_adminbar_remove_admin_bar_nodes_exclude',  
  167. $exclude,  
  168. $nodes 
  169. ); 
  170.  
  171. if ( is_array( $nodes ) ) { 
  172. foreach ( $nodes as $node ) { 
  173. if ( is_array( $exclude ) && ! in_array( $node->id, $exclude ) ) { 
  174. $wp_admin_bar->remove_node( $node->id ); 
  175.  
  176. do_action( 
  177. 'ms_controller_adminbar_remove_admin_bar_nodes',  
  178. $nodes,  
  179. $exclude 
  180. ); 
  181.  
  182. /** 
  183. * Add 'Test Memberships' node. 
  184. * 
  185. * @since 1.0.0 
  186. * 
  187. */ 
  188. private function add_test_membership_node() { 
  189. global $wp_admin_bar; 
  190.  
  191. $base_id = MS_Model_Membership::get_base()->id; 
  192.  
  193. if ( $base_id ) { 
  194. $link_url = self::get_simulation_url( $base_id ); 
  195.  
  196. $wp_admin_bar->add_node( 
  197. apply_filters( 
  198. 'ms_controller_adminbar_add_test_membership_node',  
  199. array( 
  200. 'id' => 'ms-test-memberships',  
  201. 'title' => __( 'Test Memberships', 'membership2' ),  
  202. 'href' => $link_url,  
  203. 'meta' => array( 
  204. 'class' => 'ms-test-memberships',  
  205. 'title' => __( 'Membership Simulation Menu', 'membership2' ),  
  206. 'tabindex' => '1',  
  207. ),  
  208. ); 
  209.  
  210. /** 
  211. * Add 'Unprotected' node. 
  212. * 
  213. * @since 1.0.0 
  214. * 
  215. */ 
  216. private function add_unprotected_node() { 
  217. global $wp_admin_bar; 
  218.  
  219. if ( MS_Plugin::is_enabled() ) { return; } 
  220. if ( MS_Plugin::is_wizard() ) { return; } 
  221.  
  222. $link_url = MS_Controller_Plugin::get_admin_url( 'settings' ); 
  223.  
  224. $wp_admin_bar->add_node( 
  225. apply_filters( 
  226. 'ms_controller_adminbar_add_unprotected_node',  
  227. array( 
  228. 'id' => 'ms-unprotected',  
  229. 'title' => __( 'Content Protection is disabled', 'membership2' ),  
  230. 'href' => $link_url,  
  231. 'meta' => array( 
  232. 'class' => 'ms-unprotected',  
  233. 'title' => __( 'Content of this site is unprotected', 'membership2' ),  
  234. 'tabindex' => '1',  
  235. ),  
  236. ); 
  237.  
  238. /** 
  239. * Add membership description nodes. 
  240. * 
  241. * @since 1.0.0 
  242. * 
  243. */ 
  244. private function add_detail_nodes() { 
  245. global $wp_admin_bar; 
  246.  
  247. /** 
  248. * Info menu is currently only available on the front-end. 
  249. * 
  250. * @todo add information also for admin side (Admin-Protection/Capabilities) 
  251. */ 
  252. if ( is_admin() ) { return; } 
  253.  
  254. if ( ! $this->simulate->is_simulating() ) { return; } 
  255.  
  256. $membership = MS_Factory::load( 
  257. 'MS_Model_Membership',  
  258. $this->simulate->membership_id 
  259. ); 
  260.  
  261. $wp_admin_bar->add_menu( 
  262. array( 
  263. 'id' => 'membership-details',  
  264. 'title' => __( 'Protection Details', 'membership2' ),  
  265. 'href' => '#',  
  266. ); 
  267.  
  268. $details = lib3()->session->get( 'ms-access' ); 
  269. $parent1 = ''; 
  270. $parent2 = ''; 
  271.  
  272. foreach ( $details as $req_ind => $request ) { 
  273. if ( ! is_array( $request ) ) { continue; } 
  274. $parent1 = 'membership-details-' . $req_ind; 
  275.  
  276. $url = explode( '?', $request['url'] ); 
  277. $url = str_replace( site_url(), '', reset( $url ) ); 
  278.  
  279. $wp_admin_bar->add_node( 
  280. array( 
  281. 'id' => $parent1,  
  282. 'parent' => 'membership-details',  
  283. 'title' => (1 + $req_ind) . ': ' . $url,  
  284. 'href' => $request['url'],  
  285. ); 
  286.  
  287. if ( isset( $request['reason'] ) ) { 
  288. foreach ( $request['reason'] as $key => $item ) { 
  289. if ( is_array( $item ) ) { 
  290. foreach ( $item as $child => $note ) { 
  291. $wp_admin_bar->add_node( 
  292. array( 
  293. 'id' => $parent2 . '-' . $child,  
  294. 'parent' => $parent2,  
  295. 'title' => $note,  
  296. ); 
  297. } else { 
  298. $parent2 = $parent1 . '-' . $key; 
  299. $wp_admin_bar->add_node( 
  300. array( 
  301. 'id' => $parent2,  
  302. 'parent' => $parent1,  
  303. 'title' => $item,  
  304. ); 
  305.  
  306. /** 
  307. * Customize the Admin Toolbar for front end users. 
  308. * 
  309. * Related Action Hooks: 
  310. * - wp_before_admin_bar_render 
  311. * 
  312. * @since 1.0.0 
  313. * 
  314. */ 
  315. public function customize_toolbar_front() { 
  316. if ( ! $this->is_admin_user() ) { 
  317. $this->remove_admin_bar_nodes(); 
  318.  
  319. /** 
  320. * Enqueues necessary scripts and styles. 
  321. * 
  322. * Related Action Hooks: 
  323. * - wp_enqueue_scripts 
  324. * - admin_enqueue_scripts 
  325. * 
  326. * @since 1.0.0 
  327. */ 
  328. public function enqueue_scripts() { 
  329. $data = array( 
  330. 'ms_init' => array( 'controller_adminbar' ),  
  331. 'switching_text' => __( 'Switching...', 'membership2' ),  
  332. ); 
  333.  
  334. lib3()->ui->add( 'select' ); 
  335. lib3()->ui->data( 'ms_data', $data ); 
  336.  
  337. wp_enqueue_script( 'ms-admin' ); 
  338. wp_enqueue_script( 'jquery-ui-datepicker' ); 
  339.  
  340. wp_enqueue_style( 'ms-public' ); 
  341.  
.