Skip to content

Commit

Permalink
WPML: Fix course outline for a course translation (#7453)
Browse files Browse the repository at this point in the history
- Update `_lesson_course` meta field in translated (duplicated) lessons to point the new course.
- Update 'module' taxonomies for translated lessons.
- Create lesson translations (duplicates) if they don't exist yet.
  • Loading branch information
merkushin authored Feb 22, 2024
1 parent 5484f86 commit e332a28
Show file tree
Hide file tree
Showing 8 changed files with 516 additions and 94 deletions.
4 changes: 4 additions & 0 deletions changelog/fix-wpml-save-dependent-posts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Fix Course Outline for a course translated with WPML.
52 changes: 38 additions & 14 deletions config/psalm/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,14 @@
<code>$remote-&gt;version</code>
</UndefinedPropertyFetch>
</file>
<file src="includes/admin/content-duplicators/class-course-lessons-duplicator.php">
<PossiblyInvalidArgument occurrences="1">
<code>$lesson</code>
</PossiblyInvalidArgument>
<PossiblyInvalidPropertyFetch occurrences="1">
<code>$lesson-&gt;ID</code>
</PossiblyInvalidPropertyFetch>
</file>
<file src="includes/admin/content-duplicators/class-post-duplicator.php">
<DocblockTypeContradiction occurrences="1">
<code>is_wp_error( $new_post_id )</code>
Expand Down Expand Up @@ -486,6 +494,10 @@
<code>get_time</code>
<code>get_version_hash</code>
</PossiblyInvalidMethodCall>
<PossiblyInvalidPropertyFetch occurrences="2">
<code>$lesson-&gt;ID</code>
<code>$lesson-&gt;ID</code>
</PossiblyInvalidPropertyFetch>
<UndefinedInterfaceMethod occurrences="1">
<code>debug</code>
</UndefinedInterfaceMethod>
Expand Down Expand Up @@ -1346,12 +1358,10 @@
<code>$course_quizzes</code>
<code>$url</code>
</FalsableReturnStatement>
<InvalidArgument occurrences="7">
<InvalidArgument occurrences="5">
<code>$lesson_args</code>
<code>$lesson_args</code>
<code>$lesson_args</code>
<code>$lesson_id</code>
<code>$lesson_id</code>
</InvalidArgument>
<InvalidArrayOffset occurrences="4">
<code>$dimensions['height']</code>
Expand Down Expand Up @@ -1454,13 +1464,15 @@
<code>get_the_ID()</code>
<code>get_the_ID()</code>
</PossiblyFalseArgument>
<PossiblyInvalidArgument occurrences="4">
<PossiblyInvalidArgument occurrences="6">
<code>$category_output</code>
<code>$category_output</code>
<code>$category_output</code>
<code>$lesson_id</code>
<code>$lesson_id</code>
<code>$output</code>
</PossiblyInvalidArgument>
<PossiblyInvalidPropertyFetch occurrences="16">
<PossiblyInvalidPropertyFetch occurrences="21">
<code>$course_item-&gt;ID</code>
<code>$course_item-&gt;ID</code>
<code>$course_item-&gt;post_author</code>
Expand All @@ -1470,9 +1482,14 @@
<code>$course_item-&gt;post_title</code>
<code>$course_item-&gt;post_title</code>
<code>$lesson-&gt;ID</code>
<code>$lesson-&gt;ID</code>
<code>$lesson-&gt;ID</code>
<code>$lesson_item-&gt;ID</code>
<code>$post-&gt;ID</code>
<code>$post-&gt;post_type</code>
<code>$post_item-&gt;ID</code>
<code>$post_item-&gt;ID</code>
<code>$post_item-&gt;post_title</code>
<code>$post_item-&gt;post_title</code>
<code>$taxonomy_raw_obj-&gt;labels</code>
<code>$user_info-&gt;display_name</code>
Expand Down Expand Up @@ -1598,8 +1615,7 @@
<ArgumentTypeCoercion occurrences="1">
<code>$creds</code>
</ArgumentTypeCoercion>
<InvalidArgument occurrences="4">
<code>$lesson_id</code>
<InvalidArgument occurrences="3">
<code>true</code>
<code>true</code>
<code>true</code>
Expand Down Expand Up @@ -1651,8 +1667,9 @@
<code>get_the_ID()</code>
<code>wp_get_referer()</code>
</PossiblyFalseArgument>
<PossiblyInvalidArgument occurrences="5">
<PossiblyInvalidArgument occurrences="6">
<code>$category_output</code>
<code>$lesson_id</code>
<code>$tag_link</code>
<code>$user_id</code>
<code>$user_id</code>
Expand Down Expand Up @@ -2782,6 +2799,7 @@
<code>! is_array( $lessons )</code>
<code>is_numeric( $comments )</code>
</DocblockTypeContradiction>
<InvalidArgument occurrences="1"/>
<InvalidDocblockParamName occurrences="2">
<code>$course_id</code>
<code>$lesson_id</code>
Expand Down Expand Up @@ -2832,9 +2850,13 @@
<code>empty( $course_id ) || ! $course_id</code>
<code>empty( $lesson_id ) || ! $lesson_id</code>
</ParadoxicalCondition>
<PossiblyInvalidPropertyFetch occurrences="7">
<PossiblyInvalidPropertyFetch occurrences="11">
<code>$author-&gt;display_name</code>
<code>$existing_module_by_slug-&gt;term_id</code>
<code>$lesson-&gt;ID</code>
<code>$lesson-&gt;ID</code>
<code>$lesson-&gt;ID</code>
<code>$lesson-&gt;post_author</code>
<code>$module_course-&gt;ID</code>
<code>$module_course-&gt;post_author</code>
<code>$module_course-&gt;post_title</code>
Expand Down Expand Up @@ -3079,10 +3101,6 @@
<code>false</code>
<code>wp_date( get_option( 'date_format' ), $date-&gt;getTimestamp(), $timezone )</code>
</FalsableReturnStatement>
<InvalidArgument occurrences="2">
<code>$id</code>
<code>$lesson_id</code>
</InvalidArgument>
<InvalidFalsableReturnType occurrences="1">
<code>string</code>
</InvalidFalsableReturnType>
Expand Down Expand Up @@ -3153,7 +3171,13 @@
<code>get_permalink( $quiz_id )</code>
<code>get_permalink( $quiz_id )</code>
</PossiblyFalseArgument>
<PossiblyInvalidPropertyFetch occurrences="5">
<PossiblyInvalidArgument occurrences="2">
<code>$id</code>
<code>$lesson_id</code>
</PossiblyInvalidArgument>
<PossiblyInvalidPropertyFetch occurrences="7">
<code>$lesson-&gt;ID</code>
<code>$lesson-&gt;ID</code>
<code>$plugin_information-&gt;name</code>
<code>$plugin_information-&gt;short_description</code>
<code>$plugin_information-&gt;version</code>
Expand Down
82 changes: 41 additions & 41 deletions includes/admin/class-sensei-admin-notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ class Sensei_Admin_Notices {
const DISMISSED_NOTICES_USER_META = 'sensei-dismissed-notices';
const ALL_SENSEI_SCREENS_PLACEHOLDER = 'sensei*';

const ALLOWED_HTML = [
'strong' => [],
'em' => [],
'a' => [
'target' => [],
'href' => [],
'rel' => [],
],
];

const ALLOWED_CAP_CHECKS = [
const ALLOWED_HTML = array(
'strong' => array(),
'em' => array(),
'a' => array(
'target' => array(),
'href' => array(),
'rel' => array(),
),
);

const ALLOWED_CAP_CHECKS = array(
'activate_plugins',
'install_plugins',
'manage_options',
Expand All @@ -44,9 +44,9 @@ class Sensei_Admin_Notices {
'delete_plugins',
'edit_posts',
'edit_others_posts',
];
);

const SENSEI_SCREEN_IDS = [
const SENSEI_SCREEN_IDS = array(
'edit-course',
'edit-lesson',
'edit-question',
Expand All @@ -63,9 +63,9 @@ class Sensei_Admin_Notices {
'sensei-lms_page_sensei_grading',
'sensei-lms_page_sensei-tools',
'admin_page_lesson-order',
];
);

const OTHER_ALLOWED_SCREEN_IDS = [
const OTHER_ALLOWED_SCREEN_IDS = array(
'dashboard',
'update-core',
'themes',
Expand All @@ -76,7 +76,7 @@ class Sensei_Admin_Notices {
'plugins-network',
'woocommerce_page_wc-admin',
'woocommerce_page_wc-addons',
];
);

/**
* Instance of class.
Expand Down Expand Up @@ -112,8 +112,8 @@ public function init() {
return;
}

add_filter( 'admin_notices', [ $this, 'add_admin_notices' ] );
add_action( 'wp_ajax_sensei_dismiss_notice', [ $this, 'handle_notice_dismiss' ] );
add_filter( 'admin_notices', array( $this, 'add_admin_notices' ) );
add_action( 'wp_ajax_sensei_dismiss_notice', array( $this, 'handle_notice_dismiss' ) );
}

/**
Expand All @@ -140,7 +140,7 @@ protected function get_screen_id() {
*/
protected function get_notices( $max_age = null ) {
$cur_time = Sensei()->clock->now()->getTimestamp();
$transient_key = implode( '_', [ 'sensei_notices', Sensei()->version, determine_locale() ] );
$transient_key = implode( '_', array( 'sensei_notices', Sensei()->version, determine_locale() ) );
$data = get_transient( $transient_key );
$notices = false;
// If the data is too old, fetch it again.
Expand Down Expand Up @@ -170,17 +170,17 @@ protected function get_notices( $max_age = null ) {
$notices_response_body = json_decode( wp_remote_retrieve_body( $notices_response ), true );
if ( $notices_response_body && isset( $notices_response_body['notices'] ) ) {
$notices = $notices_response_body['notices'];
$cached_data = [
$cached_data = array(
'_fetched' => $cur_time,
'notices' => $notices,
];
);
set_transient( $transient_key, $cached_data, DAY_IN_SECONDS );
}
}
}

if ( ! $notices || ! is_array( $notices ) ) {
$notices = [];
$notices = array();
}

/**
Expand Down Expand Up @@ -232,10 +232,10 @@ public function add_admin_notices() {
*/
private function add_admin_notice( $notice_id, $notice ) {
if ( empty( $notice['actions'] ) || ! is_array( $notice['actions'] ) ) {
$notice['actions'] = [];
$notice['actions'] = array();
}

$notice_classes = [];
$notice_classes = array();
$notice_classes[] = 'sensei-notice--' . $notice['level'];

$is_dismissible = $notice['dismissible'];
Expand Down Expand Up @@ -302,7 +302,7 @@ private function add_admin_notice( $notice_id, $notice ) {
* @return array
*/
public function get_notices_to_display( $screen_id = null, $max_age = null ) {
$notices = [];
$notices = array();
foreach ( $this->get_notices( $max_age ) as $notice_id => $notice ) {
$notice = $this->normalize_notice( $notice );

Expand Down Expand Up @@ -332,7 +332,7 @@ public function get_notices_to_display( $screen_id = null, $max_age = null ) {
*/
private function check_notice_conditions( $notice, $screen_id = null ) {
if ( ! isset( $notice['conditions'] ) || ! is_array( $notice['conditions'] ) ) {
$notice['conditions'] = [];
$notice['conditions'] = array();
}

$has_screen_condition = false;
Expand Down Expand Up @@ -426,7 +426,7 @@ private function check_notice_conditions( $notice, $screen_id = null ) {
}

// If no screens condition was set, only show this message on Sensei screens.
if ( $can_see_notice && ! $has_screen_condition && ! $this->condition_check_screen( [ self::ALL_SENSEI_SCREENS_PLACEHOLDER ], $screen_id ) ) {
if ( $can_see_notice && ! $has_screen_condition && ! $this->condition_check_screen( array( self::ALL_SENSEI_SCREENS_PLACEHOLDER ), $screen_id ) ) {
$can_see_notice = false;
}

Expand All @@ -439,7 +439,7 @@ private function check_notice_conditions( $notice, $screen_id = null ) {
* @param string $min_version Minimum PHP version.
* @return bool
*/
private function condition_check_min_php( string $min_version ) : bool {
private function condition_check_min_php( string $min_version ): bool {
return version_compare( phpversion(), $min_version, '>=' );
}

Expand All @@ -449,7 +449,7 @@ private function condition_check_min_php( string $min_version ) : bool {
* @param string $min_version Minimum WP version.
* @return bool
*/
private function condition_check_min_wp( string $min_version ) : bool {
private function condition_check_min_wp( string $min_version ): bool {
return version_compare( get_bloginfo( 'version' ), $min_version, '>=' );
}

Expand All @@ -459,7 +459,7 @@ private function condition_check_min_wp( string $min_version ) : bool {
* @param array $allowed_caps Array of capabilities that the user must have.
* @return bool
*/
private function condition_check_capabilities( array $allowed_caps ) : bool {
private function condition_check_capabilities( array $allowed_caps ): bool {
$condition_pass = true;

foreach ( $allowed_caps as $cap ) {
Expand All @@ -484,7 +484,7 @@ private function condition_check_capabilities( array $allowed_caps ) : bool {
*
* @return bool
*/
private function condition_check_screen( array $allowed_screens, $screen_id = null ) : bool {
private function condition_check_screen( array $allowed_screens, $screen_id = null ): bool {
/**
* Filter the array of screen IDs that are part of Sensei, and where we should show Sensei notices on.
*
Expand Down Expand Up @@ -522,7 +522,7 @@ private function condition_check_screen( array $allowed_screens, $screen_id = nu
*
* @return bool
*/
private function condition_installed_since( $installed_since ) : bool {
private function condition_installed_since( $installed_since ): bool {
$installed_at = get_option( 'sensei_installed_at' );
if ( $installed_since && is_string( $installed_since ) ) {
$installed_since = strtotime( '-' . $installed_since );
Expand All @@ -543,7 +543,7 @@ private function condition_installed_since( $installed_since ) : bool {
*
* @return bool
*/
private function condition_check_date_range( ?string $start_date_str, ?string $end_date_str ) : bool {
private function condition_check_date_range( ?string $start_date_str, ?string $end_date_str ): bool {
$now = new DateTime();

// Defaults to WP timezone, but can be overridden by passing string that includes timezone.
Expand Down Expand Up @@ -573,7 +573,7 @@ private function condition_check_date_range( ?string $start_date_str, ?string $e
*
* @return bool
*/
private function condition_check_plugin( array $allowed_plugins ) : bool {
private function condition_check_plugin( array $allowed_plugins ): bool {
$condition_pass = true;
$active_plugins = $this->get_active_plugins();

Expand Down Expand Up @@ -646,7 +646,7 @@ protected function get_active_plugins() {
*/
private function normalize_notice( $notice ) {
if ( ! isset( $notice['conditions'] ) || ! is_array( $notice['conditions'] ) ) {
$notice['conditions'] = [];
$notice['conditions'] = array();
}

if ( ! isset( $notice['type'] ) ) {
Expand All @@ -655,13 +655,13 @@ private function normalize_notice( $notice ) {

if ( 'site-wide' === $notice['type'] ) {
// Only admins can see and manage site-wide notifications.
$notice['conditions'][] = [
$notice['conditions'][] = array(
'type' => 'user_cap',
'capabilities' => [ 'manage_options' ],
];
'capabilities' => array( 'manage_options' ),
);
}

$notice_levels = [ 'error', 'warning', 'success', 'info' ];
$notice_levels = array( 'error', 'warning', 'success', 'info' );
if ( ! isset( $notice['level'] ) || ! in_array( $notice['level'], $notice_levels, true ) ) {
$notice['level'] = 'info';
}
Expand Down Expand Up @@ -698,10 +698,10 @@ private function get_dismissed_notices( $is_user_notification ) {
if ( $is_user_notification ) {
$dismissed_notices = get_user_meta( get_current_user_id(), self::DISMISSED_NOTICES_USER_META, true );
if ( ! $dismissed_notices ) {
$dismissed_notices = [];
$dismissed_notices = array();
}
} else {
$dismissed_notices = get_option( self::DISMISSED_NOTICES_OPTION, [] );
$dismissed_notices = get_option( self::DISMISSED_NOTICES_OPTION, array() );
}

return $dismissed_notices;
Expand Down
Loading

0 comments on commit e332a28

Please sign in to comment.