Skip to content

Commit

Permalink
Multichannel marketing backend and API (#36453)
Browse files Browse the repository at this point in the history
* Multichannel Marketing - Core Library (#35099)

* Create channel interface and campaign value class

* Create MarketingChannels class

* Register MarketingChannels class in DI container

* Use the new MarketingChannels class to get the installed marketing extensions' data

* Use DI container to access InstalledExtensions class

* Add InstalledExtensions to the $provides array

* Hint that campaign cost should also indicate the currency

* Initialize the channels array

* Add unit tests for MarketingCampaign

* Add unit tests for MarketingChannels

* Add Price class to represent a price with currency

* Use Price class for marketing campaign's cost

* Define a constant to indicate the MCM classes exist

This constant will be checked by third-party extensions before utilizing any of the classes/interfaces defined for this feature.

* Create MarketingSpecs class to include WC.com API calls

* Remove WC.com API calls from Marketing class

And replace them with calls from MarketingSpecs class.

* Use the const from MarketingSpecs

* Fix MarketingChannels unit tests

* Add missing settings URL to the channel data

Co-authored-by: Nima <nima.karimi@automattic.com>

* Multichannel Marketing - Changes to the marketing classes (#36012)

* Rename `get_errors_no` to `get_errors_count`

* Remove the validation for marketing channel slugs

Do not check if the marketing channel's slug exists in the list returned by WooCommerce.com Recommendation API. This allows any third-party extension to register as a marketing channel.

* Revert InstalledExtensions

The InstalledExtensions class will be used by the previous generation of the Marketing dashboard (if the user has not enabled the new "Marketing" feature); therefore, it's best to restore it to the original code.

* Fix code style

* Translate Exception message

* Remove doc references to a predetermined list of marketing channels

Co-authored-by: Nima <nima.karimi@automattic.com>

* Multichannel Marketing - API (#36222)

* Rename `get_errors_no` to `get_errors_count`

* Remove the validation for marketing channel slugs

Do not check if the marketing channel's slug exists in the list returned by WooCommerce.com Recommendation API. This essentially allows any third-party extension to register as a marketing channel.

* Revert InstalledExtensions

The InstalledExtensions class will be used by the previous generation of Marketing dashboard (if the user has not enabled the new "Marketing" feature); therefore, it's best to restore it to the original code.

* Fix code style

* Add channel property to MarketingCampaign

* Add methods to filter the recommended marketing channels and extensions

* Add `marketing/recommendations` API

* Add unit tests for `marketing/recommendations` API

* Add `marketing/channels` API

* Add unit tests for `marketing/channels` API

* Add `marketing/campaigns` API

* Add unit tests for `marketing/campaigns` API

* Translate Exception message

* Remove doc references to predetermined list of marketing channels

* Add `unregister_all` method

To allow unregistering all marketing channels.

* Unregister all channels on test tear down

* Change API access denied authorization code

* Change API access permission

* Add MarketingCampaignType class

This allows defining campaign types for each marketing channel.

* Add campaign type property to campaign class

* Add `marketing/campaign-types` API

This API returns the aggregated list of supported marketing campaign types for all registered marketing channels.

* Add unit tests for `marketing/campaign-types` API

* Remove unused jsonSerialize method

* Fix unit tests

Co-authored-by: Nima <nima.karimi@automattic.com>

* Add changelog

* Add product listing status sync failed

Co-authored-by: Nima <nima.karimi@automattic.com>
  • Loading branch information
nima-karimi and Nima committed Jan 20, 2023
1 parent bc6bc8f commit da6b491
Show file tree
Hide file tree
Showing 24 changed files with 2,233 additions and 138 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Add multichannel marketing API
4 changes: 2 additions & 2 deletions plugins/woocommerce/includes/wc-update-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
defined( 'ABSPATH' ) || exit;

use Automattic\WooCommerce\Database\Migrations\MigrationHelper;
use Automattic\WooCommerce\Internal\Admin\Marketing;
use Automattic\WooCommerce\Internal\Admin\Marketing\MarketingSpecs;
use Automattic\WooCommerce\Internal\AssignDefaultCategory;
use Automattic\WooCommerce\Internal\ProductAttributesLookup\DataRegenerator;
use Automattic\WooCommerce\Internal\ProductAttributesLookup\LookupDataStore;
Expand Down Expand Up @@ -2470,7 +2470,7 @@ function wc_update_700_remove_download_log_fk() {
* Remove the transient data for recommended marketing extensions.
*/
function wc_update_700_remove_recommended_marketing_plugins_transient() {
delete_transient( Marketing::RECOMMENDED_PLUGINS_TRANSIENT );
delete_transient( MarketingSpecs::RECOMMENDED_PLUGINS_TRANSIENT );
}

/**
Expand Down
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
20 changes: 17 additions & 3 deletions plugins/woocommerce/src/Admin/API/Marketing.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

namespace Automattic\WooCommerce\Admin\API;

use Automattic\WooCommerce\Internal\Admin\Marketing as MarketingFeature;
use Automattic\WooCommerce\Admin\PluginsHelper;
use Automattic\WooCommerce\Internal\Admin\Marketing\MarketingSpecs;

defined( 'ABSPATH' ) || exit;

Expand Down Expand Up @@ -103,9 +103,16 @@ public function get_recommended_plugins_permissions_check( $request ) {
* @return \WP_Error|\WP_REST_Response
*/
public function get_recommended_plugins( $request ) {
/**
* MarketingSpecs class.
*
* @var MarketingSpecs $marketing_specs
*/
$marketing_specs = wc_get_container()->get( MarketingSpecs::class );

// Default to marketing category (if no category set).
$category = ( ! empty( $request->get_param( 'category' ) ) ) ? $request->get_param( 'category' ) : 'marketing';
$all_plugins = MarketingFeature::get_instance()->get_recommended_plugins();
$all_plugins = $marketing_specs->get_recommended_plugins();
$valid_plugins = [];
$per_page = $request->get_param( 'per_page' );

Expand All @@ -130,7 +137,14 @@ public function get_recommended_plugins( $request ) {
* @return \WP_Error|\WP_REST_Response
*/
public function get_knowledge_base_posts( $request ) {
/**
* MarketingSpecs class.
*
* @var MarketingSpecs $marketing_specs
*/
$marketing_specs = wc_get_container()->get( MarketingSpecs::class );

$category = $request->get_param( 'category' );
return rest_ensure_response( MarketingFeature::get_instance()->get_knowledge_base_posts( $category ) );
return rest_ensure_response( $marketing_specs->get_knowledge_base_posts( $category ) );
}
}
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 );
}


}

0 comments on commit da6b491

Please sign in to comment.