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

Language PHP Lines 428
MD5 Hash ce5e570f3166a6e981c2f2bbc72d4123 Estimated Cost $6,064 (why?)
Repository https://bitbucket.org/dkrzos/phc.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<?php

class Gecka_Submenu_NavMenuHacks {

    function __construct() {

        if(is_admin() ) {
        
            /* use custom walker */     
            add_filter( 'wp_edit_nav_menu_walker', array($this, 'custom_walker') );
            
            /* add custom field */
            add_action('wp_nav_menu_item_custom_fields', array($this, 'wp_nav_menu_item_custom_fields'),10,4  );
            
            /* save custom field */
            add_action('wp_update_nav_menu_item', array($this, 'wp_update_nav_menu_item'),10,3  );
            
            /* css */
            add_action('admin_init', array($this, 'register_scripts'));
            add_action( "admin_print_styles-nav-menus.php", array($this, 'admin_print_styles') );
        
        }
        add_filter( 'wp_get_nav_menu_items', array($this, 'wp_get_nav_menu_items'),10,3 );
            
        /* set up nav menu item with custom properties */
        add_filter( 'wp_setup_nav_menu_item', array($this, 'wp_setup_nav_menu_item') );
    
        /* customize menu display */
        add_filter( 'walker_nav_menu_start_el', array($this, 'walker_nav_menu_start_el'), 10,4 );
         
    }
    
    function wp_get_nav_menu_items ($items, $menu, $args) {
    
        if( is_admin() && $GLOBALS['pagenow'] == 'nav-menus.php' ) return $items;
    
            $order = sizeof($items)+1;
    
            foreach($items as $item) {
            
                if($item->autopopulate !== '1') continue;
                
                switch ($item->autopopulate_type) {
                
                    case 'subpages':
                        
                        $pages = get_pages( apply_filters('gecka-submenu-get_pages', array('child_of' => $item->object_id, 'sort_column' => 'menu_order, post_title')) );
                        
                        $this->setup_posts ('post', &$item, &$pages, &$items, &$order);
                        
                        break;
                    
                }
            
            }
        return $items;
    }
    
    function setup_posts ($type, &$item, &$posts, &$items, &$order) {
        
        $_ids = $this->get_ids($type, $order, $posts);
        
        foreach($posts as $key=>$post) {
            
            $id = isset($post->ID) ? $post->ID : $post->term_id;
                            
            $parent_id = $item->ID;
                            
            if( isset($post->post_parent) && $post->post_parent && $post->post_parent != $item->object_id ) {
               $parent_id = $_ids[$post->post_parent];
            }
            
            if( isset($post->parent) && $post->parent && $post->parent != $item->object_id ) {
               $parent_id = $_ids[$post->parent];
            }
            
            $items[] = $this->menu_item_from_post($post, $_ids[$id], $parent_id, $order);
            
            $order++;
                            
        }
                       
        return $posts;
    
    }
    
    function get_ids ($type, $order, $items) {
    
        $ids = array();
    
        foreach($items as $item) {
        
            $id = $type == 'post' ? $item->ID : $item->term_id;
        
            $ids[$id] = -$order;
        
            $order++;
        }
        
        
        return $ids;
    
    }
    
    function menu_item_from_post ($post, $pseudo_id, $parent_id = 0, $order=0) {
        $_item = is_object($post) ? clone $post : clone get_post($post);
        
        $id = $_item->ID ;
        
        $_item->ID = $_item->db_id =  $pseudo_id ;
        $_item->post_name = '' . $pseudo_id;
        $_item->object = $post->post_type;
        $_item->post_type = 'nav_menu_item';
        $_item->menu_item_parent = $parent_id;
        $_item->object_id = $id;
        $_item->type = 'post_type';
        $_item->menu_order = $order;
        $_item->description = $_item->post_excerpt;
        
        $object = get_post_type_object( $post->post_type );
		$_item->object 		= $object->name;
		$_item->type_label 	= $object->labels->singular_name;
        
        $_item->url = get_permalink( $post->ID );
        $_item->title = $post->post_title;
        $_item->target = '';
        $_item->attr_title = '';
        $_item->classes = array ( 0 => '');
        $_item->xfn = '';
                
        $_item->db_id =  $pseudo_id;
        
        return $_item;
    
    }    
    
    function walker_nav_menu_start_el ( $item_output, $item, $depth, $args) {
 
    	 if( isset($item->autopopulate) && $item->autopopulate =='subpages') {
        	$args = array( 'depth'        => 0,
					        'child_of'     => $item->object_id,
					        'echo'         => 0, 'title_li' => '' );
        	
            //$item_output = $item_output . '<ul class="sub-menu" >' . wp_list_pages($args) . '</ul>';
           
        }
	    return $item_output;
    }
    

    /**
     * Setup the nav menu object to have the additionnal properties
     */
    function wp_setup_nav_menu_item($menu_item) {
        if ( isset( $menu_item->post_type ) ) {
        
		    if ( 'nav_menu_item' == $menu_item->post_type ) {
		       	$menu_item->autopopulate = empty( $menu_item->autopopulate ) ? get_post_meta( $menu_item->ID, '_menu_item_autopopulate', true ) : $menu_item->autopopulate;
		        $menu_item->autopopulate_type = empty( $menu_item->autopopulate_type ) ? get_post_meta( $menu_item->ID, '_menu_item_autopopulate_type', true ) : $menu_item->autopopulate_type;      
		    }
	    }
        return $menu_item;
    }
   

    /**
     * Saves the new fields
     */
    function wp_update_nav_menu_item($menu_id, $menu_item_db_id, $args) {
    
        $args['menu-item-autopopulate_type'] = isset( $_POST['menu-item-autopopulate_type'][$menu_item_db_id] ) ? $_POST['menu-item-autopopulate_type'][$menu_item_db_id] : '';
        
        if ( empty( $args['menu-item-autopopulate_type'] ) ) {
		    $args['menu-item-autopopulate_type'] = '';
        }

        update_post_meta( $menu_item_db_id, '_menu_item_autopopulate_type', $args['menu-item-autopopulate_type'] );

        
        if( $args['menu-item-autopopulate_type'] === 'subpages' )  $args['menu-item-autopopulate'] = '1';
        else  $args['menu-item-autopopulate'] = '0';

        update_post_meta( $menu_item_db_id, '_menu_item_autopopulate', $args['menu-item-autopopulate'] );

        
        /* old version compatibility */
          
        if(  get_post_meta($menu_item_db_id, '_menu_item_showsub', true) == '1') {
                
            update_post_meta( $menu_item_db_id, '_menu_item_autopopulate_type', 'subpages' );
           	update_post_meta( $menu_item_db_id, '_menu_item_autopopulate', '1' );
            

            delete_post_meta( $menu_item_db_id, '_menu_item_showsub', $args['menu-item-showsub'] );
        }
       
    }
    
    function register_scripts () {    
       wp_register_style('gecka-submenu-nav-menu-edit', GKSM_URL . '/css/admin-nav-menu-edit.css' );
    }
    
    function admin_print_styles () {
        wp_enqueue_style('gecka-submenu-nav-menu-edit');
    }
    
    function admin_print_scripts () { }

    /**
     * Adds a custom fields
     */
    function custom_walker($a) {
        return 'Gecka_Walker_Nav_Menu_Edit';
    }
    
    function wp_nav_menu_item_custom_fields ( $item_id, $item, $depth, $args) {
    	if($item->object === 'page') {
        	include GKSM_PATH . '/views/Nav-Menu-Fields.php';
    	}
    }
    
}

/**
 * Create HTML list of nav menu input items.
 *
 * @package WordPress
 * @since 3.0.0
 * @uses Walker_Nav_Menu
 */
class Gecka_Walker_Nav_Menu_Edit extends Walker_Nav_Menu  {
	/**
	 * @see Walker_Nav_Menu::start_lvl()
	 * @since 3.0.0
	 *
	 * @param string $output Passed by reference.
	 * @param int $depth Depth of page.
	 */
	function start_lvl(&$output) {}

	/**
	 * @see Walker_Nav_Menu::end_lvl()
	 * @since 3.0.0
	 *
	 * @param string $output Passed by reference.
	 * @param int $depth Depth of page.
	 */
	function end_lvl(&$output) {
	}

	/**
	 * @see Walker::start_el()
	 * @since 3.0.0
	 *
	 * @param string $output Passed by reference. Used to append additional content.
	 * @param object $item Menu item data object.
	 * @param int $depth Depth of menu item. Used for padding.
	 * @param int $current_page Menu item ID.
	 * @param object $args
	 */
	function start_el(&$output, $item, $depth, $args) 
	{
		global $_wp_nav_menu_max_depth;
		$_wp_nav_menu_max_depth = $depth > $_wp_nav_menu_max_depth ? $depth : $_wp_nav_menu_max_depth;

		$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

		ob_start();
		$item_id = esc_attr( $item->ID );
		$removed_args = array(
			'action',
			'customlink-tab',
			'edit-menu-item',
			'menu-item',
			'page-tab',
			'_wpnonce',
		);

		$original_title = '';
		if ( 'taxonomy' == $item->type ) {
			$original_title = get_term_field( 'name', $item->object_id, $item->object, 'raw' );
		} elseif ( 'post_type' == $item->type ) {
			$original_object = get_post( $item->object_id );
			$original_title = $original_object->post_title;
		}

		$classes = array(
			'menu-item menu-item-depth-' . $depth,
			'menu-item-' . esc_attr( $item->object ),
			'menu-item-edit-' . ( ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? 'active' : 'inactive'),
		);

		$title = $item->title;

		if ( isset( $item->post_status ) && 'draft' == $item->post_status ) {
			$classes[] = 'pending';
			/* translators: %s: title of menu item in draft status */
			$title = sprintf( __('%s (Pending)'), $item->title );
		}

		$title = empty( $item->label ) ? $title : $item->label;

		?>
		<li id="menu-item-<?php echo $item_id; ?>" class="<?php echo implode(' ', $classes ); ?>">
			<dl class="menu-item-bar">
				<dt class="menu-item-handle">
					<span class="item-title"><?php echo esc_html( $title ); ?></span>
					<span class="item-controls">
						<span class="item-type"><?php echo esc_html( $item->type_label ); ?></span>
						<span class="item-order">
							<a href="<?php
								echo wp_nonce_url(
									add_query_arg(
										array(
											'action' => 'move-up-menu-item',
											'menu-item' => $item_id,
										),
										remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
									),
									'move-menu_item'
								);
							?>" class="item-move-up"><abbr title="<?php esc_attr_e('Move up'); ?>">&#8593;</abbr></a>
							|
							<a href="<?php
								echo wp_nonce_url(
									add_query_arg(
										array(
											'action' => 'move-down-menu-item',
											'menu-item' => $item_id,
										),
										remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
									),
									'move-menu_item'
								);
							?>" class="item-move-down"><abbr title="<?php esc_attr_e('Move down'); ?>">&#8595;</abbr></a>
						</span>
						<a class="item-edit" id="edit-<?php echo $item_id; ?>" title="<?php _e('Edit Menu Item'); ?>" href="<?php
							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 ) ) );
						?>"><?php _e( 'Edit Menu Item' ); ?></a>
					</span>
				</dt>
			</dl>

			<div class="menu-item-settings" id="menu-item-settings-<?php echo $item_id; ?>">
				<?php if( 'custom' == $item->type ) : ?>
					<p class="field-url description description-wide">
						<label for="edit-menu-item-url-<?php echo $item_id; ?>">
							<?php _e( 'URL' ); ?><br />
							<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 ); ?>" />
						</label>
					</p>
				<?php endif; ?>
				<p class="description description-thin">
					<label for="edit-menu-item-title-<?php echo $item_id; ?>">
						<?php _e( 'Navigation Label' ); ?><br />
						<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 ); ?>" />
					</label>
				</p>
				<p class="description description-thin">
					<label for="edit-menu-item-attr-title-<?php echo $item_id; ?>">
						<?php _e( 'Title Attribute' ); ?><br />
						<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 ); ?>" />
					</label>
				</p>
				<p class="field-link-target description description-thin">
					<label for="edit-menu-item-target-<?php echo $item_id; ?>">
						<?php _e( 'Link Target' ); ?><br />
						<select id="edit-menu-item-target-<?php echo $item_id; ?>" class="widefat edit-menu-item-target" name="menu-item-target[<?php echo $item_id; ?>]">
							<option value="" <?php selected( $item->target, ''); ?>><?php _e('Same window or tab'); ?></option>
							<option value="_blank" <?php selected( $item->target, '_blank'); ?>><?php _e('New window or tab'); ?></option>
						</select>
					</label>
				</p>
				<p class="field-css-classes description description-thin">
					<label for="edit-menu-item-classes-<?php echo $item_id; ?>">
						<?php _e( 'CSS Classes (optional)' ); ?><br />
						<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 ) ); ?>" />
					</label>
				</p>
				<p class="field-xfn description description-thin">
					<label for="edit-menu-item-xfn-<?php echo $item_id; ?>">
						<?php _e( 'Link Relationship (XFN)' ); ?><br />
						<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 ); ?>" />
					</label>
				</p>
				<p class="field-description description description-wide">
					<label for="edit-menu-item-description-<?php echo $item_id; ?>">
						<?php _e( 'Description' ); ?><br />
						<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>
						<span class="description"><?php _e('The description will be displayed in the menu if the current theme supports it.'); ?></span>
					</label>
				</p>
				<?php
				do_action('wp_nav_menu_item_custom_fields', $item_id, $item, $depth, $args);
				?>
				<div class="menu-item-actions description-wide submitbox">
					<?php if( 'custom' != $item->type ) : ?>
						<p class="link-to-original">
							<?php printf( __('Original: %s'), '<a href="' . esc_attr( $item->url ) . '">' . esc_html( $original_title ) . '</a>' ); ?>
						</p>
					<?php endif; ?>
					<a class="item-delete submitdelete deletion" id="delete-<?php echo $item_id; ?>" href="<?php
					echo wp_nonce_url(
						add_query_arg(
							array(
								'action' => 'delete-menu-item',
								'menu-item' => $item_id,
							),
							remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
						),
						'delete-menu_item_' . $item_id
					); ?>"><?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' ) ) );
						?>#menu-item-settings-<?php echo $item_id; ?>"><?php _e('Cancel'); ?></a>
				</div>

				<input class="menu-item-data-db-id" type="hidden" name="menu-item-db-id[<?php echo $item_id; ?>]" value="<?php echo $item_id; ?>" />
				<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 ); ?>" />
				<input class="menu-item-data-object" type="hidden" name="menu-item-object[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object ); ?>" />
				<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 ); ?>" />
				<input class="menu-item-data-position" type="hidden" name="menu-item-position[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_order ); ?>" />
				<input class="menu-item-data-type" type="hidden" name="menu-item-type[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->type ); ?>" />
			</div><!-- .menu-item-settings-->
			<ul class="menu-item-transport"></ul>
		<?php
		$output .= ob_get_clean();
	}
}
Back to Top