-
Notifications
You must be signed in to change notification settings - Fork 0
Implement per-plan role mappings feature #2
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,6 +116,10 @@ function memberful_wp_save_postdata( $post_id ) { | |
memberful_wp_set_post_available_to_any_registered_users( $post_id, $viewable_by_any_registered_users ); | ||
|
||
$viewable_by_anybody_subscribed_to_a_plan = isset($_POST['memberful_viewable_by_anybody_subscribed_to_a_plan']) && $_POST['memberful_viewable_by_anybody_subscribed_to_a_plan'] === '1'; | ||
// Enforce mutual exclusivity at save time: if specific plans are chosen, do not allow broad access | ||
if ( !empty($subscription_plan_ids) ) { | ||
$viewable_by_anybody_subscribed_to_a_plan = false; | ||
} | ||
Comment on lines
+119
to
+122
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major 🧩 Analysis chainVerify mutual exclusivity is also enforced for term metadata. The enforcement of mutual exclusivity between specific plans and broad access is correct for posts. However, the Run the following script to confirm that term metadata save lacks this enforcement: 🏁 Script executed: #!/bin/bash
# Description: Check if term metadata save enforces mutual exclusivity
rg -n -A 5 -B 2 'memberful_wp_save_term_metadata' wordpress/wp-content/plugins/memberful-wp/src/metabox.php | grep -A 5 'viewable_by_anybody_subscribed_to_a_plan' Length of output: 0 🏁 Script executed: #!/bin/bash
rg -n 'function memberful_wp_save_term_metadata' -A 20 -B 2 wordpress/wp-content/plugins/memberful-wp/src/metabox.php Length of output: 1610 Enforce mutual exclusivity in term metadata save 🤖 Prompt for AI Agents
|
||
memberful_wp_set_post_available_to_anybody_subscribed_to_a_plan( $post_id, $viewable_by_anybody_subscribed_to_a_plan ); | ||
|
||
if(!isset($_POST['memberful_marketing_content'])) | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -16,6 +16,10 @@ public static function build(array $extra_roles_memberful_is_allowed_to_change_f | |||||||||||
); | ||||||||||||
} | ||||||||||||
|
||||||||||||
private $active_role; | ||||||||||||
private $inactive_role; | ||||||||||||
private $roles_memberful_is_allowed_to_change_from; | ||||||||||||
|
||||||||||||
public function __construct( $active_role, $inactive_role, $default_role, array $extra_roles_memberful_is_allowed_to_change_from = array() ) { | ||||||||||||
$this->active_role = $active_role; | ||||||||||||
$this->inactive_role = $inactive_role; | ||||||||||||
|
@@ -42,6 +46,40 @@ public function role_for_user($current_role, $current_subscriptions) { | |||||||||||
return $current_role; | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Check if per-plan roles are enabled | ||||||||||||
if ( memberful_wp_use_per_plan_roles() && $is_active ) { | ||||||||||||
return $this->role_for_user_with_plan_mappings( $current_subscriptions ); | ||||||||||||
} | ||||||||||||
|
||||||||||||
return $is_active ? $this->active_role : $this->inactive_role; | ||||||||||||
} | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Determine the role for a user based on their plan subscriptions | ||||||||||||
* @param array $current_subscriptions User's current subscription plans | ||||||||||||
* @return string The role to assign | ||||||||||||
*/ | ||||||||||||
private function role_for_user_with_plan_mappings( $current_subscriptions ) { | ||||||||||||
$plan_mappings = memberful_wp_get_all_plan_role_mappings(); | ||||||||||||
|
||||||||||||
// Find the highest priority role based on user's subscriptions | ||||||||||||
$assigned_roles = array(); | ||||||||||||
|
||||||||||||
foreach ( $current_subscriptions as $plan_id => $subscription_data ) { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove unused variable. The variable Apply this diff: - foreach ( $current_subscriptions as $plan_id => $subscription_data ) {
+ foreach ( $current_subscriptions as $plan_id => $_subscription_data ) {
if ( isset( $plan_mappings[ $plan_id ] ) ) {
$assigned_roles[] = $plan_mappings[ $plan_id ];
} 📝 Committable suggestion
Suggested change
🧰 Tools🪛 PHPMD (2.15.0)68-68: Avoid unused local variables such as '$subscription_data'. (undefined) (UnusedLocalVariable) 🤖 Prompt for AI Agents
|
||||||||||||
if ( isset( $plan_mappings[ $plan_id ] ) ) { | ||||||||||||
$assigned_roles[] = $plan_mappings[ $plan_id ]; | ||||||||||||
} | ||||||||||||
} | ||||||||||||
|
||||||||||||
// If user has multiple plans with different roles, we need to determine priority | ||||||||||||
if ( ! empty( $assigned_roles ) ) { | ||||||||||||
// For now, we'll use the first role found | ||||||||||||
// In the future, this could be enhanced with role priority logic | ||||||||||||
return $assigned_roles[0]; | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Fallback when per-plan roles are enabled but no mapping found | ||||||||||||
// Use the inactive role to avoid granting broad access unintentionally | ||||||||||||
return $this->inactive_role; | ||||||||||||
} | ||||||||||||
Comment on lines
+62
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reconsider the fallback role for unmapped plans. When per-plan roles are enabled but no mapping is found for a user's active subscriptions, the code falls back to Consider using Apply this diff to use the active role as fallback: - // Fallback when per-plan roles are enabled but no mapping found
- // Use the inactive role to avoid granting broad access unintentionally
- return $this->inactive_role;
+ // Fallback when per-plan roles are enabled but no mapping found
+ // Use the active role since user has active subscriptions
+ return $this->active_role; 🧰 Tools🪛 PHPMD (2.15.0)68-68: Avoid unused local variables such as '$subscription_data'. (undefined) (UnusedLocalVariable) 🤖 Prompt for AI Agents
|
||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add exit after redirect to prevent further execution.
Line 728 performs a redirect but doesn't exit, which means the script continues to execute and render the view. This can lead to unexpected behaviour and wasted resources.
Apply this diff:
🤖 Prompt for AI Agents