函数文档

wp_ajax_search_install_plugins()

💡 云策文档标注

概述

wp_ajax_search_install_plugins() 是一个 WordPress AJAX 处理函数,用于搜索并安装插件。它通过 AJAX 请求处理插件搜索,验证权限并返回 JSON 格式的搜索结果。

关键要点

  • 函数用于处理 AJAX 搜索插件的请求,核心是调用 WP_Plugin_Install_List_Table 类来显示插件列表。
  • 包含安全检查:使用 check_ajax_referer() 验证 AJAX 请求,并通过 ajax_user_can() 检查用户权限。
  • 设置当前屏幕和请求 URI,以确保分页功能正常工作。
  • 通过 wp_send_json_success() 或 wp_send_json_error() 返回 JSON 响应,包含插件列表和总数。

代码示例

function wp_ajax_search_install_plugins() {
    check_ajax_referer( 'updates' );

    $pagenow = isset( $_POST['pagenow'] ) ? sanitize_key( $_POST['pagenow'] ) : '';
    if ( 'plugin-install-network' === $pagenow || 'plugin-install' === $pagenow ) {
        set_current_screen( $pagenow );
    }

    /** @var WP_Plugin_Install_List_Table $wp_list_table */
    $wp_list_table = _get_list_table(
        'WP_Plugin_Install_List_Table',
        array(
            'screen' => get_current_screen(),
        )
    );

    $status = array();

    if ( ! $wp_list_table->ajax_user_can() ) {
        $status['errorMessage'] = __( 'Sorry, you are not allowed to manage plugins for this site.' );
        wp_send_json_error( $status );
    }

    // Set the correct requester, so pagination works.
    $_SERVER['REQUEST_URI'] = add_query_arg(
        array_diff_key(
            $_POST,
            array(
                '_ajax_nonce' => null,
                'action'      => null,
            )
        ),
        network_admin_url( 'plugin-install.php', 'relative' )
    );

    $wp_list_table->prepare_items();

    ob_start();
    $wp_list_table->display();
    $status['count'] = (int) $wp_list_table->get_pagination_arg( 'total_items' );
    $status['items'] = ob_get_clean();

    wp_send_json_success( $status );
}

注意事项

  • 函数依赖于 WP_Plugin_Install_List_Table 类,确保相关文件已加载。
  • AJAX 请求必须包含正确的 nonce 和参数,否则可能失败。
  • 用户权限检查是关键,无权限时会返回错误消息。
  • 分页设置通过修改 REQUEST_URI 实现,需注意 POST 数据处理。

📄 原文内容

Handles searching plugins to install via AJAX.

Source

function wp_ajax_search_install_plugins() {
	check_ajax_referer( 'updates' );

	$pagenow = isset( $_POST['pagenow'] ) ? sanitize_key( $_POST['pagenow'] ) : '';
	if ( 'plugin-install-network' === $pagenow || 'plugin-install' === $pagenow ) {
		set_current_screen( $pagenow );
	}

	/** @var WP_Plugin_Install_List_Table $wp_list_table */
	$wp_list_table = _get_list_table(
		'WP_Plugin_Install_List_Table',
		array(
			'screen' => get_current_screen(),
		)
	);

	$status = array();

	if ( ! $wp_list_table->ajax_user_can() ) {
		$status['errorMessage'] = __( 'Sorry, you are not allowed to manage plugins for this site.' );
		wp_send_json_error( $status );
	}

	// Set the correct requester, so pagination works.
	$_SERVER['REQUEST_URI'] = add_query_arg(
		array_diff_key(
			$_POST,
			array(
				'_ajax_nonce' => null,
				'action'      => null,
			)
		),
		network_admin_url( 'plugin-install.php', 'relative' )
	);

	$wp_list_table->prepare_items();

	ob_start();
	$wp_list_table->display();
	$status['count'] = (int) $wp_list_table->get_pagination_arg( 'total_items' );
	$status['items'] = ob_get_clean();

	wp_send_json_success( $status );
}

Changelog

Version Description
4.6.0 Introduced.