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

Exclude posts given their IDs from being seen by users with given WordPress roles. #8

Open
wants to merge 7 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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') ) {
Copy link
Contributor

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!

// 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 lifterlms/courses-lessons/exclude-posts-from-user-roles.php
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' );