Skip to content

Unschedule send usage event if it is disabled#2143

Merged
Crabcyborg merged 1 commit into
masterfrom
pro-issue-4291
Nov 29, 2024
Merged

Unschedule send usage event if it is disabled#2143
Crabcyborg merged 1 commit into
masterfrom
pro-issue-4291

Conversation

@truongwp
Copy link
Copy Markdown
Contributor

Fixes https://github.com/Strategy11/formidable-pro/issues/4291

To test, you can use the WP Crontrol to see the list of scheduled events, turn on and off the Send usage feature in the Miscellaneous tab in the Global settings and check if the formidable_send_usage event is added or removed.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Nov 28, 2024

Walkthrough

The changes in this pull request involve modifications to the FrmUsageController and FrmUsage classes. The FrmUsageController class has been updated to include a new method for checking tracking permissions and to modify the scheduling logic based on this check. The FrmUsage class has been adjusted to utilize the new tracking check method from FrmUsageController, simplifying its own logic. These changes enhance the management of usage data tracking within the application.

Changes

File Path Change Summary
classes/controllers/FrmUsageController.php - Added method: public static function tracking_allowed()
- Updated method: public static function schedule_send() to include tracking check.
classes/models/FrmUsage.php - Updated method: private function tracking_allowed() to use FrmUsageController::tracking_allowed().

Possibly related PRs

  • Usage tracking v2 #2049: The changes in FrmUsageController regarding the tracking_allowed method and the scheduling logic are directly related to the modifications made in the same class in this PR, which also enhances tracking functionality and introduces new methods for managing usage data.

Suggested labels

run analysis, deploy, run tests

Suggested reviewers

  • Crabcyborg

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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.
    • Generate unit testing code for this file.
    • 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. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • 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 src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

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

Documentation and Community

  • 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
Copy Markdown
Contributor

@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: 1

🧹 Outside diff range and nitpick comments (3)
classes/controllers/FrmUsageController.php (3)

29-36: LGTM! Consider adding debug logging.

The unscheduling logic is well-implemented. For better debugging capabilities, consider adding a debug log when events are unscheduled.

 if ( $timestamp ) {
+    if ( defined('WP_DEBUG') && WP_DEBUG ) {
+        error_log('Formidable: Unscheduling usage tracking event due to disabled tracking');
+    }
     // Remove the scheduled event if it's not allowed and it's scheduled.
     wp_unschedule_event( $timestamp, 'formidable_send_usage' );
 }

29-36: Consider adding a transient to prevent rapid re-scheduling attempts.

To optimize performance and prevent unnecessary checks, consider adding a transient that tracks the scheduling state.

 $timestamp = wp_next_scheduled( 'formidable_send_usage' );
+$cache_key = 'frm_usage_schedule_check';
+if ( get_transient( $cache_key ) ) {
+    return;
+}
+set_transient( $cache_key, true, HOUR_IN_SECONDS );
+
 if ( ! self::tracking_allowed() ) {

71-71: Update the version tag with the actual version number.

The @since x.x tag should be replaced with the actual version number where this method was introduced.

- * @since x.x
+ * @since 6.16.2
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between dc734ce and 2a65e81.

📒 Files selected for processing (2)
  • classes/controllers/FrmUsageController.php (2 hunks)
  • classes/models/FrmUsage.php (1 hunks)
🔇 Additional comments (2)
classes/controllers/FrmUsageController.php (1)

29-36: Verify the implementation with WP Crontrol plugin.

The implementation looks correct, but please verify:

  1. Event is unscheduled when tracking is disabled
  2. Event remains scheduled when tracking is enabled
classes/models/FrmUsage.php (1)

451-451: LGTM! Good refactoring to centralize tracking permission logic.

The change follows good software design principles by delegating the tracking permission check to the controller class.

Let's verify the complete implementation of the tracking permission feature:

✅ Verification successful

Verified: Event unscheduling is properly implemented when tracking is disabled

The verification confirms that:

  • The tracking_allowed() check is properly implemented in FrmUsageController and used consistently
  • Event unscheduling logic is correctly implemented in FrmUsageController:
    • Checks if tracking is allowed
    • Unschedules the formidable_send_usage event when tracking is disabled
    • Only schedules the event when tracking is allowed
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of tracking permission and event scheduling

# Check FrmUsageController implementation
echo "Checking FrmUsageController implementation..."
ast-grep --pattern 'class FrmUsageController {
  $$$
  public static function tracking_allowed() {
    $$$
  }
  $$$
}'

# Check event scheduling/unscheduling logic
echo "Checking event scheduling logic..."
rg -A 5 'formidable_send_usage'

# Check if there are any other files using the tracking permission check
echo "Checking usage of tracking permission check..."
rg 'tracking_allowed'

Length of output: 3336

Comment thread classes/controllers/FrmUsageController.php
Copy link
Copy Markdown
Contributor

@Crabcyborg Crabcyborg left a comment

Choose a reason for hiding this comment

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

Thank you @truongwp!

🚀

@Crabcyborg Crabcyborg added this to the 6.16.3 milestone Nov 29, 2024
@Crabcyborg Crabcyborg merged commit 3b2841e into master Nov 29, 2024
@Crabcyborg Crabcyborg deleted the pro-issue-4291 branch November 29, 2024 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants