From 1d62265544fb001385595fcc2d00b43528d1a479 Mon Sep 17 00:00:00 2001 From: Denis Baranov Date: Wed, 31 Jan 2018 18:05:13 +0200 Subject: [PATCH 1/2] - fixed nav menus --- includes/admin/assets/js/um-admin-nav-menu.js | 15 + includes/admin/core/class-admin-enqueue.php | 9 + includes/core/um-navmenu-walker-edit.php | 347 +++++++++++------- includes/core/um-navmenu.php | 4 +- 4 files changed, 247 insertions(+), 128 deletions(-) create mode 100644 includes/admin/assets/js/um-admin-nav-menu.js diff --git a/includes/admin/assets/js/um-admin-nav-menu.js b/includes/admin/assets/js/um-admin-nav-menu.js new file mode 100644 index 00000000..b0ab47dc --- /dev/null +++ b/includes/admin/assets/js/um-admin-nav-menu.js @@ -0,0 +1,15 @@ +jQuery(document).ready(function ($) { + + var template = wp.template('um-nav-menus-fields'); + + $(document).on('menu-item-added', function (e, $menuMarkup) { + var id = $($menuMarkup).attr('id').substr(10); + $('fieldset.field-move', $($menuMarkup)).before(template({menuItemID: id,restriction_data:{um_nav_public:0,um_nav_roles:[]}})); + }); + + $('ul#menu-to-edit > li').each(function () { + var id = $(this).attr('id').substr(10); + $('fieldset.field-move', $(this)).before(template({menuItemID: id,restriction_data:um_menu_restriction_data[id]})); + }); + +}); \ No newline at end of file diff --git a/includes/admin/core/class-admin-enqueue.php b/includes/admin/core/class-admin-enqueue.php index e26be8e7..74e818ce 100644 --- a/includes/admin/core/class-admin-enqueue.php +++ b/includes/admin/core/class-admin-enqueue.php @@ -339,6 +339,15 @@ if ( ! class_exists( 'Admin_Enqueue' ) ) { } + /*** + *** @Load jQuery custom code + ***/ + function load_nav_manus_scripts() { + + wp_register_script( 'um_admin_nav_manus', $this->js_url . 'um-admin-nav-menu.js', array('jquery','wp-util'), '', true ); + wp_enqueue_script( 'um_admin_nav_manus' ); + + } /*** *** @Load AJAX diff --git a/includes/core/um-navmenu-walker-edit.php b/includes/core/um-navmenu-walker-edit.php index c48c9010..c5a278fa 100644 --- a/includes/core/um-navmenu-walker-edit.php +++ b/includes/core/um-navmenu-walker-edit.php @@ -1,160 +1,255 @@ __( 'Display Mode'), - 'um_nav_roles' => __('By Role') - - ); - } + 'um_nav_public' => __( 'Display Mode' ), + 'um_nav_roles' => __( 'By Role' ) + + ); + + //add_action( 'wp_nav_menu_item_custom_fields', array( __CLASS__, '_fields' ), 1, 4 ); + add_action( 'wp_update_nav_menu_item', array( __CLASS__, '_save' ), 10, 3 ); + add_filter( 'manage_nav-menus_columns', array( __CLASS__, '_columns' ), 99 ); + + add_action( 'load-nav-menus.php', array( __CLASS__, 'enqueue_nav_menus_scripts' ) ); + add_action( 'admin_footer-nav-menus.php', array( __CLASS__, '_wp_template' ) ); + + } - public static function _save( $menu_id, $menu_item_db_id, $menu_item_args ) { - if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { - return; - } - - foreach ( self::$fields as $_key => $label ) { - - if( $_key == 'um_nav_roles' ){ - - $key = sprintf( 'menu-item-%s', $_key ); - - // Sanitize - if ( ! empty( $_POST[ $key ][ $menu_item_db_id ] ) ) { - // Do some checks here... - $value = $_POST[ $key ][ $menu_item_db_id ]; - } - else { - $value = null; + public static function _save( $menu_id, $menu_item_db_id, $menu_item_args ) { + if (defined( 'DOING_AJAX' ) && DOING_AJAX) { + return; } - }else{ - - $key = sprintf( 'menu-item-%s', $_key ); - - // Sanitize - if ( ! empty( $_POST[ $key ][ $menu_item_db_id ] ) ) { - // Do some checks here... - $value = $_POST[ $key ][ $menu_item_db_id ]; - } - else { - $value = null; + foreach (self::$fields as $_key => $label) { + + if ($_key == 'um_nav_roles') { + + $key = sprintf( 'menu-item-%s', $_key ); + + // Sanitize + if (!empty( $_POST[$key][$menu_item_db_id] )) { + // Do some checks here... + $value = $_POST[$key][$menu_item_db_id]; + } else { + $value = null; + } + + } else { + + $key = sprintf( 'menu-item-%s', $_key ); + + // Sanitize + if (!empty( $_POST[$key][$menu_item_db_id] )) { + // Do some checks here... + $value = $_POST[$key][$menu_item_db_id]; + } else { + $value = null; + } + } + + // Update + if (!is_null( $value )) { + update_post_meta( $menu_item_db_id, $key, $value ); + } else { + delete_post_meta( $menu_item_db_id, $key ); + } } } - // Update - if ( ! is_null( $value ) ) { - update_post_meta( $menu_item_db_id, $key, $value ); - } - else { - delete_post_meta( $menu_item_db_id, $key ); - } - } - } + public static function _fields( $id, $item, $depth, $args ) { + ?> - public static function _fields( $id, $item, $depth, $args ) { - ?> - -
- -
+
-

+
- $label ) { - $key = sprintf('menu-item-%s', $_key); - $id = sprintf('edit-%s-%s', $key, $item->ID); - $name = sprintf('%s[%s]', $key, $item->ID); - $value = get_post_meta($item->ID, $key, true); - $role_name = sprintf('%s[%s][]', $key, $item->ID); - $class = sprintf('field-%s', $_key); ?> +

- + $label) { + $key = sprintf( 'menu-item-%s', $_key ); + $id = sprintf( 'edit-%s-%s', $key, $item->ID ); + $name = sprintf( '%s[%s]', $key, $item->ID ); + $value = get_post_meta( $item->ID, $key, true ); + $role_name = sprintf( '%s[%s][]', $key, $item->ID ); + $class = sprintf( 'field-%s', $_key ); ?> -

- -

+ - + +

- if ( $_key == 'um_nav_roles' ) { ?> + -
+ if ($_key == 'um_nav_roles') { ?> - roles()->get_roles(); - $i = 0; - $html = ''; - $columns = 2; - while ( $i < $columns ) { - $per_page = ceil( count( $options ) / $columns ); - $section_fields_per_page = array_slice( $options, $i*$per_page, $per_page ); - $html .= ''; +

+
- foreach ( $section_fields_per_page as $k => $title ) { - $id_attr = ' id="' . $id . '_' . $k . '" '; - $for_attr = ' for="' . $id . '_' . $k . '" '; - $name_attr = ' name="' . $role_name . '" '; + roles()->get_roles(); + $i = 0; + $html = ''; + $columns = 2; + while ($i < $columns) { + $per_page = ceil( count( $options ) / $columns ); + $section_fields_per_page = array_slice( $options, $i * $per_page, $per_page ); + $html .= ''; - $html .= "'; - $i++; - } + $html .= ''; + $i++; + } - echo $html; ?> -

- + echo $html; ?> +

+ -
- -
- -
- public static function _columns( $columns ) { - $columns = array_merge( $columns, self::$fields ); + - return $columns; - } -} + admin_enqueue()->load_nav_manus_scripts(); + + $menus = get_posts( 'post_type=nav_menu_item&numberposts=-1' ); + $menu_restriction_data = array(); + + foreach ($menus as $data) { + $_nav_roles_meta = get_post_meta( $data->ID, 'menu-item-um_nav_roles', true ); + $um_nav_roles = array(); + if ($_nav_roles_meta) + foreach ($_nav_roles_meta as $key => $value) { + if (is_int( $key )) { + $um_nav_roles[] = $value; + } + } + $menu_restriction_data[$data->ID] = array( + 'um_nav_public' => get_post_meta( $data->ID, 'menu-item-um_nav_public', true ), + 'um_nav_roles' => $um_nav_roles, + ); + } + wp_localize_script( 'um_admin_nav_manus', 'um_menu_restriction_data', $menu_restriction_data ); + } + + public static function _wp_template() { + echo ''; + } + + } + + endif; + + UM_Menu_Item_Custom_Fields_Editor::init(); \ No newline at end of file diff --git a/includes/core/um-navmenu.php b/includes/core/um-navmenu.php index e474e9bc..67b4228c 100644 --- a/includes/core/um-navmenu.php +++ b/includes/core/um-navmenu.php @@ -8,7 +8,7 @@ class UM_Menu_Item_Custom_Fields { * */ public static function load() { - add_filter( 'wp_edit_nav_menu_walker', array( __CLASS__, '_filter_walker' ), 999 ); + //add_filter( 'wp_edit_nav_menu_walker', array( __CLASS__, '_filter_walker' ), 999 ); } @@ -51,7 +51,7 @@ class UM_Menu_Item_Custom_Fields { return $um_walker; } } -add_action( 'wp_loaded', array( 'UM_Menu_Item_Custom_Fields', 'load' ), 9 ); +//add_action( 'wp_loaded', array( 'UM_Menu_Item_Custom_Fields', 'load' ), 9 ); endif; From 1c0f7399cf55e06782150dba2daa5fdc49ee3e5e Mon Sep 17 00:00:00 2001 From: nikitozzzzzzz Date: Thu, 1 Feb 2018 12:09:46 +0200 Subject: [PATCH 2/2] - reviewed nav menu items custom fields code; --- includes/admin/assets/js/um-admin-nav-menu.js | 35 ++- includes/core/um-navmenu-walker-edit.php | 255 ------------------ includes/core/um-navmenu-walker-template.php | 37 --- includes/core/um-navmenu-walker.php | 34 --- includes/core/um-navmenu.php | 228 ++++++++++++---- 5 files changed, 211 insertions(+), 378 deletions(-) delete mode 100644 includes/core/um-navmenu-walker-edit.php delete mode 100644 includes/core/um-navmenu-walker-template.php delete mode 100644 includes/core/um-navmenu-walker.php diff --git a/includes/admin/assets/js/um-admin-nav-menu.js b/includes/admin/assets/js/um-admin-nav-menu.js index b0ab47dc..8450a0ef 100644 --- a/includes/admin/assets/js/um-admin-nav-menu.js +++ b/includes/admin/assets/js/um-admin-nav-menu.js @@ -1,15 +1,36 @@ jQuery(document).ready(function ($) { + var template = wp.template( 'um-nav-menus-fields' ); - var template = wp.template('um-nav-menus-fields'); + $( document ).on( 'menu-item-added', function ( e, $menuMarkup ) { + var id = $( $menuMarkup ).attr('id').substr(10); - $(document).on('menu-item-added', function (e, $menuMarkup) { - var id = $($menuMarkup).attr('id').substr(10); - $('fieldset.field-move', $($menuMarkup)).before(template({menuItemID: id,restriction_data:{um_nav_public:0,um_nav_roles:[]}})); + var template_content = template({ + menuItemID: id, + restriction_data:{ + um_nav_public:0, + um_nav_roles:[] + } + }); + + if ( $( $menuMarkup ).find( 'fieldset.field-move' ).length > 0 ) { + $( $menuMarkup ).find( 'fieldset.field-move' ).before( template_content ); + } else { + $( $menuMarkup ).find( '.menu-item-actions' ).before( template_content ); + } }); - $('ul#menu-to-edit > li').each(function () { + + $( 'ul#menu-to-edit > li' ).each( function(){ var id = $(this).attr('id').substr(10); - $('fieldset.field-move', $(this)).before(template({menuItemID: id,restriction_data:um_menu_restriction_data[id]})); - }); + var template_content = template({ + menuItemID: id, + restriction_data: um_menu_restriction_data[ id ] + }); + if ( $( this ).find( 'fieldset.field-move' ).length > 0 ) { + $( this ).find( 'fieldset.field-move' ).before( template_content ); + } else { + $( this ).find( '.menu-item-actions' ).before( template_content ); + } + }); }); \ No newline at end of file diff --git a/includes/core/um-navmenu-walker-edit.php b/includes/core/um-navmenu-walker-edit.php deleted file mode 100644 index c5a278fa..00000000 --- a/includes/core/um-navmenu-walker-edit.php +++ /dev/null @@ -1,255 +0,0 @@ - __( 'Display Mode' ), - 'um_nav_roles' => __( 'By Role' ) - - ); - - //add_action( 'wp_nav_menu_item_custom_fields', array( __CLASS__, '_fields' ), 1, 4 ); - add_action( 'wp_update_nav_menu_item', array( __CLASS__, '_save' ), 10, 3 ); - add_filter( 'manage_nav-menus_columns', array( __CLASS__, '_columns' ), 99 ); - - add_action( 'load-nav-menus.php', array( __CLASS__, 'enqueue_nav_menus_scripts' ) ); - add_action( 'admin_footer-nav-menus.php', array( __CLASS__, '_wp_template' ) ); - - } - - - public static function _save( $menu_id, $menu_item_db_id, $menu_item_args ) { - if (defined( 'DOING_AJAX' ) && DOING_AJAX) { - return; - } - - foreach (self::$fields as $_key => $label) { - - if ($_key == 'um_nav_roles') { - - $key = sprintf( 'menu-item-%s', $_key ); - - // Sanitize - if (!empty( $_POST[$key][$menu_item_db_id] )) { - // Do some checks here... - $value = $_POST[$key][$menu_item_db_id]; - } else { - $value = null; - } - - } else { - - $key = sprintf( 'menu-item-%s', $_key ); - - // Sanitize - if (!empty( $_POST[$key][$menu_item_db_id] )) { - // Do some checks here... - $value = $_POST[$key][$menu_item_db_id]; - } else { - $value = null; - } - } - - // Update - if (!is_null( $value )) { - update_post_meta( $menu_item_db_id, $key, $value ); - } else { - delete_post_meta( $menu_item_db_id, $key ); - } - } - } - - public static function _fields( $id, $item, $depth, $args ) { - ?> - -
- -
- -

- - $label) { - $key = sprintf( 'menu-item-%s', $_key ); - $id = sprintf( 'edit-%s-%s', $key, $item->ID ); - $name = sprintf( '%s[%s]', $key, $item->ID ); - $value = get_post_meta( $item->ID, $key, true ); - $role_name = sprintf( '%s[%s][]', $key, $item->ID ); - $class = sprintf( 'field-%s', $_key ); ?> - - - -

- -

- - - -

-
- - roles()->get_roles(); - $i = 0; - $html = ''; - $columns = 2; - while ($i < $columns) { - $per_page = ceil( count( $options ) / $columns ); - $section_fields_per_page = array_slice( $options, $i * $per_page, $per_page ); - $html .= ''; - - foreach ($section_fields_per_page as $k => $title) { - $id_attr = ' id="' . $id . '_' . $k . '" '; - $for_attr = ' for="' . $id . '_' . $k . '" '; - $name_attr = ' name="' . $role_name . '" '; - - $html .= ""; - } - - $html .= ''; - $i++; - } - - echo $html; ?> -

- - -
- -
- - admin_enqueue()->load_nav_manus_scripts(); - - $menus = get_posts( 'post_type=nav_menu_item&numberposts=-1' ); - $menu_restriction_data = array(); - - foreach ($menus as $data) { - $_nav_roles_meta = get_post_meta( $data->ID, 'menu-item-um_nav_roles', true ); - $um_nav_roles = array(); - if ($_nav_roles_meta) - foreach ($_nav_roles_meta as $key => $value) { - if (is_int( $key )) { - $um_nav_roles[] = $value; - } - } - $menu_restriction_data[$data->ID] = array( - 'um_nav_public' => get_post_meta( $data->ID, 'menu-item-um_nav_public', true ), - 'um_nav_roles' => $um_nav_roles, - ); - } - wp_localize_script( 'um_admin_nav_manus', 'um_menu_restriction_data', $menu_restriction_data ); - } - - public static function _wp_template() { - echo ''; - } - - } - - endif; - - UM_Menu_Item_Custom_Fields_Editor::init(); \ No newline at end of file diff --git a/includes/core/um-navmenu-walker-template.php b/includes/core/um-navmenu-walker-template.php deleted file mode 100644 index 43516b88..00000000 --- a/includes/core/um-navmenu-walker-template.php +++ /dev/null @@ -1,37 +0,0 @@ -get_fields( $item, $depth, $args, $id ) ){ - //$item_output = preg_replace('/(?=]+class="[^"]*submitbox)/', $new_fields, $item_output); - $item_output = preg_replace('/(?=<(fieldset|p)[^>]+class="[^"]*field-move)/', $new_fields, $item_output); - } - - $output .= $item_output; - - } - - public function get_fields( $item, $depth, $args = array(), $id = 0 ) { - ob_start(); - - if( isset( $item->ID ) ){ - $id = esc_attr( $item->ID ); - } - - do_action( 'wp_nav_menu_item_custom_fields', $id, $item, $depth, $args ); - - return ob_get_clean(); - } -} - -endif; \ No newline at end of file diff --git a/includes/core/um-navmenu-walker.php b/includes/core/um-navmenu-walker.php deleted file mode 100644 index 98e2f40b..00000000 --- a/includes/core/um-navmenu-walker.php +++ /dev/null @@ -1,34 +0,0 @@ -get_fields( $item, $depth, $args, $id ) ){ - //$item_output = preg_replace('/(?=]+class="[^"]*submitbox)/', $new_fields, $item_output); - $item_output = preg_replace('/(?=<(fieldset|p)[^>]+class="[^"]*field-move)/', $new_fields, $item_output); - } - - $output .= $item_output; - - } - - public function get_fields( $item, $depth, $args = array(), $id = 0 ) { - ob_start(); - - if( isset( $item->ID ) ){ - $id = esc_attr( $item->ID ); - } - - do_action( 'wp_nav_menu_item_custom_fields', $id, $item, $depth, $args ); - - return ob_get_clean(); - } -} - -endif; \ No newline at end of file diff --git a/includes/core/um-navmenu.php b/includes/core/um-navmenu.php index 67b4228c..38344277 100644 --- a/includes/core/um-navmenu.php +++ b/includes/core/um-navmenu.php @@ -1,58 +1,196 @@ options()->get( 'menu_item_workaround' ) ) { - //hard rewrite workaround with conflicted themes/plugins - $walker = 'Walker_Nav_Menu_Edit'; - } - - $walker_filename = dirname( __FILE__ ) . '/um-navmenu-walker.php'; - $walker_template = file_get_contents( dirname( __FILE__ ) . '/um-navmenu-walker-template.php' ); - - $current_walker_content = file_get_contents( $walker_filename ); - if ( strpos( $current_walker_content, $um_walker ) === false || - strpos( $current_walker_content, $walker ) === false ) { - - $walker_template = str_replace( - array( - '{{{%um_navmenu_walker%}}}', - '{{{%parent_walker%}}}' - ), - array( - $um_walker, - $walker - ), - $walker_template + self::$fields = array( + 'um_nav_public' => __( 'Display Mode' ), + 'um_nav_roles' => __( 'By Role' ) ); - $fp = fopen( $walker_filename, 'w+' ); - fwrite( $fp, $walker_template ); - fclose( $fp ); + add_action( 'wp_update_nav_menu_item', array( __CLASS__, '_save' ), 10, 3 ); + //add_filter( 'manage_nav-menus_columns', array( __CLASS__, '_columns' ), 99 ); + + add_action( 'load-nav-menus.php', array( __CLASS__, 'enqueue_nav_menus_scripts' ) ); + add_action( 'admin_footer-nav-menus.php', array( __CLASS__, '_wp_template' ) ); } - require_once $walker_filename; - return $um_walker; + + + /** + * @param $menu_id + * @param $menu_item_db_id + * @param $menu_item_args + */ + public static function _save( $menu_id, $menu_item_db_id, $menu_item_args ) { + if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { + return; + } + + foreach ( self::$fields as $_key => $label ) { + + if ( $_key == 'um_nav_roles' ) { + + $key = sprintf( 'menu-item-%s', $_key ); + + // Sanitize + if ( ! empty( $_POST[ $key ][ $menu_item_db_id ] ) ) { + // Do some checks here... + $value = $_POST[ $key ][ $menu_item_db_id ]; + } else { + $value = null; + } + + } else { + + $key = sprintf( 'menu-item-%s', $_key ); + + // Sanitize + if ( ! empty( $_POST[ $key ][ $menu_item_db_id ] ) ) { + // Do some checks here... + $value = $_POST[ $key ][ $menu_item_db_id ]; + } else { + $value = null; + } + } + + // Update + if ( ! is_null( $value ) ) { + update_post_meta( $menu_item_db_id, $key, $value ); + } else { + delete_post_meta( $menu_item_db_id, $key ); + } + } + } + + + /** + * @param $columns + * + * @return array + */ + public static function _columns( $columns ) { + $columns = array_merge( $columns, self::$fields ); + + return $columns; + } + + + /** + * + */ + public static function enqueue_nav_menus_scripts() { + add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_enqueue_scripts' ) ); + } + + + /** + * + */ + public static function admin_enqueue_scripts() { + UM()->admin_enqueue()->load_nav_manus_scripts(); + + $menus = get_posts( 'post_type=nav_menu_item&numberposts=-1' ); + $menu_restriction_data = array(); + + foreach ( $menus as $data ) { + $_nav_roles_meta = get_post_meta( $data->ID, 'menu-item-um_nav_roles', true ); + + $um_nav_roles = array(); + if ( $_nav_roles_meta ) { + foreach ( $_nav_roles_meta as $key => $value ) { + if ( is_int( $key ) ) { + $um_nav_roles[] = $value; + } + } + } + + $menu_restriction_data[ $data->ID ] = array( + 'um_nav_public' => get_post_meta( $data->ID, 'menu-item-um_nav_public', true ), + 'um_nav_roles' => $um_nav_roles, + ); + } + wp_localize_script( 'um_admin_nav_manus', 'um_menu_restriction_data', $menu_restriction_data ); + } + + + /** + * + */ + public static function _wp_template() { + ?> + +