Skip to content

Commit

Permalink
Merge pull request #2593 from gocodebox/fix/i18n-dashboard-language-a…
Browse files Browse the repository at this point in the history
…nd-slugs

Fix i18n dashboard language strings and allow customizing of permalink slugs
  • Loading branch information
ideadude committed Apr 16, 2024
2 parents 9e9852e + e023c83 commit 008274e
Show file tree
Hide file tree
Showing 16 changed files with 451 additions and 98 deletions.
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' ) );
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

0 comments on commit 008274e

Please sign in to comment.