Nenhuma Descrição

menus.php 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Class PUM_Modules_Menu
  10. *
  11. * This class handles the menu editor fields & adds popup classes to menu items.
  12. */
  13. class PUM_Modules_Menu {
  14. /**
  15. * Initializes this module.
  16. */
  17. public static function init() {
  18. add_filter( 'popmake_settings_misc', array( __CLASS__, 'settings' ) );
  19. if ( PUM_Utils_Options::get( 'disabled_menu_editor', false ) ) {
  20. return;
  21. }
  22. // Merge Menu Item Options
  23. add_filter( 'wp_setup_nav_menu_item', array( __CLASS__, 'merge_item_data' ) );
  24. // Admin Menu Editor
  25. add_filter( 'wp_edit_nav_menu_walker', array( __CLASS__, 'nav_menu_walker' ), 999999999 );
  26. // Admin Menu Editor Fields.
  27. add_action( 'wp_nav_menu_item_custom_fields', array( __CLASS__, 'fields' ), 10, 4 );
  28. add_action( 'wp_update_nav_menu_item', array( __CLASS__, 'save' ), 10, 2 );
  29. add_filter( 'manage_nav-menus_columns', array( __CLASS__, 'nav_menu_columns' ), 11 );
  30. }
  31. public static function settings( $settings ) {
  32. return array_merge( $settings, array(
  33. 'disabled_menu_editor' => array(
  34. 'id' => 'disabled_menu_editor',
  35. 'name' => __( 'Disable Popups Menu Editor', 'popup-maker' ),
  36. 'desc' => sprintf(
  37. esc_html_x( 'Use this if there is a conflict with your theme or another plugin in the nav menu editor. %sLearn more%s', '%s represent opening and closing link html', 'popup-maker' ),
  38. '<a href="https://docs.wppopupmaker.com/article/297-popup-maker-is-overwriting-my-menu-editor-functions-how-can-i-fix-this?utm_campaign=contextual-help&utm_medium=inline-doclink&utm_source=settings-page&utm_content=disable-popup-menu-editor" target="_blank" rel="noreferrer noopener">',
  39. '</a>'
  40. ),
  41. 'type' => 'checkbox',
  42. ),
  43. ) );
  44. }
  45. public static function nav_menu_columns( $columns = array() ) {
  46. $columns['popup_id'] = __( 'Popup', 'popup-maker' );
  47. return $columns;
  48. }
  49. /**
  50. * Override the Admin Menu Walker
  51. *
  52. * @param $walker
  53. *
  54. * @return string
  55. */
  56. public static function nav_menu_walker( $walker ) {
  57. global $wp_version;
  58. $bail_early = [
  59. // WP 5.4 adds support for custom fields, no need to do this hack at all.
  60. version_compare( $wp_version, '5.4', '>=' ),
  61. // not sure about this one, was part of the original solution.
  62. doing_filter( 'plugins_loaded' ),
  63. // No need if its already loaded by another plugin.
  64. $walker === 'Walker_Nav_Menu_Edit_Custom_Fields',
  65. ];
  66. if ( in_array( true, $bail_early ) ) {
  67. return $walker;
  68. }
  69. // Load custom nav menu walker class for custom field compatibility.
  70. if ( ! class_exists( 'Walker_Nav_Menu_Edit_Custom_Fields' ) ) {
  71. if ( version_compare( $wp_version, '3.6', '>=' ) ) {
  72. require_once POPMAKE_DIR . '/includes/modules/menus/class-nav-menu-edit-custom-fields.php';
  73. } else {
  74. require_once POPMAKE_DIR . '/includes/modules/menus/class-nav-menu-edit-custom-fields-deprecated.php';
  75. }
  76. }
  77. return 'Walker_Nav_Menu_Edit_Custom_Fields';
  78. }
  79. /**
  80. * Merge Item data into the $item object.
  81. *
  82. * @param $item
  83. *
  84. * @return mixed
  85. */
  86. public static function merge_item_data( $item ) {
  87. if ( ! is_object( $item ) || ! isset( $item->ID ) || $item->ID <= 0 ) {
  88. return $item;
  89. }
  90. // Merge Rules.
  91. foreach ( PUM_Modules_Menu::get_item_options( $item->ID ) as $key => $value ) {
  92. $item->$key = $value;
  93. }
  94. if ( is_admin() ) {
  95. return $item;
  96. }
  97. if ( isset( $item->popup_id ) ) {
  98. $item->classes[] = 'popmake-' . $item->popup_id;
  99. }
  100. /**
  101. * Check menu item's classes for popmake-###. Do this after the above conditional to catch the class we add too.
  102. * Tested both using strpos followed by preg_match as well as just doing preg_match on all and this solution
  103. * was just a tiny bit faster. But, if a site has 100 menu items, that tiny difference will add up.
  104. */
  105. foreach ( $item->classes as $class ) {
  106. if ( strpos( $class, 'popmake-' ) !== false ) {
  107. if ( 0 !== preg_match( '/popmake-(\d+)/', $class, $matches ) ) {
  108. PUM_Site_Popups::preload_popup_by_id_if_enabled( $matches[1] );
  109. }
  110. }
  111. }
  112. return $item;
  113. }
  114. /**
  115. * @param int $item_id
  116. *
  117. * @return array
  118. */
  119. public static function get_item_options( $item_id = 0 ) {
  120. // Fetch all rules for this menu item.
  121. $item_options = get_post_meta( $item_id, '_pum_nav_item_options', true );
  122. return PUM_Modules_Menu::parse_item_options( $item_options );
  123. }
  124. /**
  125. * @param array $options
  126. *
  127. * @return array
  128. */
  129. public static function parse_item_options( $options = array() ) {
  130. if ( ! is_array( $options ) ) {
  131. $options = array();
  132. }
  133. return wp_parse_args( $options, array(
  134. 'popup_id' => null,
  135. ) );
  136. }
  137. /**
  138. * Adds custom fields to the menu item editor.
  139. *
  140. * @param $item_id
  141. * @param $item
  142. * @param $depth
  143. * @param $args
  144. */
  145. public static function fields( $item_id, $item, $depth, $args ) {
  146. wp_nonce_field( 'pum-menu-editor-nonce', 'pum-menu-editor-nonce' ); ?>
  147. <p class="field-popup_id description description-wide">
  148. <label for="edit-menu-item-popup_id-<?php echo $item->ID; ?>">
  149. <?php _e( 'Trigger a Popup', 'popup-maker' ); ?><br />
  150. <select name="menu-item-pum[<?php echo $item->ID; ?>][popup_id]" id="edit-menu-item-popup_id-<?php echo $item->ID; ?>" class="widefat edit-menu-item-popup_id">
  151. <option value=""></option>
  152. <?php foreach ( PUM_Modules_Menu::popup_list() as $option => $label ) : ?>
  153. <option value="<?php echo $option; ?>" <?php selected( $option, $item->popup_id ); ?>>
  154. <?php echo esc_html( $label ); ?>
  155. </option>
  156. <?php endforeach; ?>
  157. </select>
  158. <span class="description"><?php _e( 'Choose a popup to trigger when this item is clicked.', 'popup-maker' ); ?></span>
  159. </label>
  160. </p>
  161. <?php
  162. }
  163. /**
  164. * Returns a list of popups for a dropdown.
  165. *
  166. * @return array
  167. */
  168. public static function popup_list() {
  169. static $popup_list;
  170. if ( ! isset( $popup_list ) ) {
  171. $popup_list = array();
  172. $popups = pum_get_all_popups();
  173. if ( ! empty( $popups ) ) {
  174. foreach ( $popups as $popup ) {
  175. $popup_list[ $popup->ID ] = $popup->post_title;
  176. }
  177. }
  178. }
  179. return $popup_list;
  180. }
  181. /**
  182. * Processes the saving of menu items.
  183. *
  184. * @param $menu_id
  185. * @param $item_id
  186. */
  187. public static function save( $menu_id, $item_id ) {
  188. $popups = PUM_Modules_Menu::popup_list();
  189. $allowed_popups = wp_parse_id_list( array_keys( $popups ) );
  190. if ( ! isset( $_POST['pum-menu-editor-nonce'] ) || ! wp_verify_nonce( $_POST['pum-menu-editor-nonce'], 'pum-menu-editor-nonce' ) ) {
  191. return;
  192. }
  193. /**
  194. * Return early if there are no settings.
  195. */
  196. if ( empty( $_POST['menu-item-pum'][ $item_id ] ) ) {
  197. delete_post_meta( $item_id, '_pum_nav_item_options' );
  198. return;
  199. }
  200. /**
  201. * Parse options array for valid keys.
  202. */
  203. $item_options = PUM_Modules_Menu::parse_item_options( $_POST['menu-item-pum'][ $item_id ] );
  204. /**
  205. * Check for invalid values.
  206. */
  207. if ( ! in_array( $item_options['popup_id'], $allowed_popups ) || $item_options['popup_id'] <= 0 ) {
  208. unset( $item_options['popup_id'] );
  209. }
  210. /**
  211. * Remove empty options to save space.
  212. */
  213. $item_options = array_filter( $item_options );
  214. /**
  215. * Save options or delete if empty.
  216. */
  217. if ( ! empty( $item_options ) ) {
  218. update_post_meta( $item_id, '_pum_nav_item_options', $item_options );
  219. } else {
  220. delete_post_meta( $item_id, '_pum_nav_item_options' );
  221. }
  222. }
  223. }
  224. PUM_Modules_Menu::init();