WP_Customize_Nav_Menus::ajax_load_available_items()publicWP 4.3.0

Ajax handler for loading available menu items.

Method of the class: WP_Customize_Nav_Menus{}

No Hooks.

Return

null. Nothing (null).

Usage

$WP_Customize_Nav_Menus = new WP_Customize_Nav_Menus();
$WP_Customize_Nav_Menus->ajax_load_available_items();

Changelog

Since 4.3.0 Introduced.

WP_Customize_Nav_Menus::ajax_load_available_items() code WP 6.6.2

public function ajax_load_available_items() {
	check_ajax_referer( 'customize-menus', 'customize-menus-nonce' );

	if ( ! current_user_can( 'edit_theme_options' ) ) {
		wp_die( -1 );
	}

	$all_items  = array();
	$item_types = array();
	if ( isset( $_POST['item_types'] ) && is_array( $_POST['item_types'] ) ) {
		$item_types = wp_unslash( $_POST['item_types'] );
	} elseif ( isset( $_POST['type'] ) && isset( $_POST['object'] ) ) { // Back compat.
		$item_types[] = array(
			'type'   => wp_unslash( $_POST['type'] ),
			'object' => wp_unslash( $_POST['object'] ),
			'page'   => empty( $_POST['page'] ) ? 0 : absint( $_POST['page'] ),
		);
	} else {
		wp_send_json_error( 'nav_menus_missing_type_or_object_parameter' );
	}

	foreach ( $item_types as $item_type ) {
		if ( empty( $item_type['type'] ) || empty( $item_type['object'] ) ) {
			wp_send_json_error( 'nav_menus_missing_type_or_object_parameter' );
		}
		$type   = sanitize_key( $item_type['type'] );
		$object = sanitize_key( $item_type['object'] );
		$page   = empty( $item_type['page'] ) ? 0 : absint( $item_type['page'] );
		$items  = $this->load_available_items_query( $type, $object, $page );
		if ( is_wp_error( $items ) ) {
			wp_send_json_error( $items->get_error_code() );
		}
		$all_items[ $item_type['type'] . ':' . $item_type['object'] ] = $items;
	}

	wp_send_json_success( array( 'items' => $all_items ) );
}