]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/customize/class-wp-customize-nav-menu-item-setting.php
WordPress 4.7
[autoinstalls/wordpress.git] / wp-includes / customize / class-wp-customize-nav-menu-item-setting.php
index 0bbee67e2fcb937c915d5824cf440f436f98e3ab..11b5cd6d5a8f25a545abc50d9b3d5db39648fedc 100644 (file)
@@ -132,6 +132,8 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
        /**
         * Status for calling the update method, used in customize_save_response filter.
         *
+        * See {@see 'customize_save_response'}.
+        *
         * When status is inserted, the placeholder post ID is stored in $previous_post_id.
         * When status is error, the error is stored in $update_error.
         *
@@ -231,7 +233,10 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
                        } else {
                                $value = $post_value;
                        }
-               } else if ( isset( $this->value ) ) {
+                       if ( ! empty( $value ) && empty( $value['original_title'] ) ) {
+                               $value['original_title'] = $this->get_original_title( (object) $value );
+                       }
+               } elseif ( isset( $this->value ) ) {
                        $value = $this->value;
                } else {
                        $value = false;
@@ -240,7 +245,11 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
                        if ( $this->post_id > 0 ) {
                                $post = get_post( $this->post_id );
                                if ( $post && self::POST_TYPE === $post->post_type ) {
+                                       $is_title_empty = empty( $post->post_title );
                                        $value = (array) wp_setup_nav_menu_item( $post );
+                                       if ( $is_title_empty ) {
+                                               $value['title'] = '';
+                                       }
                                }
                        }
 
@@ -254,9 +263,82 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
                        $value = $this->value;
                }
 
+               if ( ! empty( $value ) && empty( $value['type_label'] ) ) {
+                       $value['type_label'] = $this->get_type_label( (object) $value );
+               }
+
                return $value;
        }
 
+       /**
+        * Get original title.
+        *
+        * @since 4.7.0
+        * @access protected
+        *
+        * @param object $item Nav menu item.
+        * @return string The original title.
+        */
+       protected function get_original_title( $item ) {
+               $original_title = '';
+               if ( 'post_type' === $item->type && ! empty( $item->object_id ) ) {
+                       $original_object = get_post( $item->object_id );
+                       if ( $original_object ) {
+                               /** This filter is documented in wp-includes/post-template.php */
+                               $original_title = apply_filters( 'the_title', $original_object->post_title, $original_object->ID );
+
+                               if ( '' === $original_title ) {
+                                       /* translators: %d: ID of a post */
+                                       $original_title = sprintf( __( '#%d (no title)' ), $original_object->ID );
+                               }
+                       }
+               } elseif ( 'taxonomy' === $item->type && ! empty( $item->object_id ) ) {
+                       $original_term_title = get_term_field( 'name', $item->object_id, $item->object, 'raw' );
+                       if ( ! is_wp_error( $original_term_title ) ) {
+                               $original_title = $original_term_title;
+                       }
+               } elseif ( 'post_type_archive' === $item->type ) {
+                       $original_object = get_post_type_object( $item->object );
+                       if ( $original_object ) {
+                               $original_title = $original_object->labels->archives;
+                       }
+               }
+               $original_title = html_entity_decode( $original_title, ENT_QUOTES, get_bloginfo( 'charset' ) );
+               return $original_title;
+       }
+
+       /**
+        * Get type label.
+        *
+        * @since 4.7.0
+        * @access protected
+        *
+        * @param object $item Nav menu item.
+        * @returns string The type label.
+        */
+       protected function get_type_label( $item ) {
+               if ( 'post_type' === $item->type ) {
+                       $object = get_post_type_object( $item->object );
+                       if ( $object ) {
+                               $type_label = $object->labels->singular_name;
+                       } else {
+                               $type_label = $item->object;
+                       }
+               } elseif ( 'taxonomy' === $item->type ) {
+                       $object = get_taxonomy( $item->object );
+                       if ( $object ) {
+                               $type_label = $object->labels->singular_name;
+                       } else {
+                               $type_label = $item->object;
+                       }
+               } elseif ( 'post_type_archive' === $item->type ) {
+                       $type_label = __( 'Post Type Archive' );
+               } else {
+                       $type_label = __( 'Custom Link' );
+               }
+               return $type_label;
+       }
+
        /**
         * Ensure that the value is fully populated with the necessary properties.
         *
@@ -282,16 +364,7 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
                }
 
                if ( ! isset( $this->value['original_title'] ) ) {
-                       $original_title = '';
-                       if ( 'post_type' === $this->value['type'] ) {
-                               $original_title = get_the_title( $this->value['object_id'] );
-                       } elseif ( 'taxonomy' === $this->value['type'] ) {
-                               $original_title = get_term_field( 'name', $this->value['object_id'], $this->value['object'], 'raw' );
-                               if ( is_wp_error( $original_title ) ) {
-                                       $original_title = '';
-                               }
-                       }
-                       $this->value['original_title'] = html_entity_decode( $original_title, ENT_QUOTES, get_bloginfo( 'charset' ) );
+                       $this->value['original_title'] = $this->get_original_title( (object) $this->value );
                }
 
                if ( ! isset( $this->value['nav_menu_term_id'] ) && $this->post_id > 0 ) {
@@ -404,7 +477,7 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
        }
 
        /**
-        * Filter the wp_get_nav_menu_items() result to supply the previewed menu items.
+        * Filters the wp_get_nav_menu_items() result to supply the previewed menu items.
         *
         * @since 4.3.0
         * @access public
@@ -501,8 +574,9 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
                }
 
                if ( ARRAY_A === $args['output'] ) {
-                       $GLOBALS['_menu_item_sort_prop'] = $args['output_key'];
-                       usort( $items, '_sort_nav_menu_items' );
+                       $items = wp_list_sort( $items, array(
+                               $args['output_key'] => 'ASC',
+                       ) );
                        $i = 1;
 
                        foreach ( $items as $k => $item ) {
@@ -532,6 +606,12 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
                $item->menu_order = $item->position;
                unset( $item->position );
 
+               if ( empty( $item->original_title ) ) {
+                       $item->original_title = $this->get_original_title( $item );
+               }
+               if ( empty( $item->title ) && ! empty( $item->original_title ) ) {
+                       $item->title = $item->original_title;
+               }
                if ( $item->title ) {
                        $item->post_title = $item->title;
                }
@@ -545,23 +625,19 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
                }
 
                if ( ! isset( $post->type_label ) ) {
-                       if ( 'post_type' === $post->type ) {
-                               $object = get_post_type_object( $post->object );
-                               if ( $object ) {
-                                       $post->type_label = $object->labels->singular_name;
-                               } else {
-                                       $post->type_label = $post->object;
-                               }
-                       } elseif ( 'taxonomy' == $post->type ) {
-                               $object = get_taxonomy( $post->object );
-                               if ( $object ) {
-                                       $post->type_label = $object->labels->singular_name;
-                               } else {
-                                       $post->type_label = $post->object;
-                               }
-                       } else {
-                               $post->type_label = __( 'Custom Link' );
-                       }
+                       $post->type_label = $this->get_type_label( $post );
+               }
+
+               // Ensure nav menu item URL is set according to linked object.
+               if ( 'post_type' === $post->type && ! empty( $post->object_id ) ) {
+                       $post->url = get_permalink( $post->object_id );
+               } elseif ( 'taxonomy' === $post->type && ! empty( $post->object ) && ! empty( $post->object_id ) ) {
+                       $post->url = get_term_link( (int) $post->object_id, $post->object );
+               } elseif ( 'post_type_archive' === $post->type && ! empty( $post->object ) ) {
+                       $post->url = get_post_type_archive_link( $post->object );
+               }
+               if ( is_wp_error( $post->url ) ) {
+                       $post->url = '';
                }
 
                /** This filter is documented in wp-includes/nav-menu.php */
@@ -570,6 +646,9 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
                /** This filter is documented in wp-includes/nav-menu.php */
                $post->description = apply_filters( 'nav_menu_description', wp_trim_words( $post->description, 200 ) );
 
+               /** This filter is documented in wp-includes/nav-menu.php */
+               $post = apply_filters( 'wp_setup_nav_menu_item', $post );
+
                return $post;
        }
 
@@ -655,11 +734,11 @@ class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
        }
 
        /**
-        * Create/update the nav_menu_item post for this setting.
+        * Creates/updates the nav_menu_item post for this setting.
         *
         * Any created menu items will have their assigned post IDs exported to the client
-        * via the customize_save_response filter. Likewise, any errors will be exported
-        * to the client via the customize_save_response() filter.
+        * via the {@see 'customize_save_response'} filter. Likewise, any errors will be
+        * exported to the client via the customize_save_response() filter.
         *
         * To delete a menu, the client can send false as the value.
         *