Advertisements

_wp_ajax_menu_quick_search

Definition:
function _wp_ajax_menu_quick_search( $request = array() {}

Prints the appropriate response to a menu quick search.

Parameters

  • array $request: The unsanitized request values.

Source code

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 ) {

					$post_obj = get_post( $object_id );

					echo 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 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] ) ) {

			query_posts(array(

				'posts_per_page' => 10,

				'post_type' => $matches[2],

				's' => $query,

			));

			if ( ! have_posts() )

				return;

			while ( have_posts() ) {

				the_post();

				if ( 'markup' == $response_format ) {

					echo walk_nav_menu_tree( array_map('wp_setup_nav_menu_item', array( get_post( get_the_ID() ) ) ), 0, (object) $args );

				} elseif ( 'json' == $response_format ) {

					echo json_encode(

						array(

							'ID' => get_the_ID(),

							'post_title' => get_the_title(),

							'post_type' => get_post_type(),

						)

					);

					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 json_encode(

						array(

							'ID' => $term->term_id,

							'post_title' => $term->name,

							'post_type' => $matches[2],

						)

					);

					echo "\n";

				}

			}

		}

	}

}

4385

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: