diff --git a/README.md b/README.md
index 16d60d8..b68813b 100644
--- a/README.md
+++ b/README.md
@@ -92,6 +92,10 @@ If you really like what we do or want to thank us for our quick work, feel free
## Changelog
+* Version 1.1.6 :
+ * On editor load, query only the current post and not all posts, if one is selected.
+ * Replace big query (1000) posts by only 100 posts.
+ * Use Choices.js to enable search on select, load only searched posts.
* Version 1.1.5 :
* Allow to customize query_var and rewrite keyword
* Security, add some missing sanitizing
diff --git a/inc/class.admin.php b/inc/class.admin.php
index 88afd53..a1ff092 100644
--- a/inc/class.admin.php
+++ b/inc/class.admin.php
@@ -73,14 +73,23 @@ public function addRessources( $hook_suffix = '' ) {
( 'post.php' === $hook_suffix && isset( $_GET['post'] ) && SPTRANS_CPT === $post->post_type ) ||
( 'edit.php' === $hook_suffix && SPTRANS_CPT === $_GET['post_type'] )
) {
- //wp_enqueue_style( 'admin-translation', SPTRANS_URL . '/ressources/admin.css', [], SPTRANS_VERSION, 'all' );
+
+ // Choices.js
+ wp_enqueue_style( 'choices-js-choices', SPTRANS_URL . '/ressources/vendors/choices.js@11.1.0/choices.min.css', [], SPTRANS_VERSION, 'all' );
+ wp_enqueue_script( 'choices-js', SPTRANS_URL . '/ressources/vendors/choices.js@11.1.0/choices.min.js', [], SPTRANS_VERSION, true );
+
+ wp_enqueue_style( 'admin-translation', SPTRANS_URL . '/ressources/admin.css', [], SPTRANS_VERSION, 'all' );
wp_enqueue_script( 'admin-translation', SPTRANS_URL . '/ressources/admin.js', [ 'jquery' ], SPTRANS_VERSION, true );
wp_localize_script(
'admin-translation',
'translationL10n',
[
- 'successText' => __( 'This translation is unique, fine...', 'punctual-translation' ),
- 'errorText' => __( 'Duplicate translation detected !', 'punctual-translation' ),
+ 'successText' => __( 'This translation is unique, fine...', 'punctual-translation' ),
+ 'errorText' => __( 'Duplicate translation detected !', 'punctual-translation' ),
+ 'searchPlaceholder' => __( 'Search for an article...', 'punctual-translation' ),
+ 'noResultsText' => __( 'No results found', 'punctual-translation' ),
+ 'searchingText' => __( 'Searching...', 'punctual-translation' ),
+ 'pleaseSearch' => __( 'Please search for at least 2 characters', 'punctual-translation' ),
]
);
}
@@ -226,7 +235,7 @@ public function MetaboxOriginalContent( $post ) {
$current_parent_id = $current_parent->ID;
}
?>
-
+
@@ -370,19 +359,39 @@ public function ajaxBuildSelect() {
status_header( '404' );
die();
}
- $q_all_content = new WP_Query(
- [
- 'post_type' => $_REQUEST['post_type'],
- 'post_status' => 'any',
- 'posts_per_page' => 1000,
- 'no_found_rows' => true,
- 'orderby' => 'title',
- 'order' => 'ASC',
- ]
- );
+
+ // Sanitize inputs
+ $post_type = sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) );
+ $current_value = isset( $_REQUEST['current_value'] ) ? absint( $_REQUEST['current_value'] ) : 0;
+ $search_term = isset( $_REQUEST['search'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['search'] ) ) : '';
+ $load_selected_only = isset( $_REQUEST['load_selected_only'] ) && 'true' === $_REQUEST['load_selected_only'];
+ $limit = isset( $_REQUEST['limit'] ) ? absint( $_REQUEST['limit'] ) : 100;
+
+ // Build query arguments
+ $query_args = [
+ 'post_type' => $post_type,
+ 'post_status' => 'any',
+ 'posts_per_page' => $limit,
+ 'no_found_rows' => true,
+ 'orderby' => 'title',
+ 'order' => 'ASC',
+ ];
+
+ // If loading only selected value, add post__in filter
+ if ( $load_selected_only && $current_value > 0 ) {
+ $query_args['post__in'] = [ $current_value ];
+ }
+
+ // If search term provided, add search filter
+ if ( ! empty( $search_term ) && strlen( $search_term ) >= 2 ) {
+ $query_args['s'] = $search_term;
+ }
+
+ $q_all_content = new WP_Query( $query_args );
+
if ( $q_all_content->have_posts() ) {
foreach ( $q_all_content->posts as $object ) {
- echo '
' . "\n";
+ echo '
' . "\n";
}
}
}
diff --git a/punctual-translation.php b/punctual-translation.php
index bfe059a..85c9dac 100644
--- a/punctual-translation.php
+++ b/punctual-translation.php
@@ -1,7 +1,7 @@
0) {
+ loadSelectedValueOnly(currentPostType, currentValue);
+ }
- // Liste change on select post_type, for reload ajax content
+ // Liste change on select post_type, reset and clear choices
jQuery('select#original_post_type_js').on('change', function () {
- loadPostParentAjax(jQuery(this).val(), 0);
+ currentPostType = jQuery(this).val();
+ currentValue = 0;
+
+ // Clear and reset Choices
+ if (translationSelect !== null) {
+ translationSelect.destroy();
+ translationSelect = null;
+ }
+
+ const selectElement = jQuery('#post_parent_js')[0];
+ selectElement.innerHTML = ''; // Choices.js will handle the placeholder natively
+
+ initializeChoices();
});
// Ajax test for help usage on admin
jQuery('select#language_translation').on('change', function () {
checkUnicityTranslation(jQuery(this).val());
});
-});
-function loadPostParentAjax(post_type, current_value) {
- jQuery("select#post_parent_js").load(ajaxurl, {
- 'action': 'load_original_content',
- 'post_type': post_type,
- 'current_value': current_value
- }, function (response, status, xhr) {
- if (status == "error") {
- alert("Sorry but an error occured with AJAX method");
- } else {
- syncSelectParentBox();
+ /**
+ * Initialize Choices.js with empty select
+ */
+ function initializeChoices() {
+ const selectElement = jQuery('#post_parent_js')[0];
+
+ if (!selectElement.innerHTML.trim() || selectElement.innerHTML === ' AJAX Values ') {
+ selectElement.innerHTML = '';
}
- });
-}
-function syncSelectParentBox() {
- jQuery("select#parent_id").val(jQuery("select#post_parent_js").val());
- checkUnicityTranslation(jQuery("select#language_translation").val());
-}
+ if (translationSelect !== null) {
+ translationSelect.destroy();
+ }
+
+ translationSelect = new Choices(selectElement, choicesOptions);
+ attachChoicesHandlers();
+ }
+
+
+ /**
+ * Load only the selected value for retrocompatibility
+ */
+ function loadSelectedValueOnly(post_type, selected_value) {
+ jQuery.ajax({
+ url: ajaxurl,
+ type: 'POST',
+ data: {
+ 'action': 'load_original_content',
+ 'post_type': post_type,
+ 'current_value': selected_value,
+ 'limit': 1,
+ 'load_selected_only': true
+ },
+ success: function(response) {
+ const tempSelect = jQuery('