Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/wp-user-avatar/src/Admin/SettingsPages/AbstractSettingsPage.php
+++ b/wp-user-avatar/src/Admin/SettingsPages/AbstractSettingsPage.php
@@ -66,6 +66,8 @@
<div class="ppress-admin-banner__logo">
<img src="<?= $logo_url ?>" alt="">
</div>
+ <?php // users who only manage their courses (course instructors) can't use the addons and upgrade pages. ?>
+ <?php if (current_user_can('manage_options')) : ?>
<div class="ppress-admin-banner__helplinks">
<span><a rel="noopener" href="<?= PPRESS_EXTENSIONS_SETTINGS_PAGE ?>">
<span class="dashicons dashicons-star-filled"></span> <?= __('Addons', 'wp-user-avatar'); ?>
@@ -83,6 +85,7 @@
<span class="dashicons dashicons-book"></span> <?= __('Docs', 'wp-user-avatar'); ?>
</a></span>
</div>
+ <?php endif; ?>
<div class="clear"></div>
<?php $this->settings_page_header_menus($active_menu); ?>
</div>
@@ -129,6 +132,9 @@
if (count($submenus) < 2) return;
+ // e.g. an unknown ?view= in the URL.
+ if ( ! isset(self::$parent_menu_url_map[$active_menu])) return;
+
$active_menu_url = self::$parent_menu_url_map[$active_menu];
$submenus = wp_list_filter($submenus, ['parent' => $active_menu]);
--- a/wp-user-avatar/src/Admin/SettingsPages/GeneralSettings.php
+++ b/wp-user-avatar/src/Admin/SettingsPages/GeneralSettings.php
@@ -282,6 +282,40 @@
'<code>[profilepress-checkout]</code>', '<br>'
),
],
+ ProfilePressCoreMembershipCheckoutPlanGrid::SETTING => [
+ 'type' => 'select',
+ 'label' => esc_html__('Checkout Without a Plan', 'wp-user-avatar'),
+ 'options' => [
+ ProfilePressCoreMembershipCheckoutPlanGrid::SHOW_ALL => esc_html__('Show all active plans', 'wp-user-avatar'),
+ ProfilePressCoreMembershipCheckoutPlanGrid::SHOW_SELECTED => esc_html__('Show only the plans selected below', 'wp-user-avatar'),
+ ProfilePressCoreMembershipCheckoutPlanGrid::SHOW_MESSAGE => esc_html__('Show the "Your cart is empty" message', 'wp-user-avatar'),
+ ],
+ 'value' => ProfilePressCoreMembershipCheckoutPlanGrid::SHOW_ALL,
+ 'description' => esc_html__('What visitors see when they open the checkout page without choosing a plan first. Plans in a plan group share one card.', 'wp-user-avatar'),
+ ],
+ ProfilePressCoreMembershipCheckoutPlanGrid::SETTING_LAYOUT => [
+ 'type' => 'select',
+ 'label' => esc_html__('Plans Layout', 'wp-user-avatar'),
+ 'options' => [
+ ProfilePressCoreMembershipCheckoutPlanGrid::LAYOUT_GRID => esc_html__('Grid', 'wp-user-avatar'),
+ ProfilePressCoreMembershipCheckoutPlanGrid::LAYOUT_LIST => esc_html__('List', 'wp-user-avatar'),
+ ],
+ 'value' => ProfilePressCoreMembershipCheckoutPlanGrid::LAYOUT_GRID,
+ 'description' => esc_html__('How the plans are laid out: Grid (cards side by side) or List (one row per plan, easier to compare when there are many).', 'wp-user-avatar'),
+ ],
+ ProfilePressCoreMembershipCheckoutPlanGrid::SETTING_PLANS => [
+ 'type' => 'select2',
+ 'label' => esc_html__('Plans to Show', 'wp-user-avatar'),
+ 'options' => (function () {
+ $bucket = [];
+ foreach (ProfilePressCoreMembershipRepositoriesPlanRepository::init()->retrieveAll(0, 1, 'true') as $plan) {
+ $bucket[$plan->get_id()] = $plan->get_name();
+ }
+
+ return $bucket;
+ })(),
+ 'description' => esc_html__('Used when Checkout Without a Plan is set to show only the selected plans.', 'wp-user-avatar'),
+ ],
'payment_success_page_id' => [
'type' => 'custom_field_block',
'label' => esc_html__('Order Success Page', 'wp-user-avatar'),
--- a/wp-user-avatar/src/Admin/SettingsPages/Membership/CustomersPage/SettingsPage.php
+++ b/wp-user-avatar/src/Admin/SettingsPages/Membership/CustomersPage/SettingsPage.php
@@ -242,8 +242,7 @@
'payment_method' => ppress_var($customer_data, 'order_payment_method', ''),
'transaction_id' => ppress_var($customer_data, 'order_transaction_id', ''),
'date_created' => $date_created
- ],
- ppress_var($customer_data, 'order_send_receipt') == 'true'
+ ]
);
if (is_wp_error($response)) {
@@ -443,12 +442,6 @@
'type' => 'text',
'class' => 'ppress_datepicker',
'description' => esc_html__("Enter the purchase date, or leave blank for today's date.", 'wp-user-avatar')
- ],
- 'order_send_receipt' => [
- 'label' => __('Send Receipt', 'wp-user-avatar'),
- 'checkbox_label' => __('Check to send the order receipt to the customer.', 'wp-user-avatar'),
- 'type' => 'checkbox',
- 'default_value' => 'true'
]
]);
@@ -594,7 +587,7 @@
}
}).trigger('change');
- var orderRows = "#order_plan_row, #order_amount_row, #order_status_row, #order_payment_method_row, #order_transaction_id_row, #order_date_row, #order_send_receipt_row";
+ var orderRows = "#order_plan_row, #order_amount_row, #order_status_row, #order_payment_method_row, #order_transaction_id_row, #order_date_row";
$("#create_order").on("change", function () {
$(this).is(":checked") ? $(orderRows).show() : $(orderRows).hide();
}).trigger('change');
--- a/wp-user-avatar/src/Admin/SettingsPages/Membership/OrdersPage/SettingsPage.php
+++ b/wp-user-avatar/src/Admin/SettingsPages/Membership/OrdersPage/SettingsPage.php
@@ -402,8 +402,7 @@
'payment_method' => $order_data['payment_method'],
'transaction_id' => $order_data['transaction_id'],
'date_created' => $date_created
- ],
- $order_data['send_receipt'] == 'true'
+ ]
);
if (is_wp_error($response)) {
@@ -542,12 +541,6 @@
'type' => 'text',
'class' => 'ppress_datepicker',
'description' => esc_html__("Enter the purchase date, or leave blank for today's date.", 'wp-user-avatar')
- ],
- 'send_receipt' => [
- 'label' => __('Send Receipt', 'wp-user-avatar'),
- 'checkbox_label' => __('Check to send the order receipt to the customer.', 'wp-user-avatar'),
- 'type' => 'checkbox',
- 'default_value' => 'true'
]
]
]);
--- a/wp-user-avatar/src/Admin/SettingsPages/Membership/views/add-edit-plan-group.php
+++ b/wp-user-avatar/src/Admin/SettingsPages/Membership/views/add-edit-plan-group.php
@@ -79,8 +79,8 @@
<input type="text" id="ppress-group-checkout-url" onfocus="this.select();" readonly="readonly" value="<?= esc_url($group_data->get_checkout_url()) ?>" style="width: 100%;"/>
</p>
<p style="display: flex; align-items: center; gap: 10px;">
- <button type="button" class="button" id="ppress-copy-url-btn"><?php esc_html_e('Copy URL', 'wp-user-avatar'); ?></button>
- <span id="ppress-copy-msg" style="color: green; display: none;"></span>
+ <button type="button" class="button ppress-copy-btn" data-copy="#ppress-group-checkout-url"><?php esc_html_e('Copy URL', 'wp-user-avatar'); ?></button>
+ <span class="ppress-copy-msg" style="color: green; display: none;"></span>
</p>
</div>
<?php
@@ -88,6 +88,52 @@
'ppmembershipgroup',
'sidebar'
);
+
+ add_meta_box(
+ 'ppress-plan-group-shortcode',
+ __('Pricing Section Shortcode', 'wp-user-avatar'),
+ function () use ($group_data) {
+ ?>
+ <div class="ppress-subscription-plan-payment-links">
+ <p><?php esc_html_e("Shows this group's plans side by side on any page, each with a button to its checkout.", 'wp-user-avatar'); ?></p>
+ <p>
+ <input type="text" id="ppress-group-shortcode" onfocus="this.select();" readonly="readonly" value="<?= esc_attr(sprintf('[profilepress-plans group="%d"]', $group_data->get_id())) ?>" style="width: 100%;"/>
+ </p>
+ <p style="display: flex; align-items: center; gap: 10px;">
+ <button type="button" class="button ppress-copy-btn" data-copy="#ppress-group-shortcode"><?php esc_html_e('Copy Shortcode', 'wp-user-avatar'); ?></button>
+ <span class="ppress-copy-msg" style="color: green; display: none;"></span>
+ </p>
+ <div class="description ppress-shortcode-options">
+ <p><?php esc_html_e('Options you can add to it:', 'wp-user-avatar'); ?></p>
+ <ul style="list-style: disc; margin-left: 18px;">
+ <li><?php printf(
+ /* translators: 1: layout="grid", 2: layout="list", 3: name of the Plans Layout setting */
+ esc_html__('%1$s shows cards side by side; %2$s shows one row per plan. Without it, the shortcode uses the %3$s setting (Settings → General → Payment Pages), which is Grid unless you changed it. So if the plans come out as a list, add %1$s.', 'wp-user-avatar'),
+ '<code>layout="grid"</code>', '<code>layout="list"</code>', '<strong>' . esc_html__('Plans Layout', 'wp-user-avatar') . '</strong>'
+ ); ?></li>
+ <li><?php printf(
+ /* translators: %s: columns="3" */
+ esc_html__('%s sets how many cards sit in a row, from 1 to 4. It only applies to the grid; without it, as many fit as there is room for. On phones the cards always stack.', 'wp-user-avatar'),
+ '<code>columns="3"</code>'
+ ); ?></li>
+ <li><?php printf(
+ /* translators: %s: heading="yes" */
+ esc_html__('%s adds a "Choose a plan" heading and a short intro above the plans.', 'wp-user-avatar'),
+ '<code>heading="yes"</code>'
+ ); ?></li>
+ </ul>
+ <p><?php printf(
+ /* translators: %s: example shortcode */
+ esc_html__('Example: %s', 'wp-user-avatar'),
+ '<code>' . esc_html(sprintf('[profilepress-plans group="%d" layout="grid" columns="3" heading="yes"]', $group_data->get_id())) . '</code>'
+ ); ?></p>
+ </div>
+ </div>
+ <?php
+ },
+ 'ppmembershipgroup',
+ 'sidebar'
+ );
}
});
@@ -111,9 +157,9 @@
<script type="text/javascript">
(function ($) {
- $('#ppress-copy-url-btn').on('click', function () {
- var input = $('#ppress-group-checkout-url')[0];
- var msg = $('#ppress-copy-msg');
+ $('.ppress-copy-btn').on('click', function () {
+ var input = $($(this).data('copy'))[0];
+ var msg = $(this).siblings('.ppress-copy-msg');
input.select();
input.setSelectionRange(0, 99999); // For mobile
--- a/wp-user-avatar/src/Admin/SettingsPages/Membership/views/customers/data-metabox.php
+++ b/wp-user-avatar/src/Admin/SettingsPages/Membership/views/customers/data-metabox.php
@@ -127,7 +127,7 @@
sprintf(
'[edit-profile-cpf id="%1$s" key="%1$s" value="%2$s"]',
CF::BILLING_ADDRESS,
- strip_shortcodes(esc_attr(get_user_meta($user_id, CF::BILLING_ADDRESS, true)))
+ ppress_neutralize_shortcodes(esc_attr(get_user_meta($user_id, CF::BILLING_ADDRESS, true)))
),
true
);
@@ -142,7 +142,7 @@
sprintf(
'[edit-profile-cpf id="%1$s" key="%1$s" value="%2$s"]',
CF::BILLING_CITY,
- strip_shortcodes(esc_attr(get_user_meta($user_id, CF::BILLING_CITY, true)))
+ ppress_neutralize_shortcodes(esc_attr(get_user_meta($user_id, CF::BILLING_CITY, true)))
),
true
);
@@ -157,7 +157,7 @@
sprintf(
'[edit-profile-cpf id="%1$s" key="%1$s" value="%2$s"]',
CF::BILLING_COUNTRY,
- strip_shortcodes(esc_attr(get_user_meta($user_id, CF::BILLING_COUNTRY, true)))
+ ppress_neutralize_shortcodes(esc_attr(get_user_meta($user_id, CF::BILLING_COUNTRY, true)))
),
true
);
@@ -172,7 +172,7 @@
sprintf(
'[edit-profile-cpf id="%1$s" key="%1$s" value="%2$s"]',
CF::BILLING_STATE,
- strip_shortcodes(esc_attr(get_user_meta($user_id, CF::BILLING_STATE, true)))
+ ppress_neutralize_shortcodes(esc_attr(get_user_meta($user_id, CF::BILLING_STATE, true)))
),
true
);
@@ -187,7 +187,7 @@
sprintf(
'[edit-profile-cpf id="%1$s" key="%1$s" value="%2$s"]',
CF::BILLING_POST_CODE,
- strip_shortcodes(esc_attr(get_user_meta($user_id, CF::BILLING_POST_CODE, true)))
+ ppress_neutralize_shortcodes(esc_attr(get_user_meta($user_id, CF::BILLING_POST_CODE, true)))
),
true
);
@@ -202,7 +202,7 @@
sprintf(
'[edit-profile-cpf id="%1$s" key="%1$s" value="%2$s"]',
CF::BILLING_PHONE_NUMBER,
- strip_shortcodes(esc_attr(get_user_meta($user_id, CF::BILLING_PHONE_NUMBER, true)))
+ ppress_neutralize_shortcodes(esc_attr(get_user_meta($user_id, CF::BILLING_PHONE_NUMBER, true)))
),
true
);
@@ -218,7 +218,7 @@
sprintf(
'[edit-profile-cpf id="%1$s" key="%1$s" value="%2$s"]',
$field_id,
- strip_shortcodes(esc_attr(get_user_meta($user_id, $field_id, true)))
+ ppress_neutralize_shortcodes(esc_attr(get_user_meta($user_id, $field_id, true)))
),
true
);
--- a/wp-user-avatar/src/Admin/SettingsPages/Membership/views/orders/order-data-metabox.php
+++ b/wp-user-avatar/src/Admin/SettingsPages/Membership/views/orders/order-data-metabox.php
@@ -153,11 +153,11 @@
$method_name = str_replace('ppress_', '', $field_id);
- $detail = strip_shortcodes(esc_attr($order_data->$method_name));
+ $detail = ppress_neutralize_shortcodes(esc_attr($order_data->$method_name));
echo '<p class="ppress-metabox-form-field">';
printf('<label for="%s">%s</label>', $field_id, $field['label']);
- echo do_shortcode(sprintf('[edit-profile-cpf id="%1$s" key="%1$s" value="%2$s" billing_country="%3$s"]', $field_id, $detail, $order_data->billing_country), true);
+ echo do_shortcode(sprintf('[edit-profile-cpf id="%1$s" key="%1$s" value="%2$s" billing_country="%3$s"]', $field_id, $detail, ppress_neutralize_shortcodes(esc_attr($order_data->billing_country))), true);
echo '</p>';
}
?>
--- a/wp-user-avatar/src/Admin/SettingsPages/Membership/views/orders/order-item-metabox.php
+++ b/wp-user-avatar/src/Admin/SettingsPages/Membership/views/orders/order-item-metabox.php
@@ -31,6 +31,7 @@
<tr class="ppress-submetabox-item-row">
<td class="ppress-submetabox-item-column-course">
<a target="_blank" href="<?= $plan->get_edit_plan_url() ?>"><?php echo $plan->name ?></a>
+ <?php do_action('ppress_order_item_details', $order_data); ?>
</td>
<td class="ppress-submetabox-item-column-price"></td>
<td class="ppress-submetabox-item-column-total"><?= $plan_price ?></td>
--- a/wp-user-avatar/src/AdminBarDashboardAccess/Init.php
+++ b/wp-user-avatar/src/AdminBarDashboardAccess/Init.php
@@ -158,6 +158,14 @@
if (is_super_admin($current_user->ID)) return;
+ /**
+ * Let a user through to a screen they need despite the block, e.g. course instructors to the course builder.
+ *
+ * @param bool $allowed
+ * @param WP_User $current_user
+ */
+ if (apply_filters('ppress_dashboard_access_allowed', false, $current_user)) return;
+
// if no role is selected, disable for everyone by return false.
if (empty($disable_dashboard_access_roles)) return $this->disable_dashboard_access();
--- a/wp-user-avatar/src/Base.php
+++ b/wp-user-avatar/src/Base.php
@@ -65,6 +65,7 @@
define('PPRESS_MEMBERSHIP_CUSTOMERS_SETTINGS_SLUG', 'ppress-customers');
define('PPRESS_MEMBERSHIP_SUBSCRIPTIONS_SETTINGS_SLUG', 'ppress-subscriptions');
define('PPRESS_EXTENSIONS_SETTINGS_SLUG', 'ppress-extensions');
+define('PPRESS_LMS_COURSES_SETTINGS_SLUG', 'ppress-courses');
define('PPRESS_SETTINGS_SETTING_PAGE', admin_url('admin.php?page=' . PPRESS_SETTINGS_SLUG));
define('PPRESS_SETTINGS_SETTING_GENERAL_PAGE', add_query_arg(['section' => 'general'], admin_url('admin.php?page=' . PPRESS_SETTINGS_SLUG)));
@@ -86,6 +87,8 @@
define('PPRESS_MEMBERSHIP_SUBSCRIPTIONS_SETTINGS_PAGE', admin_url('admin.php?page=' . PPRESS_MEMBERSHIP_SUBSCRIPTIONS_SETTINGS_SLUG));
define('PPRESS_MEMBERSHIP_CUSTOMERS_SETTINGS_PAGE', admin_url('admin.php?page=' . PPRESS_MEMBERSHIP_CUSTOMERS_SETTINGS_SLUG));
+define('PPRESS_LMS_COURSES_SETTINGS_PAGE', admin_url('admin.php?page=' . PPRESS_LMS_COURSES_SETTINGS_SLUG));
+
define('PPRESS_LICENSE_SETTINGS_PAGE', add_query_arg('view', 'license', PPRESS_SETTINGS_SETTING_PAGE));
define('PPRESS_SETTINGS_DB_OPTION_NAME', 'ppress_settings_data');
@@ -184,6 +187,7 @@
ContentProtectionInit::get_instance();
NavigationMenuLinksInit::init();
MembershipInit::init();
+ LMSInit::get_instance();
LoginRedirect::get_instance();
@@ -284,6 +288,8 @@
$tables[] = Base::passwordless_login_db_table();
$tables[] = Base::profile_fields_db_table();
$tables[] = Base::subscription_plans_db_table();
+ $tables[] = Base::lms_enrollments_db_table();
+ $tables[] = Base::lms_progress_db_table();
$tables = apply_filters('ppress_drop_mu_database_tables', $tables);
--- a/wp-user-avatar/src/Classes/ExtensionManager.php
+++ b/wp-user-avatar/src/Classes/ExtensionManager.php
@@ -32,6 +32,8 @@
const METERED_PAYWALL = 'metered_paywall';
const CONTENT_DRIPPING = 'content_dripping';
const SUBSCRIPTION_PAUSING = 'subscription_pausing';
+ const TEAM_MEMBERSHIPS = 'team_memberships';
+ const ADVANCED_COURSES = 'advanced_courses';
const LEARNDASH = 'learndash';
const TUTORLMS = 'tutorlms';
const ACADEMYLMS = 'academylms';
@@ -83,6 +85,8 @@
self::METERED_PAYWALL => 'ProfilePressLibsodiumMeteredPaywallInit',
self::CONTENT_DRIPPING => 'ProfilePressLibsodiumContentDrippingInit',
self::SUBSCRIPTION_PAUSING => 'ProfilePressLibsodiumSubscriptionPausingInit',
+ self::TEAM_MEMBERSHIPS => 'ProfilePressLibsodiumTeamMembershipsInit',
+ self::ADVANCED_COURSES => 'ProfilePressLibsodiumAdvancedCoursesInit',
self::LEARNDASH => 'ProfilePressLibsodiumLearndashInit',
self::TUTORLMS => 'ProfilePressCoreIntegrationsTutorLMSInit',
self::ACADEMYLMS => 'ProfilePressCoreIntegrationsAcademyLMSInit',
@@ -233,6 +237,23 @@
'description' => esc_html__('Let customers pause their subscription for a few billing periods instead of cancelling.', 'wp-user-avatar'),
'icon' => '<span class="dashicons dashicons-controls-pause"></span>'
],
+ self::TEAM_MEMBERSHIPS => [
+ 'title' => esc_html__('Team Memberships', 'wp-user-avatar'),
+ 'url' => 'https://profilepress.com/addons/team-memberships/?utm_source=liteplugin&utm_medium=extension-page&utm_campaign=learn-more',
+ 'setting_url' => PPRESS_MEMBERSHIP_SUBSCRIPTION_PLANS_SETTINGS_PAGE,
+ 'description' => esc_html__("Sell membership plans with seats: one person buys for their team, invites colleagues, and everyone gets the plan's access.", 'wp-user-avatar'),
+ 'icon' => '<span class="dashicons dashicons-groups"></span>'
+ ],
+ self::ADVANCED_COURSES => [
+ 'title' => esc_html__('Advanced Courses', 'wp-user-avatar'),
+ 'url' => 'https://profilepress.com/addons/advanced-courses/?utm_source=liteplugin&utm_medium=extension-page&utm_campaign=learn-more',
+ 'setting_url' => PPRESS_LMS_COURSES_SETTINGS_PAGE,
+ 'description' => esc_html__('More for your courses: instructors who create and teach their own courses, assignments, quiz questions you grade by hand, actions when a student completes a course, and course announcements.', 'wp-user-avatar'),
+ 'icon' => '<span class="dashicons dashicons-welcome-learn-more"></span>',
+ 'is_available' => function () {
+ return ProfilePressCoreLMSInit::is_enabled() ? true : esc_html__('The course builder is turned off', 'wp-user-avatar');
+ }
+ ],
self::MULTISITE => [
'title' => esc_html__('Site Creation', 'wp-user-avatar'),
'url' => 'https://profilepress.com/addons/site-creation/?utm_source=liteplugin&utm_medium=extension-page&utm_campaign=learn-more',
--- a/wp-user-avatar/src/Classes/GlobalSiteAccess.php
+++ b/wp-user-avatar/src/Classes/GlobalSiteAccess.php
@@ -73,6 +73,12 @@
if ( ! empty($excluded_pages) && is_page($excluded_pages)) return;
+ /**
+ * Let visitors see a request despite "logged-in users only", e.g. a course's sales page or a certificate's
+ * verification link.
+ */
+ if (apply_filters('ppress_global_site_access_allowed', false)) return;
+
if ( ! is_user_logged_in()) {
nocache_headers();
wp_safe_redirect($redirect_url);
--- a/wp-user-avatar/src/Classes/PROFILEPRESS_sql.php
+++ b/wp-user-avatar/src/Classes/PROFILEPRESS_sql.php
@@ -16,6 +16,9 @@
{
global $wpdb;
+ // rules read with get_meta_data_by_key() earlier in the request must see this change.
+ ppress_cache_transform_flush('meta_data_by_key_');
+
$insert = $wpdb->insert(
Base::meta_data_db_table(),
[
@@ -44,6 +47,8 @@
{
global $wpdb;
+ ppress_cache_transform_flush('meta_data_by_key_');
+
$insert = $wpdb->update(
Base::meta_data_db_table(),
[
@@ -74,6 +79,8 @@
{
global $wpdb;
+ ppress_cache_transform_flush('meta_data_by_key_');
+
return $wpdb->update(
Base::meta_data_db_table(),
['meta_value' => serialize($meta_value)],
@@ -187,6 +194,8 @@
{
global $wpdb;
+ ppress_cache_transform_flush('meta_data_by_key_');
+
$result = $wpdb->delete(Base::meta_data_db_table(), ['id' => $meta_id], ['%d']);
return $result !== false;
@@ -196,6 +205,8 @@
{
global $wpdb;
+ ppress_cache_transform_flush('meta_data_by_key_');
+
$result = $wpdb->delete(Base::meta_data_db_table(), ['flag' => $flag], ['%s']);
return $result !== false;
@@ -205,6 +216,8 @@
{
global $wpdb;
+ ppress_cache_transform_flush('meta_data_by_key_');
+
$result = $wpdb->delete(Base::meta_data_db_table(), ['meta_key' => $meta_key], ['%s']);
return $result !== false;
--- a/wp-user-avatar/src/ContentProtection/Frontend/Checker.php
+++ b/wp-user-avatar/src/ContentProtection/Frontend/Checker.php
@@ -26,6 +26,14 @@
if ( ! is_user_logged_in()) return self::fr(true, ...$function_args);
+ /**
+ * Access granted by criteria other modules add to rules, e.g. students of selected courses.
+ * Return null when the rule doesn't use such criteria, true to grant access and false otherwise.
+ */
+ $extra_access = apply_filters('ppress_content_protection_extra_access', null, $rule, get_current_user_id());
+
+ if (true === $extra_access) return self::fr(false, ...$function_args);
+
if ( ! empty($membership_plans)) {
$customer = CustomerFactory::fromUserId(get_current_user_id());
@@ -57,7 +65,7 @@
if (in_array(get_current_user_id(), $users)) return self::fr(false, ...$function_args);
}
- if (empty($roles) && empty($wp_users) && empty($membership_plans)) return self::fr(false, ...$function_args);
+ if (empty($roles) && empty($wp_users) && empty($membership_plans) && is_null($extra_access)) return self::fr(false, ...$function_args);
// returning true to make users and user role combined rule OR instead of AND
return self::fr(true, ...$function_args);
--- a/wp-user-avatar/src/ContentProtection/views/view.access-condition.php
+++ b/wp-user-avatar/src/ContentProtection/views/view.access-condition.php
@@ -77,6 +77,7 @@
</select>
</td>
</tr>
+ <?php do_action('ppress_content_protection_access_condition_rows', $accessConditionData, $postedData); ?>
<tr id="pp-cc-access-noaccess-action-row">
<th>
<label for="pp-cc-access-noaccess-action">
@@ -184,6 +185,7 @@
switch (val) {
case 'everyone':
+ $('.pp-cc-login-only-row').hide();
$('#pp-cc-access-membership-plans-row').hide();
$('#pp-cc-access-role-row').hide();
$('#pp-cc-access-wp-users-row').hide();
@@ -204,9 +206,11 @@
$('#pp-cc-access-membership-plans-row').show();
$('#pp-cc-access-role-row').show();
$('#pp-cc-access-wp-users-row').show();
+ $('.pp-cc-login-only-row').show();
$('#pp-cc-access-noaccess-action-row').show().find('select').trigger('change');
break;
case 'logout':
+ $('.pp-cc-login-only-row').hide();
$('#pp-cc-access-membership-plans-row').hide();
$('#pp-cc-access-role-row').hide();
$('#pp-cc-access-wp-users-row').hide();
--- a/wp-user-avatar/src/DBTables.php
+++ b/wp-user-avatar/src/DBTables.php
@@ -80,4 +80,18 @@
return $wpdb->prefix . 'ppress_coupons';
}
+
+ public static function lms_enrollments_db_table()
+ {
+ global $wpdb;
+
+ return $wpdb->prefix . 'ppress_lms_enrollments';
+ }
+
+ public static function lms_progress_db_table()
+ {
+ global $wpdb;
+
+ return $wpdb->prefix . 'ppress_lms_progress';
+ }
}
No newline at end of file
--- a/wp-user-avatar/src/DBUpdates.php
+++ b/wp-user-avatar/src/DBUpdates.php
@@ -4,13 +4,14 @@
use ProfilePressCoreClassesExtensionManager;
use ProfilePressCoreMembershipDigitalProductsUploadHandler;
+use ProfilePressCoreRegisterActivationCreateDBTables;
use ProfilePressLibsodiumLicensingLicensing;
class DBUpdates
{
public static $instance;
- const DB_VER = 16;
+ const DB_VER = 17;
public function init_options()
{
@@ -239,6 +240,16 @@
);
}
+ /**
+ * Creates the LMS (course builder) tables and registers the course/lesson permalinks.
+ */
+ public function update_routine_17()
+ {
+ CreateDBTables::membership_db_make();
+
+ add_action('init', 'flush_rewrite_rules', 99999);
+ }
+
public static function get_instance()
{
if ( ! isset(self::$instance)) {
--- a/wp-user-avatar/src/Functions/GlobalFunctions.php
+++ b/wp-user-avatar/src/Functions/GlobalFunctions.php
@@ -456,9 +456,11 @@
}
/**
- * Get ProfilePress login page URL or WP default login url if it isn't set.
+ * Get ProfilePress registration page URL or WP default registration url if it isn't set.
+ *
+ * @param string $redirect where to send the new user after signing up, when auto-login after registration is on.
*/
-function ppress_registration_url()
+function ppress_registration_url($redirect = '')
{
$page_id = ppress_get_setting('set_registration_url');
@@ -468,6 +470,10 @@
$reg_url = wp_registration_url();
}
+ if ( ! empty($redirect)) {
+ $reg_url = add_query_arg('redirect_to', rawurlencode(wp_validate_redirect($redirect)), $reg_url);
+ }
+
return apply_filters('ppress_registration_url', $reg_url);
}
@@ -595,6 +601,7 @@
PPRESS_CONTENT_PROTECTION_SETTINGS_SLUG,
PPRESS_EXTENSIONS_SETTINGS_SLUG,
PPRESS_DASHBOARD_SETTINGS_SLUG,
+ PPRESS_LMS_COURSES_SETTINGS_SLUG,
MailOptin::SLUG
];
@@ -2159,15 +2166,51 @@
function ppress_cache_transform($cache_key, $callback)
{
- static $cache = [];
-
// If the cache key does not exist, compute the value and cache it.
- if ( ! isset($cache[$cache_key])) {
- $cache[$cache_key] = $callback();
+ if ( ! isset($GLOBALS['ppress_cache_transform'][$cache_key])) {
+ $GLOBALS['ppress_cache_transform'][$cache_key] = $callback();
}
// Return the cached result.
- return $cache[$cache_key];
+ return $GLOBALS['ppress_cache_transform'][$cache_key];
+}
+
+/**
+ * Removes values cached by ppress_cache_transform() for the rest of the request.
+ *
+ * @param string $prefix only remove keys starting with this. Empty removes everything.
+ */
+function ppress_cache_transform_flush($prefix = '')
+{
+ if (empty($GLOBALS['ppress_cache_transform'])) return;
+
+ if ($prefix === '') {
+ $GLOBALS['ppress_cache_transform'] = [];
+
+ return;
+ }
+
+ foreach (array_keys($GLOBALS['ppress_cache_transform']) as $key) {
+ if (strpos($key, $prefix) === 0) unset($GLOBALS['ppress_cache_transform'][$key]);
+ }
+}
+
+/**
+ * Whether checkout should refuse a plan because the customer already has an active subscription to it.
+ *
+ * @param ProfilePressCoreMembershipModelsPlanPlanEntity $plan
+ * @param int $user_id
+ *
+ * @return bool
+ */
+function ppress_is_plan_repurchase_blocked($plan, $user_id)
+{
+ if ( ! $user_id || ! ProfilePressCoreMembershipModelsCustomerCustomerFactory::fromUserId($user_id)->has_active_subscription($plan->id)) {
+ return false;
+ }
+
+ // e.g. buying a one-time plan again to renew time-limited course access.
+ return ! apply_filters('ppress_checkout_allow_repurchase', false, $plan, $user_id);
}
function ppress_form_has_field($form_id, $form_type, $field_shortcode_tag)
@@ -2244,15 +2287,44 @@
*/
function ppress_get_other_active_subscriptions_plan_extras($subscription, $plan_extras_key)
{
+ $customer = $subscription->get_customer();
+
+ if ( ! $customer->exists()) return [];
+
+ return ppress_get_user_active_plan_extras($customer->user_id, $plan_extras_key, $subscription->id);
+}
+
+/**
+ * IDs saved under a plan extras key across a user's active subscriptions, optionally leaving one out.
+ *
+ * Counts subscriptions add-ons grant through ppress_customer_active_subscriptions (e.g. a seat on a team plan), and
+ * works for users who aren't customers, e.g. a team member whose seat is being removed. Seats count in wp-admin too
+ * (ppress_team_seats_in_admin), where the add-on hides them from lists: an admin cancelling one subscription mustn't take
+ * away what the user still has through a team.
+ *
+ * @param int $user_id
+ * @param string $plan_extras_key
+ * @param int $exclude_subscription_id
+ *
+ * @return int[]
+ */
+function ppress_get_user_active_plan_extras($user_id, $plan_extras_key, $exclude_subscription_id = 0)
+{
$ids = [];
- $customer = $subscription->get_customer();
+ $customer = ProfilePressCoreMembershipModelsCustomerCustomerFactory::fromUserId($user_id);
+
+ if ( ! $customer->user_exists()) return $ids;
+
+ add_filter('ppress_team_seats_in_admin', '__return_true', 999);
+
+ $active_subs = $customer->get_active_subscriptions();
- if ( ! $customer->exists()) return $ids;
+ remove_filter('ppress_team_seats_in_admin', '__return_true', 999);
- foreach ($customer->get_active_subscriptions() as $active_sub) {
+ foreach ($active_subs as $active_sub) {
- if ((int)$active_sub->id === (int)$subscription->id) continue;
+ if ((int)$active_sub->id === (int)$exclude_subscription_id) continue;
$items = $active_sub->get_plan()->get_plan_extras($plan_extras_key);
@@ -2261,5 +2333,5 @@
}
}
- return array_unique($ids);
+ return array_values(array_unique($ids));
}
--- a/wp-user-avatar/src/Functions/MSFunctions.php
+++ b/wp-user-avatar/src/Functions/MSFunctions.php
@@ -901,11 +901,10 @@
* @param int $plan_id
* @param int $customer_id
* @param array $order_data
- * @param bool $send_receipt
*
* @return array|WP_Error
*/
-function ppress_subscribe_user_to_plan($plan_id, $customer_id, $order_data = [], $send_receipt = false)
+function ppress_subscribe_user_to_plan($plan_id, $customer_id, $order_data = [])
{
global $wpdb;
@@ -983,7 +982,7 @@
$order->save();
- if ($send_receipt && $order->is_completed()) {
+ if ($order->is_completed()) {
// important we call complete_order with synced/updated order object.
OrderFactory::fromId($order_id)->complete_order();
}
--- a/wp-user-avatar/src/Functions/custom-settings-api.php
+++ b/wp-user-avatar/src/Functions/custom-settings-api.php
@@ -553,6 +553,20 @@
return "<tr id="$tr_id"><td colspan="5" style='margin:0;padding:0;'>" . $data . $description . '</td></tr>';
}
+ /**
+ * A heading that splits a settings section into groups, with an optional description under it.
+ */
+ public function _subheading($db_options, $key, $args)
+ {
+ $label = $args['label'] ?? '';
+ $description = ! empty($args['description']) ? '<p class="description">' . $args['description'] . '</p>' : '';
+
+ return sprintf(
+ '<tr id="%s_row" class="ppress-settings-subheading-row"><td colspan="5"><div class="ppress-settings-subheading">%s</div>%s</td></tr>',
+ esc_attr($key), esc_html($label), $description
+ );
+ }
+
public function _color($db_options, $key, $args) {
$args['class'] = 'ppress-color-picker';
return $this->_text($db_options, $key, $args);
--- a/wp-user-avatar/src/Integrations/AcademyLMS/Init.php
+++ b/wp-user-avatar/src/Integrations/AcademyLMS/Init.php
@@ -34,6 +34,10 @@
add_action('ppress_subscription_cancelled', [$this, 'on_subscription_cancelled']);
add_action('ppress_subscription_expired', [$this, 'on_subscription_cancelled']);
+ // members of a team plan (Team Memberships add-on) get the plan's courses as if they'd bought it.
+ add_action('ppress_team_member_access_granted', [$this, 'on_team_member_access_granted'], 10, 2);
+ add_action('ppress_team_member_access_revoked', [$this, 'on_team_member_access_revoked'], 10, 2);
+
add_action('ppress_after_registration', [$this, 'after_user_registration'], 10, 4);
add_filter('ppress_shortcode_builder_registration_meta', [$this, 'save_shortcode_builder_settings']);
@@ -96,22 +100,7 @@
$order = $order_or_sub;
}
- $plan = $order->get_plan();
-
- $courses = $plan->get_plan_extras('academylms_courses');
-
- $user_id = $order->get_customer()->user_id;
-
- if (is_array($courses)) {
- $courses = array_filter($courses);
- if ( ! empty($courses)) {
- foreach ($courses as $course_id) {
- if ( ! Helper::is_enrolled($course_id, $user_id)) {
- Helper::do_enroll($course_id, $user_id);
- }
- }
- }
- }
+ $this->grant_plan_courses($order->get_customer()->user_id, $order->get_plan());
}
/**
@@ -124,26 +113,67 @@
// doing SubscriptionFactory call because we want to re-fetch from DB.
if (SubscriptionFactory::fromId($subscription->id)->is_active()) return;
- $plan = $subscription->get_plan();
+ $this->revoke_plan_courses($subscription->get_customer()->user_id, $subscription->get_plan(), $subscription->id);
+ }
- $user_id = $subscription->get_customer()->user_id;
+ /**
+ * @param int $user_id
+ * @param SubscriptionEntity $subscription the team's subscription.
+ */
+ public function on_team_member_access_granted($user_id, $subscription)
+ {
+ if ($subscription instanceof SubscriptionEntity) $this->grant_plan_courses($user_id, $subscription->get_plan());
+ }
+ /**
+ * @param int $user_id
+ * @param SubscriptionEntity $subscription the team's subscription.
+ */
+ public function on_team_member_access_revoked($user_id, $subscription)
+ {
+ if ($subscription instanceof SubscriptionEntity) $this->revoke_plan_courses($user_id, $subscription->get_plan(), $subscription->id);
+ }
+
+ /**
+ * Enrolls the user in the plan's courses.
+ *
+ * @param int $user_id
+ * @param ProfilePressCoreMembershipModelsPlanPlanEntity $plan
+ */
+ private function grant_plan_courses($user_id, $plan)
+ {
$courses = $plan->get_plan_extras('academylms_courses');
- if (is_array($courses)) {
- $courses = array_filter($courses);
- if ( ! empty($courses)) {
-
- $courses_still_granted = ppress_get_other_active_subscriptions_plan_extras($subscription, 'academylms_courses');
-
- foreach ($courses as $course_id) {
- // skip courses the user still has access to via another active subscription.
- if (in_array(absint($course_id), $courses_still_granted, true)) continue;
-
- if (class_exists('AcademyProEnrollmentHelper') && method_exists('AcademyProEnrollmentHelper', 'cancel_enroll')) {
- AcademyProEnrollmentHelper::cancel_enroll($course_id, $user_id);
- }
- }
+ if ( ! $user_id || ! is_array($courses)) return;
+
+ foreach (array_filter($courses) as $course_id) {
+ if ( ! Helper::is_enrolled($course_id, $user_id)) {
+ Helper::do_enroll($course_id, $user_id);
+ }
+ }
+ }
+
+ /**
+ * Unenrolls the user from the plan's courses, except those another of their active subscriptions still grants.
+ *
+ * @param int $user_id
+ * @param ProfilePressCoreMembershipModelsPlanPlanEntity $plan
+ * @param int $subscription_id the subscription that no longer gives access.
+ */
+ private function revoke_plan_courses($user_id, $plan, $subscription_id)
+ {
+ $courses = $plan->get_plan_extras('academylms_courses');
+
+ if ( ! $user_id || ! is_array($courses)) return;
+
+ $courses_still_granted = ppress_get_user_active_plan_extras($user_id, 'academylms_courses', $subscription_id);
+
+ foreach (array_filter($courses) as $course_id) {
+ // skip courses the user still has access to via another active subscription.
+ if (in_array(absint($course_id), $courses_still_granted, true)) continue;
+
+ if (class_exists('AcademyProEnrollmentHelper') && method_exists('AcademyProEnrollmentHelper', 'cancel_enroll')) {
+ AcademyProEnrollmentHelper::cancel_enroll($course_id, $user_id);
}
}
}
@@ -344,7 +374,7 @@
$customer = CustomerFactory::fromUserId(get_current_user_id());
- if ( ! $customer->exists() || ! $this->is_customer_plans_has_course_access($course_id)) {
+ if ( ! $customer->user_exists() || ! $this->is_customer_plans_has_course_access($course_id)) {
return $plan_ids[0];
}
@@ -372,7 +402,7 @@
$plan_checks = array_map(function ($plan_id) use ($customer) {
- if ( ! $customer->exists()) return false;
+ if ( ! $customer->user_exists()) return false;
return $customer->has_active_subscription($plan_id);
--- a/wp-user-avatar/src/Integrations/TutorLMS/Init.php
+++ b/wp-user-avatar/src/Integrations/TutorLMS/Init.php
@@ -38,6 +38,10 @@
add_action('ppress_subscription_cancelled', [$this, 'on_subscription_cancelled']);
add_action('ppress_subscription_expired', [$this, 'on_subscription_cancelled']);
+ // members of a team plan (Team Memberships add-on) get the plan's courses as if they'd bought it.
+ add_action('ppress_team_member_access_granted', [$this, 'on_team_member_access_granted'], 10, 2);
+ add_action('ppress_team_member_access_revoked', [$this, 'on_team_member_access_revoked'], 10, 2);
+
add_action('ppress_after_registration', [$this, 'after_user_registration'], 10, 4);
add_filter('ppress_shortcode_builder_registration_meta', [$this, 'save_shortcode_builder_settings']);
@@ -100,7 +104,7 @@
$plan_checks = array_map(function ($plan_id) use ($customer) {
- if ( ! $customer->exists()) return false;
+ if ( ! $customer->user_exists()) return false;
return $customer->has_active_subscription($plan_id);
@@ -128,7 +132,7 @@
$customer = CustomerFactory::fromUserId(get_current_user_id());
- if ( ! $customer->exists() || ! $this->is_customer_plans_has_course_access($course_id)) {
+ if ( ! $customer->user_exists() || ! $this->is_customer_plans_has_course_access($course_id)) {
$plan = ppress_get_plan($plan_ids[0]);
$billing_frequency = $plan->is_recurring() ? ' ' . SubscriptionBillingFrequency::get_label($plan->billing_frequency) : '';
@@ -167,7 +171,7 @@
$customer = CustomerFactory::fromUserId(get_current_user_id());
- if ( ! $customer->exists() || ! $this->is_customer_plans_has_course_access($course_id)) {
+ if ( ! $customer->user_exists() || ! $this->is_customer_plans_has_course_access($course_id)) {
$plan = ppress_get_plan($plan_ids[0]);
$billing_frequency = $plan->is_recurring() ? ' ' . SubscriptionBillingFrequency::get_label($plan->billing_frequency) : '';
@@ -244,22 +248,7 @@
$order = $order_or_sub;
}
- $plan = $order->get_plan();
-
- $courses = $plan->get_plan_extras('tutorlms_courses');
-
- $user_id = $order->get_customer()->user_id;
-
- if (is_array($courses)) {
- $courses = array_filter($courses);
- if ( ! empty($courses)) {
- foreach ($courses as $course_id) {
- if ( ! EnrollmentModel::is_enrolled($course_id, $user_id)) {
- EnrollmentModel::do_enroll($course_id, 0, $user_id);
- }
- }
- }
- }
+ $this->grant_plan_courses($order->get_customer()->user_id, $order->get_plan());
}
/**
@@ -272,25 +261,66 @@
// doing SubscriptionFactory call because we want to re-fetch from DB.
if (SubscriptionFactory::fromId($subscription->id)->is_active()) return;
- $plan = $subscription->get_plan();
+ $this->revoke_plan_courses($subscription->get_customer()->user_id, $subscription->get_plan(), $subscription->id);
+ }
+
+ /**
+ * @param int $user_id
+ * @param SubscriptionEntity $subscription the team's subscription.
+ */
+ public function on_team_member_access_granted($user_id, $subscription)
+ {
+ if ($subscription instanceof SubscriptionEntity) $this->grant_plan_courses($user_id, $subscription->get_plan());
+ }
+
+ /**
+ * @param int $user_id
+ * @param SubscriptionEntity $subscription the team's subscription.
+ */
+ public function on_team_member_access_revoked($user_id, $subscription)
+ {
+ if ($subscription instanceof SubscriptionEntity) $this->revoke_plan_courses($user_id, $subscription->get_plan(), $subscription->id);
+ }
+
+ /**
+ * Enrolls the user in the plan's courses.
+ *
+ * @param int $user_id
+ * @param ProfilePressCoreMembershipModelsPlanPlanEntity $plan
+ */
+ private function grant_plan_courses($user_id, $plan)
+ {
+ $courses = $plan->get_plan_extras('tutorlms_courses');
+
+ if ( ! $user_id || ! is_array($courses)) return;
- $user_id = $subscription->get_customer()->user_id;
+ foreach (array_filter($courses) as $course_id) {
+ if ( ! EnrollmentModel::is_enrolled($course_id, $user_id)) {
+ EnrollmentModel::do_enroll($course_id, 0, $user_id);
+ }
+ }
+ }
+ /**
+ * Unenrolls the user from the plan's courses, except those another of their active subscriptions still grants.
+ *
+ * @param int $user_id
+ * @param ProfilePressCoreMembershipModelsPlanPlanEntity $plan
+ * @param int $subscription_id the subscription that no longer gives access.
+ */
+ private function revoke_plan_courses($user_id, $plan, $subscription_id)
+ {
$courses = $plan->get_plan_extras('tutorlms_courses');
- if (is_array($courses)) {
- $courses = array_filter($courses);
- if ( ! empty($courses)) {
+ if ( ! $user_id || ! is_array($courses)) return;
- $courses_still_granted = ppress_get_other_active_subscriptions_plan_extras($subscription, 'tutorlms_courses');
+ $courses_still_granted = ppress_get_user_active_plan_extras($user_id, 'tutorlms_courses', $subscription_id);
- foreach ($courses as $course_id) {
- // skip courses the user still has access to via another active subscription.
- if (in_array(absint($course_id), $courses_still_granted, true)) continue;
+ foreach (array_filter($courses) as $course_id) {
+ // skip courses the user still has access to via another active subscription.
+ if (in_array(absint($course_id), $courses_still_granted, true)) continue;
- tutor_utils()->cancel_course_enrol($course_id, $user_id);
- }
- }
+ tutor_utils()->cancel_course_enrol($course_id, $user_id);
}
}
--- a/wp-user-avatar/src/LMS/Access.php
+++ b/wp-user-avatar/src/LMS/Access.php
@@ -0,0 +1,236 @@
+<?php
+
+namespace ProfilePressCoreLMS;
+
+use ProfilePressCoreLMSAccessRulesAccessRules;
+use ProfilePressCoreMembershipModelsCustomerCustomerFactory;
+
+if ( ! defined('ABSPATH')) {
+ exit;
+}
+
+class Access
+{
+ const LOCK_LOGIN = 'login';
+ const LOCK_PURCHASE = 'purchase';
+ const LOCK_DRIP = 'drip';
+ const LOCK_SEQUENTIAL = 'sequential';
+ const LOCK_PREREQUISITE = 'prerequisite';
+ const LOCK_EXPIRED = 'expired';
+ const LOCK_NOT_STARTED = 'not_started';
+ /** the lesson needs a module that isn't running, see Lesson::is_unavailable(). */
+ const LOCK_UNAVAILABLE = 'unavailable';
+ /** blocked by a Content Protection rule */
+ const LOCK_PROTECTED = 'protected';
+
+ /** @var array per-request cache of active plan IDs by user ID */
+ private static $plan_ids_cache = [];
+
+ public static function can_manage($user_id, $course_id)
+ {
+ if ( ! $user_id) return false;
+
+ $can_manage = user_can($user_id, 'manage_options') || user_can($user_id, 'edit_post', $course_id);
+
+ // e.g. false while a course editor previews the course as a student.
+ return (bool)apply_filters('ppress_lms_can_manage', $can_manage, $user_id, $course_id);
+ }
+
+ /**
+ * @param int $user_id
+ *
+ * @return int[] IDs of plans the user has an active subscription to.
+ */
+ public static function get_user_active_plan_ids($user_id)
+ {
+ if ( ! $user_id) return [];
+
+ if ( ! isset(self::$plan_ids_cache[$user_id])) {
+
+ self::$plan_ids_cache[$user_id] = [];
+
+ $customer = CustomerFactory::fromUserId($user_id);
+
+ // not only customers: add-ons can grant access to other users (e.g. a seat on a team plan).
+ if ($customer->user_exists()) {
+ foreach ($customer->get_active_subscriptions() as $sub) {
+ self::$plan_ids_cache[$user_id][] = (int)$sub->plan_id;
+ }
+ }
+ }
+
+ return self::$plan_ids_cache[$user_id];
+ }
+
+ /**
+ * Whether the user has access to the course content (not taking drip or sequential locks into account).
+ *
+ * @param int $user_id
+ * @param Course $course
+ *
+ * @return bool
+ */
+ public static function has_course_access($user_id, Course $course)
+ {
+ if ( ! $course->exists()) return false;
+
+ $user_id = absint($user_id);
+
+ $access = false;
+
+ if (self::can_manage($user_id, $course->get_id())) {
+ $access = true;
+ } else {
+
+ switch ($course->get_access_type()) {
+ case Course::ACCESS_FREE:
+ $access = true;
+ break;
+ case Course::ACCESS_REGISTERED:
+ $access = $user_id > 0;
+ break;
+ default:
+ if ($user_id > 0) {
+
+ $enrollment = Repository::get_enrollment($user_id, $course->get_id());
+
+ if ($enrollment && $enrollment->source == Repository::SOURCE_MANUAL) {
+ $access = true;
+ } else {
+ $access = ! empty(array_intersect($course->get_plan_ids(), self::get_user_active_plan_ids($user_id)));
+ }
+ }
+ }
+ }
+
+ return (bool)apply_filters('ppress_lms_has_course_access', $access, $user_id, $course);
+ }
+
+ /**
+ * @param int $user_id
+ * @param Lesson $lesson
+ * @param Course|null $course
+ *
+ * @return array ['access' => bool, 'reason' => string, 'available_at' => int timestamp]
+ */
+ public static function lesson_access($user_id, Lesson $lesson, $course = null)
+ {
+ $course = $course instanceof Course ? $course : $lesson->get_course();
+ $user_id = absint($user_id);
+
+ $result = ['access' => true, 'reason' => '', 'available_at' => 0];
+
+ $is_manager = $course->exists() && self::can_manage($user_id, $course->get_id());
+
+ $course_visible = $course->exists() && ($course->is_published() || apply_filters('ppress_lms_show_unpublished_course', false, $user_id, $course));
+
+ // lessons of unpublished courses and unpublished lessons are only visible to course managers.
+ if ( ! $course->exists() || ( ! $is_manager && ( ! $lesson->is_visible_to_students() || ! $course_visible))) {
+ $result = ['access' => false, 'reason' => self::LOCK_PURCHASE, 'available_at' => 0];
+
+ return apply_filters('ppress_lms_lesson_access', $result, $user_id, $lesson, $course);
+ }
+
+ if ($is_manager) {
+ return apply_filters('ppress_lms_lesson_access', $result, $user_id, $lesson, $course);
+ }
+
+ // e.g. an assignment while the add-on that grades it is off: completing it would skip the work.
+ if ($lesson->is_unavailable()) {
+ $result = ['access' => false, 'reason' => self::LOCK_UNAVAILABLE, 'available_at' => 0];
+
+ return apply_filters('ppress_lms_lesson_access', $result, $user_id, $lesson, $course);
+ }
+
+ if ( ! self::has_course_access($user_id, $course)) {
+
+ if ( ! $lesson->is_preview()) {
+ $result['access'] = false;
+ $result['reason'] = $user_id > 0 || $course->get_access_type() == Course::ACCESS_PAID ? self::LOCK_PURCHASE : self::LOCK_LOGIN;
+
+ if ($course->get_access_type() == Course::ACCESS_REGISTERED) {
+ $result['reason'] = self::LOCK_LOGIN;
+ }
+ }
+
+ return apply_filters('ppress_lms_lesson_access', $result, $user_id, $lesson, $course);
+ }
+
+ if ($lesson->is_preview()) {
+ return apply_filters('ppress_lms_lesson_access', $result, $user_id, $lesson, $course);
+ }
+
+ $lock = function ($reason, $available_at = 0) use ($result, $user_id, $lesson, $course) {
+ $result['access'] = false;
+ $result['reason'] = $reason;
+ $result['available_at'] = $available_at;
+
+ return apply_filters('ppress_lms_lesson_access', $result, $user_id, $lesson, $course);
+ };
+
+ // a course's access days count from enrolling, so the clock starts the first time a student with access reaches a
+ // lesson by any route (a feed, the REST API, a download), not only by opening its page.
+ if ($user_id && $user_id === get_current_user_id() && AccessRules::get_access_days($course) > 0 &&
+ ! self::can_manage($user_id, $course->get_id()) && ! Repository::get_enrollment($user_id, $course->get_id())) {
+ Repository::enroll($user_id, $course->get_id(), Progress::enrollment_source($course));
+ }
+
+ if (AccessRules::is_expired($user_id, $course)) return $lock(self::LOCK_EXPIRED);
+
+ if ( ! empty(AccessRules::get_missing_prerequisites($user_id, $course))) {
+ // guests can't have completed anything, so they need to sign in first.
+ return $lock($user_id ? self::LOCK_PREREQUISITE : self::LOCK_LOGIN);
+ }
+
+ $start = AccessRules::get_start_timestamp($course);
+
+ if ($start > time()) return $lock(self::LOCK_NOT_STARTED, $start);
+
+ $schedule = AccessRules::get_lesson_schedule($lesson, $course);
+
+ // drip-by-days schedules and sequential progress are per student, so guests must sign in.
+ if ( ! $user_id && ($schedule['type'] == 'days' || $course->is_sequential())) return $lock(self::LOCK_LOGIN);
+
+ $available_at = AccessRules::get_lesson_available_at($user_id, $lesson, $course);
+
+ if ($available_at > time()) return $lock(self::LOCK_DRIP, $available_at);
+
+ if ($course->is_sequential()) {
+
+ $completed = Progress::get_completed_lesson_ids($user_id, $course->get_id());
+
+ foreach ($course->get_lesson_ids() as $lesson_id) {
+
+ if ($lesson_id == $lesson->get_id()) break;
+
+ if (in_array($lesson_id, $completed, true)) continue;
+
+ // lets a lesson type open the next lessons before it's completed, e.g. an assignment waiting to be graded.
+ if (apply_filters('ppress_lms_lesson_unlocks_next', false, $user_id, $lesson_id, $course)) continue;
+
+ return $lock(self::LOCK_SEQUENTIAL);
+ }
+ }
+
+ return apply_filters('ppress_lms_lesson_access', $result, $user_id, $lesson, $course);
+ }
+
+ public static function can_view_lesson($user_id, Lesson $lesson, $course = null)
+ {
+ return self::lesson_access($user_id, $lesson, $course)['access'];
+ }
+
+ /**
+ * Whether a password-protected lesson's password has been entered (the post password cookie). Its files, quiz and
+ * completion need it as much as its content does, so every handler acting on a lesson checks this too. Course
+ * managers don't need it.
+ */
+ public static function lesson_password_ok($user_id, Lesson $lesson)
+ {
+ if ( ! $lesson->exists() || ! post_password_required($lesson->post)) return true;
+
+ $course = $lesson->get_course();
+
+ return $course->exists() && self::can_manage(absint($user_id), $course->get_id());
+ }
+}
--- a/wp-user-avatar/src/LMS/AccessRules/AccessRules.php
+++ b/wp-user-avatar/src/LMS/AccessRules/AccessRules.php
@@ -0,0 +1,541 @@
+<?php
+
+namespace ProfilePressCoreLM