Skip to content
50 changes: 35 additions & 15 deletions admin/class-convertkit-admin-refresh-resources.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,45 @@ class ConvertKit_Admin_Refresh_Resources {
*/
public function __construct() {

add_action( 'wp_ajax_convertkit_admin_refresh_resources', array( $this, 'refresh_resources' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'rest_api_init', array( $this, 'register_routes' ) );

}

/**
* Register REST API routes.
*
* @since 3.1.0
*/
public function register_routes() {

// Register route to return all blocks registered by the Plugin.
register_rest_route(
'kit/v1',
'/resources/refresh/(?P<resource>[a-zA-Z0-9-_]+)',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'refresh_resources' ),
'permission_callback' => function () {
return current_user_can( 'edit_posts' );
},
)
);

}

/**
* Refreshes resources (forms, landing pages or tags) from the API, returning them as a JSON string.
*
* @since 1.9.8.0
*
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response|WP_Error Response object.
*/
public function refresh_resources() {

// Check nonce.
check_ajax_referer( 'convertkit_admin_refresh_resources', 'nonce' );
public function refresh_resources( $request ) {

// Get resource type.
$resource = ( isset( $_REQUEST['resource'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['resource'] ) ) : '' );
$resource = $request->get_param( 'resource' );

// Fetch resources.
switch ( $resource ) {
Expand Down Expand Up @@ -74,7 +96,7 @@ public function refresh_resources() {

// Bail if an error occured.
if ( is_wp_error( $results_tags ) ) {
wp_send_json_error( $results_tags->get_error_message() );
return rest_ensure_response( $results_tags );
}

// Fetch Products.
Expand All @@ -83,17 +105,16 @@ public function refresh_resources() {

// Bail if an error occured.
if ( is_wp_error( $results_products ) ) {
wp_send_json_error( $results_products->get_error_message() );
return rest_ensure_response( $results_products );
}

// Return resources.
wp_send_json_success(
return rest_ensure_response(
array(
'tags' => array_values( $results_tags ),
'products' => array_values( $results_products ),
)
);
// no break as wp_send_json_success terminates.

default:
$results = new WP_Error(
Expand All @@ -108,11 +129,11 @@ public function refresh_resources() {

// Bail if an error occured.
if ( is_wp_error( $results ) ) {
wp_send_json_error( $results->get_error_message() );
return rest_ensure_response( $results );
}

// Return resources as a zero based sequential array, so that JS retains the order of resources.
wp_send_json_success( array_values( $results ) );
return rest_ensure_response( array_values( $results ) );

}

Expand Down Expand Up @@ -144,10 +165,9 @@ public function enqueue_scripts( $hook ) {
'convertkit-admin-refresh-resources',
'convertkit_admin_refresh_resources',
array(
'action' => 'convertkit_admin_refresh_resources',
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'ajaxurl' => rest_url( 'kit/v1/resources/refresh/' ),
'debug' => $settings->debug_enabled(),
'nonce' => wp_create_nonce( 'convertkit_admin_refresh_resources' ),
'nonce' => wp_create_nonce( 'wp_rest' ),
)
);

Expand Down
1 change: 1 addition & 0 deletions codeception.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extensions:
- lucatume\WPBrowser\Command\RunOriginal
- lucatume\WPBrowser\Command\RunAll
- lucatume\WPBrowser\Command\GenerateWPUnit
- lucatume\WPBrowser\Command\GenerateWPRestApi
- lucatume\WPBrowser\Command\DbExport
- lucatume\WPBrowser\Command\DbImport
- lucatume\WPBrowser\Command\MonkeyCachePath
Expand Down
8 changes: 4 additions & 4 deletions includes/blocks/class-convertkit-block-broadcasts.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ public function get_supports() {
*/
public function get_fields() {

// Bail if the request is not for the WordPress Administration or frontend editor.
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
// Bail if the request is not for the WordPress Administration, frontend editor or REST API request.
if ( ! $this->is_admin_frontend_editor_or_rest_request() ) {
return false;
}

Expand Down Expand Up @@ -380,8 +380,8 @@ public function get_fields() {
*/
public function get_panels() {

// Bail if the request is not for the WordPress Administration or frontend editor.
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
// Bail if the request is not for the WordPress Administration, frontend editor or REST API request.
if ( ! $this->is_admin_frontend_editor_or_rest_request() ) {
return false;
}

Expand Down
8 changes: 4 additions & 4 deletions includes/blocks/class-convertkit-block-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ public function get_attributes() {
*/
public function get_fields() {

// Bail if the request is not for the WordPress Administration or frontend editor.
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
// Bail if the request is not for the WordPress Administration, frontend editor or REST API request.
if ( ! $this->is_admin_frontend_editor_or_rest_request() ) {
return false;
}

Expand Down Expand Up @@ -137,8 +137,8 @@ public function get_fields() {
*/
public function get_panels() {

// Bail if the request is not for the WordPress Administration or frontend editor.
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
// Bail if the request is not for the WordPress Administration, frontend editor or REST API request.
if ( ! $this->is_admin_frontend_editor_or_rest_request() ) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ public function get_attributes() {
*/
public function get_fields() {

// Bail if the request is not for the WordPress Administration or frontend editor.
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
// Bail if the request is not for the WordPress Administration, frontend editor or REST API request.
if ( ! $this->is_admin_frontend_editor_or_rest_request() ) {
return false;
}

Expand Down Expand Up @@ -150,8 +150,8 @@ public function get_fields() {
*/
public function get_panels() {

// Bail if the request is not for the WordPress Administration or frontend editor.
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
// Bail if the request is not for the WordPress Administration, frontend editor or REST API request.
if ( ! $this->is_admin_frontend_editor_or_rest_request() ) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ public function get_attributes() {
*/
public function get_fields() {

// Bail if the request is not for the WordPress Administration or frontend editor.
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
// Bail if the request is not for the WordPress Administration, frontend editor or REST API request.
if ( ! $this->is_admin_frontend_editor_or_rest_request() ) {
return false;
}

Expand Down
8 changes: 4 additions & 4 deletions includes/blocks/class-convertkit-block-form-builder-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ public function get_supports() {
*/
public function get_fields() {

// Bail if the request is not for the WordPress Administration or frontend editor.
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
// Bail if the request is not for the WordPress Administration, frontend editor or REST API request.
if ( ! $this->is_admin_frontend_editor_or_rest_request() ) {
return false;
}

Expand Down Expand Up @@ -223,8 +223,8 @@ public function get_fields() {
*/
public function get_panels() {

// Bail if the request is not for the WordPress Administration or frontend editor.
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
// Bail if the request is not for the WordPress Administration, frontend editor or REST API request.
if ( ! $this->is_admin_frontend_editor_or_rest_request() ) {
return false;
}

Expand Down
8 changes: 4 additions & 4 deletions includes/blocks/class-convertkit-block-form-builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,8 @@ public function get_supports() {
*/
public function get_fields() {

// Bail if the request is not for the WordPress Administration or frontend editor.
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
// Bail if the request is not for the WordPress Administration, frontend editor or REST API request.
if ( ! $this->is_admin_frontend_editor_or_rest_request() ) {
return false;
}

Expand Down Expand Up @@ -568,8 +568,8 @@ public function get_fields() {
*/
public function get_panels() {

// Bail if the request is not for the WordPress Administration or frontend editor.
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
// Bail if the request is not for the WordPress Administration, frontend editor or REST API request.
if ( ! $this->is_admin_frontend_editor_or_rest_request() ) {
return false;
}

Expand Down
8 changes: 4 additions & 4 deletions includes/blocks/class-convertkit-block-form-trigger.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ public function get_supports() {
*/
public function get_fields() {

// Bail if the request is not for the WordPress Administration or frontend editor.
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
// Bail if the request is not for the WordPress Administration, frontend editor or REST API request.
if ( ! $this->is_admin_frontend_editor_or_rest_request() ) {
return false;
}

Expand Down Expand Up @@ -270,8 +270,8 @@ public function get_fields() {
*/
public function get_panels() {

// Bail if the request is not for the WordPress Administration or frontend editor.
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
// Bail if the request is not for the WordPress Administration, frontend editor or REST API request.
if ( ! $this->is_admin_frontend_editor_or_rest_request() ) {
return false;
}

Expand Down
8 changes: 4 additions & 4 deletions includes/blocks/class-convertkit-block-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ public function get_supports() {
*/
public function get_fields() {

// Bail if the request is not for the WordPress Administration or frontend editor.
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
// Bail if the request is not for the WordPress Administration, frontend editor or REST API request.
if ( ! $this->is_admin_frontend_editor_or_rest_request() ) {
return false;
}

Expand Down Expand Up @@ -285,8 +285,8 @@ public function get_fields() {
*/
public function get_panels() {

// Bail if the request is not for the WordPress Administration or frontend editor.
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
// Bail if the request is not for the WordPress Administration, frontend editor or REST API request.
if ( ! $this->is_admin_frontend_editor_or_rest_request() ) {
return false;
}

Expand Down
8 changes: 4 additions & 4 deletions includes/blocks/class-convertkit-block-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ public function get_supports() {
*/
public function get_fields() {

// Bail if the request is not for the WordPress Administration or frontend editor.
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
// Bail if the request is not for the WordPress Administration, frontend editor or REST API request.
if ( ! $this->is_admin_frontend_editor_or_rest_request() ) {
return false;
}

Expand Down Expand Up @@ -312,8 +312,8 @@ public function get_fields() {
*/
public function get_panels() {

// Bail if the request is not for the WordPress Administration or frontend editor.
if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
// Bail if the request is not for the WordPress Administration, frontend editor or REST API request.
if ( ! $this->is_admin_frontend_editor_or_rest_request() ) {
return false;
}

Expand Down
31 changes: 27 additions & 4 deletions includes/blocks/class-convertkit-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,32 @@ public function get_atts_as_html_data_attributes( $atts ) {

}

/**
* Determines if the request is a WordPress REST API request.
*
* @since 3.1.0
*
* @return bool
*/
public function is_rest_request() {

return defined( 'REST_REQUEST' ) && REST_REQUEST;

}

/**
* Determines if the request is for the WordPress Administration, frontend editor or REST API request.
*
* @since 3.1.0
*
* @return bool
*/
public function is_admin_frontend_editor_or_rest_request() {

return WP_ConvertKit()->is_admin_or_frontend_editor() || $this->is_rest_request();

}

/**
* Determines if the request for the block is from the block editor or the frontend site.
*
Expand All @@ -405,10 +431,7 @@ public function get_atts_as_html_data_attributes( $atts ) {
public function is_block_editor_request() {

// Return false if not a WordPress REST API request, which Gutenberg uses.
if ( ! defined( 'REST_REQUEST' ) ) {
return false;
}
if ( REST_REQUEST !== true ) {
if ( ! $this->is_rest_request() ) {
return false;
}

Expand Down
31 changes: 0 additions & 31 deletions includes/class-convertkit-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class ConvertKit_AJAX {
*/
public function __construct() {

add_action( 'wp_ajax_convertkit_get_blocks', array( $this, 'get_blocks' ) );

add_action( 'wp_ajax_nopriv_convertkit_store_subscriber_id_in_cookie', array( $this, 'store_subscriber_id_in_cookie' ) );
add_action( 'wp_ajax_convertkit_store_subscriber_id_in_cookie', array( $this, 'store_subscriber_id_in_cookie' ) );

Expand All @@ -36,35 +34,6 @@ public function __construct() {

}

/**
* Returns all ConvertKit registered blocks.
*
* Typically used when a refresh button in a block has been pressed when
* displayNoticeWithLink() is called, because either
* no Access Token is specified, or no resources exist in ConvertKit.
*
* @since 2.2.6
*/
public function get_blocks() {

// Check nonce.
check_ajax_referer( 'convertkit_get_blocks', 'nonce' );

// Refresh resources from the API, to reflect any changes.
$forms = new ConvertKit_Resource_Forms( 'block_edit' );
$forms->refresh();

$posts = new ConvertKit_Resource_Posts( 'block_edit' );
$posts->refresh();

$products = new ConvertKit_Resource_Products( 'block_edit' );
$products->refresh();

// Return blocks.
wp_send_json_success( convertkit_get_blocks() );

}

/**
* Stores the ConvertKit Subscriber's ID in a cookie.
*
Expand Down
Loading