Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: updates data api and scheduler add #7

Merged
merged 8 commits into from
Sep 18, 2024

Conversation

itsrafsanjani
Copy link
Contributor

@itsrafsanjani itsrafsanjani commented Aug 19, 2024

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Introduced a new API endpoint for managing updates within the WordPress environment.
    • Added functionality to schedule periodic updates data submission to a remote API.
  • Enhancements

    • Improved configurability by allowing dynamic API endpoint settings.
    • Enhanced error logging for API interactions to facilitate better troubleshooting and monitoring.
    • Simplified site URL structure for better integration.
    • Improved input sanitization for enhanced security.
  • Documentation

    • Updated method documentation for consistency and clarity.

Copy link

coderabbitai bot commented Aug 19, 2024

Walkthrough

The recent changes enhance the FlyWP plugin by adding a new API endpoint property for configurability, a dedicated deactivation method for lifecycle management, and an UpdatesData class for handling update data. Improvements also include refined error handling and input sanitization in API calls, contributing to a more robust framework for API interactions.

Changes

Files Change Summary
flywp.php Added $api_endpoint property and deactivate() method; updated define_constants() method.
includes/Api.php New instantiation of Api\UpdatesData; improved sanitization in get_bearer_token().
includes/Api/UpdatesData.php Introduced UpdatesData class for managing updates; added methods for data handling and cron scheduling.
includes/FlyApi.php Modified get_endpoint() to return a constant; improved error logging in post() method.
includes/Admin.php Updated get_site_url() to simplify URL structure by using only site_id; enhanced input sanitization in render_admin_page().

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant FlyWP_Plugin
    participant Api\UpdatesData

    User->>FlyWP_Plugin: Deactivate Plugin
    FlyWP_Plugin->>Api\UpdatesData: Call deactivate()
    Api\UpdatesData->>Api\UpdatesData: Unschedule Cron Event
    Api\UpdatesData-->>FlyWP_Plugin: Deactivation Complete
    FlyWP_Plugin-->>User: Plugin Deactivated
Loading

🐇 In the garden of code I hop,
With endpoints new and errors stopped.
Data flows like a gentle stream,
In FlyWP's world, we chase the dream.
A plugin reborn, sleek and bright,
Hopping high with code delight! ✨

Tip

OpenAI O1 model for chat
  • We have deployed OpenAI's latest O1 model for chat.
  • OpenAI claims that this model has superior reasoning capabilities than their GPT-4o model.
  • Please share any feedback with us in the discussions post.

Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8aec760 and 9e92b72.

Files selected for processing (2)
  • flywp.php (3 hunks)
  • includes/FlyApi.php (2 hunks)
Files skipped from review due to trivial changes (1)
  • includes/FlyApi.php
Additional comments not posted (2)
flywp.php (2)

56-56: LGTM!

Registering a deactivation hook is a good practice for performing cleanup tasks when the plugin is deactivated. The deactivate method is called appropriately.


88-95: Verify the UpdatesData class implementation.

The deactivate method is correctly defined and called when the plugin is deactivated. Instantiating the UpdatesData class and calling its deactivate method suggests that it performs necessary cleanup or state management.

Please ensure that the UpdatesData class is correctly defined and its deactivate method performs the necessary cleanup tasks.

Run the following script to verify the UpdatesData class implementation:

Verification successful

Verified: The UpdatesData class correctly implements the deactivate method.

The deactivate method is properly defined in the UpdatesData class and performs the necessary cleanup tasks:

  • It's a public method, allowing it to be called from the FlyWP_Plugin class.
  • The method unschedules any pending cron jobs associated with the plugin.

This implementation ensures proper cleanup when the plugin is deactivated, preventing any lingering scheduled tasks.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the `UpdatesData` class implementation.

# Test: Search for the class definition. Expect: The class is defined in the codebase.
rg --type php -A 10 $'class UpdatesData'

# Test: Search for the `deactivate` method definition. Expect: The method is defined in the `UpdatesData` class.
ast-grep --lang php --pattern $'class UpdatesData {
  $$$
  deactivate() {
    $$$
  }
  $$$
}'

Length of output: 747


Script:

#!/bin/bash
# Description: Search for the deactivate method in the UpdatesData class

# Test: Search for any method named 'deactivate' in the UpdatesData class
ast-grep --lang php --pattern 'class UpdatesData {
  $$$
  function deactivate($$$) {
    $$$
  }
  $$$
}'

# Test: If not found as a method, search for any occurrence of 'deactivate' in the UpdatesData class
rg --type php -A 5 -B 5 'deactivate' includes/Api/UpdatesData.php

Length of output: 546


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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.

@itsrafsanjani itsrafsanjani marked this pull request as ready for review August 19, 2024 09:56
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 3d39e44 and 9aaf4de.

Files selected for processing (4)
  • flywp.php (5 hunks)
  • includes/Api.php (2 hunks)
  • includes/Api/UpdatesData.php (1 hunks)
  • includes/FlyApi.php (3 hunks)
Additional comments not posted (16)
includes/Api.php (2)

30-30: Approved: New instantiation of Api\UpdatesData.

The addition of new Api\UpdatesData(); in the constructor enhances the initialization process by incorporating update data handling capabilities. This change aligns with the goal of expanding functionality related to API updates.


56-56: Approved: Improved input sanitization in get_bearer_token.

The use of sanitize_text_field on the HTTP authorization header enhances security by ensuring that unsanitized input is cleaned before further processing, mitigating risks associated with potentially harmful data.

includes/FlyApi.php (2)

39-39: Approved: Configurable API endpoint in get_endpoint.

The use of apply_filters with FLYWP_API_ENDPOINT allows the API endpoint to be configurable, enhancing flexibility and adaptability across different environments or deployment configurations.


94-95: Approved: Enhanced error logging in post method.

The addition of error_log( print_r( $response, true ) ); improves error handling by logging WP_Error responses, facilitating easier debugging and monitoring of API interactions.

flywp.php (3)

51-51: Approved: New $api_endpoint property.

The addition of the $api_endpoint property with a default URL enhances the configurability of the plugin by providing a centralized location for the API endpoint.


78-80: Approved: Conditional definition of FLYWP_API_ENDPOINT.

The conditional definition of FLYWP_API_ENDPOINT based on the $api_endpoint property ensures that the constant is only set if it hasn't been previously defined, promoting better constant management practices.


104-106: Approved: New deactivate method.

The addition of the deactivate method enhances lifecycle management by utilizing FlyWP\Api\UpdatesData to perform necessary cleanup or state management upon plugin deactivation.

includes/Api/UpdatesData.php (9)

9-19: Approved: Constructor setup in UpdatesData.

The constructor effectively initializes the UpdatesData class by registering a route and scheduling a cron event if not already scheduled, ensuring proper setup for managing update data.


26-32: Approved: Sending updates data to API.

The send_updates_data_to_api method is well-structured and effectively sends formatted updates data to a remote API using the flyapi instance.


39-45: Approved: JSON response handling in respond.

The respond method is correctly implemented to handle requests and return formatted updates data as a JSON response, ensuring proper API interaction.


52-58: Approved: Aggregation of updates data.

The get_formatted_updates_data method is well-structured and effectively aggregates formatted updates data for core, plugins, and themes.


65-72: Approved: Core updates data formatting.

The get_formatted_core_updates method correctly retrieves and formats core updates data, ensuring accurate information is available for further processing.


79-120: Approved: Plugin updates data formatting.

The get_formatted_plugin_updates method comprehensively retrieves and formats plugin updates data, including additional plugin information, ensuring detailed and accurate data representation.


128-156: Approved: Theme updates data formatting.

The get_formatted_theme_updates method effectively gathers and formats theme updates data, ensuring comprehensive and accurate information is available for further processing.


164-187: Approved: Core updates check and formatting.

The core_updates method is well-implemented to check for WordPress core updates and format the response, ensuring accurate update status is available.


192-196: Approved: Scheduler deactivation.

The deactivate method effectively handles the deactivation of scheduled events, ensuring proper cleanup when the plugin is deactivated.

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9aaf4de and ec206ee.

Files selected for processing (1)
  • includes/Api/UpdatesData.php (1 hunks)
Files skipped from review due to trivial changes (1)
  • includes/Api/UpdatesData.php

Copy link
Member

@alaminfirdows alaminfirdows left a comment

Choose a reason for hiding this comment

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

LGTM

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: 12

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between ec206ee and 0cc8b74.

Files selected for processing (1)
  • includes/Api/UpdatesData.php (1 hunks)
Additional context used
GitHub Check: Run PHPCS inspection
includes/Api/UpdatesData.php

[failure] 20-20:
Method name "initializeRoutes" in class UpdatesData is not in snake case format, try "initialize_routes"


[failure] 27-27:
Method name "initializeCronJob" in class UpdatesData is not in snake case format, try "initialize_cron_job"


[failure] 38-38:
Method name "sendUpdatesDataToApi" in class UpdatesData is not in snake case format, try "send_updates_data_to_api"


[failure] 39-39:
Variable "$updatesData" is not in valid snake_case format, try "$updates_data"


[failure] 40-40:
Variable "$updatesData" is not in valid snake_case format, try "$updates_data"


[failure] 46-46:
Method name "handleApiRequest" in class UpdatesData is not in snake case format, try "handle_api_request"


[failure] 55-55:
Method name "getUpdatesData" in class UpdatesData is not in snake case format, try "get_updates_data"


[failure] 67-67:
Method name "getFormattedUpdatesData" in class UpdatesData is not in snake case format, try "get_formatted_updates_data"


[failure] 80-80:
Method name "getFormattedCoreUpdates" in class UpdatesData is not in snake case format, try "get_formatted_core_updates"


[failure] 81-81:
Variable "$coreData" is not in valid snake_case format, try "$core_data"

Additional comments not posted (3)
includes/Api/UpdatesData.php (3)

12-15: Constructor Initialization Looks Good!

The constructor correctly initializes routes and cron jobs.


229-236: Loading Required Files Looks Good!

The method correctly loads necessary WordPress files if they are not already loaded.


241-246: Deactivation Method Looks Good!

The method correctly unschedules the cron job when the plugin is deactivated.

includes/Api/UpdatesData.php Outdated Show resolved Hide resolved
includes/Api/UpdatesData.php Outdated Show resolved Hide resolved
includes/Api/UpdatesData.php Outdated Show resolved Hide resolved
includes/Api/UpdatesData.php Outdated Show resolved Hide resolved
includes/Api/UpdatesData.php Outdated Show resolved Hide resolved
includes/Api/UpdatesData.php Outdated Show resolved Hide resolved
includes/Api/UpdatesData.php Outdated Show resolved Hide resolved
includes/Api/UpdatesData.php Outdated Show resolved Hide resolved
includes/Api/UpdatesData.php Outdated Show resolved Hide resolved
includes/Api/UpdatesData.php Outdated Show resolved Hide resolved
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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 0cc8b74 and 08bea45.

Files selected for processing (1)
  • includes/Api/UpdatesData.php (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • includes/Api/UpdatesData.php

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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 08bea45 and f9da23a.

Files selected for processing (1)
  • includes/Admin.php (1 hunks)
Additional comments not posted (1)
includes/Admin.php (1)

148-150: Approve URL format change with suggestions.

The URL format change simplifies the structure and aligns with the AI-generated summary. However, consider the following suggestions:

  1. Add validation for the id key in the $info array to prevent potential errors.

Consider adding a validation check:

 private function get_site_url( $info ) {
     if ( false === $info ) {
         return 'https://app.flywp.com';
     }
 
+    if ( ! isset( $info['id'] ) || ! is_numeric( $info['id'] ) ) {
+        return 'https://app.flywp.com';
+    }
+
     return sprintf(
         'https://app.flywp.com/site/%d',
         $info['id']
     );
 }

To ensure that all occurrences of the old URL format have been updated, run the following script:

If the old URL format is found, please update those occurrences to use the new format or the get_site_url method.

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: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f9da23a and 8aec760.

Files selected for processing (1)
  • includes/Admin.php (3 hunks)
Additional comments not posted (1)
includes/Admin.php (1)

149-151: Simplified site URL format.

The URL format has been simplified from https://app.flywp.com/servers/%d/sites/%d to https://app.flywp.com/site/%d.

Please ensure that this new URL format is consistent with any changes in the FlyWP app's architecture or API. You may want to run the following script to check for any other occurrences of the old URL format in the codebase:

If any matches are found, they may need to be updated to the new format as well.

Verification successful

Old URL format successfully removed or updated.

The search did not find any occurrences of the old URL format or its variations in the codebase. This indicates that the transition to the new URL format is complete and consistent across the codebase. No further action is required.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Search for occurrences of the old URL format

# Test: Search for the old URL format. Expect: No matches if all occurrences have been updated.
rg --type php 'https://app\.flywp\.com/servers/\d+/sites/\d+'

Length of output: 63


Script:

#!/bin/bash
# Description: Search for variations of the old URL format

# Test: Search for any similar patterns to the old URL format. Expect: No matches if all occurrences have been updated.
rg --type php 'https://app\.flywp\.com/servers/\d+/sites/\d*'

Length of output: 63

includes/Admin.php Show resolved Hide resolved
includes/Admin.php Show resolved Hide resolved
@itsrafsanjani
Copy link
Contributor Author

@Rat01047 bhai, need to this plugin. Manually pull from git and use composer.

Copy link
Member

@alaminfirdows alaminfirdows left a comment

Choose a reason for hiding this comment

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

LGTM

@alaminfirdows alaminfirdows merged commit 0aa2360 into develop Sep 18, 2024
1 check passed
@alaminfirdows alaminfirdows deleted the feat/updates-data-api-and-scheduler branch September 18, 2024 10:03
@alaminfirdows alaminfirdows mentioned this pull request Sep 18, 2024
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.

2 participants