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

Add Traffic Boost permissions infrastructure #2939

Merged

Conversation

acicovic
Copy link
Collaborator

@acicovic acicovic commented Nov 13, 2024

Description

This PR adds the Traffic Boost permissions infrastructure.

Permissions can be changed in the plugin's settings page, and (to facilitate manual testing) the Traffic Boost enabled status is currently shown in our dashboard page.

Motivation and context

Being able to set permissions for Traffic Boost, like for other PCH features.

How has this been tested?

  • Manually tested by changing the settings and visiting the dashboard page.
  • Our permissions integration test has been updated to check for Traffic Boost.

Screenshots

image image

Summary by CodeRabbit

  • New Features
    • Introduced a new "Traffic Boost" feature in the dashboard and settings page, allowing users to enable or disable it based on permissions.
    • Added a checkbox for "Traffic Boost" in the admin settings for easier management.
  • Bug Fixes
    • Enhanced permission checks for the new "Traffic Boost" feature to ensure proper access control.
  • Tests
    • Expanded integration tests to include validation for the new "Traffic Boost" feature.

@acicovic acicovic added this to the 3.18.0 milestone Nov 13, 2024
@acicovic acicovic self-assigned this Nov 13, 2024
Copy link
Contributor

coderabbitai bot commented Nov 13, 2024

📝 Walkthrough

Walkthrough

The changes in this pull request introduce a new feature called traffic_boost across multiple components of the application. This includes updates to the Dashboard_Page and Settings_Page classes to manage user permissions and settings for the feature. The Parsely class is modified to integrate the traffic_boost option into its configuration, while the Permissions class is enhanced to check user access for this feature. Additionally, JavaScript and test files are updated to support the new functionality and ensure proper validation and permissions testing.

Changes

File Change Summary
src/UI/class-dashboard-page.php Added private property $parsely and constructor to accept Parsely instance; modified add_dashboard_page_placeholder for traffic_boost permission check.
src/UI/class-settings-page.php Added traffic_boost to Parsely_Settings_Options_Content_Helper; registered new settings field in initialize_content_helper_section.
src/class-parsely.php Updated Parsely_Options_Content_Helper to include traffic_boost option; modified option_defaults to initialize this option.
src/class-permissions.php Enhanced get_pch_permissions_json and build_pch_permissions_settings_array methods to include TrafficBoost feature.
src/js/admin-settings.ts Added checkbox for traffic_boost in feature management; updated event handlers to include this checkbox.
tests/Integration/PermissionsTest.php Added traffic_boost to $features_to_test array for permission validation.
tests/Integration/UI/SettingsPageTest.php Updated transform_pch_options_for_validation to include traffic_boost in validation process.
wp-parsely.php Updated instantiation of Dashboard_Page to accept $GLOBALS['parsely'] as a parameter.

Possibly related PRs

Suggested labels

Changelog: Added

Suggested reviewers

  • vaurdan

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.

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.

@acicovic acicovic marked this pull request as ready for review November 13, 2024 08:12
@acicovic acicovic requested a review from a team as a code owner November 13, 2024 08:12
Copy link
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)
src/class-permissions.php (2)

165-165: Document the new TrafficBoost feature mapping.

The TrafficBoost feature mapping follows the existing pattern and is well-integrated. However, consider updating the class-level PHPDoc to document this new feature.

Add a description of the TrafficBoost feature in the class documentation:

 /**
  * Class implementing user/role permissions functionality.
  *
  * @since 3.16.0
  *
  * @phpstan-import-type Parsely_Options_Content_Helper from Parsely
  * @phpstan-import-type Parsely_Options_Content_Helper_Feature from Parsely
+ *
+ * Supported features:
+ * - SmartLinking: Enables smart linking functionality
+ * - TitleSuggestions: Provides title suggestions
+ * - ExcerptSuggestions: Provides excerpt suggestions
+ * - TrafficBoost: Controls traffic boost functionality
  */

209-212: LGTM! Consider improving type hints.

The traffic_boost settings implementation follows the existing pattern and maintains consistency with other features.

Consider adding a more specific PHPDoc type for the settings array structure:

/**
 * @phpstan-type Parsely_Feature_Settings array{
 *   enabled: bool,
 *   allowed_user_roles: array<int, string>
 * }
 * @phpstan-type Parsely_Options_Content_Helper array{
 *   ai_features_enabled: bool,
 *   smart_linking: Parsely_Feature_Settings,
 *   title_suggestions: Parsely_Feature_Settings,
 *   excerpt_suggestions: Parsely_Feature_Settings,
 *   traffic_boost: Parsely_Feature_Settings
 * }
 */
tests/Integration/UI/SettingsPageTest.php (1)

753-753: LGTM! Consider adding a period to the inline comment.

The addition of traffic_boost to the content helper options is consistent with the existing pattern and properly implements the permissions infrastructure.

Following WordPress coding standards, consider adding a period to the end of the inline comment:

-		$options['content_helper']['traffic_boost']['allowed_user_roles']       = array( 'administrator' => true );
+		$options['content_helper']['traffic_boost']['allowed_user_roles']       = array( 'administrator' => true ); // Transform traffic_boost options for validation.
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between a6f1b07 and 212bc17.

⛔ Files ignored due to path filters (2)
  • build/admin-settings.asset.php is excluded by !build/**
  • build/admin-settings.js is excluded by !build/**
📒 Files selected for processing (8)
  • src/UI/class-dashboard-page.php (3 hunks)
  • src/UI/class-settings-page.php (3 hunks)
  • src/class-parsely.php (2 hunks)
  • src/class-permissions.php (2 hunks)
  • src/js/admin-settings.ts (1 hunks)
  • tests/Integration/PermissionsTest.php (1 hunks)
  • tests/Integration/UI/SettingsPageTest.php (1 hunks)
  • wp-parsely.php (1 hunks)
🧰 Additional context used
📓 Path-based instructions (8)
src/UI/class-dashboard-page.php (1)

Pattern **/*.{html,php}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the HTML and PHP code to ensure it is well-structured and adheres to best practices.
  • Ensure the code follows WordPress coding standards and is well-documented.
  • Confirm the code is secure and free from vulnerabilities.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Verify code compatibility with the latest version of WordPress, avoiding deprecated functions or features."
src/UI/class-settings-page.php (1)

Pattern **/*.{html,php}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the HTML and PHP code to ensure it is well-structured and adheres to best practices.
  • Ensure the code follows WordPress coding standards and is well-documented.
  • Confirm the code is secure and free from vulnerabilities.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Verify code compatibility with the latest version of WordPress, avoiding deprecated functions or features."
src/class-parsely.php (1)

Pattern **/*.{html,php}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the HTML and PHP code to ensure it is well-structured and adheres to best practices.
  • Ensure the code follows WordPress coding standards and is well-documented.
  • Confirm the code is secure and free from vulnerabilities.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Verify code compatibility with the latest version of WordPress, avoiding deprecated functions or features."
src/class-permissions.php (1)

Pattern **/*.{html,php}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the HTML and PHP code to ensure it is well-structured and adheres to best practices.
  • Ensure the code follows WordPress coding standards and is well-documented.
  • Confirm the code is secure and free from vulnerabilities.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Verify code compatibility with the latest version of WordPress, avoiding deprecated functions or features."
src/js/admin-settings.ts (1)

Pattern **/*.{js,ts,tsx,jsx}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the code to ensure it is well-structured and adheres to best practices.
  • Verify compliance with WordPress coding standards.
  • Ensure the code is well-documented.
  • Check for security vulnerabilities and confirm the code is secure.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate JSDoc comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Confirm every JSDoc comment includes a @SInCE tag indicating the next version of the plugin to include the code.
  • Guarantee compatibility with the latest version of WordPress, avoiding deprecated functions or features."
tests/Integration/PermissionsTest.php (1)

Pattern **/*.{html,php}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the HTML and PHP code to ensure it is well-structured and adheres to best practices.
  • Ensure the code follows WordPress coding standards and is well-documented.
  • Confirm the code is secure and free from vulnerabilities.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Verify code compatibility with the latest version of WordPress, avoiding deprecated functions or features."
tests/Integration/UI/SettingsPageTest.php (1)

Pattern **/*.{html,php}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the HTML and PHP code to ensure it is well-structured and adheres to best practices.
  • Ensure the code follows WordPress coding standards and is well-documented.
  • Confirm the code is secure and free from vulnerabilities.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Verify code compatibility with the latest version of WordPress, avoiding deprecated functions or features."
wp-parsely.php (1)

Pattern **/*.{html,php}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the HTML and PHP code to ensure it is well-structured and adheres to best practices.
  • Ensure the code follows WordPress coding standards and is well-documented.
  • Confirm the code is secure and free from vulnerabilities.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Verify code compatibility with the latest version of WordPress, avoiding deprecated functions or features."
🔇 Additional comments (10)
wp-parsely.php (2)

112-112: Verify Dashboard_Page constructor implementation.

Ensure that the Dashboard_Page constructor properly handles the Parsely instance parameter.

#!/bin/bash
# Check Dashboard_Page constructor implementation
ast-grep --pattern 'class Dashboard_Page {
  $$$
  function __construct($parsely) {
    $$$
  }
  $$$
}'

112-112: LGTM! Verify backward compatibility.

The update to pass the Parsely instance to Dashboard_Page is correct and aligns with the Traffic Boost permissions infrastructure requirements.

Let's verify backward compatibility with older versions:

✅ Verification successful

No backward compatibility concerns - change is safe

The Dashboard_Page class has been designed from the start (since version 3.18.0) to require a Parsely instance in its constructor, and there's only one instantiation of this class in the entire codebase. The change in wp-parsely.php correctly aligns with the existing constructor signature.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any other instantiations of Dashboard_Page that might need updating
rg "new\s+Dashboard_Page\s*\(" --type php

Length of output: 106


Script:

#!/bin/bash
# Let's try a more comprehensive search for Dashboard_Page instantiation
rg -i "dashboard_page" --type php -A 2 -B 2

# Also search for the class definition to understand constructor
ast-grep --pattern 'class Dashboard_Page {
  $$$
  function __construct($_) {
    $$$
  }
  $$$
}'

Length of output: 5215


Script:

#!/bin/bash
# Let's check the constructor implementation in Dashboard_Page
rg -A 10 "class Dashboard_Page" src/UI/class-dashboard-page.php

# Also check git history for any recent changes to the constructor
git log -p src/UI/class-dashboard-page.php | rg -A 5 -B 5 "__construct"

Length of output: 940

src/js/admin-settings.ts (1)

72-73: LGTM: Traffic Boost checkbox selector added correctly.

The addition of the Traffic Boost checkbox selector follows the established pattern and maintains consistency with other feature selectors.

src/UI/class-dashboard-page.php (1)

25-41: LGTM! Well-structured property and constructor implementation.

The code follows WordPress coding standards with proper type declarations and comprehensive PHPDoc documentation.

tests/Integration/PermissionsTest.php (1)

48-48: LGTM! Comprehensive test coverage for Traffic Boost permissions.

The addition of 'traffic_boost' to $features_to_test ensures that the new feature will be automatically tested across all permission scenarios, including:

  • Enabled/disabled feature states
  • Allowed/disallowed user roles
  • Super admin access
  • Filter hook behavior
  • Invalid configurations

Let's verify the test coverage:

✅ Verification successful

Verified: Traffic Boost feature is thoroughly tested across all permission scenarios

The test coverage is comprehensive as the traffic_boost feature will be automatically tested in all test methods through the $features_to_test array:

  • Super admin access verification
  • Editor role permissions check
  • Disabled feature state testing
  • Multisite environment handling
  • Invalid configuration testing
  • Permission denial scenarios
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that all test methods handle the traffic_boost feature

# Test: Search for test methods that will process traffic_boost. Expect: Multiple test methods.
rg -B 2 -A 2 'foreach.*features_to_test' tests/Integration/PermissionsTest.php

Length of output: 902

src/class-parsely.php (2)

56-57: LGTM: PHPDoc type definition is correct and consistent.

The addition of traffic_boost to the Parsely_Options_Content_Helper type definition follows the established pattern and correctly references the Parsely_Options_Content_Helper_Feature type.


132-135: LGTM: Option defaults are properly configured.

The addition of traffic_boost to the content helper options maintains consistency with other features:

  • Default enabled state is set to true
  • Default user role is set to administrator
  • Follows the same structure and indentation as other feature configurations
src/UI/class-settings-page.php (3)

75-75: LGTM: Type definition addition is consistent and well-documented.

The new traffic_boost type follows the established pattern for Content Helper features.


122-122: LGTM: Feature addition is consistent with existing pattern.

The traffic_boost feature is properly added to the configurable features array.


511-526: LGTM: Settings field registration is well-implemented.

The Traffic Boost settings field is properly registered with:

  • Consistent implementation pattern
  • Proper sanitization and escaping
  • Internationalization support

src/UI/class-dashboard-page.php Show resolved Hide resolved
Copy link
Contributor

@vaurdan vaurdan left a comment

Choose a reason for hiding this comment

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

Looks good! I believe we should limit only the "Traffic Boost" functionality if disabled, by removing that submenu item. But we can extend that once we start implementing TB.

@acicovic
Copy link
Collaborator Author

Yes, we'll see how we handle TB disablement once we have more elements.

Base automatically changed from add/plugin-dashboard-page to add/traffic-boost November 13, 2024 12:32
@acicovic acicovic merged commit 41465de into add/traffic-boost Nov 13, 2024
38 checks passed
@acicovic acicovic deleted the add/traffic-boost-permissions-infrastructure branch November 13, 2024 12:32
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