Skip to content

fix(reader-activation): return all labels when no key is requested#4765

Closed
thisismyurl wants to merge 1 commit into
Automattic:trunkfrom
thisismyurl:fix/4560-reader-activation-labels-loop-var
Closed

fix(reader-activation): return all labels when no key is requested#4765
thisismyurl wants to merge 1 commit into
Automattic:trunkfrom
thisismyurl:fix/4560-reader-activation-labels-loop-var

Conversation

@thisismyurl

Copy link
Copy Markdown

Hey folks,

Calling get_reader_activation_labels() with no key returns a single label string instead of the full labels array. The label-building loop reuses the method's own $key parameter as its foreach variable, so by the time the loop ends $key holds the last label key rather than null. The if ( ! $key ) guard then falls through and returns one label. Since that array is what wp_localize_script() hands to the auth script, the front end receives a broken labels object.

Renaming the loop variable is the whole fix. I added a test that calls the getter with no key and asserts it returns the full array; it resets the static label cache afterward so it leaves no residue for other tests in the process.

To verify by hand: register a filter on newspack_reader_activation_auth_labels, load a page that enqueues the auth script, and confirm the localized newspack_reader_activation_labels is the full object rather than a single string. Or run test_get_reader_activation_labels_returns_full_array. I proved the logic with a standalone harness (a no-key call returns a string without the fix, the full array with it); I don't have a local wp-env, so the PHPUnit case is written to match the existing tests and runs on CI.

Closes #4560

(full disclosure: AI helped me identify the issue and verify my work)

…utomattic#4560)

get_reader_activation_labels() used its own $key parameter as the foreach
loop variable while building the labels, so after the loop $key held the last
label key instead of null. A no-argument call then failed the `if ( ! $key )`
check and returned a single label string instead of the full labels array,
which is what wp_localize_script() passes to the auth script.

Rename the loop variable so the parameter is preserved. Adds a regression test
that invokes the getter with no key and asserts it returns the full array.
Copilot AI review requested due to automatic review settings July 7, 2026 14:51
@thisismyurl thisismyurl requested a review from a team as a code owner July 7, 2026 14:51
@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown

👋 Thanks for your interest in contributing to Newspack!

Newspack development has moved to a single monorepo: Automattic/newspack-workspace. This repository is now a read-only mirror, so we're automatically closing new pull requests here.

Please reopen your change against the monorepo – newspack-plugin now lives at plugins/newspack-plugin/ there. Thank you! 💙

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a Reader Activation label retrieval bug where calling get_reader_activation_labels() with no key could incorrectly return a single string (breaking wp_localize_script() usage) due to variable shadowing inside the label-building foreach.

Changes:

  • Rename the foreach loop key variable in Reader_Activation::get_reader_activation_labels() to avoid overwriting the method parameter.
  • Add a PHPUnit regression test ensuring a no-key call returns the full labels array.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
includes/reader-activation/class-reader-activation.php Prevents $key parameter shadowing so no-arg calls correctly return the full labels array.
tests/unit-tests/reader-activation.php Adds regression coverage for the no-key labels getter behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +43 to +61
public function test_get_reader_activation_labels_returns_full_array() {
$reflection = new \ReflectionClass( Reader_Activation::class );

// Reset the cached labels so the label-building loop runs on this call.
$cache = $reflection->getProperty( 'reader_activation_labels' );
$cache->setAccessible( true );
$cache->setValue( null, [] );

$method = $reflection->getMethod( 'get_reader_activation_labels' );
$method->setAccessible( true );

$labels = $method->invoke( null );

$this->assertIsArray( $labels, 'A no-key call must return the full labels array, not a single label.' );
$this->assertArrayHasKey( 'account_link', $labels );

// Leave no static residue for later tests in this process.
$cache->setValue( null, [] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reader-activation foreach variable overwrites method parameter (WP_Scripts::localize notice)

2 participants