Skip to content

Sharing service: Prevent various undefined array key warnings#43599

Merged
coder-karen merged 5 commits intotrunkfrom
update/sharing-service-undefined-array-key
May 27, 2025
Merged

Sharing service: Prevent various undefined array key warnings#43599
coder-karen merged 5 commits intotrunkfrom
update/sharing-service-undefined-array-key

Conversation

@coder-karen
Copy link
Copy Markdown
Contributor

@coder-karen coder-karen commented May 23, 2025

Fixes VULCAN-137

Proposed changes:

  • This PR prevents a few undefined array key warnings: Warning: Undefined array key "sharing_label", Undefined array key "custom", Undefined array key "hidden", Undefined array key "visible", as well as a rarer fatal - Uncaught Error: Call to a member function get_name() on false
  • The approach to fix these is just to check if they are set
  • In the case of the fatal, we check to see if $this->service an instance of Sharing_Source, as it can be an bool.
  • Logs: c9a04af81a4f4f4eec9167d272bcbe2f-logstash

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Jetpack product discussion

pdWQjU-1g0-p2

Does this pull request change what data or activity we track or use?

No.

Testing instructions:

I've been unable to replicate the warnings in practice, but it would seem they are likely happening when options are corrupted or not fully initialized. In most cases the warnings can be replicated by modifiying the relevant options values. So, a good way to test this is with a sandboxed Jurassic Ninja site. I recommend using a JN site because we're modifying options (so if you are using the JN site for anything else you may want to note the options when retrieveing them, and reset them back to original values afterwards):

  • Create a Jurassic Ninja site using the ?specialops=true options - make sure to select 'Enable Sandbox access' (you don't need the Jetpack Beta tester plugin).
  • Visit Settings > Jetpack Constants, and add your sandbox domain
  • Connect Jetpack
  • Retrieve your new blog ID from the Jetpack debugger
  • Open up your sandbox, and in wpsh run the following:
switch_to_blog(YOUR_BLOG_ID);
return get_option('sharing-options');
$options['global'] = array()
update_option('sharing-options', $options);
  • Then visit the Jetpack Stats page (Jetpack > Stats).
  • Make sure you are tailing errors - tail -f /tmp/php-errors - and you should see undefined array key warnings related to the custom and sharing_label keys.
  • Then, run the following:
return get_option('sharing-services');
$services = array();
update_option('sharing-services', $services);
  • Then visit the Jetpack Stats page again (Jetpack > Stats).
  • You should see undefined array key warnings related to the visible and hidden keys now.

The only other change is the check to see if $this->service is an instance of Sharing_Source - this has only occurred twice over the last month, and proof-reading the change should be sufficient.

  • That said the warning can be replicated (Uncaught Error: Call to a member function get_name() on false) by directly setting $this->service to false here, before we set $this->name.
  • You'd be best making the change via moon or sun in your sandbox (whichever is active for testing).
  • Then sandbox a Simple test site and visit the following page: /wp-admin/admin.php?page=stats. You should see the warning on trunk
  • With the PR applied using the command in the generated comment below, and with $this->service still set to false, the warning should not exist.

@coder-karen coder-karen self-assigned this May 23, 2025
@coder-karen coder-karen added Bug When a feature is broken and / or not performing as intended [Status] In Progress [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ labels May 23, 2025
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 23, 2025

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the update/sharing-service-undefined-array-key branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack update/sharing-service-undefined-array-key

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions
Copy link
Copy Markdown
Contributor

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add a "[Type]" label (Bug, Enhancement, Janitorial, Task).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!


Jetpack plugin:

The Jetpack plugin has different release cadences depending on the platform:

  • WordPress.com Simple releases happen as soon as you deploy your changes after merging this PR (PCYsg-Jjm-p2).
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly:
    • Scheduled release: June 3, 2025
    • Code freeze: June 2, 2025

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@github-actions github-actions Bot added the [Feature] Sharing Post sharing, sharing buttons label May 23, 2025
@jp-launch-control
Copy link
Copy Markdown

jp-launch-control Bot commented May 23, 2025

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/plugins/jetpack/modules/sharedaddy/sharing-service.php 185/467 (39.61%) 0.04% 1 ❤️‍🩹

Full summary · PHP report · JS report

@coder-karen coder-karen marked this pull request as ready for review May 26, 2025 10:05
@coder-karen coder-karen added [Status] Needs Review This PR is ready for review. and removed [Status] In Progress labels May 26, 2025
@coder-karen coder-karen requested a review from a team May 26, 2025 10:43
$this->total = (int) $total;

$this->name = $this->service->get_name();
if ( is_object( $this->service ) ) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would it work to be more specific, and check for the Sharing_Source instead, so we know get_name will be in there?

Copy link
Copy Markdown
Contributor Author

@coder-karen coder-karen May 27, 2025

Choose a reason for hiding this comment

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

Yes, good point. I was also able to come up with steps to replicate the warning and test the fix now for this specific change too (although it looks straightforward to check if it's an instance of Sharing_Source, I wanted to be sure there wouldn't be additional errors) , so updated the testing instructions.

@coder-karen coder-karen added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Review This PR is ready for review. labels May 27, 2025
@coder-karen coder-karen merged commit 7d8cd95 into trunk May 27, 2025
69 of 71 checks passed
@coder-karen coder-karen deleted the update/sharing-service-undefined-array-key branch May 27, 2025 14:42
@github-actions github-actions Bot removed the [Status] Ready to Merge Go ahead, you can push that green button! label May 27, 2025
@github-actions github-actions Bot added this to the jetpack/14.7 milestone May 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug When a feature is broken and / or not performing as intended [Feature] Sharing Post sharing, sharing buttons [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants