-
Notifications
You must be signed in to change notification settings - Fork 5
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
Exclude posts given their IDs from being seen by users with given WordPress roles. #8
Open
dominiquemariano
wants to merge
7
commits into
gocodebox:trunk
Choose a base branch
from
dominiquemariano:trunk
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8f4d37e
Create exclude-posts-from-user-roles.php
dominiquemariano e017d87
Update exclude-posts-from-user-roles.php
dominiquemariano 193ab5b
Round 1 code review.
shutathis bd139a9
Fixed some formatting issues.
shutathis 06f6718
Fixed code formatting issue.
shutathis 2f968f7
Merge branch 'gocodebox:trunk' into trunk
dominiquemariano 5e9730e
Automatically create group with group admin for every WooCommerce pur…
shutathis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
...lms-groups/automatically-create-group-with-group-admin-when-purchased-via-woocommerce.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php // Do not copy this line! | ||
/** | ||
* Create a LifterLMS Group for every WooCommerce order. Set the number of seats | ||
* to be equal to the "quantity" of items bought in WooCommerce. | ||
* | ||
* You can add this recipe to your site by creating a custom plugin | ||
* or using the Code Snippets plugin available for free in the WordPress repository. | ||
* Read this companion documentation for step-by-step directions on either method. | ||
* https://lifterlms.com/docs/adding-custom-code/ | ||
* | ||
* @param int $order_id The WooCommerce order ID. | ||
* | ||
* @return void | ||
*/ | ||
function create_llms_group_with_admin($order_id) | ||
{ | ||
if (class_exists('LLMS_Group') && class_exists('LLMS_Groups_Enrollment') ) { | ||
// Get integration. | ||
$integration = llms_groups()->get_integration(); | ||
$name = $integration->get_option('post_name_singular'); | ||
$visibility = $integration->get_option('visibility') | ||
|
||
// Setup arguments. | ||
$args = wp_parse_args( | ||
$args, | ||
array( | ||
'post_status' => 'publish', | ||
'post_title' => sprintf(__('New %s', 'lifterlms-groups'), $name), | ||
'meta_input' => array( | ||
'_llms_visibility' => $visibility, | ||
), | ||
) | ||
); | ||
|
||
// Create the group. | ||
$group = new LLMS_Group('new', $args); | ||
|
||
// Assign group administrator. | ||
$order = wc_get_order($order_id); | ||
LLMS_Groups_Enrollment::add( | ||
$order->get_user_id(), | ||
$group->get('id'), | ||
'woocommerce_order', | ||
'admin' | ||
); | ||
|
||
$order_data = $order->get_data(); | ||
|
||
// Assign the number of seats in the group. | ||
// You're developer can modify this logic to be as custom as you want. | ||
foreach ($order->get_items() as $item_key => $item ) { | ||
$product = wc_get_product($item['product_id']); | ||
$quantity = $item->get_quantity(); | ||
$group->set('seats', $quantity); | ||
} | ||
} | ||
} | ||
|
||
add_action('woocommerce_order_status_completed', 'create_llms_group_with_admin'); | ||
add_action('woocommerce_order_status_processing', 'create_llms_group_with_admin'); | ||
|
54 changes: 54 additions & 0 deletions
54
lifterlms/courses-lessons/exclude-posts-from-user-roles.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php // Do not copy this line! | ||
/** | ||
* Exclude posts (given their IDs) from being seen by users given a list of roles. | ||
* | ||
* Learn more at: https://lifterlms.com/link-to-content-if-available-or-remove-this-line/ | ||
* | ||
* You can add this recipe to your site by creating a custom plugin | ||
* or using the Code Snippets plugin available for free in the WordPress repository. | ||
* Read this companion documentation for step-by-step directions on either method. | ||
* https://lifterlms.com/docs/adding-custom-code/ | ||
* | ||
* @param WP_Query $query The WP_Query to modify. | ||
*/ | ||
function exclude_posts_for_roles( $query ) { | ||
// Do not exclude when the user is the WordPress administrator. | ||
if ( is_admin() ) { | ||
return; | ||
} | ||
|
||
// Replace the array with your actual roles you want. | ||
$roles_to_exclude = array( | ||
'student', | ||
'editor', | ||
); | ||
|
||
// Replace the array with your actual post IDs to exclude. | ||
$posts_to_exclude = array( | ||
20, | ||
22, | ||
); | ||
|
||
// If the user is not logged in, we cannot check their role. | ||
// So exclude the posts from non-logged in visitors. | ||
if ( ! is_user_logged_in() ) { | ||
$query->set( 'post__not_in', $posts_to_exclude ); | ||
return; | ||
} | ||
|
||
// Check if the logged-in user has any of the specified roles. | ||
$user_has_excluded_role = false; | ||
$user = wp_get_current_user(); | ||
$user_roles = (array) $user->roles; | ||
$intersect = array_intersect( $user_roles, $roles_to_exclude ); | ||
if ( ! empty( $intersect ) ) { | ||
$user_has_excluded_role = true; | ||
} | ||
|
||
// If the user has any of the specified roles, exclude the specific posts. | ||
if ( $user_has_excluded_role ) { | ||
$query->set( 'post__not_in', $posts_to_exclude ); | ||
} | ||
} | ||
|
||
add_action( 'pre_get_posts', 'exclude_posts_for_roles' ); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
It's not critical, but possible to add spacing before/after brackets here and elsewhere? Thanks!