Нет описания

admin-bar.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Class PUM_Modules_Admin_Bar
  10. *
  11. * This class adds admin bar menu for Popup Management.
  12. */
  13. class PUM_Modules_Admin_Bar {
  14. /**
  15. * Initializes this module.
  16. */
  17. public static function init() {
  18. add_action( 'admin_bar_menu', array( __CLASS__, 'toolbar_links' ), 999 );
  19. add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_files' ) );
  20. add_action( 'init', array( __CLASS__, 'show_debug_bar' ) );
  21. }
  22. /**
  23. * Renders the admin debug bar when PUM Debug is enabled.
  24. */
  25. public static function show_debug_bar() {
  26. if ( self::should_render() && Popup_Maker::debug_mode() ) {
  27. show_admin_bar( true );
  28. }
  29. }
  30. /**
  31. * Returns true only if all of the following are true:
  32. * - User is logged in.
  33. * - Not in WP Admin.
  34. * - The admin bar is showing.
  35. * - PUM Admin bar is not disabled.
  36. * - Current user can edit others posts or manage options.
  37. *
  38. * @return bool
  39. */
  40. public static function should_render() {
  41. $tests = array(
  42. is_user_logged_in(),
  43. ! is_admin(),
  44. is_admin_bar_showing(),
  45. ! pum_get_option( 'disabled_admin_bar' ),
  46. ( current_user_can( 'edit_others_posts' ) || current_user_can( 'manage_options' ) ),
  47. );
  48. return ! in_array( false, $tests );
  49. }
  50. /**
  51. * Add additional toolbar menu items to the front end.
  52. *
  53. * @param WP_Admin_Bar $wp_admin_bar
  54. */
  55. public static function toolbar_links( $wp_admin_bar ) {
  56. if ( ! self::should_render() ) {
  57. return;
  58. }
  59. $wp_admin_bar->add_node( array(
  60. 'id' => 'popup-maker',
  61. 'title' => __( 'Popup Maker', 'popup-maker' ),
  62. 'href' => '#popup-maker',
  63. 'meta' => array( 'class' => 'popup-maker-toolbar' ),
  64. 'parent' => false,
  65. ) );
  66. $wp_admin_bar->add_node( array(
  67. 'id' => 'popups',
  68. 'title' => __( 'Popups', 'popup-maker' ),
  69. 'href' => '#',
  70. 'parent' => 'popup-maker',
  71. ) );
  72. $popups = PUM_Modules_Admin_Bar::loaded_popups();
  73. if ( count( $popups ) ) {
  74. foreach ( $popups as $popup ) {
  75. /** @var WP_Post $popup */
  76. $node_id = 'popup-' . $popup->ID;
  77. $can_edit = current_user_can( 'edit_post', $popup->ID );
  78. $edit_url = $can_edit ? admin_url( 'post.php?post=' . $popup->ID . '&action=edit' ) : '#';
  79. // Single Popup Menu Node
  80. $wp_admin_bar->add_node( array(
  81. 'id' => $node_id,
  82. 'title' => esc_html( $popup->post_title ),
  83. 'href' => $edit_url,
  84. 'parent' => 'popups',
  85. ) );
  86. // Trigger Link
  87. $wp_admin_bar->add_node( array(
  88. 'id' => $node_id . '-open',
  89. 'title' => __( 'Open Popup', 'popup-maker' ),
  90. 'meta' => array(
  91. 'onclick' => 'PUM.open(' . $popup->ID . '); return false;',
  92. ),
  93. 'href' => '#popup-maker-open-popup-' . $popup->ID,
  94. 'parent' => $node_id,
  95. ) );
  96. $wp_admin_bar->add_node( array(
  97. 'id' => $node_id . '-close',
  98. 'title' => __( 'Close Popup', 'popup-maker' ),
  99. 'meta' => array(
  100. 'onclick' => 'PUM.close(' . $popup->ID . '); return false;',
  101. ),
  102. 'href' => '#popup-maker-close-popup-' . $popup->ID,
  103. 'parent' => $node_id,
  104. ) );
  105. if ( pum_get_popup( $popup->ID )->has_conditions( array( 'js_only' => true ) ) ) {
  106. $wp_admin_bar->add_node( array(
  107. 'id' => $node_id . '-conditions',
  108. 'title' => __( 'Check Conditions', 'popup-maker' ),
  109. 'meta' => array(
  110. 'onclick' => 'alert(PUM.checkConditions(' . $popup->ID . ') ? "Pass" : "Fail"); return false;',
  111. ),
  112. 'href' => '#popup-maker-check-conditions-popup-' . $popup->ID,
  113. 'parent' => $node_id,
  114. ) );
  115. }
  116. $wp_admin_bar->add_node( array(
  117. 'id' => $node_id . '-reset-cookies',
  118. 'title' => __( 'Reset Cookies', 'popup-maker' ),
  119. 'meta' => array(
  120. 'onclick' => 'PUM.clearCookies(' . $popup->ID . '); alert("' . __( 'Success', 'popup-maker' ) . '"); return false;',
  121. ),
  122. 'href' => '#popup-maker-reset-cookies-popup-' . $popup->ID,
  123. 'parent' => $node_id,
  124. ) );
  125. if ( $can_edit ) {
  126. // Edit Popup Link
  127. $wp_admin_bar->add_node( array(
  128. 'id' => $node_id . '-edit',
  129. 'title' => __( 'Edit Popup', 'popup-maker' ),
  130. 'href' => $edit_url,
  131. 'parent' => $node_id,
  132. ) );
  133. }
  134. }
  135. } else {
  136. $wp_admin_bar->add_node( array(
  137. 'id' => 'no-popups-loaded',
  138. 'title' => __( 'No Popups Loaded', 'popup-maker' ) . '<strong style="color:#fff; margin-left: 5px;">?</strong>',
  139. 'href' => 'https://docs.wppopupmaker.com/article/265-my-popup-wont-work-how-can-i-fix-it?utm_campaign=contextual-help&utm_medium=inline-doclink&utm_source=plugin-admin-bar&utm_content=no-popups-loaded',
  140. 'parent' => 'popups',
  141. 'meta' => array(
  142. 'target' => '_blank',
  143. ),
  144. ) );
  145. }
  146. if ( current_user_can( 'edit_posts' ) ) {
  147. $wp_admin_bar->add_node( array(
  148. 'id' => 'all-popups',
  149. 'title' => __( 'All Popups', 'popup-maker' ),
  150. 'href' => admin_url( 'edit.php?post_type=popup' ),
  151. 'parent' => 'popup-maker',
  152. ) );
  153. $wp_admin_bar->add_node( array(
  154. 'id' => 'new-popups', // Just `new-popup` moves this to the top of the menu for some reason. Leave the `s` to keep it in the right place.
  155. 'title' => __( 'Create New Popup', 'popup-maker' ),
  156. 'href' => admin_url( 'post-new.php?post_type=popup' ),
  157. 'parent' => 'popup-maker',
  158. ) );
  159. }
  160. /**
  161. * Tools
  162. */
  163. $wp_admin_bar->add_node( array(
  164. 'id' => 'pum-tools',
  165. 'title' => __( 'Tools', 'popup-maker' ),
  166. 'href' => '#popup-maker-tools',
  167. 'parent' => 'popup-maker',
  168. ) );
  169. $wp_admin_bar->add_node(
  170. array(
  171. 'id' => 'flush-popup-cache',
  172. 'title' => __( 'Flush Popup Cache', 'popup-maker' ),
  173. 'href' => add_query_arg( 'flush_popup_cache', 'yes' ),
  174. 'parent' => 'pum-tools',
  175. )
  176. );
  177. /**
  178. * Get Selector
  179. */
  180. $wp_admin_bar->add_node( array(
  181. 'id' => 'pum-get-selector',
  182. 'title' => __( 'Get Selector', 'popup-maker' ),
  183. 'href' => '#popup-maker-get-selector-tool',
  184. 'parent' => 'pum-tools',
  185. ) );
  186. }
  187. /**
  188. * @return array
  189. */
  190. public static function loaded_popups() {
  191. static $popups;
  192. if ( ! isset( $popups ) ) {
  193. $loaded = PUM_Site_Popups::get_loaded_popups();
  194. $popups = $loaded->posts;
  195. }
  196. return $popups;
  197. }
  198. /**
  199. * Enqueues and prepares our styles and scripts for the admin bar
  200. *
  201. * @since 1.11.0
  202. */
  203. public static function enqueue_files() {
  204. if ( ! self::should_render() ) {
  205. return;
  206. }
  207. $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
  208. wp_enqueue_script( 'pum-admin-bar', Popup_Maker::$URL . 'assets/js/admin-bar' . $suffix . '.js', array( 'jquery' ), Popup_Maker::$VER, true );
  209. wp_enqueue_style( 'pum-admin-bar-style', Popup_Maker::$URL . 'assets/css/pum-admin-bar' . $suffix . '.css', array(), Popup_Maker::$VER );
  210. $admin_bar_text = array(
  211. 'instructions' => __( 'After clicking ok, click the element you want a selector for.', 'popup-maker' ),
  212. 'results' => _x( 'Selector', 'JS alert for CSS get selector tool', 'popup-maker' ),
  213. );
  214. wp_localize_script( 'pum-admin-bar', 'pumAdminBarText', $admin_bar_text );
  215. }
  216. }
  217. PUM_Modules_Admin_Bar::init();