- reviewed nav menu items custom fields code;

This commit is contained in:
nikitozzzzzzz
2018-02-01 12:09:46 +02:00
parent 1d62265544
commit 1c0f7399cf
5 changed files with 211 additions and 378 deletions
+28 -7
View File
@@ -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 );
}
});
});
-255
View File
@@ -1,255 +0,0 @@
<?php
if (!defined( 'ABSPATH' )) exit;
if (!class_exists( 'UM_Menu_Item_Custom_Fields_Editor' )) :
class UM_Menu_Item_Custom_Fields_Editor {
protected static $fields = array();
/**
* Initialize plugin
*/
public static function init() {
self::$fields = array(
'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;
}
} 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 ) {
?>
<div class="um-nav-edit">
<div class="clear"></div>
<h4 style="margin-bottom: 0.6em;"><?php _e( "Ultimate Member Menu Settings", 'ultimate-member' ) ?></h4>
<?php foreach (self::$fields as $_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 ); ?>
<?php if ($_key == 'um_nav_public') { ?>
<p class="description description-wide um-nav-mode">
<label for="<?php echo $id ?>">
<?php _e( "Who can see this menu link?", 'ultimate-member' ); ?><br/>
<select id="<?php echo $id ?>" name="<?php echo $name ?>" style="width:100%;">
<option value="0" <?php selected( !isset( $value ) || $value == '' ); ?>>
<?php _e( 'Everyone', 'ultimate-member' ) ?>
</option>
<option value="1" <?php selected( 1, $value ); ?>>
<?php _e( 'Logged Out Users', 'ultimate-member' ) ?>
</option>
<option value="2" <?php selected( 2, $value ); ?>>
<?php _e( 'Logged In Users', 'ultimate-member' ) ?>
</option>
</select>
</label>
</p>
<?php }
if ($_key == 'um_nav_roles') { ?>
<p class="description description-wide um-nav-roles">
<?php _e( "Select the member roles that can see this link", 'ultimate-member' ) ?><br/>
<?php $options = UM()->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 .= '<span class="um-form-fields-section" style="width:' . floor( 100 / $columns ) . '% !important;">';
foreach ($section_fields_per_page as $k => $title) {
$id_attr = ' id="' . $id . '_' . $k . '" ';
$for_attr = ' for="' . $id . '_' . $k . '" ';
$name_attr = ' name="' . $role_name . '" ';
$html .= "<label $for_attr>
<input type=\"checkbox\" " . checked( ( is_array( $value ) && in_array( $k, $value ) ) || ( isset( $value ) && $k == $value ), true, false ) . "$id_attr $name_attr value=\"" . $k . "\">
<span>$title</span>
</label>";
}
$html .= '</span>';
$i++;
}
echo $html; ?>
</p>
<?php }
} ?>
<div class="clear"></div>
</div>
<?php
}
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() {
echo '<script type="text/html" id="tmpl-um-nav-menus-fields">';
?>
<div class="um-nav-edit">
<div class="clear"></div>
<h4 style="margin-bottom: 0.6em;"><?php _e( "Ultimate Member Menu Settings", 'ultimate-member' ) ?></h4>
<p class="description description-wide um-nav-mode">
<label for="edit-menu-item-um_nav_public-{{data.menuItemID}}">
<?php _e( "Who can see this menu link?", 'ultimate-member' ); ?><br/>
<select id="edit-menu-item-um_nav_public-{{data.menuItemID}}"
name="menu-item-um_nav_public[{{data.menuItemID}}]" style="width:100%;">
<option value="0"
<# if(data.restriction_data.um_nav_public == '0'){ #>selected="selected"<# } #>>
<?php _e( 'Everyone', 'ultimate-member' ) ?>
</option>
<option value="1"
<# if(data.restriction_data.um_nav_public == '1'){ #>selected="selected"<# } #>>
<?php _e( 'Logged Out Users', 'ultimate-member' ) ?>
</option>
<option value="2"
<# if(data.restriction_data.um_nav_public == '2'){ #>selected="selected"<# } #>>
<?php _e( 'Logged In Users', 'ultimate-member' ) ?>
</option>
</select>
</label>
</p>
<p class="description description-wide um-nav-roles" <# if(data.restriction_data.um_nav_public == '2'){ #>style="display: block;"<# } #>>
<?php _e( "Select the member roles that can see this link", 'ultimate-member' ) ?><br/>
<?php $options = UM()->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 .= '<span class="um-form-fields-section" style="width:' . floor( 100 / $columns ) . '% !important;">';
foreach ($section_fields_per_page as $k => $title) {
$id_attr = ' id="edit-menu-item-um_nav_roles-{{data.menuItemID}}_' . $k . '" ';
$for_attr = ' for="edit-menu-item-um_nav_roles-{{data.menuItemID}}_' . $k . '" ';
$html .= "<label $for_attr>
<input type='checkbox' {$id_attr} name='menu-item-um_nav_roles[{{data.menuItemID}}][]' value='{$k}' <# if(_.contains(data.restriction_data.um_nav_roles,'{$k}')){ #>checked='checked'<# } #> />
<span>{$title}</span>
</label>";
}
$html .= '</span>';
$i++;
}
echo $html; ?>
</p>
<div class="clear"></div>
</div>
<?php
echo '</script>';
}
}
endif;
UM_Menu_Item_Custom_Fields_Editor::init();
@@ -1,37 +0,0 @@
<?php
/**
* This is template for NavMenu Walker Class which extends from latest parents
*/
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! class_exists( '{{{%um_navmenu_walker%}}}' ) ) :
class {{{%um_navmenu_walker%}}} extends {{{%parent_walker%}}} {
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$item_output = '';
parent::start_el( $item_output, $item, $depth, $args, $id );
if( $new_fields = $this->get_fields( $item, $depth, $args, $id ) ){
//$item_output = preg_replace('/(?=<div[^>]+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;
-34
View File
@@ -1,34 +0,0 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! class_exists( 'UM_Menu_Item_Custom_Fields_Walker' ) ) :
class UM_Menu_Item_Custom_Fields_Walker extends Walker_Nav_Menu_Edit {
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$item_output = '';
parent::start_el( $item_output, $item, $depth, $args, $id );
if( $new_fields = $this->get_fields( $item, $depth, $args, $id ) ){
//$item_output = preg_replace('/(?=<div[^>]+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;
+183 -45
View File
@@ -1,58 +1,196 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit;
if ( ! class_exists( 'UM_Menu_Item_Custom_Fields' ) ) :
if ( ! class_exists( 'UM_Menu_Item_Custom_Fields_Editor' ) ) {
class UM_Menu_Item_Custom_Fields {
/**
*
*/
public static function load() {
//add_filter( 'wp_edit_nav_menu_walker', array( __CLASS__, '_filter_walker' ), 999 );
}
class UM_Menu_Item_Custom_Fields_Editor {
/**
* @var array
*/
protected static $fields = array();
/**
* @param $walker
* @return string
*/
public static function _filter_walker( $walker ) {
$um_walker = 'UM_Menu_Item_Custom_Fields_Walker';
/**
* Initialize plugin
*/
public static function init() {
if ( UM()->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() {
?>
<script type="text/html" id="tmpl-um-nav-menus-fields">
<div class="um-nav-edit">
<div class="clear"></div>
<h4 style="margin-bottom: 0.6em;"><?php _e( "Ultimate Member Menu Settings", 'ultimate-member' ) ?></h4>
<p class="description description-wide um-nav-mode">
<label for="edit-menu-item-um_nav_public-{{data.menuItemID}}">
<?php _e( "Who can see this menu link?", 'ultimate-member' ); ?><br/>
<select id="edit-menu-item-um_nav_public-{{data.menuItemID}}"
name="menu-item-um_nav_public[{{data.menuItemID}}]" style="width:100%;">
<option value="0" <# if( data.restriction_data.um_nav_public == '0' ){ #>selected="selected"<# } #>>
<?php _e( 'Everyone', 'ultimate-member' ) ?>
</option>
<option value="1" <# if( data.restriction_data.um_nav_public == '1' ){ #>selected="selected"<# } #>>
<?php _e( 'Logged Out Users', 'ultimate-member' ) ?>
</option>
<option value="2" <# if( data.restriction_data.um_nav_public == '2' ){ #>selected="selected"<# } #>>
<?php _e( 'Logged In Users', 'ultimate-member' ) ?>
</option>
</select>
</label>
</p>
<p class="description description-wide um-nav-roles" <# if( data.restriction_data.um_nav_public == '2' ){ #>style="display: block;"<# } #>>
<?php _e( "Select the member roles that can see this link", 'ultimate-member' ) ?><br/>
<?php $options = UM()->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 .= '<span class="um-form-fields-section" style="width:' . floor( 100 / $columns ) . '% !important;">';
foreach ( $section_fields_per_page as $k => $title ) {
$id_attr = ' id="edit-menu-item-um_nav_roles-{{data.menuItemID}}_' . $k . '" ';
$for_attr = ' for="edit-menu-item-um_nav_roles-{{data.menuItemID}}_' . $k . '" ';
$html .= "<label $for_attr>
<input type='checkbox' {$id_attr} name='menu-item-um_nav_roles[{{data.menuItemID}}][]' value='{$k}' <# if( _.contains( data.restriction_data.um_nav_roles,'{$k}' ) ){ #>checked='checked'<# } #> />
<span>{$title}</span>
</label>";
}
$html .= '</span>';
$i++;
}
echo $html; ?>
</p>
<div class="clear"></div>
</div>
</script>
<?php
}
}
}
//add_action( 'wp_loaded', array( 'UM_Menu_Item_Custom_Fields', 'load' ), 9 );
endif;
require_once dirname( __FILE__ ) . '/um-navmenu-walker-edit.php';
UM_Menu_Item_Custom_Fields_Editor::init();