Skip to content

Commit

Permalink
WPML: Add custom fields to the config (#7588)
Browse files Browse the repository at this point in the history
  • Loading branch information
merkushin committed Apr 12, 2024
1 parent ae239a2 commit 16178aa
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
33 changes: 33 additions & 0 deletions includes/wpml/class-custom-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function init() {
add_filter( 'wpml_sync_custom_field_copied_value', array( $this, 'update_lesson_course_before_copied' ), 10, 4 );
add_filter( 'wpml_sync_custom_field_copied_value', array( $this, 'update_course_prerequisite_before_copied' ), 10, 4 );
add_filter( 'wpml_sync_custom_field_copied_value', array( $this, 'update_quiz_id_before_copied' ), 10, 4 );
add_filter( 'wpml_sync_custom_field_copied_value', array( $this, 'update_course_woocommerce_product_before_copied' ), 10, 4 );
}

/**
Expand Down Expand Up @@ -133,4 +134,36 @@ public function update_quiz_id_before_copied( $copied_value, $post_id_from, $pos

return $this->get_object_id( $quiz_id, 'quiz', false, $target_language_code );
}

/**
* Update course WooCommerce product before copied.
*
* @since $$next-version$$
*
* @internal
*
* @param mixed $copied_value Copied value.
* @param int $post_id_from Post ID from.
* @param int $post_id_to Post ID to.
* @param string $meta_key Meta key.
* @return mixed
*/
public function update_course_woocommerce_product_before_copied( $copied_value, $post_id_from, $post_id_to, $meta_key ) {
if ( '_course_woocommerce_product' !== $meta_key ) {
return $copied_value;
}

if ( empty( $copied_value ) ) {
return $copied_value;
}

$product_id = (int) $copied_value;

$target_language_code = $this->get_element_language_code( $post_id_to, 'course' );
if ( ! $target_language_code ) {
$target_language_code = $this->get_current_language();
}

return $this->get_object_id( $product_id, 'product', false, $target_language_code );
}
}
24 changes: 24 additions & 0 deletions tests/unit-tests/wpml/test-class-custom-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,28 @@ public function testUpdateQuizIdBeforeCopied_WhenCalled_ReturnsMatchingCourseFor

$this->assertSame( 4, $actual );
}

public function testUpdateCourseWooCommerceProductBeforeCopied_WhenCalled_ReturnsMatchingCourseForNewLesson() {
/* Arrange. */
$custom_fields = new Custom_Fields();

$language_code_filter = function () {
return 'a';
};
add_filter( 'wpml_element_language_code', $language_code_filter, 10, 0 );

$object_id_fitler = function () {
return 4;
};
add_filter( 'wpml_object_id', $object_id_fitler, 10, 0 );

/* Act. */
$actual = $custom_fields->update_course_woocommerce_product_before_copied( 1, 2, 3, '_course_woocommerce_product' );

/* Clean up & Assert. */
remove_filter( 'wpml_element_language_code', $language_code_filter );
remove_filter( 'wpml_object_id', $object_id_fitler );

$this->assertSame( 4, $actual );
}
}
38 changes: 37 additions & 1 deletion wpml-config.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
<wpml-config>
<custom-fields>
<custom-field action="translate">_question_right_answer</custom-field>
<custom-field action="translate">_question_wrong_answers</custom-field>
<custom-field action="translate">_sensei_email_description</custom-field>
<custom-field action="ignore">_quiz_id</custom-field>
<custom-field action="ignore">category</custom-field>
<custom-field action="ignore">number</custom-field>
<custom-field action="ignore">_lesson_order</custom-field>
<custom-field action="ignore">_question_order</custom-field>
<custom-field action="copy">_lesson_complexity</custom-field>
<custom-field action="copy">_lesson_length</custom-field>
<custom-field action="copy">_lesson_prerequisite</custom-field>
<custom-field action="copy">_lesson_course</custom-field>
<custom-field action="copy">_quiz_lesson</custom-field>
<custom-field action="copy">_lesson_quiz</custom-field>
<custom-field action="ignore">_quiz_id</custom-field>
<custom-field action="copy">_course_prerequisite</custom-field>
<custom-field action="copy">_course_video_embed</custom-field>
<custom-field action="copy">_course_featured</custom-field>
Expand All @@ -16,6 +23,35 @@
<custom-field action="copy">_sensei_self_enrollment_not_allowed</custom-field>
<custom-field action="copy">_open_access</custom-field>
<custom-field action="copy">disable_notification</custom-field>
<custom-field action="copy">_pagination</custom-field>
<custom-field action="copy">_pass_required</custom-field>
<custom-field action="copy">_question_grade</custom-field>
<custom-field action="copy">_quiz_grade_type</custom-field>
<custom-field action="copy">_quiz_has_questions</custom-field>
<custom-field action="copy">_random_order</custom-field>
<custom-field action="copy">_right_answer_count</custom-field>
<custom-field action="copy">_show_questions</custom-field>
<custom-field action="copy">_wrong_answer_count</custom-field>
<custom-field action="copy">_lesson_preview</custom-field>
<custom-field action="copy">_enable_quiz_reset</custom-field>
<custom-field action="copy">_course_enrolment_version</custom-field>
<custom-field action="copy">_answer_order</custom-field>
<custom-field action="copy">_quiz_timer</custom-field>
<custom-field action="copy">_enable_quiz_timer</custom-field>
<custom-field action="copy">_course_woocommerce_product</custom-field>
<custom-field action="copy">sensei_course_audience</custom-field>
<custom-field action="copy">sensei_course_skill_level</custom-field>
<custom-field action="copy">_course_expiration_length</custom-field>
<custom-field action="copy">_course_expires_on_date</custom-field>
<custom-field action="copy">_course_starts_on_date</custom-field>
<custom-field action="copy">_sensei_email_identifier</custom-field>
<custom-field action="copy">_sensei_email_is_pro</custom-field>
<custom-field action="copy">_sensei_email_type</custom-field>
<custom-field action="copy">_random_question_order</custom-field>
<custom-field action="copy">_quiz_passmark</custom-field>
<custom-field action="copy">_sensei_content_drip_type</custom-field>
<custom-field action="copy">_sensei_already_published</custom-field>
<custom-field action="copy">sensei_course_publish_lessons</custom-field>
</custom-fields>
<custom-types>
<custom-type translate="1">course</custom-type>
Expand Down

0 comments on commit 16178aa

Please sign in to comment.