PageRenderTime 4ms CodeModel.GetById 150ms app.highlight 25ms RepoModel.GetById 36ms app.codeStats 0ms

/htdocs/wp-content/plugins/_gecka-submenu/models/NavMenuHacks.php

https://bitbucket.org/dkrzos/phc
PHP | 427 lines | 285 code | 93 blank | 49 comment | 40 complexity | ce5e570f3166a6e981c2f2bbc72d4123 MD5 | raw file
  1<?php
  2
  3class Gecka_Submenu_NavMenuHacks {
  4
  5    function __construct() {
  6
  7        if(is_admin() ) {
  8        
  9            /* use custom walker */     
 10            add_filter( 'wp_edit_nav_menu_walker', array($this, 'custom_walker') );
 11            
 12            /* add custom field */
 13            add_action('wp_nav_menu_item_custom_fields', array($this, 'wp_nav_menu_item_custom_fields'),10,4  );
 14            
 15            /* save custom field */
 16            add_action('wp_update_nav_menu_item', array($this, 'wp_update_nav_menu_item'),10,3  );
 17            
 18            /* css */
 19            add_action('admin_init', array($this, 'register_scripts'));
 20            add_action( "admin_print_styles-nav-menus.php", array($this, 'admin_print_styles') );
 21        
 22        }
 23        add_filter( 'wp_get_nav_menu_items', array($this, 'wp_get_nav_menu_items'),10,3 );
 24            
 25        /* set up nav menu item with custom properties */
 26        add_filter( 'wp_setup_nav_menu_item', array($this, 'wp_setup_nav_menu_item') );
 27    
 28        /* customize menu display */
 29        add_filter( 'walker_nav_menu_start_el', array($this, 'walker_nav_menu_start_el'), 10,4 );
 30         
 31    }
 32    
 33    function wp_get_nav_menu_items ($items, $menu, $args) {
 34    
 35        if( is_admin() && $GLOBALS['pagenow'] == 'nav-menus.php' ) return $items;
 36    
 37            $order = sizeof($items)+1;
 38    
 39            foreach($items as $item) {
 40            
 41                if($item->autopopulate !== '1') continue;
 42                
 43                switch ($item->autopopulate_type) {
 44                
 45                    case 'subpages':
 46                        
 47                        $pages = get_pages( apply_filters('gecka-submenu-get_pages', array('child_of' => $item->object_id, 'sort_column' => 'menu_order, post_title')) );
 48                        
 49                        $this->setup_posts ('post', &$item, &$pages, &$items, &$order);
 50                        
 51                        break;
 52                    
 53                }
 54            
 55            }
 56        return $items;
 57    }
 58    
 59    function setup_posts ($type, &$item, &$posts, &$items, &$order) {
 60        
 61        $_ids = $this->get_ids($type, $order, $posts);
 62        
 63        foreach($posts as $key=>$post) {
 64            
 65            $id = isset($post->ID) ? $post->ID : $post->term_id;
 66                            
 67            $parent_id = $item->ID;
 68                            
 69            if( isset($post->post_parent) && $post->post_parent && $post->post_parent != $item->object_id ) {
 70               $parent_id = $_ids[$post->post_parent];
 71            }
 72            
 73            if( isset($post->parent) && $post->parent && $post->parent != $item->object_id ) {
 74               $parent_id = $_ids[$post->parent];
 75            }
 76            
 77            $items[] = $this->menu_item_from_post($post, $_ids[$id], $parent_id, $order);
 78            
 79            $order++;
 80                            
 81        }
 82                       
 83        return $posts;
 84    
 85    }
 86    
 87    function get_ids ($type, $order, $items) {
 88    
 89        $ids = array();
 90    
 91        foreach($items as $item) {
 92        
 93            $id = $type == 'post' ? $item->ID : $item->term_id;
 94        
 95            $ids[$id] = -$order;
 96        
 97            $order++;
 98        }
 99        
100        
101        return $ids;
102    
103    }
104    
105    function menu_item_from_post ($post, $pseudo_id, $parent_id = 0, $order=0) {
106        $_item = is_object($post) ? clone $post : clone get_post($post);
107        
108        $id = $_item->ID ;
109        
110        $_item->ID = $_item->db_id =  $pseudo_id ;
111        $_item->post_name = '' . $pseudo_id;
112        $_item->object = $post->post_type;
113        $_item->post_type = 'nav_menu_item';
114        $_item->menu_item_parent = $parent_id;
115        $_item->object_id = $id;
116        $_item->type = 'post_type';
117        $_item->menu_order = $order;
118        $_item->description = $_item->post_excerpt;
119        
120        $object = get_post_type_object( $post->post_type );
121		$_item->object 		= $object->name;
122		$_item->type_label 	= $object->labels->singular_name;
123        
124        $_item->url = get_permalink( $post->ID );
125        $_item->title = $post->post_title;
126        $_item->target = '';
127        $_item->attr_title = '';
128        $_item->classes = array ( 0 => '');
129        $_item->xfn = '';
130                
131        $_item->db_id =  $pseudo_id;
132        
133        return $_item;
134    
135    }    
136    
137    function walker_nav_menu_start_el ( $item_output, $item, $depth, $args) {
138 
139    	 if( isset($item->autopopulate) && $item->autopopulate =='subpages') {
140        	$args = array( 'depth'        => 0,
141					        'child_of'     => $item->object_id,
142					        'echo'         => 0, 'title_li' => '' );
143        	
144            //$item_output = $item_output . '<ul class="sub-menu" >' . wp_list_pages($args) . '</ul>';
145           
146        }
147	    return $item_output;
148    }
149    
150
151    /**
152     * Setup the nav menu object to have the additionnal properties
153     */
154    function wp_setup_nav_menu_item($menu_item) {
155        if ( isset( $menu_item->post_type ) ) {
156        
157		    if ( 'nav_menu_item' == $menu_item->post_type ) {
158		       	$menu_item->autopopulate = empty( $menu_item->autopopulate ) ? get_post_meta( $menu_item->ID, '_menu_item_autopopulate', true ) : $menu_item->autopopulate;
159		        $menu_item->autopopulate_type = empty( $menu_item->autopopulate_type ) ? get_post_meta( $menu_item->ID, '_menu_item_autopopulate_type', true ) : $menu_item->autopopulate_type;      
160		    }
161	    }
162        return $menu_item;
163    }
164   
165
166    /**
167     * Saves the new fields
168     */
169    function wp_update_nav_menu_item($menu_id, $menu_item_db_id, $args) {
170    
171        $args['menu-item-autopopulate_type'] = isset( $_POST['menu-item-autopopulate_type'][$menu_item_db_id] ) ? $_POST['menu-item-autopopulate_type'][$menu_item_db_id] : '';
172        
173        if ( empty( $args['menu-item-autopopulate_type'] ) ) {
174		    $args['menu-item-autopopulate_type'] = '';
175        }
176
177        update_post_meta( $menu_item_db_id, '_menu_item_autopopulate_type', $args['menu-item-autopopulate_type'] );
178
179        
180        if( $args['menu-item-autopopulate_type'] === 'subpages' )  $args['menu-item-autopopulate'] = '1';
181        else  $args['menu-item-autopopulate'] = '0';
182
183        update_post_meta( $menu_item_db_id, '_menu_item_autopopulate', $args['menu-item-autopopulate'] );
184
185        
186        /* old version compatibility */
187          
188        if(  get_post_meta($menu_item_db_id, '_menu_item_showsub', true) == '1') {
189                
190            update_post_meta( $menu_item_db_id, '_menu_item_autopopulate_type', 'subpages' );
191           	update_post_meta( $menu_item_db_id, '_menu_item_autopopulate', '1' );
192            
193
194            delete_post_meta( $menu_item_db_id, '_menu_item_showsub', $args['menu-item-showsub'] );
195        }
196       
197    }
198    
199    function register_scripts () {    
200       wp_register_style('gecka-submenu-nav-menu-edit', GKSM_URL . '/css/admin-nav-menu-edit.css' );
201    }
202    
203    function admin_print_styles () {
204        wp_enqueue_style('gecka-submenu-nav-menu-edit');
205    }
206    
207    function admin_print_scripts () { }
208
209    /**
210     * Adds a custom fields
211     */
212    function custom_walker($a) {
213        return 'Gecka_Walker_Nav_Menu_Edit';
214    }
215    
216    function wp_nav_menu_item_custom_fields ( $item_id, $item, $depth, $args) {
217    	if($item->object === 'page') {
218        	include GKSM_PATH . '/views/Nav-Menu-Fields.php';
219    	}
220    }
221    
222}
223
224/**
225 * Create HTML list of nav menu input items.
226 *
227 * @package WordPress
228 * @since 3.0.0
229 * @uses Walker_Nav_Menu
230 */
231class Gecka_Walker_Nav_Menu_Edit extends Walker_Nav_Menu  {
232	/**
233	 * @see Walker_Nav_Menu::start_lvl()
234	 * @since 3.0.0
235	 *
236	 * @param string $output Passed by reference.
237	 * @param int $depth Depth of page.
238	 */
239	function start_lvl(&$output) {}
240
241	/**
242	 * @see Walker_Nav_Menu::end_lvl()
243	 * @since 3.0.0
244	 *
245	 * @param string $output Passed by reference.
246	 * @param int $depth Depth of page.
247	 */
248	function end_lvl(&$output) {
249	}
250
251	/**
252	 * @see Walker::start_el()
253	 * @since 3.0.0
254	 *
255	 * @param string $output Passed by reference. Used to append additional content.
256	 * @param object $item Menu item data object.
257	 * @param int $depth Depth of menu item. Used for padding.
258	 * @param int $current_page Menu item ID.
259	 * @param object $args
260	 */
261	function start_el(&$output, $item, $depth, $args) 
262	{
263		global $_wp_nav_menu_max_depth;
264		$_wp_nav_menu_max_depth = $depth > $_wp_nav_menu_max_depth ? $depth : $_wp_nav_menu_max_depth;
265
266		$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
267
268		ob_start();
269		$item_id = esc_attr( $item->ID );
270		$removed_args = array(
271			'action',
272			'customlink-tab',
273			'edit-menu-item',
274			'menu-item',
275			'page-tab',
276			'_wpnonce',
277		);
278
279		$original_title = '';
280		if ( 'taxonomy' == $item->type ) {
281			$original_title = get_term_field( 'name', $item->object_id, $item->object, 'raw' );
282		} elseif ( 'post_type' == $item->type ) {
283			$original_object = get_post( $item->object_id );
284			$original_title = $original_object->post_title;
285		}
286
287		$classes = array(
288			'menu-item menu-item-depth-' . $depth,
289			'menu-item-' . esc_attr( $item->object ),
290			'menu-item-edit-' . ( ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? 'active' : 'inactive'),
291		);
292
293		$title = $item->title;
294
295		if ( isset( $item->post_status ) && 'draft' == $item->post_status ) {
296			$classes[] = 'pending';
297			/* translators: %s: title of menu item in draft status */
298			$title = sprintf( __('%s (Pending)'), $item->title );
299		}
300
301		$title = empty( $item->label ) ? $title : $item->label;
302
303		?>
304		<li id="menu-item-<?php echo $item_id; ?>" class="<?php echo implode(' ', $classes ); ?>">
305			<dl class="menu-item-bar">
306				<dt class="menu-item-handle">
307					<span class="item-title"><?php echo esc_html( $title ); ?></span>
308					<span class="item-controls">
309						<span class="item-type"><?php echo esc_html( $item->type_label ); ?></span>
310						<span class="item-order">
311							<a href="<?php
312								echo wp_nonce_url(
313									add_query_arg(
314										array(
315											'action' => 'move-up-menu-item',
316											'menu-item' => $item_id,
317										),
318										remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
319									),
320									'move-menu_item'
321								);
322							?>" class="item-move-up"><abbr title="<?php esc_attr_e('Move up'); ?>">&#8593;</abbr></a>
323							|
324							<a href="<?php
325								echo wp_nonce_url(
326									add_query_arg(
327										array(
328											'action' => 'move-down-menu-item',
329											'menu-item' => $item_id,
330										),
331										remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
332									),
333									'move-menu_item'
334								);
335							?>" class="item-move-down"><abbr title="<?php esc_attr_e('Move down'); ?>">&#8595;</abbr></a>
336						</span>
337						<a class="item-edit" id="edit-<?php echo $item_id; ?>" title="<?php _e('Edit Menu Item'); ?>" href="<?php
338							echo ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? admin_url( 'nav-menus.php' ) : add_query_arg( 'edit-menu-item', $item_id, remove_query_arg( $removed_args, admin_url( 'nav-menus.php#menu-item-settings-' . $item_id ) ) );
339						?>"><?php _e( 'Edit Menu Item' ); ?></a>
340					</span>
341				</dt>
342			</dl>
343
344			<div class="menu-item-settings" id="menu-item-settings-<?php echo $item_id; ?>">
345				<?php if( 'custom' == $item->type ) : ?>
346					<p class="field-url description description-wide">
347						<label for="edit-menu-item-url-<?php echo $item_id; ?>">
348							<?php _e( 'URL' ); ?><br />
349							<input type="text" id="edit-menu-item-url-<?php echo $item_id; ?>" class="widefat code edit-menu-item-url" name="menu-item-url[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->url ); ?>" />
350						</label>
351					</p>
352				<?php endif; ?>
353				<p class="description description-thin">
354					<label for="edit-menu-item-title-<?php echo $item_id; ?>">
355						<?php _e( 'Navigation Label' ); ?><br />
356						<input type="text" id="edit-menu-item-title-<?php echo $item_id; ?>" class="widefat edit-menu-item-title" name="menu-item-title[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->title ); ?>" />
357					</label>
358				</p>
359				<p class="description description-thin">
360					<label for="edit-menu-item-attr-title-<?php echo $item_id; ?>">
361						<?php _e( 'Title Attribute' ); ?><br />
362						<input type="text" id="edit-menu-item-attr-title-<?php echo $item_id; ?>" class="widefat edit-menu-item-attr-title" name="menu-item-attr-title[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->post_excerpt ); ?>" />
363					</label>
364				</p>
365				<p class="field-link-target description description-thin">
366					<label for="edit-menu-item-target-<?php echo $item_id; ?>">
367						<?php _e( 'Link Target' ); ?><br />
368						<select id="edit-menu-item-target-<?php echo $item_id; ?>" class="widefat edit-menu-item-target" name="menu-item-target[<?php echo $item_id; ?>]">
369							<option value="" <?php selected( $item->target, ''); ?>><?php _e('Same window or tab'); ?></option>
370							<option value="_blank" <?php selected( $item->target, '_blank'); ?>><?php _e('New window or tab'); ?></option>
371						</select>
372					</label>
373				</p>
374				<p class="field-css-classes description description-thin">
375					<label for="edit-menu-item-classes-<?php echo $item_id; ?>">
376						<?php _e( 'CSS Classes (optional)' ); ?><br />
377						<input type="text" id="edit-menu-item-classes-<?php echo $item_id; ?>" class="widefat code edit-menu-item-classes" name="menu-item-classes[<?php echo $item_id; ?>]" value="<?php echo esc_attr( implode(' ', $item->classes ) ); ?>" />
378					</label>
379				</p>
380				<p class="field-xfn description description-thin">
381					<label for="edit-menu-item-xfn-<?php echo $item_id; ?>">
382						<?php _e( 'Link Relationship (XFN)' ); ?><br />
383						<input type="text" id="edit-menu-item-xfn-<?php echo $item_id; ?>" class="widefat code edit-menu-item-xfn" name="menu-item-xfn[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->xfn ); ?>" />
384					</label>
385				</p>
386				<p class="field-description description description-wide">
387					<label for="edit-menu-item-description-<?php echo $item_id; ?>">
388						<?php _e( 'Description' ); ?><br />
389						<textarea id="edit-menu-item-description-<?php echo $item_id; ?>" class="widefat edit-menu-item-description" rows="3" cols="20" name="menu-item-description[<?php echo $item_id; ?>]"><?php echo esc_html( $item->description ); ?></textarea>
390						<span class="description"><?php _e('The description will be displayed in the menu if the current theme supports it.'); ?></span>
391					</label>
392				</p>
393				<?php
394				do_action('wp_nav_menu_item_custom_fields', $item_id, $item, $depth, $args);
395				?>
396				<div class="menu-item-actions description-wide submitbox">
397					<?php if( 'custom' != $item->type ) : ?>
398						<p class="link-to-original">
399							<?php printf( __('Original: %s'), '<a href="' . esc_attr( $item->url ) . '">' . esc_html( $original_title ) . '</a>' ); ?>
400						</p>
401					<?php endif; ?>
402					<a class="item-delete submitdelete deletion" id="delete-<?php echo $item_id; ?>" href="<?php
403					echo wp_nonce_url(
404						add_query_arg(
405							array(
406								'action' => 'delete-menu-item',
407								'menu-item' => $item_id,
408							),
409							remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
410						),
411						'delete-menu_item_' . $item_id
412					); ?>"><?php _e('Remove'); ?></a> <span class="meta-sep"> | </span> <a class="item-cancel submitcancel" id="cancel-<?php echo $item_id; ?>" href="<?php	echo add_query_arg( array('edit-menu-item' => $item_id, 'cancel' => time()), remove_query_arg( $removed_args, admin_url( 'nav-menus.php' ) ) );
413						?>#menu-item-settings-<?php echo $item_id; ?>"><?php _e('Cancel'); ?></a>
414				</div>
415
416				<input class="menu-item-data-db-id" type="hidden" name="menu-item-db-id[<?php echo $item_id; ?>]" value="<?php echo $item_id; ?>" />
417				<input class="menu-item-data-object-id" type="hidden" name="menu-item-object-id[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object_id ); ?>" />
418				<input class="menu-item-data-object" type="hidden" name="menu-item-object[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object ); ?>" />
419				<input class="menu-item-data-parent-id" type="hidden" name="menu-item-parent-id[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_item_parent ); ?>" />
420				<input class="menu-item-data-position" type="hidden" name="menu-item-position[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_order ); ?>" />
421				<input class="menu-item-data-type" type="hidden" name="menu-item-type[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->type ); ?>" />
422			</div><!-- .menu-item-settings-->
423			<ul class="menu-item-transport"></ul>
424		<?php
425		$output .= ob_get_clean();
426	}
427}