Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multichannel Marketing - API #36222

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
8d8f416
Rename `get_errors_no` to `get_errors_count`
Dec 14, 2022
7734d41
Remove the validation for marketing channel slugs
Dec 14, 2022
91e0a0c
Revert InstalledExtensions
Dec 14, 2022
08c5404
Merge branch 'feature/34548-multichannel-marketing-backend' into feat…
Dec 15, 2022
53dac1d
Fix code style
Dec 15, 2022
6415f3f
Add channel property to MarketingCampaign
Dec 28, 2022
7dcdbd8
Add methods to filter the recommended marketing channels and extensions
Dec 28, 2022
677416f
Add `marketing/recommendations` API
Dec 28, 2022
086ce8f
Add unit tests for `marketing/recommendations` API
Dec 28, 2022
a1468ec
Add `marketing/channels` API
Dec 28, 2022
200156d
Add unit tests for `marketing/channels` API
Dec 28, 2022
817ca2a
Add `marketing/campaigns` API
Dec 28, 2022
e82feb2
Add unit tests for `marketing/campaigns` API
Dec 28, 2022
4404263
Translate Exception message
Dec 28, 2022
f7be32d
Remove doc references to predetermined list of marketing channels
Dec 28, 2022
4e192e7
Merge branch 'feature/34548-multichannel-marketing-backend' into feat…
Dec 28, 2022
4ef2d57
Merge branch 'feature/35956-mcm-library-changes' into feature/34556-m…
Dec 28, 2022
5913a4f
Merge branch 'feature/34548-multichannel-marketing-backend' into feat…
Dec 28, 2022
130b2a9
Add `unregister_all` method
Dec 28, 2022
4d6ef8e
Unregister all channels on test tear down
Dec 28, 2022
264c92a
Change API access denied authorization code
Jan 3, 2023
20efcfb
Change API access permission
Jan 3, 2023
1687416
Add MarketingCampaignType class
Jan 3, 2023
8ef6532
Add campaign type property to campaign class
Jan 3, 2023
1b668d9
Add `marketing/campaign-types` API
Jan 3, 2023
02cd258
Add unit tests for `marketing/campaign-types` API
Jan 3, 2023
7200174
Remove unused jsonSerialize method
Jan 13, 2023
c6ba33e
Fix unit tests
Jan 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions plugins/woocommerce/src/Admin/API/Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public function rest_api_init() {
'Automattic\WooCommerce\Admin\API\Experiments',
'Automattic\WooCommerce\Admin\API\Marketing',
'Automattic\WooCommerce\Admin\API\MarketingOverview',
'Automattic\WooCommerce\Admin\API\MarketingRecommendations',
'Automattic\WooCommerce\Admin\API\MarketingChannels',
'Automattic\WooCommerce\Admin\API\MarketingCampaigns',
'Automattic\WooCommerce\Admin\API\MarketingCampaignTypes',
'Automattic\WooCommerce\Admin\API\Options',
'Automattic\WooCommerce\Admin\API\Orders',
'Automattic\WooCommerce\Admin\API\PaymentGatewaySuggestions',
Expand Down
211 changes: 211 additions & 0 deletions plugins/woocommerce/src/Admin/API/MarketingCampaignTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
<?php
/**
* REST API MarketingCampaignTypes Controller
*
* Handles requests to /marketing/campaign-types.
*/

namespace Automattic\WooCommerce\Admin\API;

use Automattic\WooCommerce\Admin\Marketing\MarketingCampaignType;
use Automattic\WooCommerce\Admin\Marketing\MarketingChannels as MarketingChannelsService;
use WC_REST_Controller;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;

defined( 'ABSPATH' ) || exit;

/**
* MarketingCampaignTypes Controller.
*
* @internal
* @extends WC_REST_Controller
* @since x.x.x
*/
class MarketingCampaignTypes extends WC_REST_Controller {

/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';

/**
* Route base.
*
* @var string
*/
protected $rest_base = 'marketing/campaign-types';

/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}

/**
* Retrieves the query params for the collections.
*
* @return array Query parameters for the collection.
*/
public function get_collection_params() {
$params = parent::get_collection_params();
unset( $params['search'] );

return $params;
}

/**
* Check whether a given request has permission to view marketing campaigns.
*
* @param WP_REST_Request $request Full details about the request.
*
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}

return true;
}

/**
* Returns an aggregated array of marketing campaigns for all active marketing channels.
*
* @param WP_REST_Request $request Request data.
*
* @return WP_Error|WP_REST_Response
*/
public function get_items( $request ) {
/**
* MarketingChannels class.
*
* @var MarketingChannelsService $marketing_channels_service
*/
$marketing_channels_service = wc_get_container()->get( MarketingChannelsService::class );

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

return rest_ensure_response( $responses );
}

/**
* Prepares the item for the REST response.
*
* @param MarketingCampaignType $item WordPress representation of the item.
* @param WP_REST_Request $request Request object.
*
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function prepare_item_for_response( $item, $request ) {
$data = [
'id' => $item->get_id(),
'name' => $item->get_name(),
'description' => $item->get_description(),
'channel' => [
'slug' => $item->get_channel()->get_slug(),
'name' => $item->get_channel()->get_name(),
],
'create_url' => $item->get_create_url(),
'icon_url' => $item->get_icon_url(),
];

$context = $request['context'] ?? 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );

return rest_ensure_response( $data );
}

/**
* Retrieves the item's schema, conforming to JSON Schema.
*
* @return array Item schema data.
*/
public function get_item_schema() {
$schema = [
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'marketing_campaign_type',
'type' => 'object',
'properties' => [
'id' => [
'description' => __( 'The unique identifier for the marketing campaign type.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'name' => [
'description' => __( 'Name of the marketing campaign type.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'description' => [
'description' => __( 'Description of the marketing campaign type.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'channel' => [
'description' => __( 'The marketing channel that this campaign type belongs to.', 'woocommerce' ),
'type' => 'object',
'context' => [ 'view' ],
'readonly' => true,
'properties' => [
'slug' => [
'description' => __( 'The unique identifier of the marketing channel that this campaign type belongs to.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'name' => [
'description' => __( 'The name of the marketing channel that this campaign type belongs to.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
],
],
'create_url' => [
'description' => __( 'URL to the create campaign page for this campaign type.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
'icon_url' => [
'description' => __( 'URL to an image/icon for the campaign type.', 'woocommerce' ),
'type' => 'string',
'context' => [ 'view' ],
'readonly' => true,
],
],
];

return $this->add_additional_fields_schema( $schema );
}


}