From 8f4d37e737c59d7505e9e97c4ae83d41bd46f63d Mon Sep 17 00:00:00 2001 From: dominiquemariano <127170846+dominiquemariano@users.noreply.github.com> Date: Fri, 19 Jan 2024 21:02:13 +0800 Subject: [PATCH 1/6] Create exclude-posts-from-user-roles.php Initial commit. Needs re-testing and modification. --- .../exclude-posts-from-user-roles.php | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 lifterlms/courses-lessons/exclude-posts-from-user-roles.php diff --git a/lifterlms/courses-lessons/exclude-posts-from-user-roles.php b/lifterlms/courses-lessons/exclude-posts-from-user-roles.php new file mode 100644 index 0000000..3990113 --- /dev/null +++ b/lifterlms/courses-lessons/exclude-posts-from-user-roles.php @@ -0,0 +1,45 @@ +/** + * 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/ + */ + +function exclude_posts_for_roles($query) { + // Check if it is the main query and the user is logged in + if (is_admin() || !$query->is_main_query() || !is_user_logged_in()) { + 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( + 98, + 109 + ); + + // Check if the user has any of the specified roles + $user_has_excluded_role = false; + foreach ($roles_to_exclude as $role) { + if (current_user_can($role)) { + $user_has_excluded_role = true; + break; + } + } + + // 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'); From e017d8714fe6590fe4e7aa36f761479c8f321386 Mon Sep 17 00:00:00 2001 From: dominiquemariano <127170846+dominiquemariano@users.noreply.github.com> Date: Fri, 19 Jan 2024 21:20:12 +0800 Subject: [PATCH 2/6] Update exclude-posts-from-user-roles.php Fixed the bugs on code. --- .../exclude-posts-from-user-roles.php | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/lifterlms/courses-lessons/exclude-posts-from-user-roles.php b/lifterlms/courses-lessons/exclude-posts-from-user-roles.php index 3990113..0997ef5 100644 --- a/lifterlms/courses-lessons/exclude-posts-from-user-roles.php +++ b/lifterlms/courses-lessons/exclude-posts-from-user-roles.php @@ -10,24 +10,31 @@ */ function exclude_posts_for_roles($query) { - // Check if it is the main query and the user is logged in - if (is_admin() || !$query->is_main_query() || !is_user_logged_in()) { + // 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' - ); + 'student', + 'editor' + ); // Replace the array with your actual post IDs to exclude $posts_to_exclude = array( - 98, - 109 - ); + 20, + 22 + ); - // Check if the user has any of the specified roles + // 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; foreach ($roles_to_exclude as $role) { if (current_user_can($role)) { From 193ab5b0d5389f2999b83ac6b039644998f66f5e Mon Sep 17 00:00:00 2001 From: shutathis Date: Tue, 23 Jan 2024 23:29:51 +0800 Subject: [PATCH 3/6] Round 1 code review. --- .../exclude-posts-from-user-roles.php | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/lifterlms/courses-lessons/exclude-posts-from-user-roles.php b/lifterlms/courses-lessons/exclude-posts-from-user-roles.php index 0997ef5..4a43261 100644 --- a/lifterlms/courses-lessons/exclude-posts-from-user-roles.php +++ b/lifterlms/courses-lessons/exclude-posts-from-user-roles.php @@ -1,8 +1,10 @@ +set('post__not_in', $posts_to_exclude); - return; - } + 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; - foreach ($roles_to_exclude as $role) { - if (current_user_can($role)) { - $user_has_excluded_role = true; - break; - } + $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) { + if ( $user_has_excluded_role ) { $query->set('post__not_in', $posts_to_exclude); } } From bd139a9a5b2f85e6d2ca32def49ee9b127c03b04 Mon Sep 17 00:00:00 2001 From: shutathis Date: Thu, 25 Jan 2024 23:03:19 +0800 Subject: [PATCH 4/6] Fixed some formatting issues. --- .../exclude-posts-from-user-roles.php | 83 ++++++++++--------- 1 file changed, 42 insertions(+), 41 deletions(-) diff --git a/lifterlms/courses-lessons/exclude-posts-from-user-roles.php b/lifterlms/courses-lessons/exclude-posts-from-user-roles.php index 4a43261..6e11264 100644 --- a/lifterlms/courses-lessons/exclude-posts-from-user-roles.php +++ b/lifterlms/courses-lessons/exclude-posts-from-user-roles.php @@ -1,4 +1,4 @@ -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); - } +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_action( 'pre_get_posts', 'exclude_posts_for_roles' ); From 06f6718d95764272144d7cc3eca5f41072fd432e Mon Sep 17 00:00:00 2001 From: shutathis Date: Thu, 25 Jan 2024 23:10:49 +0800 Subject: [PATCH 5/6] Fixed code formatting issue. --- lifterlms/courses-lessons/exclude-posts-from-user-roles.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lifterlms/courses-lessons/exclude-posts-from-user-roles.php b/lifterlms/courses-lessons/exclude-posts-from-user-roles.php index 6e11264..7c9c5d6 100644 --- a/lifterlms/courses-lessons/exclude-posts-from-user-roles.php +++ b/lifterlms/courses-lessons/exclude-posts-from-user-roles.php @@ -1,5 +1,4 @@ - Date: Tue, 6 Feb 2024 18:15:44 +0800 Subject: [PATCH 6/6] Automatically create group with group admin for every WooCommerce purchase. --- ...p-admin-when-purchased-via-woocommerce.php | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 lifterlms-groups/automatically-create-group-with-group-admin-when-purchased-via-woocommerce.php diff --git a/lifterlms-groups/automatically-create-group-with-group-admin-when-purchased-via-woocommerce.php b/lifterlms-groups/automatically-create-group-with-group-admin-when-purchased-via-woocommerce.php new file mode 100644 index 0000000..ca55146 --- /dev/null +++ b/lifterlms-groups/automatically-create-group-with-group-admin-when-purchased-via-woocommerce.php @@ -0,0 +1,61 @@ +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'); +