MS_Controller_Adminbar

Controller to add functionality to the admin toolbar.

Defined (1)

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

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