返回介绍

_wp_ajax_menu_quick_search()

发布于 2017-09-11 13:39:31 字数 5545 浏览 1162 评论 0 收藏 0

_wp_ajax_menu_quick_search( array $request = array() )

Prints the appropriate response to a menu quick search.


description


参数

$request

(array) (Optional) The unsanitized request values.

Default value: array()


源代码

File: wp-admin/includes/nav-menu.php

function _wp_ajax_menu_quick_search( $request = array() ) {
	$args = array();
	$type = isset( $request['type'] ) ? $request['type'] : '';
	$object_type = isset( $request['object_type'] ) ? $request['object_type'] : '';
	$query = isset( $request['q'] ) ? $request['q'] : '';
	$response_format = isset( $request['response-format'] ) && in_array( $request['response-format'], array( 'json', 'markup' ) ) ? $request['response-format'] : 'json';

	if ( 'markup' == $response_format ) {
		$args['walker'] = new Walker_Nav_Menu_Checklist;
	}

	if ( 'get-post-item' == $type ) {
		if ( post_type_exists( $object_type ) ) {
			if ( isset( $request['ID'] ) ) {
				$object_id = (int) $request['ID'];
				if ( 'markup' == $response_format ) {
					echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( get_post( $object_id ) ) ), 0, (object) $args );
				} elseif ( 'json' == $response_format ) {
					echo wp_json_encode(
array(
'ID' => $object_id,
'post_title' => get_the_title( $object_id ),
'post_type' => get_post_type( $object_id ),
)
					);
					echo "\n";
				}
			}
		} elseif ( taxonomy_exists( $object_type ) ) {
			if ( isset( $request['ID'] ) ) {
				$object_id = (int) $request['ID'];
				if ( 'markup' == $response_format ) {
					echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( get_term( $object_id, $object_type ) ) ), 0, (object) $args );
				} elseif ( 'json' == $response_format ) {
					$post_obj = get_term( $object_id, $object_type );
					echo wp_json_encode(
array(
'ID' => $object_id,
'post_title' => $post_obj->name,
'post_type' => $object_type,
)
					);
					echo "\n";
				}
			}

		}

	} elseif ( preg_match('/quick-search-(posttype|taxonomy)-([a-zA-Z_-]*\b)/', $type, $matches) ) {
		if ( 'posttype' == $matches[1] && get_post_type_object( $matches[2] ) ) {
			$post_type_obj = _wp_nav_menu_meta_box_object( get_post_type_object( $matches[2] ) );
			$args = array_merge(
				$args,
				array(
					'no_found_rows'          => true,
					'update_post_meta_cache' => false,
					'update_post_term_cache' => false,
					'posts_per_page'         => 10,
					'post_type'              => $matches[2],
					's'                      => $query,
				)
			);
			if ( isset( $post_type_obj->_default_query ) ) {
				$args = array_merge( $args, (array) $post_type_obj->_default_query );
			}
			$search_results_query = new WP_Query( $args );
			if ( ! $search_results_query->have_posts() ) {
				return;
			}
			while ( $search_results_query->have_posts() ) {
				$post = $search_results_query->next_post();
				if ( 'markup' == $response_format ) {
					$var_by_ref = $post->ID;
					echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( get_post( $var_by_ref ) ) ), 0, (object) $args );
				} elseif ( 'json' == $response_format ) {
					echo wp_json_encode(
array(
'ID' => $post->ID,
'post_title' => get_the_title( $post->ID ),
'post_type' => $matches[2],
)
					);
					echo "\n";
				}
			}
		} elseif ( 'taxonomy' == $matches[1] ) {
			$terms = get_terms( $matches[2], array(
				'name__like' => $query,
				'number' => 10,
			));
			if ( empty( $terms ) || is_wp_error( $terms ) )
				return;
			foreach ( (array) $terms as $term ) {
				if ( 'markup' == $response_format ) {
					echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( $term ) ), 0, (object) $args );
				} elseif ( 'json' == $response_format ) {
					echo wp_json_encode(
array(
'ID' => $term->term_id,
'post_title' => $term->name,
'post_type' => $matches[2],
)
					);
					echo "\n";
				}
			}
		}
	}
}

更新日志

Versiondescription
3.0.0Introduced.

相关函数

Uses

  • wp-includes/functions.php: wp_json_encode()
  • wp-admin/includes/class-walker-nav-menu-checklist.php: Walker_Nav_Menu_Checklist::__construct()
  • wp-admin/includes/nav-menu.php: _wp_nav_menu_meta_box_object()
  • wp-includes/class-wp-query.php: WP_Query::__construct()
  • wp-includes/taxonomy.php: get_terms()
  • wp-includes/taxonomy.php: taxonomy_exists()
  • wp-includes/taxonomy.php: get_term()
  • wp-includes/nav-menu-template.php: walk_nav_menu_tree()
  • wp-includes/post-template.php: get_the_title()
  • wp-includes/post.php: post_type_exists()
  • wp-includes/post.php: get_post()
  • wp-includes/post.php: get_post_type()
  • wp-includes/post.php: get_post_type_object()
  • wp-includes/load.php: is_wp_error()
  • Show 9 more uses Hide more uses

Used By

  • wp-admin/includes/ajax-actions.php: wp_ajax_menu_quick_search()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文