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

Fix i18n dashboard language strings and allow customizing of permalink slugs #2593

Merged
merged 36 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
13bae57
Working version of fixing the admin menu items, and things like the r…
brianhogg Feb 5, 2024
1cab2bf
Undoing changes that aren't required to fix admin menu and notice lan…
brianhogg Feb 6, 2024
8da8c62
Load localize before loading of block settings
brianhogg Feb 6, 2024
48c5e61
WIP: Starting permalink settings with default loading and saved optio…
brianhogg Feb 6, 2024
7b2c0c2
Unload only the given domain not lifterlms each time
brianhogg Feb 6, 2024
2a0e6bc
Refactor to ensure locale set to the site locale when loading permali…
brianhogg Feb 6, 2024
00914b3
Adding courses archive setting
brianhogg Feb 6, 2024
765ee77
Removing old WP < 5.0 code
brianhogg Feb 6, 2024
f68970e
Do not use the translations for the courses (single and archive) url …
brianhogg Feb 6, 2024
bba738f
Adding remaining always-show slug translation options
brianhogg Feb 7, 2024
4254dee
Adding membership base
brianhogg Feb 7, 2024
139981b
Removing unneeded whitespace
brianhogg Feb 8, 2024
188d03b
Conditionally show archive page and helpful message if course or memb…
brianhogg Feb 8, 2024
dfa67c3
Removing locale switch when loading the permalink structure. By this …
brianhogg Feb 8, 2024
ed5c66b
Saving permalink settings
brianhogg Feb 8, 2024
f9da343
On update, ensure the slug defaults are set in the site language. The…
brianhogg Feb 8, 2024
dfbe58a
Formatting fixes
brianhogg Feb 8, 2024
6708b91
PHPCS Fix. May want to ignore this.
brianhogg Feb 8, 2024
a1cebbc
Sanitize nonce
brianhogg Feb 8, 2024
db1fa28
Merge branch 'dev' into fix/i18n-dashboard-language-and-slugs
brianhogg Feb 8, 2024
acb044f
Test for slug editing and menu language display
brianhogg Feb 9, 2024
7b8f705
Fixing typo when saving lesson base slug
brianhogg Feb 12, 2024
f866e69
Adding domain to slug field labels
brianhogg Feb 13, 2024
9d4723e
Add option to specify the textdomain, so add-ons can use the locale s…
brianhogg Feb 13, 2024
3a42a84
Fix comment
brianhogg Feb 13, 2024
34cc161
Styling fix
brianhogg Feb 13, 2024
23b1427
Adding plugin and language dirs to switch/restore locale calls
brianhogg Feb 14, 2024
45cb587
Moving permalink load logic into the class itself
brianhogg Feb 15, 2024
507da92
Merge branch 'dev' into fix/i18n-dashboard-language-and-slugs
brianhogg Feb 15, 2024
6b927cf
Removing E2E tests. Due to tests being inter-dependent and race condi…
brianhogg Feb 15, 2024
8626a74
Merge branch 'dev' into fix/i18n-dashboard-language-and-slugs
brianhogg Feb 16, 2024
c35516e
Avoid checking for file existence when loading script translations
brianhogg Feb 16, 2024
2d66d35
Adding changelogs
brianhogg Feb 16, 2024
0c360c1
Adding dev changelog
brianhogg Feb 16, 2024
647716d
Removing test that does not appear to reflect reality of loading the …
brianhogg Feb 16, 2024
e023c83
Merge branch 'dev' into fix/i18n-dashboard-language-and-slugs
ideadude Apr 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changelogs/fix_i18n-dashboard-language-and-slugs-1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
significance: minor
type: added
links:
- "#2429"
- "#2525"
entry: Loads translation files later for compatibility with plugins like Loco
Translate.
5 changes: 5 additions & 0 deletions .changelogs/fix_i18n-dashboard-language-and-slugs-2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
significance: patch
type: fixed
links:
- "#2525"
entry: LifterLMS block editor strings now appear in the user's language.
4 changes: 4 additions & 0 deletions .changelogs/fix_i18n-dashboard-language-and-slugs-3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
significance: minor
type: added
entry: Adds settings in the Permalinks page to edit the custom post type and
taxonomy slugs. Slugs are saved in the site language on install on update.
5 changes: 5 additions & 0 deletions .changelogs/fix_i18n-dashboard-language-and-slugs-4.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
significance: patch
type: added
entry: Adds `llms_switch_to_site_locale` and `llms_restore_locale` to help
LifterLMS add-ons switch to the site language when getting translation
strings.
5 changes: 5 additions & 0 deletions .changelogs/fix_i18n-dashboard-language-and-slugs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
significance: patch
type: fixed
links:
- "#2324"
entry: Fixed user's language setting not honored on backend.
15 changes: 4 additions & 11 deletions class-lifterlms.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,22 @@ 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
*/
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();

$this->init_assets();

$this->query = new LLMS_Query();

// Hooks.
register_activation_hook( __FILE__, array( 'LLMS_Install', 'install' ) );
Copy link
Contributor Author

@brianhogg brianhogg Feb 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The register_activation_hook needs the main plugin filename. Since the class filename is being used here, this would never run. It's been moved into the main plugin file instead so the verify_permalinks runs on activation.

add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'add_action_links' ), 10, 1 );

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 );
Expand Down Expand Up @@ -415,9 +407,10 @@ public function add_action_links( $links ) {
*/
public function localize() {

require_once LLMS_PLUGIN_DIR . 'includes/functions/llms-functions-l10n.php';
llms_load_textdomain( 'lifterlms' );

}

}


269 changes: 269 additions & 0 deletions includes/admin/class-llms-admin-permalinks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
<?php
/**
* LLMS_Admin_Header class file
*
* @package LifterLMS/Admin/Classes
*
* @since [version]
* @version [version]
*/

defined( 'ABSPATH' ) || exit;

/**
* Permalink settings class
*/
class LLMS_Admin_Permalinks {

/**
* Permalink settings.
*
* @var array
*/
private $permalinks = array();

/**
* Constructor.
*
* @since [version]
*
* @return void
*/
public function __construct() {
add_action( 'current_screen', array( $this, 'load_on_permalinks_screen' ) );
}

/**
* Ensure we're on the permalinks screen.
*
* @since [version]
*
* @return void
*/
public function load_on_permalinks_screen() {
$screen = get_current_screen();

if ( $screen && 'options-permalink' === $screen->id ) {
$this->settings_init();
$this->settings_save();
}
}

/**
* 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() {
?>
<p><?php _e( 'LifterLMS uses custom post types and taxonomies to organize your courses and memberships. You can customize the URLs for these items here.', 'lifterlms' ); ?></p>

<?php
$course_catalog_id = llms_get_page_id( 'courses' );
if ( $course_catalog_id && get_post( $course_catalog_id ) ) {
?>
<p>
<?php echo esc_html__( 'Note: The Courses Catalog is currently set to a static page.', 'lifterlms' ); ?>
<a href="<?php echo esc_url( get_edit_post_link( $course_catalog_id ) ); ?>">
<?php echo esc_html__( 'You can edit the page slug to change its location.', 'lifterlms' ); ?>
</a>
</p>
<?php
}

$memberships_catalog_id = llms_get_page_id( 'memberships' );
if ( $memberships_catalog_id && get_post( $memberships_catalog_id ) ) {
?>
<p>
<?php echo esc_html__( 'Note: The Memberships Catalog is currently set to a static page.', 'lifterlms' ); ?>
<a href="<?php echo esc_url( get_edit_post_link( $memberships_catalog_id ) ); ?>">
<?php echo esc_html__( 'You can edit the page slug to change its location.', 'lifterlms' ); ?>
</a>
</p>
<?php
}
?>

<table class="form-table" role="presentation">
<tbody>
<tr>
<th>
<label for="course_base">
<?php esc_html_e( 'Course Post Type', 'lifterlms' ); ?>
</label>
</th>
<td>
<input name="llms_course_base" id="course_base" type="text" value="<?php echo esc_attr( $this->permalinks['course_base'] ); ?>" class="regular-text code" required>
</td>
</tr>
<?php if ( ! $course_catalog_id || ! get_post( $course_catalog_id ) ) : ?>
<tr>
<th>
<label for="courses_base">
<?php esc_html_e( 'Course Archive base', 'lifterlms' ); ?>
</label>
</th>
<td>
<input name="llms_courses_base" id="courses_base" type="text" value="<?php echo esc_attr( $this->permalinks['courses_base'] ); ?>" class="regular-text code" required>
</td>
</tr>
<?php endif; ?>
<?php if ( ! $memberships_catalog_id || ! get_post( $memberships_catalog_id ) ) : ?>
<tr>
<th>
<label for="memberships_base">
<?php esc_html_e( 'Memberships Archive base', 'lifterlms' ); ?>
</label>
</th>
<td>
<input name="llms_memberships_base" id="memberships_base" type="text" value="<?php echo esc_attr( $this->permalinks['memberships_base'] ); ?>" class="regular-text code" required>
</td>
</tr>
<?php endif; ?>
<tr>
<th>
<label for="lesson_base">
<?php esc_html_e( 'Lesson Post Type', 'lifterlms' ); ?>
</label>
</th>
<td>
<input name="llms_lesson_base" id="lesson_base" type="text" value="<?php echo esc_attr( $this->permalinks['lesson_base'] ); ?>" class="regular-text code" required>
</td>
</tr>
<tr>
<th>
<label for="quiz_base">
<?php esc_html_e( 'Quiz Post Type', 'lifterlms' ); ?>
</label>
</th>
<td>
<input name="llms_quiz_base" id="quiz_base" type="text" value="<?php echo esc_attr( $this->permalinks['quiz_base'] ); ?>" class="regular-text code" required>
</td>
</tr>
<tr>
<th>
<label for="certificate_template_base">
<?php esc_html_e( 'Certificate Template Post Type', 'lifterlms' ); ?>
</label>
</th>
<td>
<input name="llms_certificate_template_base" id="certificate_template_base" type="text" value="<?php echo esc_attr( $this->permalinks['certificate_template_base'] ); ?>" class="regular-text code" required>
</td>
</tr>
<tr>
<th>
<label for="certificate_base">
<?php esc_html_e( 'Earned Certificate Post Type', 'lifterlms' ); ?>
</label>
</th>
<td>
<input name="llms_certificate_base" id="certificate_base" type="text" value="<?php echo esc_attr( $this->permalinks['certificate_base'] ); ?>" class="regular-text code" required>
</td>
</tr>
<tr>
<th>
<label for="course_category_base">
<?php esc_html_e( 'Course Category base', 'lifterlms' ); ?>
</label>
</th>
<td>
<input name="llms_course_category_base" id="course_category_base" type="text" value="<?php echo esc_attr( $this->permalinks['course_category_base'] ); ?>" class="regular-text code" required>
</td>
</tr>
<tr>
<th>
<label for="course_tag_base">
<?php esc_html_e( 'Course Tag base', 'lifterlms' ); ?>
</label>
</th>
<td>
<input name="llms_course_tag_base" id="course_tag_base" type="text" value="<?php echo esc_attr( $this->permalinks['course_tag_base'] ); ?>" class="regular-text code" required>
</td>
</tr>
<tr>
<th>
<label for="course_track_base">
<?php esc_html_e( 'Course Track base', 'lifterlms' ); ?>
</label>
</th>
<td>
<input name="llms_course_track_base" id="course_track_base" type="text" value="<?php echo esc_attr( $this->permalinks['course_track_base'] ); ?>" class="regular-text code" required>
</td>
</tr>
<tr>
<th>
<label for="course_difficulty_base">
<?php esc_html_e( 'Course Difficulty base', 'lifterlms' ); ?>
</label>
</th>
<td>
<input name="llms_course_difficulty_base" id="course_difficulty_base" type="text" value="<?php echo esc_attr( $this->permalinks['course_difficulty_base'] ); ?>" class="regular-text code" required>
</td>
</tr>
<tr>
<th>
<label for="membership_category_base">
<?php esc_html_e( 'Membership Category base', 'lifterlms' ); ?>
</label>
</th>
<td>
<input name="llms_membership_category_base" id="membership_category_base" type="text" value="<?php echo esc_attr( $this->permalinks['membership_category_base'] ); ?>" class="regular-text code" required>
</td>
</tr>
<tr>
<th>
<label for="membership_tag_base">
<?php esc_html_e( 'Membership Tag base', 'lifterlms' ); ?>
</label>
</th>
<td>
<input name="llms_membership_tag_base" id="membership_tag_base" type="text" value="<?php echo esc_attr( $this->permalinks['membership_tag_base'] ); ?>" class="regular-text code" required>
</td>
</tr>
<?php do_action( 'llms_permalink_setting_fields' ); ?>
</tbody>
</table>

<?php wp_nonce_field( 'llms-permalinks', 'llms-permalinks-nonce' ); ?>
<?php
}

/**
* Save the permalink settings
*/
public function settings_save() {
if ( ! is_admin() ) {
return;
}

if ( isset( $_POST['llms-permalinks-nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['llms-permalinks-nonce'] ), 'llms-permalinks' ) ) {
llms_switch_to_site_locale();

$permalinks = llms_get_permalink_structure();

$permalinks['course_base'] = isset( $_POST['llms_course_base'] ) ? sanitize_text_field( wp_unslash( $_POST['llms_course_base'] ) ) : $permalinks['course_base'];
$permalinks['courses_base'] = isset( $_POST['llms_courses_base'] ) ? sanitize_text_field( wp_unslash( $_POST['llms_courses_base'] ) ) : $permalinks['courses_base'];
$permalinks['memberships_base'] = isset( $_POST['llms_memberships_base'] ) ? sanitize_text_field( wp_unslash( $_POST['llms_memberships_base'] ) ) : $permalinks['memberships_base'];
$permalinks['lesson_base'] = isset( $_POST['llms_lesson_base'] ) ? sanitize_text_field( wp_unslash( $_POST['llms_lesson_base'] ) ) : $permalinks['lesson_base'];
$permalinks['quiz_base'] = isset( $_POST['llms_quiz_base'] ) ? sanitize_text_field( wp_unslash( $_POST['llms_quiz_base'] ) ) : $permalinks['quiz_base'];
$permalinks['certificate_template_base'] = isset( $_POST['llms_certificate_template_base'] ) ? sanitize_text_field( wp_unslash( $_POST['llms_certificate_template_base'] ) ) : $permalinks['certificate_template_base'];
$permalinks['certificate_base'] = isset( $_POST['llms_certificate_base'] ) ? sanitize_text_field( wp_unslash( $_POST['llms_certificate_base'] ) ) : $permalinks['certificate_base'];
$permalinks['course_category_base'] = isset( $_POST['llms_course_category_base'] ) ? sanitize_text_field( wp_unslash( $_POST['llms_course_category_base'] ) ) : $permalinks['course_category_base'];
$permalinks['course_tag_base'] = isset( $_POST['llms_course_tag_base'] ) ? sanitize_text_field( wp_unslash( $_POST['llms_course_tag_base'] ) ) : $permalinks['course_tag_base'];
$permalinks['course_track_base'] = isset( $_POST['llms_course_track_base'] ) ? sanitize_text_field( wp_unslash( $_POST['llms_course_track_base'] ) ) : $permalinks['course_track_base'];
$permalinks['course_difficulty_base'] = isset( $_POST['llms_course_difficulty_base'] ) ? sanitize_text_field( wp_unslash( $_POST['llms_course_difficulty_base'] ) ) : $permalinks['course_difficulty_base'];
$permalinks['membership_category_base'] = isset( $_POST['llms_membership_category_base'] ) ? sanitize_text_field( wp_unslash( $_POST['llms_membership_category_base'] ) ) : $permalinks['membership_category_base'];
$permalinks['membership_tag_base'] = isset( $_POST['llms_membership_tag_base'] ) ? sanitize_text_field( wp_unslash( $_POST['llms_membership_tag_base'] ) ) : $permalinks['membership_tag_base'];

llms_set_permalink_structure( $permalinks );

llms_restore_locale();
}
}
}

return new LLMS_Admin_Permalinks();
10 changes: 1 addition & 9 deletions includes/class-llms-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,10 +680,6 @@ protected function set_script_translations( $script ) {
$plugin_data = get_plugin_data( $script['base_file'], false, false );
$domain = $plugin_data['TextDomain'];

// Setup the script's filename based on the md5 of it's relative path.
$relative_path = sprintf( '%1$s/%2$s%3$s', $script['path'], $script['file_name'], $script['extension'] );
$file = sprintf( '%1$s-%2$s-%3$s.json', $domain, llms_get_locale(), md5( $relative_path ) );

// Possible directories where the language files may be found.
$dirs = array(
llms_l10n_get_safe_directory(),
Expand All @@ -692,11 +688,7 @@ protected function set_script_translations( $script ) {
);

foreach ( $dirs as $dir ) {
// If the file exists, set the script translations.
if ( file_exists( sprintf( '%1$s/%2$s', $dir, $file ) ) ) {
wp_set_script_translations( $script['handle'], $domain, $dir );
break;
}
wp_set_script_translations( $script['handle'], $domain, $dir );
}

}
Expand Down
1 change: 1 addition & 0 deletions includes/class-llms-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ public function includes_admin() {
require_once LLMS_PLUGIN_DIR . 'includes/admin/class-llms-mailhawk.php';
require_once LLMS_PLUGIN_DIR . 'includes/admin/class-llms-sendwp.php';
require_once LLMS_PLUGIN_DIR . 'includes/forms/class-llms-forms-unsupported-versions.php';
require_once LLMS_PLUGIN_DIR . 'includes/admin/class-llms-admin-permalinks.php';

// Admin classes (files to be renamed).
require_once LLMS_PLUGIN_DIR . 'includes/admin/class.llms.admin.dashboard.php';
Expand Down