Created
August 31, 2026 17:07
-
-
Save rickalday/af823584e7e7d0a1d20701881872a35f to your computer and use it in GitHub Desktop.
Prevent LearnDash Notifications from matching notifications created for a different WPML language translation of the same course.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_filter( 'learndash_notifications_are_triggering_objects_valid', 'ldn_require_same_wpml_language_after_compat', 9999, 4 ); | |
| function ldn_require_same_wpml_language_after_compat( $valid, $trigger, $notification, $args ) { | |
| // Only act when the notification was already considered valid. | |
| if ( ! $valid ) { | |
| return $valid; | |
| } | |
| if ( ! is_object( $notification ) || ! is_array( $args ) ) { | |
| return $valid; | |
| } | |
| // If WPML isn't present, do nothing. | |
| if ( ! has_filter( 'wpml_post_language_details' ) && ! has_filter( 'wpml_element_language_code' ) ) { | |
| return $valid; | |
| } | |
| $get_lang = function ( $post_id ) { | |
| $post_id = absint( $post_id ); | |
| if ( $post_id <= 0 ) { | |
| return ''; | |
| } | |
| // Try post language details first. | |
| $details = apply_filters( 'wpml_post_language_details', null, $post_id ); | |
| if ( is_array( $details ) ) { | |
| if ( ! empty( $details['language_code'] ) ) { | |
| return (string) $details['language_code']; | |
| } | |
| if ( ! empty( $details['language'] ) ) { | |
| return (string) $details['language']; | |
| } | |
| if ( ! empty( $details['code'] ) ) { | |
| return (string) $details['code']; | |
| } | |
| } | |
| // Fallback to element language code filter. | |
| $code = apply_filters( 'wpml_element_language_code', '', $post_id ); | |
| if ( is_string( $code ) && $code !== '' ) { | |
| return $code; | |
| } | |
| return ''; | |
| }; | |
| // Determine notification post ID. | |
| $notif_post_id = 0; | |
| if ( isset( $notification->post ) && ( is_object( $notification->post ) || is_int( $notification->post ) ) ) { | |
| $notif_post_id = is_object( $notification->post ) ? ( isset( $notification->post->ID ) ? (int) $notification->post->ID : 0 ) : (int) $notification->post; | |
| } elseif ( isset( $notification->id ) ) { | |
| $notif_post_id = (int) $notification->id; | |
| } | |
| if ( ! $notif_post_id ) { | |
| return $valid; | |
| } | |
| $notif_lang = $get_lang( $notif_post_id ); | |
| if ( '' === $notif_lang ) { | |
| // Couldn't determine language; don't interfere. | |
| return $valid; | |
| } | |
| $checks = [ | |
| 'group_id' => 'groups', | |
| 'course_id' => 'sfwd-courses', | |
| 'lesson_id' => 'sfwd-lessons', | |
| 'topic_id' => 'sfwd-topic', | |
| 'quiz_id' => 'sfwd-quiz', | |
| ]; | |
| foreach ( $checks as $arg_key => $post_type ) { | |
| if ( empty( $args[ $arg_key ] ) ) { | |
| continue; | |
| } | |
| $raw = $args[ $arg_key ]; | |
| if ( ! is_int( $raw ) && ! is_string( $raw ) ) { | |
| continue; | |
| } | |
| $arg_id = (int) $raw; | |
| if ( $arg_id <= 0 ) { | |
| continue; | |
| } | |
| $arg_lang = $get_lang( $arg_id ); | |
| if ( $arg_lang === '' ) { | |
| continue; | |
| } | |
| // If languages differ, prevent this notification from matching. | |
| if ( $notif_lang !== $arg_lang ) { | |
| return false; | |
| } | |
| } | |
| return $valid; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment