From 36305a5f8650338277c26b101a5fd927e87ca33b Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 24 Aug 2026 18:22:28 +0800 Subject: [PATCH 1/2] Custom Content Shortcode: Support Signed Subscriber IDs --- .../blocks/class-convertkit-block-content.php | 37 ++++++-- .../tags/PageShortcodeCustomContentCest.php | 89 ++++++++++++++++++- 2 files changed, 117 insertions(+), 9 deletions(-) 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..87c9167ce 100644 --- a/tests/EndToEnd/tags/PageShortcodeCustomContentCest.php +++ b/tests/EndToEnd/tags/PageShortcodeCustomContentCest.php @@ -112,7 +112,7 @@ public function testCustomContentShortcodeWithValidTagParameterAndInvalidSubscri // Confirm that the Custom Content is not yet displayed. $I->dontSee('KitCustomContent'); - // Reload the page, this time with an invalid subscriber ID . + // Reload the page, this time with an invalid subscriber ID. $I->amOnPage('/kit-custom-content-shortcode-valid-tag-param-and-invalid-subscriber-id?ck_subscriber_id=1'); // Check that no PHP warnings or notices were output. @@ -149,8 +149,91 @@ public function testCustomContentShortcodeWithValidTagParameterAndValidSubscribe // Confirm that the Custom Content is not yet displayed. $I->dontSee('KitCustomContent'); - // Reload the page, this time with a subscriber ID who is already subscribed to the tag. - $I->amOnPage('/kit-custom-content-shortcode-valid-tag-param-and-valid-subscriber-id?ck_subscriber_id=' . $_ENV['CONVERTKIT_API_SUBSCRIBER_ID']); + // Set cookie with an invalid signed subscriber ID. + $I->setRestrictContentCookie($I, 'invalid-signed-subscriber-id'); + + // Reload the page, this time with an invalid signed subscriber ID. + $I->amOnPage('/kit-custom-content-shortcode-valid-tag-param-and-valid-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'); + } + + /** + * Test the [convertkit_content] shortcode works when a valid Tag ID is specified, + * and an invalid signed subscriber ID is used who is subscribed to the tag. + * + * @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-valid-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-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-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'); + } + + /** + * 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-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-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-subscriber-id'); // Check that no PHP warnings or notices were output. $I->checkNoWarningsAndNoticesOnScreen($I); From cd4e746d1194595f2cc615e157058c9c310b1916 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Wed, 26 Aug 2026 10:27:50 +0800 Subject: [PATCH 2/2] Fix tests --- .../tags/PageShortcodeCustomContentCest.php | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/tests/EndToEnd/tags/PageShortcodeCustomContentCest.php b/tests/EndToEnd/tags/PageShortcodeCustomContentCest.php index 87c9167ce..ddad2ded3 100644 --- a/tests/EndToEnd/tags/PageShortcodeCustomContentCest.php +++ b/tests/EndToEnd/tags/PageShortcodeCustomContentCest.php @@ -112,7 +112,7 @@ public function testCustomContentShortcodeWithValidTagParameterAndInvalidSubscri // Confirm that the Custom Content is not yet displayed. $I->dontSee('KitCustomContent'); - // Reload the page, this time with an invalid subscriber ID. + // Reload the page, this time with an invalid subscriber ID . $I->amOnPage('/kit-custom-content-shortcode-valid-tag-param-and-invalid-subscriber-id?ck_subscriber_id=1'); // Check that no PHP warnings or notices were output. @@ -149,22 +149,19 @@ public function testCustomContentShortcodeWithValidTagParameterAndValidSubscribe // Confirm that the Custom Content is not yet displayed. $I->dontSee('KitCustomContent'); - // Set cookie with an invalid signed subscriber ID. - $I->setRestrictContentCookie($I, 'invalid-signed-subscriber-id'); - - // Reload the page, this time with an invalid signed subscriber ID. - $I->amOnPage('/kit-custom-content-shortcode-valid-tag-param-and-valid-subscriber-id'); + // Reload the page, this time with a subscriber ID who is already subscribed to the tag. + $I->amOnPage('/kit-custom-content-shortcode-valid-tag-param-and-valid-subscriber-id?ck_subscriber_id=' . $_ENV['CONVERTKIT_API_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'); + // Confirm that the Custom Content is now displayed. + $I->see('KitCustomContent'); } /** * Test the [convertkit_content] shortcode works when a valid Tag ID is specified, - * and an invalid signed subscriber ID is used who is subscribed to the tag. + * and an invalid signed subscriber ID is used. * * @since 3.4.0 * @@ -175,13 +172,13 @@ public function testCustomContentShortcodeWithValidTagParameterAndInvalidSignedS // Create Page with Shortcode. $I->havePageInDatabase( [ - 'post_name' => 'kit-custom-content-shortcode-valid-tag-param-and-valid-subscriber-id', + '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-valid-subscriber-id'); + $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); @@ -189,17 +186,17 @@ public function testCustomContentShortcodeWithValidTagParameterAndInvalidSignedS // 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']); + // 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-valid-subscriber-id'); + $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->see('KitCustomContent'); + $I->dontSee('KitCustomContent'); } /** @@ -215,13 +212,13 @@ public function testCustomContentShortcodeWithValidTagParameterAndValidSignedSub // Create Page with Shortcode. $I->havePageInDatabase( [ - 'post_name' => 'kit-custom-content-shortcode-valid-tag-param-and-valid-subscriber-id', + '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-subscriber-id'); + $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); @@ -233,7 +230,7 @@ public function testCustomContentShortcodeWithValidTagParameterAndValidSignedSub $I->setRestrictContentCookie($I, $_ENV['CONVERTKIT_API_SIGNED_SUBSCRIBER_ID']); // Reload the page. - $I->amOnPage('/kit-custom-content-shortcode-valid-tag-param-and-valid-subscriber-id'); + $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);