From e367bcfc07dd492c4d22f29d1b215993b47f80e9 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 13 Feb 2026 10:36:45 +0800 Subject: [PATCH 1/4] Broadcasts: REST API: Improvements --- .../blocks/class-convertkit-block-broadcasts.php | 6 +++--- resources/frontend/js/broadcasts.js | 12 +++++------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/includes/blocks/class-convertkit-block-broadcasts.php b/includes/blocks/class-convertkit-block-broadcasts.php index ac7abb3fd..82382888c 100644 --- a/includes/blocks/class-convertkit-block-broadcasts.php +++ b/includes/blocks/class-convertkit-block-broadcasts.php @@ -45,9 +45,9 @@ public function register_routes() { register_rest_route( 'kit/v1', - '/broadcasts/render', + '/broadcasts', array( - 'methods' => WP_REST_Server::CREATABLE, + 'methods' => WP_REST_Server::READABLE, 'args' => array( 'date_format' => array( 'default' => $this->get_default_value( 'date_format' ), @@ -122,7 +122,7 @@ public function enqueue_scripts() { 'convertkit_broadcasts', array( // REST API URL endpoint. - 'ajax_url' => rest_url( 'kit/v1/broadcasts/render' ), + 'ajax_url' => rest_url( 'kit/v1/broadcasts' ), // Whether debugging is enabled. 'debug' => $settings->debug_enabled(), diff --git a/resources/frontend/js/broadcasts.js b/resources/frontend/js/broadcasts.js index 36c327ccb..b7dcc39e0 100644 --- a/resources/frontend/js/broadcasts.js +++ b/resources/frontend/js/broadcasts.js @@ -58,14 +58,12 @@ function convertKitBroadcastsRender(blockContainer, atts) { // Show loading indicator. blockContainer.classList.add('convertkit-broadcasts-loading'); + // Build URL with query string parameters. + const params = new URLSearchParams(atts); + const url = `${convertkit_broadcasts.ajax_url}?${params.toString()}`; + // Fetch HTML. - fetch(convertkit_broadcasts.ajax_url, { - method: 'POST', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - }, - body: new URLSearchParams(atts), - }) + fetch(url) .then(function (response) { if (convertkit_broadcasts.debug) { console.log(response); From 4e0f6613343451f7cd00cd0b0d2b1a8ac3fcf50c Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 13 Feb 2026 11:00:07 +0800 Subject: [PATCH 2/4] Return a blank string if no Broadcasts --- .../blocks/class-convertkit-block-broadcasts.php | 15 +++++++++++++-- resources/frontend/js/broadcasts.js | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/includes/blocks/class-convertkit-block-broadcasts.php b/includes/blocks/class-convertkit-block-broadcasts.php index 82382888c..e416ed1b3 100644 --- a/includes/blocks/class-convertkit-block-broadcasts.php +++ b/includes/blocks/class-convertkit-block-broadcasts.php @@ -95,8 +95,7 @@ public function register_routes() { ), ), 'callback' => function ( $request ) { - $html = $this->render_ajax( $request ); - return rest_ensure_response( array( 'data' => $html ) ); + return rest_ensure_response( $this->render_ajax( $request ) ); }, // No authentication required, as this is on the frontend site. @@ -629,9 +628,21 @@ public function render_ajax( $request ) { // and moving some attributes (such as Gutenberg's styles), if defined. $atts = $this->sanitize_and_declare_atts( $atts ); + // Setup Settings class. + $settings = new ConvertKit_Settings(); + // Fetch Posts. $posts = new ConvertKit_Resource_Posts( 'output_broadcasts' ); + // If no Posts exist, bail. + if ( ! $posts->exist() ) { + if ( $settings->debug_enabled() ) { + return ''; + } + + return ''; + } + // Build HTML. $html = $this->build_html( $posts, $atts, false ); diff --git a/resources/frontend/js/broadcasts.js b/resources/frontend/js/broadcasts.js index b7dcc39e0..6783e817b 100644 --- a/resources/frontend/js/broadcasts.js +++ b/resources/frontend/js/broadcasts.js @@ -80,7 +80,7 @@ function convertKitBroadcastsRender(blockContainer, atts) { blockContainer.classList.remove('convertkit-broadcasts-loading'); // Replace block container's HTML with response data. - blockContainer.innerHTML = result.data; + blockContainer.innerHTML = result; }) .catch(function (error) { if (convertkit.debug) { From 7a22765d7ff64167c87f1a945360292556650afd Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 13 Feb 2026 11:00:10 +0800 Subject: [PATCH 3/4] Added tests --- tests/Integration/RESTAPIBroadcastsTest.php | 106 ++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 tests/Integration/RESTAPIBroadcastsTest.php diff --git a/tests/Integration/RESTAPIBroadcastsTest.php b/tests/Integration/RESTAPIBroadcastsTest.php new file mode 100644 index 000000000..5907ec126 --- /dev/null +++ b/tests/Integration/RESTAPIBroadcastsTest.php @@ -0,0 +1,106 @@ +settings = new \ConvertKit_Settings(); + $this->settings->save( + array( + 'access_token' => $_ENV['CONVERTKIT_OAUTH_ACCESS_TOKEN'], + 'refresh_token' => $_ENV['CONVERTKIT_OAUTH_REFRESH_TOKEN'], + 'token_expires' => ( time() + 10000 ), + ) + ); + + // Tell WordPress that we're making REST API requests. + // This constant isn't set by the WP_REST_Server class in tests. + if ( ! defined( 'REST_REQUEST' ) ) { + define( 'REST_REQUEST', true ); + } + } + + /** + * Performs actions after each test. + * + * @since 3.1.9 + */ + public function tearDown(): void + { + // Delete Credentials from Plugin's settings. + $this->settings->delete_credentials(); + parent::tearDown(); + } + + /** + * Test that the /wp-json/kit/v1/broadcasts REST API route returns a 200 + * with no data when no broadcasts exist. + * + * @since 3.1.9 + */ + public function testWhenNoBroadcastsExist() + { + $request = new \WP_REST_Request( 'GET', '/kit/v1/broadcasts' ); + $response = rest_get_server()->dispatch( $request ); + + // Assert response is successful. + $this->assertSame( 200, $response->get_status() ); + $this->assertEquals( '', $response->get_data()['data'] ); + } + + /** + * Test that the /wp-json/kit/v1/broadcasts REST API route returns a 200 + * with data when broadcasts exist. + * + * @since 3.1.9 + */ + public function testWhenBroadcastsExist() + { + // Refresh resources. + new \ConvertKit_Resource_Posts( 'output_broadcasts' )->refresh(); + + // Send request. + $request = new \WP_REST_Request( 'GET', '/kit/v1/broadcasts' ); + $response = rest_get_server()->dispatch( $request ); + + // Assert response is successful. + $this->assertSame( 200, $response->get_status() ); + $this->assertNotEmpty( $response->get_data()['data'] ); + } +} From abacf18dcca8564e3c93ee4759f827ce15aabbe6 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Fri, 13 Feb 2026 16:27:19 +0800 Subject: [PATCH 4/4] Fix tests --- tests/Integration/RESTAPIBroadcastsTest.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/Integration/RESTAPIBroadcastsTest.php b/tests/Integration/RESTAPIBroadcastsTest.php index 5907ec126..cacbdbbe8 100644 --- a/tests/Integration/RESTAPIBroadcastsTest.php +++ b/tests/Integration/RESTAPIBroadcastsTest.php @@ -81,7 +81,7 @@ public function testWhenNoBroadcastsExist() // Assert response is successful. $this->assertSame( 200, $response->get_status() ); - $this->assertEquals( '', $response->get_data()['data'] ); + $this->assertEquals( '', $response->get_data() ); } /** @@ -93,7 +93,8 @@ public function testWhenNoBroadcastsExist() public function testWhenBroadcastsExist() { // Refresh resources. - new \ConvertKit_Resource_Posts( 'output_broadcasts' )->refresh(); + $broadcasts = new \ConvertKit_Resource_Posts( 'output_broadcasts' ); + $broadcasts->refresh(); // Send request. $request = new \WP_REST_Request( 'GET', '/kit/v1/broadcasts' ); @@ -101,6 +102,6 @@ public function testWhenBroadcastsExist() // Assert response is successful. $this->assertSame( 200, $response->get_status() ); - $this->assertNotEmpty( $response->get_data()['data'] ); + $this->assertNotEmpty( $response->get_data() ); } }