From f9617316571601a9bbda4353bc601e02cda4928a Mon Sep 17 00:00:00 2001 From: Truong Giang Date: Tue, 17 Dec 2024 15:39:51 +0700 Subject: [PATCH 01/13] Temp commit --- classes/helpers/FrmAppHelper.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/classes/helpers/FrmAppHelper.php b/classes/helpers/FrmAppHelper.php index e6c2258b6d..fafc15ec80 100644 --- a/classes/helpers/FrmAppHelper.php +++ b/classes/helpers/FrmAppHelper.php @@ -1625,10 +1625,12 @@ public static function get_post_ids_and_titles( $post_type = 'page' ) { */ public static function maybe_autocomplete_pages_options( $args ) { $args = self::preformat_selection_args( $args ); + var_dump( $args ); $pages_count = wp_count_posts( $args['post_type'] ); + var_dump( $pages_count ); - if ( $pages_count->publish <= 50 ) { + if ( isset( $pages_count->publish ) && $pages_count->publish <= 50 ) { self::wp_pages_dropdown( $args ); return; } @@ -1653,6 +1655,16 @@ class="frm_autocomplete_value_input" Date: Tue, 17 Dec 2024 23:32:20 +0700 Subject: [PATCH 02/13] Temp commit --- classes/helpers/FrmAppHelper.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/classes/helpers/FrmAppHelper.php b/classes/helpers/FrmAppHelper.php index fafc15ec80..7c8d84f3d2 100644 --- a/classes/helpers/FrmAppHelper.php +++ b/classes/helpers/FrmAppHelper.php @@ -1656,13 +1656,16 @@ class="frm_autocomplete_value_input" } public static function maybe_autocomplete_options( $args ) { - if ( ! empty( $args['source'] ) && is_callable( $args['source'] ) ) { - $options = call_user_func( $args['source'], $args ); - var_dump( $options ); - return; - } + $defaults = array( + 'truncate' => false, + 'placeholder' => ' ', + 'name' => '', + 'selected' => '', + 'source' => array(), + 'autocomplete_placeholder' => __( 'Select an option', 'formidable' ), + ); - self::maybe_autocomplete_pages_options( $args ); + $args = wp_parse_args( $args, $defaults ); } /** From 4f3c92e2b726832540939e87176ce9d834576aad Mon Sep 17 00:00:00 2001 From: Truong Giang Date: Thu, 19 Dec 2024 21:22:32 +0700 Subject: [PATCH 03/13] Support autocomplete with custom options --- classes/helpers/FrmAppHelper.php | 39 ++++++++++++++++++++++++++++- classes/views/shared/views-info.php | 18 +++++++++++++ js/admin/dom.js | 12 ++++++++- 3 files changed, 67 insertions(+), 2 deletions(-) diff --git a/classes/helpers/FrmAppHelper.php b/classes/helpers/FrmAppHelper.php index 7c8d84f3d2..1f072c8e05 100644 --- a/classes/helpers/FrmAppHelper.php +++ b/classes/helpers/FrmAppHelper.php @@ -1660,12 +1660,50 @@ public static function maybe_autocomplete_options( $args ) { 'truncate' => false, 'placeholder' => ' ', 'name' => '', + 'id' => '', 'selected' => '', 'source' => array(), + 'dropdown_limit' => 50, 'autocomplete_placeholder' => __( 'Select an option', 'formidable' ), ); $args = wp_parse_args( $args, $defaults ); + + $html_attrs = array(); + if ( ! empty( $args['name'] ) ) { + $html_attrs['name'] = $args['name']; + } + + if ( ! empty( $args['id'] ) ) { + $html_attrs['id'] = $args['id']; + } + + if ( count( $args['source'] ) <= $args['dropdown_limit'] ) { + ?> + + $label ) { + $options[] = array( + 'value' => $value, + 'label' => $label, + ); + } + ?> + + + > - $label ) : ?> + $source ) : + if ( is_array( $source ) ) { + $value = isset( $source[ $args['value_key'] ] ) ? $source[ $args['value_key'] ] : ''; + $label = isset( $source[ $args['label_key'] ] ) ? $source[ $args['label_key'] ] : ''; + } else { + $value = $key; + $label = $source; + } + ?> $label ) { - $options[] = array( - 'value' => $value, - 'label' => $label, - ); + $autocomplete_value = ''; + foreach ( $args['source'] as $key => $source ) { + if ( is_array( $source ) ) { + $value = isset( $source[ $args['value_key'] ] ) ? $source[ $args['value_key'] ] : ''; + $label = isset( $source[ $args['label_key'] ] ) ? $source[ $args['label_key'] ] : ''; + } else { + $value = $key; + $label = $source; + } + + if ( $value === $args['selected'] ) { + $autocomplete_value = $label; + } + + $options[] = compact( 'value', 'label' ); } ?> + value="" /> diff --git a/js/admin/dom.js b/js/admin/dom.js index 3393a8c42d..79dfa81932 100644 --- a/js/admin/dom.js +++ b/js/admin/dom.js @@ -249,11 +249,11 @@ }; const autocomplete = { - initSelectionAutocomplete: function() { + initSelectionAutocomplete: function( container ) { if ( jQuery.fn.autocomplete ) { - autocomplete.initAutocomplete( 'page' ); - autocomplete.initAutocomplete( 'user' ); - autocomplete.initAutocomplete( 'custom' ); + autocomplete.initAutocomplete( 'page', container ); + autocomplete.initAutocomplete( 'user', container ); + autocomplete.initAutocomplete( 'custom', container ); } }, /** @@ -344,6 +344,13 @@ e.preventDefault(); this.value = ui.item.value === '' ? '' : ui.item.label; this.nextElementSibling.value = ui.item.value; + + /** + * Fires when an autocomplete item is selected. + * + * @since x.x + */ + wp.hooks.doAction( 'frm_autocomplete_select', e, ui, this ); } }; diff --git a/js/formidable_admin.js b/js/formidable_admin.js index 6b785a337a..02106b6090 100644 --- a/js/formidable_admin.js +++ b/js/formidable_admin.js @@ -686,7 +686,7 @@ function frmAdminBuildJS() { inside.html( html ); initiateMultiselect(); showInputIcon( '#' + cont.attr( 'id' ) ); - frmDom.autocomplete.initAutocomplete( 'page', inside ); + frmDom.autocomplete.initSelectionAutocomplete( inside ); jQuery( b ).trigger( 'frm-action-loaded' ); /** @@ -8379,7 +8379,7 @@ function frmAdminBuildJS() { * Handles 'change' event on the document. * * @since 6.16.3 - * + * * @param {Event} event * @returns {Void} */ @@ -8445,7 +8445,7 @@ function frmAdminBuildJS() { onClickPreventDefault( continueButton, () => { saveAndReloadFormBuilder(); } ); - + const cancelButton = frmDom.modal.footerButton({ text: __( 'Cancel', 'formidable' ), buttonType: 'cancel' From 2e60845e1814b8d8adb7016809234e552d2a88a8 Mon Sep 17 00:00:00 2001 From: Truong Giang Date: Tue, 31 Dec 2024 19:52:44 +0700 Subject: [PATCH 06/13] Improve autocomplete dropdown code --- classes/helpers/FrmAppHelper.php | 61 +++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/classes/helpers/FrmAppHelper.php b/classes/helpers/FrmAppHelper.php index d876ac960f..a64e615e97 100644 --- a/classes/helpers/FrmAppHelper.php +++ b/classes/helpers/FrmAppHelper.php @@ -1653,6 +1653,13 @@ class="frm_autocomplete_value_input" false, @@ -1683,15 +1690,9 @@ public static function maybe_autocomplete_options( $args ) { $source ) { - if ( is_array( $source ) ) { - $value = isset( $source[ $args['value_key'] ] ) ? $source[ $args['value_key'] ] : ''; - $label = isset( $source[ $args['label_key'] ] ) ? $source[ $args['label_key'] ] : ''; - } else { - $value = $key; - $label = $source; - } + $value_label = self::get_dropdown_value_and_label_from_option( $source, $key, $args ); - if ( $value === $args['selected'] ) { - $autocomplete_value = $label; + if ( $value_label['value'] === $args['selected'] ) { + $autocomplete_value = $value_label['label']; } - $options[] = compact( 'value', 'label' ); + $options[] = $value_label; } + + $html_attrs['type'] = 'hidden'; + $html_attrs['class'] = 'frm_autocomplete_value_input'; + $html_attrs['value'] = $args['selected']; ?> - + /> Date: Wed, 8 Jan 2025 23:33:06 +0700 Subject: [PATCH 07/13] Fix autocomplete dropdown truncate --- classes/helpers/FrmAppHelper.php | 3 +++ js/admin/dom.js | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/classes/helpers/FrmAppHelper.php b/classes/helpers/FrmAppHelper.php index a64e615e97..3f6373f741 100644 --- a/classes/helpers/FrmAppHelper.php +++ b/classes/helpers/FrmAppHelper.php @@ -1691,6 +1691,9 @@ public static function maybe_autocomplete_options( $args ) { $source ) : $value_label = self::get_dropdown_value_and_label_from_option( $source, $key, $args ); + if ( ! empty( $args['truncate'] ) ) { + $value_label['label'] = self::truncate( $value_label['label'], $args['truncate'] ); + } ?> diff --git a/js/admin/dom.js b/js/admin/dom.js index 79dfa81932..4849603b1c 100644 --- a/js/admin/dom.js +++ b/js/admin/dom.js @@ -337,6 +337,13 @@ selectBlank: function( e, ui ) { if ( ui.item === null ) { this.nextElementSibling.value = ''; + + /** + * Fires when an autocomplete value is cleared. + * + * @since x.x + */ + wp.hooks.doAction( 'frm_autocomplete_clear_value', e, ui, this ); } }, From 7812b389d793321ca4ada9865c29c27829b77cc3 Mon Sep 17 00:00:00 2001 From: Truong Giang Date: Wed, 8 Jan 2025 23:36:24 +0700 Subject: [PATCH 08/13] Fix dropdown placeholder not working --- classes/helpers/FrmAppHelper.php | 1 + 1 file changed, 1 insertion(+) diff --git a/classes/helpers/FrmAppHelper.php b/classes/helpers/FrmAppHelper.php index 3f6373f741..51fa1adcbf 100644 --- a/classes/helpers/FrmAppHelper.php +++ b/classes/helpers/FrmAppHelper.php @@ -1688,6 +1688,7 @@ public static function maybe_autocomplete_options( $args ) { if ( count( $args['source'] ) <= $args['dropdown_limit'] ) { ?> /> 'DESC', ) ); + $source = array(); foreach ( $posts as $post ) { $source[ $post->ID ] = $post->post_title; } - FrmAppHelper::maybe_autocomplete_options( array( - 'source' => $source, - 'selected' => 1, - 'dropdown_limit' => 30, - ) ); + FrmAppHelper::maybe_autocomplete_options( + array( + 'source' => $source, + 'selected' => 1, + 'dropdown_limit' => 30, + ) + ); } FrmAppHelper::get_admin_header( array( From 9961d93f535a20f55c39a31bd289768b2ae3312c Mon Sep 17 00:00:00 2001 From: Truong Giang Date: Mon, 17 Feb 2025 23:37:41 +0700 Subject: [PATCH 10/13] Fix phpcs --- classes/helpers/FrmAppHelper.php | 4 ++-- classes/views/shared/views-info.php | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/classes/helpers/FrmAppHelper.php b/classes/helpers/FrmAppHelper.php index 80cd4da9e4..1052f9be55 100644 --- a/classes/helpers/FrmAppHelper.php +++ b/classes/helpers/FrmAppHelper.php @@ -1701,7 +1701,7 @@ public static function maybe_autocomplete_options( $args ) { $source ) { $value_label = self::get_dropdown_value_and_label_from_option( $source, $key, $args ); @@ -1723,7 +1723,7 @@ public static function maybe_autocomplete_options( $args ) { value="" /> /> $source, - 'selected' => 1, + 'source' => $source, + 'selected' => 1, 'dropdown_limit' => 30, ) ); - } + }//end if + FrmAppHelper::get_admin_header( array( 'label' => __( 'Views', 'formidable' ), From 75e48acae425866fc51748f5926fc34b40e211b2 Mon Sep 17 00:00:00 2001 From: Truong Giang Date: Mon, 10 Mar 2025 23:09:57 +0700 Subject: [PATCH 11/13] Remove test code --- classes/views/shared/views-info.php | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/classes/views/shared/views-info.php b/classes/views/shared/views-info.php index 17452b038c..0ef65d8ea2 100644 --- a/classes/views/shared/views-info.php +++ b/classes/views/shared/views-info.php @@ -5,28 +5,6 @@ ?>
-1, - 'post_type' => 'any', - 'order' => 'DESC', - ) - ); - - $source = array(); - foreach ( $posts as $post ) { - $source[ $post->ID ] = $post->post_title; - } - FrmAppHelper::maybe_autocomplete_options( - array( - 'source' => $source, - 'selected' => 1, - 'dropdown_limit' => 30, - ) - ); - }//end if - FrmAppHelper::get_admin_header( array( 'label' => __( 'Views', 'formidable' ), From af8d12e4562e48e9c2997bbdf20e0afe7e521ffc Mon Sep 17 00:00:00 2001 From: Truong Giang Date: Fri, 11 Apr 2025 23:09:42 +0700 Subject: [PATCH 12/13] Fix php cs fixer --- classes/helpers/FrmAppHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/helpers/FrmAppHelper.php b/classes/helpers/FrmAppHelper.php index d10f509427..d19928a439 100644 --- a/classes/helpers/FrmAppHelper.php +++ b/classes/helpers/FrmAppHelper.php @@ -1785,7 +1785,7 @@ public static function maybe_autocomplete_options( $args ) { * * @since x.x * - * @param string|array $option Autocomplete option. + * @param array|string $option Autocomplete option. * @param string $key Array key of the option. * @param array $args See {@see FrmAppHelper::maybe_autocomplete_options()}. * @return array From 00bd3ec77f8de9e5fb29d4e31873454ef6b5ff03 Mon Sep 17 00:00:00 2001 From: Truong Giang Date: Wed, 23 Apr 2025 23:45:02 +0700 Subject: [PATCH 13/13] So simple pages dropdown if not posts found --- classes/helpers/FrmAppHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/helpers/FrmAppHelper.php b/classes/helpers/FrmAppHelper.php index c3b5cfd28c..59ff0dfb2f 100644 --- a/classes/helpers/FrmAppHelper.php +++ b/classes/helpers/FrmAppHelper.php @@ -1691,7 +1691,7 @@ public static function maybe_autocomplete_pages_options( $args ) { $pages_count = wp_count_posts( $args['post_type'] ); - if ( isset( $pages_count->publish ) && $pages_count->publish <= 50 ) { + if ( ! isset( $pages_count->publish ) || $pages_count->publish <= 50 ) { self::wp_pages_dropdown( $args ); return; }