wp/wp-includes/class-wp-customize-nav-menus.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
   129 	/**
   129 	/**
   130 	 * Performs the post_type and taxonomy queries for loading available menu items.
   130 	 * Performs the post_type and taxonomy queries for loading available menu items.
   131 	 *
   131 	 *
   132 	 * @since 4.3.0
   132 	 * @since 4.3.0
   133 	 *
   133 	 *
   134 	 * @param string $type   Optional. Accepts any custom object type and has built-in support for
   134 	 * @param string $object_type Optional. Accepts any custom object type and has built-in support for
   135 	 *                         'post_type' and 'taxonomy'. Default is 'post_type'.
   135 	 *                            'post_type' and 'taxonomy'. Default is 'post_type'.
   136 	 * @param string $object Optional. Accepts any registered taxonomy or post type name. Default is 'page'.
   136 	 * @param string $object_name Optional. Accepts any registered taxonomy or post type name. Default is 'page'.
   137 	 * @param int    $page   Optional. The page number used to generate the query offset. Default is '0'.
   137 	 * @param int    $page        Optional. The page number used to generate the query offset. Default is '0'.
   138 	 * @return array|WP_Error An array of menu items on success, a WP_Error object on failure.
   138 	 * @return array|WP_Error An array of menu items on success, a WP_Error object on failure.
   139 	 */
   139 	 */
   140 	public function load_available_items_query( $type = 'post_type', $object = 'page', $page = 0 ) {
   140 	public function load_available_items_query( $object_type = 'post_type', $object_name = 'page', $page = 0 ) {
   141 		$items = array();
   141 		$items = array();
   142 
   142 
   143 		if ( 'post_type' === $type ) {
   143 		if ( 'post_type' === $object_type ) {
   144 			$post_type = get_post_type_object( $object );
   144 			$post_type = get_post_type_object( $object_name );
   145 			if ( ! $post_type ) {
   145 			if ( ! $post_type ) {
   146 				return new WP_Error( 'nav_menus_invalid_post_type' );
   146 				return new WP_Error( 'nav_menus_invalid_post_type' );
   147 			}
   147 			}
   148 
   148 
   149 			/*
   149 			/*
   150 			 * If we're dealing with pages, let's prioritize the Front Page,
   150 			 * If we're dealing with pages, let's prioritize the Front Page,
   151 			 * Posts Page and Privacy Policy Page at the top of the list.
   151 			 * Posts Page and Privacy Policy Page at the top of the list.
   152 			 */
   152 			 */
   153 			$important_pages   = array();
   153 			$important_pages   = array();
   154 			$suppress_page_ids = array();
   154 			$suppress_page_ids = array();
   155 			if ( 0 === $page && 'page' === $object ) {
   155 			if ( 0 === $page && 'page' === $object_name ) {
   156 				// Insert Front Page or custom "Home" link.
   156 				// Insert Front Page or custom "Home" link.
   157 				$front_page = 'page' === get_option( 'show_on_front' ) ? (int) get_option( 'page_on_front' ) : 0;
   157 				$front_page = 'page' === get_option( 'show_on_front' ) ? (int) get_option( 'page_on_front' ) : 0;
   158 				if ( ! empty( $front_page ) ) {
   158 				if ( ! empty( $front_page ) ) {
   159 					$front_page_obj      = get_post( $front_page );
   159 					$front_page_obj      = get_post( $front_page );
   160 					$important_pages[]   = $front_page_obj;
   160 					$important_pages[]   = $front_page_obj;
   186 					if ( $privacy_policy_page instanceof WP_Post && 'publish' === $privacy_policy_page->post_status ) {
   186 					if ( $privacy_policy_page instanceof WP_Post && 'publish' === $privacy_policy_page->post_status ) {
   187 						$important_pages[]   = $privacy_policy_page;
   187 						$important_pages[]   = $privacy_policy_page;
   188 						$suppress_page_ids[] = $privacy_policy_page->ID;
   188 						$suppress_page_ids[] = $privacy_policy_page->ID;
   189 					}
   189 					}
   190 				}
   190 				}
   191 			} elseif ( 'post' !== $object && 0 === $page && $post_type->has_archive ) {
   191 			} elseif ( 'post' !== $object_name && 0 === $page && $post_type->has_archive ) {
   192 				// Add a post type archive link.
   192 				// Add a post type archive link.
   193 				$items[] = array(
   193 				$items[] = array(
   194 					'id'         => $object . '-archive',
   194 					'id'         => $object_name . '-archive',
   195 					'title'      => $post_type->labels->archives,
   195 					'title'      => $post_type->labels->archives,
   196 					'type'       => 'post_type_archive',
   196 					'type'       => 'post_type_archive',
   197 					'type_label' => __( 'Post Type Archive' ),
   197 					'type_label' => __( 'Post Type Archive' ),
   198 					'object'     => $object,
   198 					'object'     => $object_name,
   199 					'url'        => get_post_type_archive_link( $object ),
   199 					'url'        => get_post_type_archive_link( $object_name ),
   200 				);
   200 				);
   201 			}
   201 			}
   202 
   202 
   203 			// Prepend posts with nav_menus_created_posts on first page.
   203 			// Prepend posts with nav_menus_created_posts on first page.
   204 			$posts = array();
   204 			$posts = array();
   214 			$args = array(
   214 			$args = array(
   215 				'numberposts' => 10,
   215 				'numberposts' => 10,
   216 				'offset'      => 10 * $page,
   216 				'offset'      => 10 * $page,
   217 				'orderby'     => 'date',
   217 				'orderby'     => 'date',
   218 				'order'       => 'DESC',
   218 				'order'       => 'DESC',
   219 				'post_type'   => $object,
   219 				'post_type'   => $object_name,
   220 			);
   220 			);
   221 
   221 
   222 			// Add suppression array to arguments for get_posts.
   222 			// Add suppression array to arguments for get_posts.
   223 			if ( ! empty( $suppress_page_ids ) ) {
   223 			if ( ! empty( $suppress_page_ids ) ) {
   224 				$args['post__not_in'] = $suppress_page_ids;
   224 				$args['post__not_in'] = $suppress_page_ids;
   251 					'object'     => $post->post_type,
   251 					'object'     => $post->post_type,
   252 					'object_id'  => (int) $post->ID,
   252 					'object_id'  => (int) $post->ID,
   253 					'url'        => get_permalink( (int) $post->ID ),
   253 					'url'        => get_permalink( (int) $post->ID ),
   254 				);
   254 				);
   255 			}
   255 			}
   256 		} elseif ( 'taxonomy' === $type ) {
   256 		} elseif ( 'taxonomy' === $object_type ) {
   257 			$terms = get_terms(
   257 			$terms = get_terms(
   258 				array(
   258 				array(
   259 					'taxonomy'     => $object,
   259 					'taxonomy'     => $object_name,
   260 					'child_of'     => 0,
   260 					'child_of'     => 0,
   261 					'exclude'      => '',
   261 					'exclude'      => '',
   262 					'hide_empty'   => false,
   262 					'hide_empty'   => false,
   263 					'hierarchical' => 1,
   263 					'hierarchical' => 1,
   264 					'include'      => '',
   264 					'include'      => '',
   290 		/**
   290 		/**
   291 		 * Filters the available menu items.
   291 		 * Filters the available menu items.
   292 		 *
   292 		 *
   293 		 * @since 4.3.0
   293 		 * @since 4.3.0
   294 		 *
   294 		 *
   295 		 * @param array  $items  The array of menu items.
   295 		 * @param array  $items       The array of menu items.
   296 		 * @param string $type   The object type.
   296 		 * @param string $object_type The object type.
   297 		 * @param string $object The object name.
   297 		 * @param string $object_name The object name.
   298 		 * @param int    $page   The current page number.
   298 		 * @param int    $page        The current page number.
   299 		 */
   299 		 */
   300 		$items = apply_filters( 'customize_nav_menu_available_items', $items, $type, $object, $page );
   300 		$items = apply_filters( 'customize_nav_menu_available_items', $items, $object_type, $object_name, $page );
   301 
   301 
   302 		return $items;
   302 		return $items;
   303 	}
   303 	}
   304 
   304 
   305 	/**
   305 	/**
   474 
   474 
   475 		return $items;
   475 		return $items;
   476 	}
   476 	}
   477 
   477 
   478 	/**
   478 	/**
   479 	 * Enqueue scripts and styles for Customizer pane.
   479 	 * Enqueues scripts and styles for Customizer pane.
   480 	 *
   480 	 *
   481 	 * @since 4.3.0
   481 	 * @since 4.3.0
   482 	 */
   482 	 */
   483 	public function enqueue_scripts() {
   483 	public function enqueue_scripts() {
   484 		wp_enqueue_style( 'customize-nav-menus' );
   484 		wp_enqueue_style( 'customize-nav-menus' );
   596 		}
   596 		}
   597 		return $setting_args;
   597 		return $setting_args;
   598 	}
   598 	}
   599 
   599 
   600 	/**
   600 	/**
   601 	 * Allow non-statically created settings to be constructed with custom WP_Customize_Setting subclass.
   601 	 * Allows non-statically created settings to be constructed with custom WP_Customize_Setting subclass.
   602 	 *
   602 	 *
   603 	 * @since 4.3.0
   603 	 * @since 4.3.0
   604 	 *
   604 	 *
   605 	 * @param string $setting_class WP_Customize_Setting or a subclass.
   605 	 * @param string $setting_class WP_Customize_Setting or a subclass.
   606 	 * @param string $setting_id    ID for dynamic setting, usually coming from `$_POST['customized']`.
   606 	 * @param string $setting_id    ID for dynamic setting, usually coming from `$_POST['customized']`.
   617 		}
   617 		}
   618 		return $setting_class;
   618 		return $setting_class;
   619 	}
   619 	}
   620 
   620 
   621 	/**
   621 	/**
   622 	 * Add the customizer settings and controls.
   622 	 * Adds the customizer settings and controls.
   623 	 *
   623 	 *
   624 	 * @since 4.3.0
   624 	 * @since 4.3.0
   625 	 */
   625 	 */
   626 	public function customize_register() {
   626 	public function customize_register() {
   627 		$changeset = $this->manager->unsanitized_post_values();
   627 		$changeset = $this->manager->unsanitized_post_values();
   861 			)
   861 			)
   862 		);
   862 		);
   863 	}
   863 	}
   864 
   864 
   865 	/**
   865 	/**
   866 	 * Get the base10 intval.
   866 	 * Gets the base10 intval.
   867 	 *
   867 	 *
   868 	 * This is used as a setting's sanitize_callback; we can't use just plain
   868 	 * This is used as a setting's sanitize_callback; we can't use just plain
   869 	 * intval because the second argument is not what intval() expects.
   869 	 * intval because the second argument is not what intval() expects.
   870 	 *
   870 	 *
   871 	 * @since 4.3.0
   871 	 * @since 4.3.0
   876 	public function intval_base10( $value ) {
   876 	public function intval_base10( $value ) {
   877 		return intval( $value, 10 );
   877 		return intval( $value, 10 );
   878 	}
   878 	}
   879 
   879 
   880 	/**
   880 	/**
   881 	 * Return an array of all the available item types.
   881 	 * Returns an array of all the available item types.
   882 	 *
   882 	 *
   883 	 * @since 4.3.0
   883 	 * @since 4.3.0
   884 	 * @since 4.7.0  Each array item now includes a `$type_label` in addition to `$title`, `$type`, and `$object`.
   884 	 * @since 4.7.0  Each array item now includes a `$type_label` in addition to `$title`, `$type`, and `$object`.
   885 	 *
   885 	 *
   886 	 * @return array The available menu item types.
   886 	 * @return array The available menu item types.
   927 
   927 
   928 		return $item_types;
   928 		return $item_types;
   929 	}
   929 	}
   930 
   930 
   931 	/**
   931 	/**
   932 	 * Add a new `auto-draft` post.
   932 	 * Adds a new `auto-draft` post.
   933 	 *
   933 	 *
   934 	 * @since 4.7.0
   934 	 * @since 4.7.0
   935 	 *
   935 	 *
   936 	 * @param array $postarr {
   936 	 * @param array $postarr {
   937 	 *     Post array. Note that post_status is overridden to be `auto-draft`.
   937 	 *     Post array. Note that post_status is overridden to be `auto-draft`.
  1054 			wp_send_json_success( $data );
  1054 			wp_send_json_success( $data );
  1055 		}
  1055 		}
  1056 	}
  1056 	}
  1057 
  1057 
  1058 	/**
  1058 	/**
  1059 	 * Print the JavaScript templates used to render Menu Customizer components.
  1059 	 * Prints the JavaScript templates used to render Menu Customizer components.
  1060 	 *
  1060 	 *
  1061 	 * Templates are imported into the JS use wp.template.
  1061 	 * Templates are imported into the JS use wp.template.
  1062 	 *
  1062 	 *
  1063 	 * @since 4.3.0
  1063 	 * @since 4.3.0
  1064 	 */
  1064 	 */
  1117 			<p class="customize-control-description customize-section-title-menu_locations-description">{{ data.l10n.locationsDescription }}</p>
  1117 			<p class="customize-control-description customize-section-title-menu_locations-description">{{ data.l10n.locationsDescription }}</p>
  1118 		</script>
  1118 		</script>
  1119 
  1119 
  1120 		<script type="text/html" id="tmpl-nav-menu-create-menu-section-title">
  1120 		<script type="text/html" id="tmpl-nav-menu-create-menu-section-title">
  1121 			<p class="add-new-menu-notice">
  1121 			<p class="add-new-menu-notice">
  1122 				<?php _e( 'It doesn&#8217;t look like your site has any menus yet. Want to build one? Click the button to start.' ); ?>
  1122 				<?php _e( 'It does not look like your site has any menus yet. Want to build one? Click the button to start.' ); ?>
  1123 			</p>
  1123 			</p>
  1124 			<p class="add-new-menu-notice">
  1124 			<p class="add-new-menu-notice">
  1125 				<?php _e( 'You&#8217;ll create a menu, assign it a location, and add menu items like links to pages and categories. If your theme has multiple menu areas, you might need to create more than one.' ); ?>
  1125 				<?php _e( 'You&#8217;ll create a menu, assign it a location, and add menu items like links to pages and categories. If your theme has multiple menu areas, you might need to create more than one.' ); ?>
  1126 			</p>
  1126 			</p>
  1127 			<h3>
  1127 			<h3>
  1132 		</script>
  1132 		</script>
  1133 		<?php
  1133 		<?php
  1134 	}
  1134 	}
  1135 
  1135 
  1136 	/**
  1136 	/**
  1137 	 * Print the HTML template used to render the add-menu-item frame.
  1137 	 * Prints the HTML template used to render the add-menu-item frame.
  1138 	 *
  1138 	 *
  1139 	 * @since 4.3.0
  1139 	 * @since 4.3.0
  1140 	 */
  1140 	 */
  1141 	public function available_items_template() {
  1141 	public function available_items_template() {
  1142 		?>
  1142 		?>
  1190 		</div><!-- #available-menu-items -->
  1190 		</div><!-- #available-menu-items -->
  1191 		<?php
  1191 		<?php
  1192 	}
  1192 	}
  1193 
  1193 
  1194 	/**
  1194 	/**
  1195 	 * Print the markup for new menu items.
  1195 	 * Prints the markup for new menu items.
  1196 	 *
  1196 	 *
  1197 	 * To be used in the template #available-menu-items.
  1197 	 * To be used in the template #available-menu-items.
  1198 	 *
  1198 	 *
  1199 	 * @since 4.7.0
  1199 	 * @since 4.7.0
  1200 	 *
  1200 	 *
  1201 	 * @param array $available_item_type Menu item data to output, including title, type, and label.
  1201 	 * @param array $available_item_type Menu item data to output, including title, type, and label.
  1202 	 * @return void
       
  1203 	 */
  1202 	 */
  1204 	protected function print_post_type_container( $available_item_type ) {
  1203 	protected function print_post_type_container( $available_item_type ) {
  1205 		$id = sprintf( 'available-menu-items-%s-%s', $available_item_type['type'], $available_item_type['object'] );
  1204 		$id = sprintf( 'available-menu-items-%s-%s', $available_item_type['type'], $available_item_type['object'] );
  1206 		?>
  1205 		?>
  1207 		<div id="<?php echo esc_attr( $id ); ?>" class="accordion-section">
  1206 		<div id="<?php echo esc_attr( $id ); ?>" class="accordion-section">
  1235 		</div>
  1234 		</div>
  1236 		<?php
  1235 		<?php
  1237 	}
  1236 	}
  1238 
  1237 
  1239 	/**
  1238 	/**
  1240 	 * Print the markup for available menu item custom links.
  1239 	 * Prints the markup for available menu item custom links.
  1241 	 *
  1240 	 *
  1242 	 * @since 4.7.0
  1241 	 * @since 4.7.0
  1243 	 *
       
  1244 	 * @return void
       
  1245 	 */
  1242 	 */
  1246 	protected function print_custom_links_available_menu_item() {
  1243 	protected function print_custom_links_available_menu_item() {
  1247 		?>
  1244 		?>
  1248 		<div id="new-custom-menu-item" class="accordion-section">
  1245 		<div id="new-custom-menu-item" class="accordion-section">
  1249 			<h4 class="accordion-section-title" role="presentation">
  1246 			<h4 class="accordion-section-title" role="presentation">
  1315 
  1312 
  1316 		return $partial_args;
  1313 		return $partial_args;
  1317 	}
  1314 	}
  1318 
  1315 
  1319 	/**
  1316 	/**
  1320 	 * Add hooks for the Customizer preview.
  1317 	 * Adds hooks for the Customizer preview.
  1321 	 *
  1318 	 *
  1322 	 * @since 4.3.0
  1319 	 * @since 4.3.0
  1323 	 */
  1320 	 */
  1324 	public function customize_preview_init() {
  1321 	public function customize_preview_init() {
  1325 		add_action( 'wp_enqueue_scripts', array( $this, 'customize_preview_enqueue_deps' ) );
  1322 		add_action( 'wp_enqueue_scripts', array( $this, 'customize_preview_enqueue_deps' ) );
  1328 		add_filter( 'wp_footer', array( $this, 'export_preview_data' ), 1 );
  1325 		add_filter( 'wp_footer', array( $this, 'export_preview_data' ), 1 );
  1329 		add_filter( 'customize_render_partials_response', array( $this, 'export_partial_rendered_nav_menu_instances' ) );
  1326 		add_filter( 'customize_render_partials_response', array( $this, 'export_partial_rendered_nav_menu_instances' ) );
  1330 	}
  1327 	}
  1331 
  1328 
  1332 	/**
  1329 	/**
  1333 	 * Make the auto-draft status protected so that it can be queried.
  1330 	 * Makes the auto-draft status protected so that it can be queried.
  1334 	 *
  1331 	 *
  1335 	 * @since 4.7.0
  1332 	 * @since 4.7.0
  1336 	 *
  1333 	 *
  1337 	 * @global array $wp_post_statuses List of post statuses.
  1334 	 * @global stdClass[] $wp_post_statuses List of post statuses.
  1338 	 */
  1335 	 */
  1339 	public function make_auto_draft_status_previewable() {
  1336 	public function make_auto_draft_status_previewable() {
  1340 		global $wp_post_statuses;
  1337 		global $wp_post_statuses;
  1341 		$wp_post_statuses['auto-draft']->protected = true;
  1338 		$wp_post_statuses['auto-draft']->protected = true;
  1342 	}
  1339 	}
  1343 
  1340 
  1344 	/**
  1341 	/**
  1345 	 * Sanitize post IDs for posts created for nav menu items to be published.
  1342 	 * Sanitizes post IDs for posts created for nav menu items to be published.
  1346 	 *
  1343 	 *
  1347 	 * @since 4.7.0
  1344 	 * @since 4.7.0
  1348 	 *
  1345 	 *
  1349 	 * @param array $value Post IDs.
  1346 	 * @param array $value Post IDs.
  1350 	 * @return array Post IDs.
  1347 	 * @return array Post IDs.
  1370 		}
  1367 		}
  1371 		return $post_ids;
  1368 		return $post_ids;
  1372 	}
  1369 	}
  1373 
  1370 
  1374 	/**
  1371 	/**
  1375 	 * Publish the auto-draft posts that were created for nav menu items.
  1372 	 * Publishes the auto-draft posts that were created for nav menu items.
  1376 	 *
  1373 	 *
  1377 	 * The post IDs will have been sanitized by already by
  1374 	 * The post IDs will have been sanitized by already by
  1378 	 * `WP_Customize_Nav_Menu_Items::sanitize_nav_menus_created_posts()` to
  1375 	 * `WP_Customize_Nav_Menu_Items::sanitize_nav_menus_created_posts()` to
  1379 	 * remove any post IDs for which the user cannot publish or for which the
  1376 	 * remove any post IDs for which the user cannot publish or for which the
  1380 	 * post is not an auto-draft.
  1377 	 * post is not an auto-draft.
  1411 			}
  1408 			}
  1412 		}
  1409 		}
  1413 	}
  1410 	}
  1414 
  1411 
  1415 	/**
  1412 	/**
  1416 	 * Keep track of the arguments that are being passed to wp_nav_menu().
  1413 	 * Keeps track of the arguments that are being passed to wp_nav_menu().
  1417 	 *
  1414 	 *
  1418 	 * @since 4.3.0
  1415 	 * @since 4.3.0
  1419 	 *
  1416 	 *
  1420 	 * @see wp_nav_menu()
  1417 	 * @see wp_nav_menu()
  1421 	 * @see WP_Customize_Widgets::filter_dynamic_sidebar_params()
  1418 	 * @see WP_Customize_Widgets::filter_dynamic_sidebar_params()
  1515 	public function hash_nav_menu_args( $args ) {
  1512 	public function hash_nav_menu_args( $args ) {
  1516 		return wp_hash( serialize( $args ) );
  1513 		return wp_hash( serialize( $args ) );
  1517 	}
  1514 	}
  1518 
  1515 
  1519 	/**
  1516 	/**
  1520 	 * Enqueue scripts for the Customizer preview.
  1517 	 * Enqueues scripts for the Customizer preview.
  1521 	 *
  1518 	 *
  1522 	 * @since 4.3.0
  1519 	 * @since 4.3.0
  1523 	 */
  1520 	 */
  1524 	public function customize_preview_enqueue_deps() {
  1521 	public function customize_preview_enqueue_deps() {
  1525 		wp_enqueue_script( 'customize-preview-nav-menus' ); // Note that we have overridden this.
  1522 		wp_enqueue_script( 'customize-preview-nav-menus' ); // Note that we have overridden this.
  1538 		);
  1535 		);
  1539 		printf( '<script>var _wpCustomizePreviewNavMenusExports = %s;</script>', wp_json_encode( $exports ) );
  1536 		printf( '<script>var _wpCustomizePreviewNavMenusExports = %s;</script>', wp_json_encode( $exports ) );
  1540 	}
  1537 	}
  1541 
  1538 
  1542 	/**
  1539 	/**
  1543 	 * Export any wp_nav_menu() calls during the rendering of any partials.
  1540 	 * Exports any wp_nav_menu() calls during the rendering of any partials.
  1544 	 *
  1541 	 *
  1545 	 * @since 4.5.0
  1542 	 * @since 4.5.0
  1546 	 *
  1543 	 *
  1547 	 * @param array $response Response.
  1544 	 * @param array $response Response.
  1548 	 * @return array Response.
  1545 	 * @return array Response.
  1551 		$response['nav_menu_instance_args'] = $this->preview_nav_menu_instance_args;
  1548 		$response['nav_menu_instance_args'] = $this->preview_nav_menu_instance_args;
  1552 		return $response;
  1549 		return $response;
  1553 	}
  1550 	}
  1554 
  1551 
  1555 	/**
  1552 	/**
  1556 	 * Render a specific menu via wp_nav_menu() using the supplied arguments.
  1553 	 * Renders a specific menu via wp_nav_menu() using the supplied arguments.
  1557 	 *
  1554 	 *
  1558 	 * @since 4.3.0
  1555 	 * @since 4.3.0
  1559 	 *
  1556 	 *
  1560 	 * @see wp_nav_menu()
  1557 	 * @see wp_nav_menu()
  1561 	 *
  1558 	 *