PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wpforms-lite/src/Admin/AdminBarMenu.php

https://gitlab.com/ebrjose/comcebu
PHP | 380 lines | 216 code | 56 blank | 108 comment | 16 complexity | 62371e5fbcc3d5f82299943f19dec8f3 MD5 | raw file
  1. <?php
  2. namespace WPForms\Admin;
  3. /**
  4. * WPForms admin bar menu.
  5. *
  6. * @since 1.6.0
  7. */
  8. class AdminBarMenu {
  9. /**
  10. * Initialize class.
  11. *
  12. * @since 1.6.0
  13. */
  14. public function init() {
  15. if ( ! $this->has_access() ) {
  16. return;
  17. }
  18. $this->hooks();
  19. }
  20. /**
  21. * Register hooks.
  22. *
  23. * @since 1.6.0
  24. */
  25. public function hooks() {
  26. add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_css' ] );
  27. add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_css' ] );
  28. add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_js' ] );
  29. add_action( 'admin_bar_menu', [ $this, 'register' ], 999 );
  30. add_action( 'wpforms_wp_footer_end', [ $this, 'menu_forms_data_html' ] );
  31. }
  32. /**
  33. * Determine whether the current user has access to see admin bar menu.
  34. *
  35. * @since 1.6.0
  36. *
  37. * @return bool
  38. */
  39. public function has_access() {
  40. $access = false;
  41. if (
  42. is_admin_bar_showing() &&
  43. wpforms_current_user_can() &&
  44. ! wpforms_setting( 'hide-admin-bar', false )
  45. ) {
  46. $access = true;
  47. }
  48. return (bool) apply_filters( 'wpforms_admin_adminbarmenu_has_access', $access );
  49. }
  50. /**
  51. * Determine whether new notifications are available.
  52. *
  53. * @since 1.6.0
  54. *
  55. * @return bool
  56. */
  57. public function has_notifications() {
  58. return wpforms()->get( 'notifications' )->get_count();
  59. }
  60. /**
  61. * Enqueue CSS styles.
  62. *
  63. * @since 1.6.0
  64. */
  65. public function enqueue_css() {
  66. $min = wpforms_get_min_suffix();
  67. wp_enqueue_style(
  68. 'wpforms-admin-bar',
  69. WPFORMS_PLUGIN_URL . "assets/css/admin-bar{$min}.css",
  70. [],
  71. WPFORMS_VERSION
  72. );
  73. // Apply WordPress pre/post 5.7 accent color, only when admin bar is displayed on the frontend or we're
  74. // inside the Form Builder - it does not load some WP core admin styles, including themes.
  75. if ( wpforms_is_admin_page( 'builder' ) || ! is_admin() ) {
  76. wp_add_inline_style(
  77. 'wpforms-admin-bar',
  78. sprintf(
  79. '#wpadminbar .wpforms-menu-notification-counter, #wpadminbar .wpforms-menu-notification-indicator {
  80. background-color: %s !important;
  81. color: #ffffff !important;
  82. }',
  83. version_compare( get_bloginfo( 'version' ), '5.7', '<' ) ? '#ca4a1f' : '#d63638'
  84. )
  85. );
  86. }
  87. }
  88. /**
  89. * Enqueue JavaScript files.
  90. *
  91. * @since 1.6.5
  92. */
  93. public function enqueue_js() {
  94. // In WordPress 5.3.1 the `admin-bar.js` file was rewritten and removed all jQuery dependencies.
  95. $is_wp_531_plus = version_compare( get_bloginfo( 'version' ), '5.3.1', '>=' );
  96. $inline_script = sprintf(
  97. "( function() {
  98. function wpforms_admin_bar_menu_init() {
  99. var template = document.getElementById( 'tmpl-wpforms-admin-menubar-data' ),
  100. notifications = document.getElementById( 'wp-admin-bar-wpforms-notifications' );
  101. if ( ! template ) {
  102. return;
  103. }
  104. if ( ! notifications ) {
  105. var menu = document.getElementById( 'wp-admin-bar-wpforms-menu-default' );
  106. if ( ! menu ) {
  107. return;
  108. }
  109. menu.insertAdjacentHTML( 'afterBegin', template.innerHTML );
  110. } else {
  111. notifications.insertAdjacentHTML( 'afterend', template.innerHTML );
  112. }
  113. };
  114. %s
  115. }() );",
  116. $is_wp_531_plus ? "document.addEventListener( 'DOMContentLoaded', wpforms_admin_bar_menu_init );" : "if ( typeof( jQuery ) != 'undefined' ) { jQuery( wpforms_admin_bar_menu_init ); } else { document.addEventListener( 'DOMContentLoaded', wpforms_admin_bar_menu_init ); }"
  117. );
  118. wp_add_inline_script( 'admin-bar', $inline_script, 'before' );
  119. }
  120. /**
  121. * Register and render admin bar menu items.
  122. *
  123. * @since 1.6.0
  124. *
  125. * @param \WP_Admin_Bar $wp_admin_bar WordPress Admin Bar object.
  126. */
  127. public function register( \WP_Admin_Bar $wp_admin_bar ) {
  128. $items = (array) apply_filters(
  129. 'wpforms_admin_adminbarmenu_register',
  130. [
  131. 'main_menu',
  132. 'notification_menu',
  133. 'all_forms_menu',
  134. 'add_new_menu',
  135. 'community_menu',
  136. 'support_menu',
  137. ],
  138. $wp_admin_bar
  139. );
  140. foreach ( $items as $item ) {
  141. $this->{ $item }( $wp_admin_bar );
  142. do_action( "wpforms_admin_adminbarmenu_register_{$item}_after", $wp_admin_bar );
  143. }
  144. }
  145. /**
  146. * Render primary top-level admin bar menu item.
  147. *
  148. * @since 1.6.0
  149. *
  150. * @param \WP_Admin_Bar $wp_admin_bar WordPress Admin Bar object.
  151. */
  152. public function main_menu( \WP_Admin_Bar $wp_admin_bar ) {
  153. $indicator = '';
  154. $notifications = $this->has_notifications();
  155. if ( $notifications ) {
  156. $count = $notifications < 10 ? $notifications : '!';
  157. $indicator = ' <div class="wp-core-ui wp-ui-notification wpforms-menu-notification-counter">' . $count . '</div>';
  158. }
  159. $wp_admin_bar->add_menu(
  160. [
  161. 'id' => 'wpforms-menu',
  162. 'title' => 'WPForms' . $indicator,
  163. 'href' => admin_url( 'admin.php?page=wpforms-overview' ),
  164. ]
  165. );
  166. }
  167. /**
  168. * Render Notifications admin bar menu item.
  169. *
  170. * @since 1.6.0
  171. *
  172. * @param \WP_Admin_Bar $wp_admin_bar WordPress Admin Bar object.
  173. */
  174. public function notification_menu( \WP_Admin_Bar $wp_admin_bar ) {
  175. if ( ! $this->has_notifications() ) {
  176. return;
  177. }
  178. $wp_admin_bar->add_menu(
  179. [
  180. 'parent' => 'wpforms-menu',
  181. 'id' => 'wpforms-notifications',
  182. 'title' => esc_html__( 'Notifications', 'wpforms-lite' ) . ' <div class="wp-core-ui wp-ui-notification wpforms-menu-notification-indicator"></div>',
  183. 'href' => admin_url( 'admin.php?page=wpforms-overview' ),
  184. ]
  185. );
  186. }
  187. /**
  188. * Render All Forms admin bar menu item.
  189. *
  190. * @since 1.6.0
  191. *
  192. * @param \WP_Admin_Bar $wp_admin_bar WordPress Admin Bar object.
  193. */
  194. public function all_forms_menu( \WP_Admin_Bar $wp_admin_bar ) {
  195. $wp_admin_bar->add_menu(
  196. [
  197. 'parent' => 'wpforms-menu',
  198. 'id' => 'wpforms-forms',
  199. 'title' => esc_html__( 'All Forms', 'wpforms-lite' ),
  200. 'href' => admin_url( 'admin.php?page=wpforms-overview' ),
  201. ]
  202. );
  203. }
  204. /**
  205. * Render Add New admin bar menu item.
  206. *
  207. * @since 1.6.0
  208. *
  209. * @param \WP_Admin_Bar $wp_admin_bar WordPress Admin Bar object.
  210. */
  211. public function add_new_menu( \WP_Admin_Bar $wp_admin_bar ) {
  212. $wp_admin_bar->add_menu(
  213. [
  214. 'parent' => 'wpforms-menu',
  215. 'id' => 'wpforms-add-new',
  216. 'title' => esc_html__( 'Add New', 'wpforms-lite' ),
  217. 'href' => admin_url( 'admin.php?page=wpforms-builder' ),
  218. ]
  219. );
  220. }
  221. /**
  222. * Render Community admin bar menu item.
  223. *
  224. * @since 1.6.0
  225. *
  226. * @param \WP_Admin_Bar $wp_admin_bar WordPress Admin Bar object.
  227. */
  228. public function community_menu( \WP_Admin_Bar $wp_admin_bar ) {
  229. $wp_admin_bar->add_menu(
  230. [
  231. 'parent' => 'wpforms-menu',
  232. 'id' => 'wpforms-community',
  233. 'title' => esc_html__( 'Community', 'wpforms-lite' ),
  234. 'href' => 'https://www.facebook.com/groups/wpformsvip/',
  235. 'meta' => [
  236. 'target' => '_blank',
  237. 'rel' => 'noopener noreferrer',
  238. ],
  239. ]
  240. );
  241. }
  242. /**
  243. * Render Support admin bar menu item.
  244. *
  245. * @since 1.6.0
  246. *
  247. * @param \WP_Admin_Bar $wp_admin_bar WordPress Admin Bar object.
  248. */
  249. public function support_menu( \WP_Admin_Bar $wp_admin_bar ) {
  250. $wp_admin_bar->add_menu(
  251. [
  252. 'parent' => 'wpforms-menu',
  253. 'id' => 'wpforms-support',
  254. 'title' => esc_html__( 'Support', 'wpforms-lite' ),
  255. 'href' => 'https://wpforms.com/docs/',
  256. 'meta' => [
  257. 'target' => '_blank',
  258. 'rel' => 'noopener noreferrer',
  259. ],
  260. ]
  261. );
  262. }
  263. /**
  264. * Get form data for JS to modify the admin bar menu.
  265. *
  266. * @since 1.6.5
  267. *
  268. * @param array $forms Forms array.
  269. *
  270. * @return array
  271. */
  272. protected function get_forms_data( $forms ) {
  273. $data = [
  274. 'has_notifications' => $this->has_notifications(),
  275. 'edit_text' => esc_html__( 'Edit Form', 'wpforms-lite' ),
  276. 'entry_text' => esc_html__( 'View Entries', 'wpforms-lite' ),
  277. 'survey_text' => esc_html__( 'Survey Results', 'wpforms-lite' ),
  278. 'forms' => [],
  279. ];
  280. foreach ( $forms as $form ) {
  281. $form_id = absint( $form['id'] );
  282. if ( empty( $form_id ) ) {
  283. continue;
  284. }
  285. /* translators: %d - Form ID */
  286. $form_title = sprintf( esc_html__( 'Form ID: %d', 'wpforms-lite' ), $form_id );
  287. if ( ! empty( $form['settings'] ) && ! empty( $form['settings']['form_title'] ) ) {
  288. $form_title = wp_html_excerpt(
  289. sanitize_text_field( $form['settings']['form_title'] ),
  290. 99,
  291. '&hellip;'
  292. );
  293. }
  294. $data['forms'][] = apply_filters(
  295. 'wpforms_admin_adminbarmenu_get_form_data',
  296. [
  297. 'form_id' => $form_id,
  298. 'title' => $form_title,
  299. 'edit_url' => admin_url( 'admin.php?page=wpforms-builder&view=fields&form_id=' . $form_id ),
  300. ]
  301. );
  302. }
  303. return $data;
  304. }
  305. /**
  306. * Add form(s) data to the page.
  307. *
  308. * @since 1.6.5
  309. *
  310. * @param array $forms Forms array.
  311. */
  312. public function menu_forms_data_html( $forms ) {
  313. if ( empty( $forms ) ) {
  314. return;
  315. }
  316. echo wpforms_render( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  317. 'admin-bar-menu',
  318. [
  319. 'forms_data' => $this->get_forms_data( $forms ),
  320. ],
  321. true
  322. );
  323. }
  324. }