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

feat: support send lists in ESP classes #1631

Merged
merged 9 commits into from
Sep 9, 2024
12 changes: 1 addition & 11 deletions includes/class-newspack-newsletters-ads.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,11 @@ public static function rest_api_init() {
[
'callback' => [ __CLASS__, 'get_ads_config' ],
'methods' => 'GET',
'permission_callback' => [ __CLASS__, 'permission_callback' ],
'permission_callback' => [ 'Newspack_Newsletters', 'api_authoring_permissions_check' ],
]
);
}

/**
* Check capabilities for using the API for authoring tasks.
*
* @param WP_REST_Request $request API request object.
* @return bool|WP_Error
*/
public static function permission_callback( $request ) {
return current_user_can( 'edit_posts' );
}

/**
* Register custom fields.
*/
Expand Down
2 changes: 2 additions & 0 deletions includes/class-newspack-newsletters-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ public static function enqueue_block_editor_assets() {
'byline_connector_label' => __( 'and ', 'newspack-newsletters' ),
],
'supported_social_icon_services' => Newspack_Newsletters_Renderer::get_supported_social_icons_services(),
'supported_esps' => Newspack_Newsletters::get_supported_providers(),
];

if ( self::is_editing_email() ) {
Expand Down Expand Up @@ -381,6 +382,7 @@ public static function enqueue_block_editor_assets() {
'is_service_provider_configured' => Newspack_Newsletters::is_service_provider_configured(),
'service_provider' => Newspack_Newsletters::service_provider(),
'user_test_emails' => self::get_current_user_test_emails(),
'labels' => $provider ? $provider::get_labels() : [],
]
);
wp_register_style(
Expand Down
4 changes: 2 additions & 2 deletions includes/class-newspack-newsletters-subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static function register_api_endpoints() {
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ __CLASS__, 'api_get_lists' ],
'permission_callback' => [ 'Newspack_Newsletters', 'api_permission_callback' ],
'permission_callback' => [ 'Newspack_Newsletters', 'api_administration_permissions_check' ],
]
);
register_rest_route(
Expand All @@ -82,7 +82,7 @@ public static function register_api_endpoints() {
[
'methods' => \WP_REST_Server::EDITABLE,
'callback' => [ __CLASS__, 'api_update_lists' ],
'permission_callback' => [ 'Newspack_Newsletters', 'api_permission_callback' ],
'permission_callback' => [ 'Newspack_Newsletters', 'api_administration_permissions_check' ],
'args' => [
'lists' => [
'type' => 'array',
Expand Down
79 changes: 70 additions & 9 deletions includes/class-newspack-newsletters.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ public static function register_meta() {
'type' => 'string',
'single' => true,
'auth_callback' => '__return_true',
'default' => '',
]
);
\register_meta(
Expand All @@ -285,6 +286,70 @@ public static function register_meta() {
'default' => -1,
]
);
\register_meta(
'post',
'send_list_id',
[
'object_subtype' => self::NEWSPACK_NEWSLETTERS_CPT,
'show_in_rest' => [
'schema' => [
'context' => [ 'edit' ],
],
],
'type' => 'string',
'single' => true,
'auth_callback' => '__return_true',
'default' => '',
]
);
\register_meta(
'post',
'send_sublist_id',
[
'object_subtype' => self::NEWSPACK_NEWSLETTERS_CPT,
'show_in_rest' => [
'schema' => [
'context' => [ 'edit' ],
],
],
'type' => 'string',
'single' => true,
'auth_callback' => '__return_true',
'default' => '',
]
);
\register_meta(
'post',
'senderName',
[
'object_subtype' => self::NEWSPACK_NEWSLETTERS_CPT,
'show_in_rest' => [
'schema' => [
'context' => [ 'edit' ],
],
],
'type' => 'string',
'single' => true,
'auth_callback' => '__return_true',
'default' => '',
]
);
\register_meta(
'post',
'senderEmail',
[
'object_subtype' => self::NEWSPACK_NEWSLETTERS_CPT,
'show_in_rest' => [
'schema' => [
'context' => [ 'edit' ],
],
],
'type' => 'string',
'single' => true,
'auth_callback' => '__return_true',
'default' => '',
]
);
\register_meta(
'post',
'newsletter_sent',
Expand Down Expand Up @@ -314,6 +379,7 @@ public static function register_meta() {
'type' => 'string',
'single' => true,
'auth_callback' => '__return_true',
'default' => '',
]
);
\register_meta(
Expand All @@ -329,6 +395,7 @@ public static function register_meta() {
'type' => 'string',
'single' => true,
'auth_callback' => '__return_true',
'default' => '',
]
);
\register_meta(
Expand All @@ -344,6 +411,7 @@ public static function register_meta() {
'type' => 'string',
'single' => true,
'auth_callback' => '__return_true',
'default' => '',
]
);
\register_meta(
Expand All @@ -359,6 +427,7 @@ public static function register_meta() {
'type' => 'string',
'single' => true,
'auth_callback' => '__return_true',
'default' => '',
]
);
\register_meta(
Expand All @@ -374,6 +443,7 @@ public static function register_meta() {
'type' => 'boolean',
'single' => true,
'auth_callback' => '__return_true',
'default' => false,
]
);
\register_meta(
Expand Down Expand Up @@ -835,15 +905,6 @@ public static function api_set_settings( $request ) {
return $wp_error->has_errors() ? $wp_error : self::api_get_settings();
}

/**
* Whether the current user can manage admin settings.
*
* @return bool Whether the current user can manage admin settings.
*/
public static function api_permission_callback() {
return current_user_can( 'manage_options' );
}

/**
* Retrieve settings.
*/
Expand Down
19 changes: 11 additions & 8 deletions includes/class-send-lists.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
namespace Newspack\Newsletters;

use Newspack_Newsletters;
use Newspack_Newsletters_Settings;
use Newspack_Newsletters_Subscription;
use WP_Error;
use WP_Post;

defined( 'ABSPATH' ) || exit;

Expand All @@ -34,7 +31,7 @@ public static function init() {
return;
}

add_action( 'rest_api_init', [ __CLASS__, 'register_api_endpoints' ] );
\add_action( 'rest_api_init', [ __CLASS__, 'register_api_endpoints' ] );
}

/**
Expand All @@ -55,13 +52,13 @@ public static function should_initialize_send_lists() {
* Register the endpoints needed to fetch send lists.
*/
public static function register_api_endpoints() {
register_rest_route(
\register_rest_route(
Newspack_Newsletters::API_NAMESPACE,
'/send-lists',
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ __CLASS__, 'api_get_send_lists' ],
'permission_callback' => [ 'Newspack_Newsletters', 'api_permission_callback' ],
'permission_callback' => [ 'Newspack_Newsletters', 'api_administration_permissions_check' ],
'args' => [
'ids' => [
'type' => [ 'array', 'string' ],
Expand Down Expand Up @@ -171,9 +168,15 @@ public static function api_get_send_lists( $request ) {
foreach ( $defaults as $key => $value ) {
$args[ $key ] = $request[ $key ] ?? $value;
}

$send_lists = $provider->get_send_lists( $args );
return \rest_ensure_response(
$provider->get_send_lists( $args )
\is_wp_error( $send_lists ) ? $send_lists :
array_map(
function( $send_list ) {
return $send_list->to_array();
},
$send_lists
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function register_routes() {
[
'methods' => \WP_REST_Server::READABLE,
'callback' => [ $this, 'api_retrieve' ],
'permission_callback' => [ $this->service_provider, 'api_authoring_permissions_check' ],
'permission_callback' => [ 'Newspack_Newsletters', 'api_authoring_permissions_check' ],
'args' => [
'id' => [
'sanitize_callback' => 'absint',
Expand Down
Loading