Automattic\WooCommerce\Admin\API

MarketingCampaigns::get_items()publicWC 1.0

Returns an aggregated array of marketing campaigns for all active marketing channels.

Метод класса: MarketingCampaigns{}

Хуков нет.

Возвращает

WP_Error|WP_REST_Response.

Использование

$MarketingCampaigns = new MarketingCampaigns();
$MarketingCampaigns->get_items( $request );
$request(WP_REST_Request) (обязательный)
Request data.

Код MarketingCampaigns::get_items() WC 9.2.3

public function get_items( $request ) {
	/**
	 * MarketingChannels class.
	 *
	 * @var MarketingChannelsService $marketing_channels_service
	 */
	$marketing_channels_service = wc_get_container()->get( MarketingChannelsService::class );

	// Aggregate the campaigns from all registered marketing channels.
	$responses = array();
	foreach ( $marketing_channels_service->get_registered_channels() as $channel ) {
		foreach ( $channel->get_campaigns() as $campaign ) {
			$response    = $this->prepare_item_for_response( $campaign, $request );
			$responses[] = $this->prepare_response_for_collection( $response );
		}
	}

	// Pagination.
	$page              = $request['page'];
	$items_per_page    = $request['per_page'];
	$offset            = ( $page - 1 ) * $items_per_page;
	$paginated_results = array_slice( $responses, $offset, $items_per_page );

	$response = rest_ensure_response( $paginated_results );

	$total_campaigns = count( $responses );
	$max_pages       = ceil( $total_campaigns / $items_per_page );
	$response->header( 'X-WP-Total', $total_campaigns );
	$response->header( 'X-WP-TotalPages', (int) $max_pages );

	// Add previous and next page links to response header.
	$request_params = $request->get_query_params();
	$base           = add_query_arg( urlencode_deep( $request_params ), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) );
	if ( $page > 1 ) {
		$prev_page = $page - 1;
		if ( $prev_page > $max_pages ) {
			$prev_page = $max_pages;
		}
		$prev_link = add_query_arg( 'page', $prev_page, $base );
		$response->link_header( 'prev', $prev_link );
	}
	if ( $max_pages > $page ) {
		$next_page = $page + 1;
		$next_link = add_query_arg( 'page', $next_page, $base );
		$response->link_header( 'next', $next_link );
	}

	return $response;
}