diff --git a/includes/blocks/class-convertkit-block-content.php b/includes/blocks/class-convertkit-block-content.php index 73a6fa01d..f9f812b02 100644 --- a/includes/blocks/class-convertkit-block-content.php +++ b/includes/blocks/class-convertkit-block-content.php @@ -254,15 +254,40 @@ public function render( $atts, $content = '' ) { ); // Get the subscriber's tags, to see if they subscribed to this tag. - $tags = $api->get_subscriber_tags( $subscriber_id ); + if ( is_numeric( $subscriber_id ) ) { + $tags = $api->get_subscriber_tags( $subscriber_id ); - // Bail if an error occurred. - if ( is_wp_error( $tags ) ) { - if ( $settings->debug_enabled() ) { - return ''; + // Bail if an error occurred. + if ( is_wp_error( $tags ) ) { + if ( $settings->debug_enabled() ) { + return ''; + } + + return ''; + } + } else { + // Subscriber ID is a signed subscriber ID. + // Get tags that the subscriber has access to via the profile() method. + $result = $api->profile( $subscriber_id ); + + // If an error occurred, the subscriber ID is invalid. + if ( is_wp_error( $result ) ) { + if ( $settings->debug_enabled() ) { + return ''; + } + + return ''; } - return ''; + // Build tags array. + $tags = array( + 'tags' => array(), + ); + foreach ( $result['tags'] as $tag_id ) { + $tags['tags'][] = array( + 'id' => $tag_id, + ); + } } // Bail if the subscriber has no tags. diff --git a/tests/EndToEnd/tags/PageShortcodeCustomContentCest.php b/tests/EndToEnd/tags/PageShortcodeCustomContentCest.php index 837da3349..ddad2ded3 100644 --- a/tests/EndToEnd/tags/PageShortcodeCustomContentCest.php +++ b/tests/EndToEnd/tags/PageShortcodeCustomContentCest.php @@ -159,6 +159,86 @@ public function testCustomContentShortcodeWithValidTagParameterAndValidSubscribe $I->see('KitCustomContent'); } + /** + * Test the [convertkit_content] shortcode works when a valid Tag ID is specified, + * and an invalid signed subscriber ID is used. + * + * @since 3.4.0 + * + * @param EndToEndTester $I Tester. + */ + public function testCustomContentShortcodeWithValidTagParameterAndInvalidSignedSubscriberID(EndToEndTester $I) + { + // Create Page with Shortcode. + $I->havePageInDatabase( + [ + 'post_name' => 'kit-custom-content-shortcode-valid-tag-param-and-invalid-signed-subscriber-id', + 'post_content' => '[convertkit_content tag="' . $_ENV['CONVERTKIT_API_TAG_ID'] . '"]KitCustomContent[/convertkit_content]', + ] + ); + + // Load the Page on the frontend site. + $I->amOnPage('/kit-custom-content-shortcode-valid-tag-param-and-invalid-signed-subscriber-id'); + + // Check that no PHP warnings or notices were output. + $I->checkNoWarningsAndNoticesOnScreen($I); + + // Confirm that the Custom Content is not yet displayed. + $I->dontSee('KitCustomContent'); + + // Set cookie with invalid signed subscriber ID. + $I->setRestrictContentCookie($I, 'invalid-signed-subscriber-id'); + + // Reload the page. + $I->amOnPage('/kit-custom-content-shortcode-valid-tag-param-and-invalid-signed-subscriber-id'); + + // Check that no PHP warnings or notices were output. + $I->checkNoWarningsAndNoticesOnScreen($I); + + // Confirm that the Custom Content is now displayed. + $I->dontSee('KitCustomContent'); + } + + /** + * Test the [convertkit_content] shortcode works when a valid Tag ID is specified, + * and a valid signed subscriber ID is used who is subscribed to the tag. + * + * @since 3.4.0 + * + * @param EndToEndTester $I Tester. + */ + public function testCustomContentShortcodeWithValidTagParameterAndValidSignedSubscriberID(EndToEndTester $I) + { + // Create Page with Shortcode. + $I->havePageInDatabase( + [ + 'post_name' => 'kit-custom-content-shortcode-valid-tag-param-and-valid-signed-subscriber-id', + 'post_content' => '[convertkit_content tag="' . $_ENV['CONVERTKIT_API_TAG_ID'] . '"]KitCustomContent[/convertkit_content]', + ] + ); + + // Load the Page on the frontend site. + $I->amOnPage('/kit-custom-content-shortcode-valid-tag-param-and-valid-signed-subscriber-id'); + + // Check that no PHP warnings or notices were output. + $I->checkNoWarningsAndNoticesOnScreen($I); + + // Confirm that the Custom Content is not yet displayed. + $I->dontSee('KitCustomContent'); + + // Set cookie with signed subscriber ID. + $I->setRestrictContentCookie($I, $_ENV['CONVERTKIT_API_SIGNED_SUBSCRIBER_ID']); + + // Reload the page. + $I->amOnPage('/kit-custom-content-shortcode-valid-tag-param-and-valid-signed-subscriber-id'); + + // Check that no PHP warnings or notices were output. + $I->checkNoWarningsAndNoticesOnScreen($I); + + // Confirm that the Custom Content is now displayed. + $I->see('KitCustomContent'); + } + /** * Deactivate and reset Plugin(s) after each test, if the test passes. * We don't use _after, as this would provide a screenshot of the Plugin