diff --git a/includes/blocks/class-convertkit-block-broadcasts.php b/includes/blocks/class-convertkit-block-broadcasts.php index ac7abb3fd..e416ed1b3 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' ), @@ -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. @@ -122,7 +121,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(), @@ -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 36c327ccb..6783e817b 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); @@ -82,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) { diff --git a/resources/frontend/js/restrict-content.js b/resources/frontend/js/restrict-content.js index 48dd67290..9ec1442e7 100644 --- a/resources/frontend/js/restrict-content.js +++ b/resources/frontend/js/restrict-content.js @@ -150,10 +150,10 @@ function convertKitRestrictContentSubscriberAuthenticationSendCode( fetch(convertkit_restrict_content.subscriber_authentication_url, { method: 'POST', headers: { - 'Content-Type': 'application/x-www-form-urlencoded', + 'Content-Type': 'application/json', 'X-WP-Nonce': nonce, }, - body: new URLSearchParams({ + body: JSON.stringify({ convertkit_email: email, convertkit_resource_type: resource_type, convertkit_resource_id: resource_id, @@ -223,10 +223,10 @@ function convertKitRestrictContentSubscriberVerification( fetch(convertkit_restrict_content.subscriber_verification_url, { method: 'POST', headers: { - 'Content-Type': 'application/x-www-form-urlencoded', + 'Content-Type': 'application/json', 'X-WP-Nonce': nonce, }, - body: new URLSearchParams({ + body: JSON.stringify({ subscriber_code, token, convertkit_post_id: post_id, diff --git a/tests/Integration/RESTAPIBroadcastsTest.php b/tests/Integration/RESTAPIBroadcastsTest.php new file mode 100644 index 000000000..cacbdbbe8 --- /dev/null +++ b/tests/Integration/RESTAPIBroadcastsTest.php @@ -0,0 +1,107 @@ +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() ); + } + + /** + * 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. + $broadcasts = new \ConvertKit_Resource_Posts( 'output_broadcasts' ); + $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() ); + } +} diff --git a/tests/Integration/RESTAPIRestrictContentTest.php b/tests/Integration/RESTAPIRestrictContentTest.php new file mode 100644 index 000000000..02cd7e5eb --- /dev/null +++ b/tests/Integration/RESTAPIRestrictContentTest.php @@ -0,0 +1,309 @@ +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 ), + ) + ); + } + + /** + * 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/restrict-content/subscriber-authentication REST API route when + * requesting the subscriber authentication email to be sent for a given Form ID and subscriber + * + * @since 3.1.0 + */ + public function testRestrictContentSubscriberAuthenticationForm() + { + // Create a Post. + $post_id = static::factory()->post->create( [ 'post_title' => 'Test Post' ] ); + + // Build request. + $request = new \WP_REST_Request( 'POST', '/kit/v1/restrict-content/subscriber-authentication' ); + $request->set_header( 'Content-Type', 'application/x-www-form-urlencoded' ); + $request->set_body_params( + [ + 'convertkit_email' => $_ENV['CONVERTKIT_API_SUBSCRIBER_EMAIL'], + 'convertkit_resource_type' => 'form', + 'convertkit_resource_id' => $_ENV['CONVERTKIT_API_FORM_ID'], + 'convertkit_post_id' => $post_id, + ] + ); + + // Send request. + $response = rest_get_server()->dispatch( $request ); + + // Assert response is successful. + $this->assertSame( 200, $response->get_status() ); + + // Assert response data has the expected keys and data. + $data = $response->get_data(); + $this->assertIsArray( $data ); + $this->assertTrue( $data['success'] ); + $this->assertArrayHasKey( 'data', $data ); + } + + /** + * Test that the /wp-json/kit/v1/restrict-content/subscriber-authentication REST API route when + * requesting the subscriber authentication email to be sent for a given Form ID and an invalid subscriber email is given + * + * @since 3.1.0 + */ + public function testRestrictContentSubscriberAuthenticationFormInvalidEmail() + { + // Create a Post. + $post_id = static::factory()->post->create( [ 'post_title' => 'Test Post' ] ); + + // Build request. + $request = new \WP_REST_Request( 'POST', '/kit/v1/restrict-content/subscriber-authentication' ); + $request->set_header( 'Content-Type', 'application/x-www-form-urlencoded' ); + $request->set_body_params( + [ + 'convertkit_email' => 'fail@kit.com', + 'convertkit_resource_type' => 'form', + 'convertkit_resource_id' => $_ENV['CONVERTKIT_API_FORM_ID'], + 'convertkit_post_id' => $post_id, + ] + ); + + // Send request. + $response = rest_get_server()->dispatch( $request ); + + // Assert response is successful. + $this->assertSame( 200, $response->get_status() ); + + // Assert response data has the expected keys and data. + $data = $response->get_data(); + $this->assertIsArray( $data ); + $this->assertFalse( $data['success'] ); + $this->assertArrayHasKey( 'data', $data ); + } + + /** + * Test that the /wp-json/kit/v1/restrict-content/subscriber-authentication REST API route when + * requesting the subscriber authentication email to be sent for a given Tag ID and subscriber + * + * @since 3.1.0 + */ + public function testRestrictContentSubscriberAuthenticationTag() + { + // Create a Post. + $post_id = static::factory()->post->create( [ 'post_title' => 'Test Post' ] ); + + // Build request. + $request = new \WP_REST_Request( 'POST', '/kit/v1/restrict-content/subscriber-authentication' ); + $request->set_header( 'Content-Type', 'application/x-www-form-urlencoded' ); + $request->set_body_params( + [ + 'convertkit_email' => $_ENV['CONVERTKIT_API_SUBSCRIBER_EMAIL'], + 'convertkit_resource_type' => 'tag', + 'convertkit_resource_id' => $_ENV['CONVERTKIT_API_TAG_ID'], + 'convertkit_post_id' => $post_id, + ] + ); + + // Send request. + $response = rest_get_server()->dispatch( $request ); + + // Assert response is successful. + $this->assertSame( 200, $response->get_status() ); + + // Assert response data has the expected keys and data. + $data = $response->get_data(); + $this->assertIsArray( $data ); + $this->assertTrue( $data['success'] ); + $this->assertArrayHasKey( 'data', $data ); + } + + /** + * Test that the /wp-json/kit/v1/restrict-content/subscriber-authentication REST API route when + * requesting the subscriber authentication email to be sent for a given Tag ID and an invalid subscriber email is given + * + * @since 3.1.0 + */ + public function testRestrictContentSubscriberAuthenticationTagInvalidEmail() + { + // Create a Post. + $post_id = static::factory()->post->create( [ 'post_title' => 'Test Post' ] ); + + // Build request. + $request = new \WP_REST_Request( 'POST', '/kit/v1/restrict-content/subscriber-authentication' ); + $request->set_header( 'Content-Type', 'application/x-www-form-urlencoded' ); + $request->set_body_params( + [ + 'convertkit_email' => 'fail@kit.com', + 'convertkit_resource_type' => 'tag', + 'convertkit_resource_id' => $_ENV['CONVERTKIT_API_TAG_ID'], + 'convertkit_post_id' => $post_id, + ] + ); + + // Send request. + $response = rest_get_server()->dispatch( $request ); + + // Assert response is successful. + $this->assertSame( 200, $response->get_status() ); + + // Assert response data has the expected keys and data. + $data = $response->get_data(); + $this->assertIsArray( $data ); + $this->assertFalse( $data['success'] ); + $this->assertArrayHasKey( 'data', $data ); + } + + /** + * Test that the /wp-json/kit/v1/restrict-content/subscriber-authentication REST API route when + * requesting the subscriber authentication email to be sent for a given Product ID and subscriber + * + * @since 3.1.0 + */ + public function testRestrictContentSubscriberAuthenticationProduct() + { + // Create a Post. + $post_id = static::factory()->post->create( [ 'post_title' => 'Test Post' ] ); + + // Build request. + $request = new \WP_REST_Request( 'POST', '/kit/v1/restrict-content/subscriber-authentication' ); + $request->set_header( 'Content-Type', 'application/x-www-form-urlencoded' ); + $request->set_body_params( + [ + 'convertkit_email' => $_ENV['CONVERTKIT_API_SUBSCRIBER_EMAIL'], + 'convertkit_resource_type' => 'product', + 'convertkit_resource_id' => $_ENV['CONVERTKIT_API_PRODUCT_ID'], + 'convertkit_post_id' => $post_id, + ] + ); + + // Send request. + $response = rest_get_server()->dispatch( $request ); + + // Assert response is successful. + $this->assertSame( 200, $response->get_status() ); + + // Assert response data has the expected keys and data. + $data = $response->get_data(); + $this->assertIsArray( $data ); + $this->assertTrue( $data['success'] ); + $this->assertArrayHasKey( 'data', $data ); + } + + /** + * Test that the /wp-json/kit/v1/restrict-content/subscriber-authentication REST API route when + * requesting the subscriber authentication email to be sent for a given Product ID and an invalid subscriber email is given + * + * @since 3.1.0 + */ + public function testRestrictContentSubscriberAuthenticationProductInvalidEmail() + { + // Create a Post. + $post_id = static::factory()->post->create( [ 'post_title' => 'Test Post' ] ); + + // Build request. + $request = new \WP_REST_Request( 'POST', '/kit/v1/restrict-content/subscriber-authentication' ); + $request->set_header( 'Content-Type', 'application/x-www-form-urlencoded' ); + $request->set_body_params( + [ + 'convertkit_email' => 'fail@kit.com', + 'convertkit_resource_type' => 'product', + 'convertkit_resource_id' => $_ENV['CONVERTKIT_API_PRODUCT_ID'], + 'convertkit_post_id' => $post_id, + ] + ); + + // Send request. + $response = rest_get_server()->dispatch( $request ); + + // Assert response is successful. + $this->assertSame( 200, $response->get_status() ); + + // Assert response data has the expected keys and data. + $data = $response->get_data(); + $this->assertIsArray( $data ); + $this->assertFalse( $data['success'] ); + $this->assertArrayHasKey( 'data', $data ); + } + + /** + * Test that the /wp-json/kit/v1/subscriber/store-email-as-id-in-cookie REST API route stores + * the subscriber ID in a cookie when a valid email address is given. + * + * @since 3.1.7 + */ + public function testStoreEmailAsIDInCookie() + { + // Build request. + $request = new \WP_REST_Request( 'POST', '/kit/v1/subscriber/store-email-as-id-in-cookie' ); + $request->set_header( 'Content-Type', 'application/json' ); + $request->set_body_params( + [ + 'email' => $_ENV['CONVERTKIT_API_SUBSCRIBER_EMAIL'], + ], + ); + + // Send request. + $response = rest_get_server()->dispatch( $request ); + + // Assert response is successful. + $this->assertSame( 200, $response->get_status() ); + + // Assert response data has the expected keys and data. + $data = $response->get_data(); + $this->assertIsArray( $data ); + $this->assertEquals( (int) $_ENV['CONVERTKIT_API_SUBSCRIBER_ID'], (int) $data['id'] ); + } +} diff --git a/tests/Integration/RESTAPITest.php b/tests/Integration/RESTAPITest.php index e84ce8c4d..ff085d920 100644 --- a/tests/Integration/RESTAPITest.php +++ b/tests/Integration/RESTAPITest.php @@ -306,222 +306,6 @@ public function testRefreshResourcesRestrictContent() $this->assertArrayHasKeys( $data['products'][0], [ 'id', 'name', 'url', 'published' ] ); } - /** - * Test that the /wp-json/kit/v1/restrict-content/subscriber-authentication REST API route when - * requesting the subscriber authentication email to be sent for a given Form ID and subscriber - * - * @since 3.1.0 - */ - public function testRestrictContentSubscriberAuthenticationForm() - { - // Create a Post. - $post_id = static::factory()->post->create( [ 'post_title' => 'Test Post' ] ); - - // Build request. - $request = new \WP_REST_Request( 'POST', '/kit/v1/restrict-content/subscriber-authentication' ); - $request->set_header( 'Content-Type', 'application/x-www-form-urlencoded' ); - $request->set_body_params( - [ - 'convertkit_email' => $_ENV['CONVERTKIT_API_SUBSCRIBER_EMAIL'], - 'convertkit_resource_type' => 'form', - 'convertkit_resource_id' => $_ENV['CONVERTKIT_API_FORM_ID'], - 'convertkit_post_id' => $post_id, - ] - ); - - // Send request. - $response = rest_get_server()->dispatch( $request ); - - // Assert response is successful. - $this->assertSame( 200, $response->get_status() ); - - // Assert response data has the expected keys and data. - $data = $response->get_data(); - $this->assertIsArray( $data ); - $this->assertTrue( $data['success'] ); - $this->assertArrayHasKey( 'data', $data ); - } - - /** - * Test that the /wp-json/kit/v1/restrict-content/subscriber-authentication REST API route when - * requesting the subscriber authentication email to be sent for a given Form ID and an invalid subscriber email is given - * - * @since 3.1.0 - */ - public function testRestrictContentSubscriberAuthenticationFormInvalidEmail() - { - // Create a Post. - $post_id = static::factory()->post->create( [ 'post_title' => 'Test Post' ] ); - - // Build request. - $request = new \WP_REST_Request( 'POST', '/kit/v1/restrict-content/subscriber-authentication' ); - $request->set_header( 'Content-Type', 'application/x-www-form-urlencoded' ); - $request->set_body_params( - [ - 'convertkit_email' => 'fail@kit.com', - 'convertkit_resource_type' => 'form', - 'convertkit_resource_id' => $_ENV['CONVERTKIT_API_FORM_ID'], - 'convertkit_post_id' => $post_id, - ] - ); - - // Send request. - $response = rest_get_server()->dispatch( $request ); - - // Assert response is successful. - $this->assertSame( 200, $response->get_status() ); - - // Assert response data has the expected keys and data. - $data = $response->get_data(); - $this->assertIsArray( $data ); - $this->assertFalse( $data['success'] ); - $this->assertArrayHasKey( 'data', $data ); - } - - /** - * Test that the /wp-json/kit/v1/restrict-content/subscriber-authentication REST API route when - * requesting the subscriber authentication email to be sent for a given Tag ID and subscriber - * - * @since 3.1.0 - */ - public function testRestrictContentSubscriberAuthenticationTag() - { - // Create a Post. - $post_id = static::factory()->post->create( [ 'post_title' => 'Test Post' ] ); - - // Build request. - $request = new \WP_REST_Request( 'POST', '/kit/v1/restrict-content/subscriber-authentication' ); - $request->set_header( 'Content-Type', 'application/x-www-form-urlencoded' ); - $request->set_body_params( - [ - 'convertkit_email' => $_ENV['CONVERTKIT_API_SUBSCRIBER_EMAIL'], - 'convertkit_resource_type' => 'tag', - 'convertkit_resource_id' => $_ENV['CONVERTKIT_API_TAG_ID'], - 'convertkit_post_id' => $post_id, - ] - ); - - // Send request. - $response = rest_get_server()->dispatch( $request ); - - // Assert response is successful. - $this->assertSame( 200, $response->get_status() ); - - // Assert response data has the expected keys and data. - $data = $response->get_data(); - $this->assertIsArray( $data ); - $this->assertTrue( $data['success'] ); - $this->assertArrayHasKey( 'data', $data ); - } - - /** - * Test that the /wp-json/kit/v1/restrict-content/subscriber-authentication REST API route when - * requesting the subscriber authentication email to be sent for a given Tag ID and an invalid subscriber email is given - * - * @since 3.1.0 - */ - public function testRestrictContentSubscriberAuthenticationTagInvalidEmail() - { - // Create a Post. - $post_id = static::factory()->post->create( [ 'post_title' => 'Test Post' ] ); - - // Build request. - $request = new \WP_REST_Request( 'POST', '/kit/v1/restrict-content/subscriber-authentication' ); - $request->set_header( 'Content-Type', 'application/x-www-form-urlencoded' ); - $request->set_body_params( - [ - 'convertkit_email' => 'fail@kit.com', - 'convertkit_resource_type' => 'tag', - 'convertkit_resource_id' => $_ENV['CONVERTKIT_API_TAG_ID'], - 'convertkit_post_id' => $post_id, - ] - ); - - // Send request. - $response = rest_get_server()->dispatch( $request ); - - // Assert response is successful. - $this->assertSame( 200, $response->get_status() ); - - // Assert response data has the expected keys and data. - $data = $response->get_data(); - $this->assertIsArray( $data ); - $this->assertFalse( $data['success'] ); - $this->assertArrayHasKey( 'data', $data ); - } - - /** - * Test that the /wp-json/kit/v1/restrict-content/subscriber-authentication REST API route when - * requesting the subscriber authentication email to be sent for a given Product ID and subscriber - * - * @since 3.1.0 - */ - public function testRestrictContentSubscriberAuthenticationProduct() - { - // Create a Post. - $post_id = static::factory()->post->create( [ 'post_title' => 'Test Post' ] ); - - // Build request. - $request = new \WP_REST_Request( 'POST', '/kit/v1/restrict-content/subscriber-authentication' ); - $request->set_header( 'Content-Type', 'application/x-www-form-urlencoded' ); - $request->set_body_params( - [ - 'convertkit_email' => $_ENV['CONVERTKIT_API_SUBSCRIBER_EMAIL'], - 'convertkit_resource_type' => 'product', - 'convertkit_resource_id' => $_ENV['CONVERTKIT_API_PRODUCT_ID'], - 'convertkit_post_id' => $post_id, - ] - ); - - // Send request. - $response = rest_get_server()->dispatch( $request ); - - // Assert response is successful. - $this->assertSame( 200, $response->get_status() ); - - // Assert response data has the expected keys and data. - $data = $response->get_data(); - $this->assertIsArray( $data ); - $this->assertTrue( $data['success'] ); - $this->assertArrayHasKey( 'data', $data ); - } - - /** - * Test that the /wp-json/kit/v1/restrict-content/subscriber-authentication REST API route when - * requesting the subscriber authentication email to be sent for a given Product ID and an invalid subscriber email is given - * - * @since 3.1.0 - */ - public function testRestrictContentSubscriberAuthenticationProductInvalidEmail() - { - // Create a Post. - $post_id = static::factory()->post->create( [ 'post_title' => 'Test Post' ] ); - - // Build request. - $request = new \WP_REST_Request( 'POST', '/kit/v1/restrict-content/subscriber-authentication' ); - $request->set_header( 'Content-Type', 'application/x-www-form-urlencoded' ); - $request->set_body_params( - [ - 'convertkit_email' => 'fail@kit.com', - 'convertkit_resource_type' => 'product', - 'convertkit_resource_id' => $_ENV['CONVERTKIT_API_PRODUCT_ID'], - 'convertkit_post_id' => $post_id, - ] - ); - - // Send request. - $response = rest_get_server()->dispatch( $request ); - - // Assert response is successful. - $this->assertSame( 200, $response->get_status() ); - - // Assert response data has the expected keys and data. - $data = $response->get_data(); - $this->assertIsArray( $data ); - $this->assertFalse( $data['success'] ); - $this->assertArrayHasKey( 'data', $data ); - } - /** * Test that the /wp-json/kit/v1/subscriber/store-email-as-id-in-cookie REST API route stores * the subscriber ID in a cookie when a valid email address is given.