Skip to content

fix: when doing a rest api call, a php error stackable_add_welcome_notification is undefined can trigger#3579

Merged
bfintal merged 1 commit intodevelopfrom
fix/notification-error-rest-api
Aug 20, 2025
Merged

fix: when doing a rest api call, a php error stackable_add_welcome_notification is undefined can trigger#3579
bfintal merged 1 commit intodevelopfrom
fix/notification-error-rest-api

Conversation

@bfintal
Copy link
Contributor

@bfintal bfintal commented Aug 18, 2025

Summary by CodeRabbit

  • Bug Fixes
    • Premium and rating notifications now appear only within the admin dashboard, preventing any unintended display on the front end.
  • Performance
    • Reduced front-end overhead by avoiding registration of admin-only hooks outside the admin area, improving page load efficiency for visitors.

Copilot AI review requested due to automatic review settings August 18, 2025 04:14
@coderabbitai
Copy link

coderabbitai bot commented Aug 18, 2025

Walkthrough

Added is_admin() guards so admin_menu hooks are registered only in admin requests. Affected constructors in src/pro.php and src/welcome/notification-rate.php; no public signatures changed. Runtime behavior now skips admin hook registration on front-end.

Changes

Cohort / File(s) Summary
Admin-only hook registration
src/pro.php, src/welcome/notification-rate.php
Wrapped add_action('admin_menu', ...) calls in is_admin() checks to prevent registering admin hooks during non-admin (front-end) requests; no other logic or signatures altered.

Sequence Diagram(s)

sequenceDiagram
    participant R as Request
    participant WP as WordPress
    participant P as Plugin (Pro)
    participant N as NotificationRate

    R->>WP: Bootstrap plugin
    WP->>P: Construct
    alt is_admin() true
        P->>WP: add_action('admin_menu', check_pro_notice_date)
        P->>WP: add_action('admin_menu', go_premium_notice_old_raters)
    else front-end
        P-->>WP: Skip admin_menu registrations
    end

    WP->>N: Construct
    alt is_admin() true
        N->>WP: add_action('admin_menu', check_activation_date)
    else front-end
        N-->>WP: Skip admin_menu registration
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

In admin burrows, hooks now hop,
Front-end fields? They softly stop.
I twitch my nose—guards in place,
Less clutter in the public space.
Thump, thump! Clean tracks I leave—
Only where the carrots weave. 🥕🐇

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/notification-error-rest-api

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a PHP error where stackable_add_welcome_notification is undefined during REST API calls by adding is_admin() checks to prevent admin-specific actions from being registered in non-admin contexts.

  • Added is_admin() guards around admin action registrations in notification classes
  • Prevents admin menu hooks from being registered during REST API requests

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/welcome/notification-rate.php Added is_admin() check around admin menu action registration
src/pro.php Added is_admin() check around admin menu action registrations

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@bfintal bfintal self-assigned this Aug 18, 2025
@github-actions
Copy link

🤖 Pull request artifacts

file commit
pr3579-stackable-3579-merge.zip c85b35e

github-actions bot added a commit that referenced this pull request Aug 18, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
src/welcome/notification-rate.php (1)

24-26: Optional hardening: guard the notification call and/or instantiate only in admin

To further bulletproof against edge loads where the function may not be present, consider guarding the call site too. Also optionally instantiate the notifier only in admin to skip doing any work outside wp-admin.

You can add a simple existence check in check_activation_date (outside this hunk):

if ( $elapsed_time > self::RATING_NOTICE_TIME && function_exists( 'stackable_add_welcome_notification' ) ) {
    stackable_add_welcome_notification( 'rate', sprintf( __( 'If Stackable has been valuable to you, consider %sgiving us a 5-star rating on WordPress.org%s. It would not only make our day but also help others discover us too.', STACKABLE_I18N ), '<a href="https://wordpress.org/support/plugin/stackable-ultimate-gutenberg-blocks/reviews/?rate=5#new-post" target="_rate">', '</a>' ) );
}

Or instantiate only in admin:

if ( is_admin() ) {
    new Stackable_Welcome_Notification_Rate();
}
src/pro.php (1)

90-93: Optional hardening: guard the show_notification call site

For extra safety if load order varies, you can guard the call to stackable_add_welcome_notification (outside this hunk):

public function show_notification() {
    if ( function_exists( 'stackable_add_welcome_notification' ) ) {
        stackable_add_welcome_notification(
            'premium',
            sprintf(
                __( 'We hope you\'re enjoying Stackable. If you want more, you may want to check out %sStackable Premium%s. Ready to upgrade and do more? %sGo premium now%s', STACKABLE_I18N ),
                '<a href="https://wpstackable.com/premium/?utm_source=wp-settings-notification&utm_campaign=gopremium&utm_medium=wp-dashboard" target="_blank">', '</a>',
                '<a href="https://wpstackable.com/premium/?utm_source=wp-settings-notification&utm_campaign=gopremium&utm_medium=wp-dashboard">', '</a>'
            )
        );
    }
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 398f38b and c85b35e.

📒 Files selected for processing (2)
  • src/pro.php (1 hunks)
  • src/welcome/notification-rate.php (1 hunks)
🔇 Additional comments (3)
src/welcome/notification-rate.php (2)

24-26: Good fix: admin-only hook registration prevents REST/CLI undefined-function errors

Wrapping the admin_menu hook registration with is_admin() is the right scope boundary and should stop stackable_add_welcome_notification from being referenced in REST requests where it isn’t loaded. Low-risk, preserves admin behavior.


24-26: All notifier calls are admin-only

I’ve verified every invocation of stackable_add_welcome_notification and confirmed:

• It’s defined in src/welcome/notification.php and only called in:
src/pro.php within show_notification() (triggered via check_pro_notice_date / go_premium_notice_old_raters on the admin_menu hook)
src/welcome/notification-rate.php inside check_activation_date on the admin_menu hook
• No other hooks or direct calls (including REST or CLI contexts) reach the notifier
• No admin_notices or other non-admin hooks register these methods

All notifier paths are safely gated by is_admin(). No further changes needed.

src/pro.php (1)

90-93: Correct scoping: admin-only registration of admin_menu hooks

Gating the admin_menu registrations with is_admin() addresses the REST-triggered undefined-function error without changing admin behavior. Looks good.

@bfintal bfintal merged commit 45272e1 into develop Aug 20, 2025
8 of 9 checks passed
@bfintal bfintal deleted the fix/notification-error-rest-api branch August 20, 2025 04:17
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.

1 participant