From 13bae57a9cbd4f2c162df26905b8b0ed4c3402fe Mon Sep 17 00:00:00 2001 From: Brian Hogg Date: Mon, 5 Feb 2024 17:02:13 +0100 Subject: [PATCH 01/32] Working version of fixing the admin menu items, and things like the review prompt. Will test undoing these one by one to know what the bare minimum change is. --- class-lifterlms.php | 10 +--------- includes/class-llms-loader.php | 6 ++++-- includes/functions/llms-functions-l10n.php | 2 ++ includes/llms.functions.core.php | 3 +++ 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/class-lifterlms.php b/class-lifterlms.php index 6f8cad0cef..209e5b1d95 100644 --- a/class-lifterlms.php +++ b/class-lifterlms.php @@ -73,14 +73,6 @@ final class LifterLMS { */ private function __construct() { - /** - * Localize as early as possible. - * - * Since 4.6 the "just_in_time" l10n will load the default (not custom) file first - * so we must localize before any l10n functions (like `__()`) are used - * so that our custom "safe" location will always load first. - */ - $this->localize(); $this->define_constants(); @@ -93,6 +85,7 @@ private function __construct() { add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_action_links' ), 10, 1 ); add_action( 'init', array( $this, 'init' ), 0 ); + add_action( 'init', array( $this, 'localize' ), 0 ); add_action( 'init', array( $this, 'integrations' ), 1 ); add_action( 'init', array( $this, 'processors' ), 5 ); add_action( 'init', array( $this, 'events' ), 5 ); @@ -415,7 +408,6 @@ public function add_action_links( $links ) { */ public function localize() { - require_once LLMS_PLUGIN_DIR . 'includes/functions/llms-functions-l10n.php'; llms_load_textdomain( 'lifterlms' ); } diff --git a/includes/class-llms-loader.php b/includes/class-llms-loader.php index c8c9c79188..24187b549f 100644 --- a/includes/class-llms-loader.php +++ b/includes/class-llms-loader.php @@ -129,10 +129,12 @@ public function __construct() { $this->includes(); + //add_action( 'init', array( $this, 'includes' ) ); + add_action( 'init', array( $this, 'includes_admin' ) ); if ( is_admin() ) { - $this->includes_admin(); + // $this->includes_admin(); } else { - $this->includes_frontend(); + //$this->includes_frontend(); } } diff --git a/includes/functions/llms-functions-l10n.php b/includes/functions/llms-functions-l10n.php index 5ec7a2cb41..d9f4b4662e 100644 --- a/includes/functions/llms-functions-l10n.php +++ b/includes/functions/llms-functions-l10n.php @@ -96,6 +96,8 @@ function llms_load_textdomain( $domain, $plugin_dir = null, $language_dir = null $plugin_dir = $plugin_dir ? $plugin_dir : LLMS_PLUGIN_DIR; $language_dir = $language_dir ? $language_dir : 'languages'; + unload_textdomain( 'lifterlms' ); + /** * Load from the custom LifterLMS "safe" directory (if it exists). * diff --git a/includes/llms.functions.core.php b/includes/llms.functions.core.php index 5343de2f32..f34b2b300e 100644 --- a/includes/llms.functions.core.php +++ b/includes/llms.functions.core.php @@ -10,6 +10,8 @@ defined( 'ABSPATH' ) || exit; +require_once 'functions/llms-functions-l10n.php'; + require_once 'functions/llms-functions-access-plans.php'; require_once 'functions/llms-functions-deprecated.php'; require_once 'functions/llms-functions-forms.php'; @@ -19,6 +21,7 @@ require_once 'functions/llms-functions-user-information-fields.php'; require_once 'functions/llms-functions-wrappers.php'; + require_once 'functions/llms.functions.access.php'; require_once 'functions/llms.functions.certificate.php'; require_once 'functions/llms.functions.course.php'; From 1cab2bfe7e47638349f6281535cded7963bf6630 Mon Sep 17 00:00:00 2001 From: Brian Hogg Date: Tue, 6 Feb 2024 10:25:17 +0100 Subject: [PATCH 02/32] Undoing changes that aren't required to fix admin menu and notice language --- includes/class-llms-loader.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/includes/class-llms-loader.php b/includes/class-llms-loader.php index 24187b549f..c8c9c79188 100644 --- a/includes/class-llms-loader.php +++ b/includes/class-llms-loader.php @@ -129,12 +129,10 @@ public function __construct() { $this->includes(); - //add_action( 'init', array( $this, 'includes' ) ); - add_action( 'init', array( $this, 'includes_admin' ) ); if ( is_admin() ) { - // $this->includes_admin(); + $this->includes_admin(); } else { - //$this->includes_frontend(); + $this->includes_frontend(); } } From 8da8c62b7ec460f307534352d4e36af876c03fdf Mon Sep 17 00:00:00 2001 From: Brian Hogg Date: Tue, 6 Feb 2024 10:51:35 +0100 Subject: [PATCH 03/32] Load localize before loading of block settings --- class-lifterlms.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/class-lifterlms.php b/class-lifterlms.php index 209e5b1d95..62ed489723 100644 --- a/class-lifterlms.php +++ b/class-lifterlms.php @@ -68,6 +68,7 @@ final class LifterLMS { * @since 5.3.0 Move the loading of the LifterLMS autoloader to the main `lifterlms.php` file. * @since 6.1.0 Automatically load payment gateways. * @since 6.4.0 Moved registration of `LLMS_Shortcodes::init()` with the 'init' hook to `LLMS_Shortcodes::__construct()`. + * @since [version] Lood locale textdomain on `init` instead of immediately * * @return void */ @@ -84,8 +85,8 @@ private function __construct() { register_activation_hook( __FILE__, array( 'LLMS_Install', 'install' ) ); add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_action_links' ), 10, 1 ); - add_action( 'init', array( $this, 'init' ), 0 ); add_action( 'init', array( $this, 'localize' ), 0 ); + add_action( 'init', array( $this, 'init' ), 0 ); add_action( 'init', array( $this, 'integrations' ), 1 ); add_action( 'init', array( $this, 'processors' ), 5 ); add_action( 'init', array( $this, 'events' ), 5 ); From 48c5e61110962a36118a090b17c9e68c727f8150 Mon Sep 17 00:00:00 2001 From: Brian Hogg Date: Tue, 6 Feb 2024 11:45:53 +0100 Subject: [PATCH 04/32] WIP: Starting permalink settings with default loading and saved option structure --- .../admin/class-llms-admin-permalinks.php | 86 +++++++++++++++++++ includes/class-llms-loader.php | 21 +++++ includes/functions/llms-functions-l10n.php | 22 +++++ 3 files changed, 129 insertions(+) create mode 100644 includes/admin/class-llms-admin-permalinks.php diff --git a/includes/admin/class-llms-admin-permalinks.php b/includes/admin/class-llms-admin-permalinks.php new file mode 100644 index 0000000000..491f9dbfd7 --- /dev/null +++ b/includes/admin/class-llms-admin-permalinks.php @@ -0,0 +1,86 @@ +settings_init(); + + restore_previous_locale(); + + // Remove filter. + remove_filter( 'plugin_locale', 'get_locale' ); + + llms_load_textdomain( 'lifterlms' ); + + // TODO: Save settings + } + + /** + * Show the available permalink settings + */ + public function settings_init() { + add_settings_section( 'lifterlms-permalink', __( 'LifterLMS Permalinks', 'lifterlms' ), array( $this, 'settings' ), 'permalink' ); + + $this->permalinks = llms_get_permalink_structure(); + } + + public function settings() { + // TODO: Conditionally show message for Course Catalog and Membership Catalog if they are enabled + ?> +

+ + + + + + + + + + + + id === 'options-permalink' ) { + require_once LLMS_PLUGIN_DIR . 'includes/admin/class-llms-admin-permalinks.php'; + } + } + /** * Include libraries * diff --git a/includes/functions/llms-functions-l10n.php b/includes/functions/llms-functions-l10n.php index d9f4b4662e..0424cbaddd 100644 --- a/includes/functions/llms-functions-l10n.php +++ b/includes/functions/llms-functions-l10n.php @@ -114,3 +114,25 @@ function llms_load_textdomain( $domain, $plugin_dir = null, $language_dir = null load_plugin_textdomain( $domain, false, sprintf( '%1$s/%2$s', basename( $plugin_dir ), $language_dir ) ); } + +function llms_get_permalink_structure() { + $saved_permalinks = (array) get_option( 'llms_permalinks', array() ); + + // TODO: Verify this will load the site language and not user language when permalink first set. + // TODO: Verify this will load the site language when visiting a front-end page and permalink cache cleared + $permalinks = wp_parse_args( + // Remove false or empty entries so we can use the default values. + array_filter( $saved_permalinks ), + array( + 'course_base' => _x( 'course', 'course url slug', 'lifterlms' ), + ) + ); + + array_filter( $permalinks, 'untrailingslashit' ); + + if ( $saved_permalinks !== $permalinks ) { + update_option( 'llms_permalinks', $permalinks ); + } + + return $permalinks; +}; From 7b2c0c27453c0d5e8d95039b0bd2b7ca8da31a92 Mon Sep 17 00:00:00 2001 From: Brian Hogg Date: Tue, 6 Feb 2024 14:22:37 +0100 Subject: [PATCH 05/32] Unload only the given domain not lifterlms each time --- includes/functions/llms-functions-l10n.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/functions/llms-functions-l10n.php b/includes/functions/llms-functions-l10n.php index 0424cbaddd..a6307e10aa 100644 --- a/includes/functions/llms-functions-l10n.php +++ b/includes/functions/llms-functions-l10n.php @@ -96,7 +96,7 @@ function llms_load_textdomain( $domain, $plugin_dir = null, $language_dir = null $plugin_dir = $plugin_dir ? $plugin_dir : LLMS_PLUGIN_DIR; $language_dir = $language_dir ? $language_dir : 'languages'; - unload_textdomain( 'lifterlms' ); + unload_textdomain( $domain ); /** * Load from the custom LifterLMS "safe" directory (if it exists). From 2a0e6bceed65032caacbe8bb0848fc61ba530ad2 Mon Sep 17 00:00:00 2001 From: Brian Hogg Date: Tue, 6 Feb 2024 14:33:26 +0100 Subject: [PATCH 06/32] Refactor to ensure locale set to the site locale when loading permalinks for the settings page --- .../admin/class-llms-admin-permalinks.php | 20 +++-------- includes/functions/llms-functions-l10n.php | 35 +++++++++++++++++++ 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/includes/admin/class-llms-admin-permalinks.php b/includes/admin/class-llms-admin-permalinks.php index 491f9dbfd7..41197b83ae 100644 --- a/includes/admin/class-llms-admin-permalinks.php +++ b/includes/admin/class-llms-admin-permalinks.php @@ -30,22 +30,7 @@ class LLMS_Admin_Permalinks { * @return void */ public function __construct() { - switch_to_locale( get_locale() ); - - // Filter on plugin_locale so load_plugin_textdomain loads the correct locale. - add_filter( 'plugin_locale', 'get_locale' ); - - llms_load_textdomain( 'lifterlms' ); - $this->settings_init(); - - restore_previous_locale(); - - // Remove filter. - remove_filter( 'plugin_locale', 'get_locale' ); - - llms_load_textdomain( 'lifterlms' ); - // TODO: Save settings } @@ -55,7 +40,12 @@ public function __construct() { public function settings_init() { add_settings_section( 'lifterlms-permalink', __( 'LifterLMS Permalinks', 'lifterlms' ), array( $this, 'settings' ), 'permalink' ); + // Make sure we're on the site locale so any defaults are not in the user's language + llms_switch_to_site_locale(); + $this->permalinks = llms_get_permalink_structure(); + + llms_restore_locale(); } public function settings() { diff --git a/includes/functions/llms-functions-l10n.php b/includes/functions/llms-functions-l10n.php index a6307e10aa..059fd643b5 100644 --- a/includes/functions/llms-functions-l10n.php +++ b/includes/functions/llms-functions-l10n.php @@ -136,3 +136,38 @@ function llms_get_permalink_structure() { return $permalinks; }; + +/** + * Switch LifterLMS language to site language. + * + * @since [version] + */ +function llms_switch_to_site_locale() { + global $wp_locale_switcher; + + if ( function_exists( 'switch_to_locale' ) && isset( $wp_locale_switcher ) ) { + switch_to_locale( get_locale() ); + + // Filter on plugin_locale so load_plugin_textdomain loads the correct locale. + add_filter( 'plugin_locale', 'get_locale' ); + + llms_load_textdomain( 'lifterlms' ); + } +} + +/** + * Switch LifterLMS language to original. + * + * @since [version] + */ +function llms_restore_locale() { + global $wp_locale_switcher; + + if ( function_exists( 'restore_previous_locale' ) && isset( $wp_locale_switcher ) ) { + restore_previous_locale(); + + remove_filter( 'plugin_locale', 'get_locale' ); + + llms_load_textdomain( 'lifterlms' ); + } +} From 00914b36402f3422e5e294a4d6ed86cd56511bee Mon Sep 17 00:00:00 2001 From: Brian Hogg Date: Tue, 6 Feb 2024 15:06:30 +0100 Subject: [PATCH 07/32] Adding courses archive setting --- includes/admin/class-llms-admin-permalinks.php | 10 ++++++++++ includes/functions/llms-functions-l10n.php | 1 + 2 files changed, 11 insertions(+) diff --git a/includes/admin/class-llms-admin-permalinks.php b/includes/admin/class-llms-admin-permalinks.php index 41197b83ae..a23b14b04c 100644 --- a/includes/admin/class-llms-admin-permalinks.php +++ b/includes/admin/class-llms-admin-permalinks.php @@ -65,6 +65,16 @@ public function settings() { + + + + + + + + diff --git a/includes/functions/llms-functions-l10n.php b/includes/functions/llms-functions-l10n.php index 059fd643b5..ecf5049725 100644 --- a/includes/functions/llms-functions-l10n.php +++ b/includes/functions/llms-functions-l10n.php @@ -125,6 +125,7 @@ function llms_get_permalink_structure() { array_filter( $saved_permalinks ), array( 'course_base' => _x( 'course', 'course url slug', 'lifterlms' ), + 'courses_base' => _x( 'courses', 'course archive url slug', 'lifterlms' ), ) ); From 765ee772a4b7b6cc510912a564ad26954ac519c5 Mon Sep 17 00:00:00 2001 From: Brian Hogg Date: Tue, 6 Feb 2024 16:25:47 +0100 Subject: [PATCH 08/32] Removing old WP < 5.0 code --- includes/functions/llms-functions-l10n.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/includes/functions/llms-functions-l10n.php b/includes/functions/llms-functions-l10n.php index ecf5049725..6f64b064ab 100644 --- a/includes/functions/llms-functions-l10n.php +++ b/includes/functions/llms-functions-l10n.php @@ -20,12 +20,7 @@ */ function llms_get_locale( $domain = 'lifterlms' ) { - if ( function_exists( 'determine_locale' ) ) { - $locale = determine_locale(); - } else { - // @TODO: This can be removed when minimum supported version is 5.0. - $locale = is_admin() ? get_user_locale() : get_locale(); - } + $locale = determine_locale(); /** * Filter the plugin's locale From f68970ece83a4a02a95a81c535a84b5f252857db Mon Sep 17 00:00:00 2001 From: Brian Hogg Date: Tue, 6 Feb 2024 16:27:03 +0100 Subject: [PATCH 09/32] Do not use the translations for the courses (single and archive) url slugs, to avoid issues when visiting Settings > Permalink as a user with a different language than the site --- includes/class.llms.post-types.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/includes/class.llms.post-types.php b/includes/class.llms.post-types.php index b3070b08d4..8b61ea756d 100644 --- a/includes/class.llms.post-types.php +++ b/includes/class.llms.post-types.php @@ -446,6 +446,7 @@ public static function register_post_type( $name, $data ) { * @return void */ public static function register_post_types() { + $permalinks = llms_get_permalink_structure(); // Course. $catalog_id = llms_get_page_id( 'shop' ); @@ -477,13 +478,13 @@ public static function register_post_types() { 'exclude_from_search' => false, 'hierarchical' => false, 'rewrite' => array( - 'slug' => _x( 'course', 'course url slug', 'lifterlms' ), + 'slug' => $permalinks['course_base'], 'with_front' => false, 'feeds' => true, ), 'query_var' => true, 'supports' => array( 'title', 'author', 'editor', 'thumbnail', 'comments', 'custom-fields', 'page-attributes', 'revisions', 'llms-clone-post', 'llms-export-post', 'llms-sales-page' ), - 'has_archive' => ( $catalog_id && get_post( $catalog_id ) ) ? get_page_uri( $catalog_id ) : _x( 'courses', 'course archive url slug', 'lifterlms' ), + 'has_archive' => ( $catalog_id && get_post( $catalog_id ) ) ? get_page_uri( $catalog_id ) : $permalinks['courses_base'], 'show_in_nav_menus' => true, 'menu_position' => 52, ) From bba738fed6b53ff08aa464aef60add1a48b2101c Mon Sep 17 00:00:00 2001 From: Brian Hogg Date: Wed, 7 Feb 2024 09:29:21 +0100 Subject: [PATCH 10/32] Adding remaining always-show slug translation options --- .../admin/class-llms-admin-permalinks.php | 100 ++++++++++++++++++ includes/class.llms.post-types.php | 22 ++-- includes/functions/llms-functions-l10n.php | 10 ++ 3 files changed, 122 insertions(+), 10 deletions(-) diff --git a/includes/admin/class-llms-admin-permalinks.php b/includes/admin/class-llms-admin-permalinks.php index a23b14b04c..dbc7f0f4e8 100644 --- a/includes/admin/class-llms-admin-permalinks.php +++ b/includes/admin/class-llms-admin-permalinks.php @@ -75,6 +75,106 @@ public function settings() { + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/includes/class.llms.post-types.php b/includes/class.llms.post-types.php index 8b61ea756d..a90105e470 100644 --- a/includes/class.llms.post-types.php +++ b/includes/class.llms.post-types.php @@ -553,7 +553,7 @@ public static function register_post_types() { 'show_in_menu' => 'edit.php?post_type=course', 'hierarchical' => false, 'rewrite' => array( - 'slug' => _x( 'lesson', 'lesson url slug', 'lifterlms' ), + 'slug' => $permalinks['lesson_base'], 'with_front' => false, 'feeds' => true, ), @@ -592,7 +592,7 @@ public static function register_post_types() { 'show_in_menu' => 'edit.php?post_type=course', 'hierarchical' => false, 'rewrite' => array( - 'slug' => _x( 'quiz', 'quiz url slug', 'lifterlms' ), + 'slug' => $permalinks['quiz_base'], 'with_front' => false, 'feeds' => true, ), @@ -903,7 +903,7 @@ public static function register_post_types() { array( 'map_meta_cap' => true, ), - _x( 'certificate-template', 'slug', 'lifterlms' ), + $permalinks['certificate_template_base'], /** * Filters the WordPress user capability required for a user to manage certificate templates on the admin panel. * @@ -936,7 +936,7 @@ public static function register_post_types() { 'capabilities' => self::get_post_type_caps( 'my_certificate' ), 'map_meta_cap' => false, ), - _x( 'certificate', 'slug', 'lifterlms' ), + $permalinks['certificate_base'], /** * Filters the needed capability to generate and allow a UI for managing `llms_my_certificate` post type in the admin. * @@ -1276,6 +1276,8 @@ public static function register_taxonomy( $name, $object, $data ) { */ public static function register_taxonomies() { + $permalinks = llms_get_permalink_structure(); + // Course cat. self::register_taxonomy( 'course_cat', @@ -1301,7 +1303,7 @@ public static function register_taxonomies() { 'show_admin_column' => true, 'show_ui' => true, 'rewrite' => array( - 'slug' => _x( 'course-category', 'slug', 'lifterlms' ), + 'slug' => $permalinks['course_category_base'], 'with_front' => false, 'hierarchical' => true, ), @@ -1334,7 +1336,7 @@ public static function register_taxonomies() { 'show_admin_column' => true, 'show_ui' => true, 'rewrite' => array( - 'slug' => _x( 'course-difficulty', 'slug', 'lifterlms' ), + 'slug' => $permalinks['course_difficulty_base'], 'with_front' => false, ), 'show_in_llms_rest' => true, @@ -1366,7 +1368,7 @@ public static function register_taxonomies() { 'show_admin_column' => true, 'show_ui' => true, 'rewrite' => array( - 'slug' => _x( 'course-tag', 'slug', 'lifterlms' ), + 'slug' => $permalinks['course_tag_base'], 'with_front' => false, ), 'show_in_llms_rest' => true, @@ -1398,7 +1400,7 @@ public static function register_taxonomies() { 'show_admin_column' => true, 'show_ui' => true, 'rewrite' => array( - 'slug' => _x( 'course-track', 'slug', 'lifterlms' ), + 'slug' => $permalinks['course_track_base'], 'with_front' => false, 'hierarchical' => true, ), @@ -1432,7 +1434,7 @@ public static function register_taxonomies() { 'query_var' => true, 'show_admin_column' => true, 'rewrite' => array( - 'slug' => _x( 'membership-category', 'slug', 'lifterlms' ), + 'slug' => $permalinks['membership_category_base'], 'with_front' => false, 'hierarchical' => true, ), @@ -1466,7 +1468,7 @@ public static function register_taxonomies() { 'query_var' => true, 'show_admin_column' => true, 'rewrite' => array( - 'slug' => _x( 'membership-tag', 'slug', 'lifterlms' ), + 'slug' => $permalinks['membership_tag_base'], 'with_front' => false, ), 'show_in_llms_rest' => true, diff --git a/includes/functions/llms-functions-l10n.php b/includes/functions/llms-functions-l10n.php index 6f64b064ab..29e786e5e0 100644 --- a/includes/functions/llms-functions-l10n.php +++ b/includes/functions/llms-functions-l10n.php @@ -121,6 +121,16 @@ function llms_get_permalink_structure() { array( 'course_base' => _x( 'course', 'course url slug', 'lifterlms' ), 'courses_base' => _x( 'courses', 'course archive url slug', 'lifterlms' ), + 'lesson_base' => _x( 'lesson', 'lesson url slug', 'lifterlms' ), + 'quiz_base' => _x( 'quiz', 'quiz url slug', 'lifterlms' ), + 'certificate_template_base' => _x( 'certificate-template', 'slug', 'lifterlms' ), + 'certificate_base' => _x( 'certificate', 'slug', 'lifterlms' ), + 'course_category_base' => _x( 'course-category', 'slug', 'lifterlms' ), + 'course_tag_base' => _x( 'course-tag', 'slug', 'lifterlms' ), + 'course_track_base' => _x( 'course-track', 'slug', 'lifterlms' ), + 'course_difficulty_base' => _x( 'course-difficulty', 'slug', 'lifterlms' ), + 'membership_category_base' => _x( 'membership-category', 'slug', 'lifterlms' ), + 'membership_tag_base' => _x( 'membership-tag', 'slug', 'lifterlms' ), ) ); From 4254dee371efa53b6d684b2315d6c5a7ffb4b0b4 Mon Sep 17 00:00:00 2001 From: Brian Hogg Date: Wed, 7 Feb 2024 09:38:09 +0100 Subject: [PATCH 11/32] Adding membership base --- includes/admin/class-llms-admin-permalinks.php | 11 +++++++++++ includes/class.llms.post-types.php | 2 +- includes/functions/llms-functions-l10n.php | 1 + 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/includes/admin/class-llms-admin-permalinks.php b/includes/admin/class-llms-admin-permalinks.php index dbc7f0f4e8..fb1a823dd7 100644 --- a/includes/admin/class-llms-admin-permalinks.php +++ b/includes/admin/class-llms-admin-permalinks.php @@ -75,6 +75,17 @@ public function settings() { + + + + + + + + +