From 24c72e4867f342ea84e755e6af2f4bb0e8c93244 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Sun, 5 Jan 2025 16:04:27 -0300 Subject: [PATCH 1/7] fix: assets path on enqueue --- init-custom-actions.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/init-custom-actions.php b/init-custom-actions.php index c8a24f6..b32829a 100644 --- a/init-custom-actions.php +++ b/init-custom-actions.php @@ -39,8 +39,8 @@ function eef_register_custom_action( $form_actions_registrar ) { * @since 2.0 */ function eef_register_plugin_assets() { - wp_enqueue_script( 'eef-frontend-script', EEF_PLUGN_URL . 'assets/frontend-scripts.min.js', array( 'jquery' ), EEF_VERSION ); - wp_enqueue_style( 'eef-frontend-style', EEF_PLUGN_URL . 'assets/style.min.css', array(), EEF_VERSION ); + wp_enqueue_script( 'eef-frontend-script', EEF_PLUGN_URL . 'assets/js/frontend-scripts.min.js', array( 'jquery' ), EEF_VERSION ); + wp_enqueue_style( 'eef-frontend-style', EEF_PLUGN_URL . 'assets/css/style.min.css', array(), EEF_VERSION ); } add_action( 'wp_enqueue_scripts', 'eef_register_plugin_assets' ); @@ -51,7 +51,7 @@ function eef_register_plugin_assets() { * @since 2.0 */ function eef_register_editor_scripts() { - wp_register_script( 'eef-editor-scripts', EEF_PLUGN_URL . 'assets/editor-scripts.min.js', array(), EEF_VERSION ); + wp_register_script( 'eef-editor-scripts', EEF_PLUGN_URL . 'assets/js/editor-scripts.min.js', array(), EEF_VERSION ); wp_enqueue_script( 'eef-editor-scripts' ); } From 7be137c3db43cb3419a63db8d375d36dd6b9aab3 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Sun, 5 Jan 2025 16:05:26 -0300 Subject: [PATCH 2/7] chore: change custo message class name + scripts improvements --- extensions-for-elementor-form.php | 8 ++--- ...t.php => class-custom-success-message.php} | 32 ++++++++++--------- webpack.config.js | 6 +--- 3 files changed, 21 insertions(+), 25 deletions(-) rename includes/{class-show-content-after-submit.php => class-custom-success-message.php} (71%) diff --git a/extensions-for-elementor-form.php b/extensions-for-elementor-form.php index 1b49093..308f21b 100644 --- a/extensions-for-elementor-form.php +++ b/extensions-for-elementor-form.php @@ -2,7 +2,7 @@ /** * Plugin Name: Extensions for Elementor Form * Plugin URI: https://eduardovillao.me/extensions-for-elementor-form - * Description: Extensions for Elementor Form create many actions and controls to Elementor Form. This plugin require the Elementor Pro (Form Widget). + * Description: This plugin empowers your Elementor Forms with advanced functionality that simplifies workflows, improves usability, and integrates seamlessly with tools like WhatsApp. * Author: EduardoVillao.me * Author URI: https://eduardovillao.me/ * Text Domain: extensions-for-elementor-form @@ -30,12 +30,10 @@ */ if ( ! version_compare( PHP_VERSION, EEF_PHP_MINIMUM_VERSION, '>=' ) ) { add_action( 'admin_notices', 'eef_admin_notice_php_version_fail' ); - } elseif ( ! version_compare( get_bloginfo( 'version' ), EEF_WP_MINIMUM_VERSION, '>=' ) ) { add_action( 'admin_notices', 'eef_admin_notice_wp_version_fail' ); - } else { - add_action( 'plugins_loaded', 'eef_init_plugin', 10 ); + add_action( 'plugins_loaded', 'eef_init_plugin', 10 ); } /** @@ -50,7 +48,7 @@ function eef_init_plugin() { } include_once EEF_PLUGIN_PATH . '/init-custom-actions.php'; - include_once EEF_PLUGIN_PATH . '/includes/class-show-content-after-submit.php'; + include_once EEF_PLUGIN_PATH . '/includes/class-custom-success-message.php'; } /** diff --git a/includes/class-show-content-after-submit.php b/includes/class-custom-success-message.php similarity index 71% rename from includes/class-show-content-after-submit.php rename to includes/class-custom-success-message.php index cbedd65..a40d4f8 100644 --- a/includes/class-show-content-after-submit.php +++ b/includes/class-custom-success-message.php @@ -1,16 +1,18 @@ start_controls_section( 'evcode_message_template', [ - 'label' => \esc_html__( 'Custom Sucess Message', 'extensions_elementor_form' ), + 'label' => \esc_html__( 'Custom Success Message', 'extensions_elementor_form' ), ] ); @@ -43,7 +45,7 @@ public function evcode_add_message_control ( $element, $args ) { 'template-custom-sucess-message', [ 'label' => \esc_html__( 'Message Template', 'extensions_elementor_form' ), - 'type' => Elementor\Controls_Manager::TEXT, + 'type' => \Elementor\Controls_Manager::TEXT, 'placeholder' => \esc_html__( '[your-shortcode-here]', 'extensions_elementor_form' ), 'label_block' => true, 'render_type' => 'none', @@ -61,16 +63,16 @@ public function evcode_add_message_control ( $element, $args ) { * @param [type] $form * @return void */ - public function evcode_add_message_class ( $form ) { + public function add_message_class ( $form ) { if( 'form' === $form->get_name() ) { - $settings = $form->get_settings(); + $settings = $form->get_settings(); - add_action( 'elementor-pro/forms/pre_render', [ $this, 'template_message' ] ); + add_action( 'elementor-pro/forms/pre_render', [ $this, 'template_message' ] ); - if( 'yes' == $settings['hide_form_after_submit'] ) { - $form->add_render_attribute( 'wrapper', 'class', 'ele-extensions-hide-form', true ); - } - } + if( 'yes' == $settings['hide_form_after_submit'] ) { + $form->add_render_attribute( 'wrapper', 'class', 'ele-extensions-hide-form', true ); + } + } } /** @@ -86,4 +88,4 @@ public function template_message ( $instance ) { } } -new Evcode_Elementor_Custom_Sucess_Message(); +new Custom_Success_Message(); diff --git a/webpack.config.js b/webpack.config.js index 445378b..1d5ae36 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -94,8 +94,4 @@ const generalCSS = { ], }; -module.exports = [ - adminJs, - generalJs, - generalCSS, -]; +module.exports = [adminJs, generalJs, generalCSS]; From ab1b599503c8ec20d629844a955da699dd425193 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Sun, 5 Jan 2025 16:20:46 -0300 Subject: [PATCH 3/7] fix: readme title typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0ff44d6..21548d1 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Extensiona for Elementor Form +# Extensions for Elementor Form Extensions for Elementor Form adds powerful new actions and controls to the Elementor Pro Form widget, enhancing its default capabilities. From 6af77ae374a5191d5c5ce7e2b1e64954c5d3598f Mon Sep 17 00:00:00 2001 From: Eduardo Date: Sun, 5 Jan 2025 16:21:14 -0300 Subject: [PATCH 4/7] chore: code format improvements --- .../class-register-create-post-fields.php | 28 +++++++++---------- includes/class-register-post.php | 24 ++++++++-------- src/js/frontend-scripts.js | 16 ++++------- 3 files changed, 31 insertions(+), 37 deletions(-) diff --git a/includes/class-register-create-post-fields.php b/includes/class-register-create-post-fields.php index 73c025b..1c6ae03 100644 --- a/includes/class-register-create-post-fields.php +++ b/includes/class-register-create-post-fields.php @@ -16,9 +16,9 @@ * @since 2.0 */ class Register_Create_Post_Fields { - /** - * Contructor... - */ + /** + * Contructor. + */ public function __construct() { add_action( 'elementor/element/form/section_form_fields/before_section_end', [ $this, 'add_control_fields' ], 100, 2 ); } @@ -45,26 +45,26 @@ public function add_control_fields( $element, $args ) { 'tabs_wrapper' => 'form_fields_tabs', 'inner_tab' => 'form_fields_advanced_tab', 'classes' => 'elementor-hidden-control', - 'description' => \esc_html__( 'Use this input to define what post field will receive this data when post is registered', 'extensions-for-elementor-form' ), - 'default' => 'select', - 'options' => [ - 'select' => \esc_html__( 'Select', 'extensions-for-elementor-form' ), - 'post_title' => \esc_html__( 'Post Title', 'extensions-for-elementor-form' ), - 'post_content' => \esc_html__( 'Post Content', 'extensions-for-elementor-form' ), + 'description' => \esc_html__( 'Use this input to define what post field will receive this data when post is registered', 'extensions-for-elementor-form' ), + 'default' => 'select', + 'options' => [ + 'select' => \esc_html__( 'Select', 'extensions-for-elementor-form' ), + 'post_title' => \esc_html__( 'Post Title', 'extensions-for-elementor-form' ), + 'post_content' => \esc_html__( 'Post Content', 'extensions-for-elementor-form' ), 'post_excerpt' => \esc_html__( 'Post Excerpt', 'extensions-for-elementor-form' ), 'post_author' => \esc_html__( 'Post Author', 'extensions-for-elementor-form' ), - 'custom_field' => \esc_html__( 'Custom Field', 'extensions-for-elementor-form' ), - ], + 'custom_field' => \esc_html__( 'Custom Field', 'extensions-for-elementor-form' ), + ], ]; - $new_control_2 = [ + $new_control_2 = [ 'label' => \esc_html__( 'Custom Field Name', 'extensions-for-elementor-form' ), 'type' => ElementorControls::TEXT, - 'placeholder' => \esc_html__( 'custom_field_name', 'extensions-for-elementor-form' ), + 'placeholder' => \esc_html__( 'custom_field_name', 'extensions-for-elementor-form' ), 'tab' => 'content', 'tabs_wrapper' => 'form_fields_tabs', 'inner_tab' => 'form_fields_advanced_tab', - 'description' => \esc_html__( 'Add the Custom Field name here. You can use default fields or custom created with ACF or similars', 'extensions-for-elementor-form' ), + 'description' => \esc_html__( 'Add the Custom Field name here. You can use default fields or custom created with ACF or similars', 'extensions-for-elementor-form' ), 'condition' => [ 'eef-register-post-field' => 'custom_field', ], diff --git a/includes/class-register-post.php b/includes/class-register-post.php index 1dc6612..2cb22e2 100644 --- a/includes/class-register-post.php +++ b/includes/class-register-post.php @@ -63,38 +63,38 @@ public function register_settings_section( $widget ) { ] ); - /** - * TODO: - * 1. Add or edit existent post. - * 2. Post terms (taxonomy). - * 3. Redirect to post after submit/created?. - * 4. Se post status === private add campo de senha. - */ + /** + * TODO: + * 1. Add or edit existent post. + * 2. Post terms (taxonomy). + * 3. Redirect to post after submit/created?. + * 4. Se post status === private add campo de senha. + */ $widget->add_control( 'eef-register-post-post-type', [ 'label' => \esc_html__( 'Post Type', 'extensions-for-elementor-form' ), 'type' => \Elementor\Controls_Manager::SELECT, - 'default' => 'post', + 'default' => 'post', 'options' => $this->get_registered_post_types(), ] ); - $widget->add_control( + $widget->add_control( 'eef-register-post-post-status', [ 'label' => \esc_html__( 'Post Status', 'extensions-for-elementor-form' ), 'type' => \Elementor\Controls_Manager::SELECT, - 'default' => 'draft', + 'default' => 'draft', 'options' => [ 'draft' => \esc_html__( 'Draft', 'extensions-for-elementor-form' ), 'publish' => \esc_html__( 'Publish', 'extensions-for-elementor-form' ), - 'pending' => \esc_html__( 'Pending', 'extensions-for-elementor-form' ), + 'pending' => \esc_html__( 'Pending', 'extensions-for-elementor-form' ), ], ] ); - $widget->add_control( + $widget->add_control( 'eef-register-post-user-permission', [ 'label' => \esc_html__( 'Run only to logged in users?', 'extensions-for-elementor-form' ), diff --git a/src/js/frontend-scripts.js b/src/js/frontend-scripts.js index 02d7278..088dc2b 100644 --- a/src/js/frontend-scripts.js +++ b/src/js/frontend-scripts.js @@ -1,23 +1,17 @@ jQuery( document ).ready( function( $ ) { - $('.elementor-form').submit( function( event ) { - var form = $(this); - var form_id = $(this).find('input[name="form_id"]').val(); + var form_id = $(this).find('input[name="form_id"]').val(); $(form).addClass(form_id); - $(document).ajaxSuccess( function( event ) { - if ($('.elementor-form-fields-wrapper').hasClass( "ele-extensions-hide-form" ) != false ) { - - $(form).find('.ele-extensions-hide-form').hide(); + $(form).find('.ele-extensions-hide-form').hide(); } if ($('.' + form_id).prev().hasClass( "custom-sucess-message" ) != false ) { - - $('.elementor-message-success').hide(); - $('.' + form_id).prev('.custom-sucess-message').delay( 100 ).fadeIn(); + $('.elementor-message-success').hide(); + $('.' + form_id).prev('.custom-sucess-message').delay( 100 ).fadeIn(); } }) }) -}); \ No newline at end of file +}); From 31b545303c5e3d507816a10bde2bcf727c4a1b69 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Sun, 5 Jan 2025 21:03:18 -0300 Subject: [PATCH 5/7] feat: improve class plugin --- extensions-for-elementor-form.php | 57 ++------- .../class-register-post-fields-controls.php} | 17 +-- .../{ => actions}/class-register-post.php | 2 +- .../class-whatsapp-redirect.php} | 8 +- includes/class-custom-success-message.php | 15 +-- includes/class-plugin.php | 121 ++++++++++++++++++ .../init-custom-actions.php | 30 ++--- 7 files changed, 161 insertions(+), 89 deletions(-) rename includes/{class-register-create-post-fields.php => actions/class-register-post-fields-controls.php} (91%) rename includes/{ => actions}/class-register-post.php (99%) rename includes/{class-whatsapp-action.php => actions/class-whatsapp-redirect.php} (92%) create mode 100644 includes/class-plugin.php rename init-custom-actions.php => includes/init-custom-actions.php (67%) diff --git a/extensions-for-elementor-form.php b/extensions-for-elementor-form.php index 308f21b..6594dfd 100644 --- a/extensions-for-elementor-form.php +++ b/extensions-for-elementor-form.php @@ -22,6 +22,7 @@ define( 'EEF_VERSION', '2.2' ); define( 'EEF_PHP_MINIMUM_VERSION', '7.4' ); define( 'EEF_WP_MINIMUM_VERSION', '5.5' ); +define( 'EEF_PLUGIN_MAIN_FILE', __FILE__ ); /** * Check PHP and WP version before include plugin class @@ -29,26 +30,12 @@ * @since 2.0 */ if ( ! version_compare( PHP_VERSION, EEF_PHP_MINIMUM_VERSION, '>=' ) ) { - add_action( 'admin_notices', 'eef_admin_notice_php_version_fail' ); + \add_action( 'admin_notices', 'eef_admin_notice_php_version_fail' ); } elseif ( ! version_compare( get_bloginfo( 'version' ), EEF_WP_MINIMUM_VERSION, '>=' ) ) { - add_action( 'admin_notices', 'eef_admin_notice_wp_version_fail' ); + \add_action( 'admin_notices', 'eef_admin_notice_wp_version_fail' ); } else { - add_action( 'plugins_loaded', 'eef_init_plugin', 10 ); -} - -/** - * Init plugin (temp. code) - * - * @since 2.0 - */ -function eef_init_plugin() { - if ( ! eef_plugin_is_active( 'elementor-pro/elementor-pro.php' ) ) { - add_action( 'admin_notices', 'eef_notice_elementor_pro_inactive' ); - return; - } - - include_once EEF_PLUGIN_PATH . '/init-custom-actions.php'; - include_once EEF_PLUGIN_PATH . '/includes/class-custom-success-message.php'; + include_once EEF_PLUGIN_PATH . '/includes/class-plugin.php'; + \add_action( 'plugins_loaded', array( 'EEF\Includes\Plugin', 'instance' ), 10 ); } /** @@ -59,13 +46,13 @@ function eef_init_plugin() { */ function eef_admin_notice_php_version_fail() { $message = sprintf( - esc_html__( '%1$s requires PHP version %2$s or greater.', 'extensions-for-elementor-form' ), + \esc_html__( '%1$s requires PHP version %2$s or greater.', 'extensions-for-elementor-form' ), 'Extensions for Elementor Form', EEF_PHP_MINIMUM_VERSION ); $html_message = sprintf( '

%1$s

', $message ); - echo wp_kses_post( $html_message ); + echo \wp_kses_post( $html_message ); } /** @@ -76,37 +63,11 @@ function eef_admin_notice_php_version_fail() { */ function eef_admin_notice_wp_version_fail() { $message = sprintf( - esc_html__( '%1$s requires WordPress version %2$s or greater.', 'extensions-for-elementor-form' ), + \esc_html__( '%1$s requires WordPress version %2$s or greater.', 'extensions-for-elementor-form' ), 'Extensions for Elementor Form', EEF_WP_MINIMUM_VERSION ); $html_message = sprintf( '

%1$s

', $message ); - echo wp_kses_post( $html_message ); -} - -/** - * Admin notice Elementor Pro disabled - * - * @since 2.0 - * @return string - */ -function eef_notice_elementor_pro_inactive() { - $message = sprintf( - esc_html__( '%1$s requires %2$s to be installed and activated.', 'extensions-for-elementor-form' ), - 'Extensions for Elementor Form', - 'Elementor Pro' - ); - - $html_message = sprintf( '

%1$s

', $message ); - echo wp_kses_post( $html_message ); -} - -/** - * Check if plugin is active - * - * @since 2.0 - */ -function eef_plugin_is_active( $plugin_name ) { - return function_exists( 'is_plugin_active' ) ? is_plugin_active( $plugin_name ) : in_array( $plugin_name, (array) get_option( 'active_plugins', array() ), true ); + echo \wp_kses_post( $html_message ); } diff --git a/includes/class-register-create-post-fields.php b/includes/actions/class-register-post-fields-controls.php similarity index 91% rename from includes/class-register-create-post-fields.php rename to includes/actions/class-register-post-fields-controls.php index 1c6ae03..a01f6b9 100644 --- a/includes/class-register-create-post-fields.php +++ b/includes/actions/class-register-post-fields-controls.php @@ -1,6 +1,6 @@ register_control_in_form_advanced_tab( $element, $control_data, $pattern_field ); } @@ -90,8 +85,6 @@ public function add_control_fields( $element, $args ) { * @param array $control_data * @param array $pattern_field * @return void - * - * @since 2.0 */ public function register_control_in_form_advanced_tab( $element, $control_data, $pattern_field ) { foreach( $pattern_field as $key => $control ) { diff --git a/includes/class-register-post.php b/includes/actions/class-register-post.php similarity index 99% rename from includes/class-register-post.php rename to includes/actions/class-register-post.php index 2cb22e2..8e919f9 100644 --- a/includes/class-register-post.php +++ b/includes/actions/class-register-post.php @@ -1,6 +1,6 @@ get_name() ) { @@ -79,7 +81,6 @@ public function add_message_class ( $form ) { * Custom temlate message. * * @param [type] $instance - * @return void */ public function template_message ( $instance ) { if ( ! $instance['template-custom-sucess-message'] == '' ) { @@ -87,5 +88,3 @@ public function template_message ( $instance ) { } } } - -new Custom_Success_Message(); diff --git a/includes/class-plugin.php b/includes/class-plugin.php new file mode 100644 index 0000000..b0fb2ea --- /dev/null +++ b/includes/class-plugin.php @@ -0,0 +1,121 @@ +load_required_files(); + + $custom_success_message = new Custom_Success_Message(); + $custom_success_message->set_hooks(); + } + + /** + * Load required files + */ + public function load_required_files() : void { + include_once EEF_PLUGIN_PATH . '/includes/init-custom-actions.php'; + include_once EEF_PLUGIN_PATH . '/includes/class-custom-success-message.php'; + } + + /** + * Enqueu admin styles/scripts + */ + public function enqueue_admin_scripts() : void {} + + /** + * Enqueue front end styles/scripts + */ + public function enqueue_frondend_scripts() : void {} + + /** + * Check Elementor Pro loaded + */ + public function check_elementor_pro_loaded() : void { + if ( ! \did_action('elementor_pro/init') ) { + \add_action( 'admin_notices', array( $this, 'notice_elementor_pro_inactive' ) ); + } + } + + /** + * Admin notice Elementor Pro disabled + */ + function notice_elementor_pro_inactive() { + $message = sprintf( + \esc_html__( '%1$s requires %2$s to be installed and activated.', 'extensions-for-elementor-form' ), + 'Extensions for Elementor Form', + 'Elementor Pro' + ); + + $html_message = sprintf( '

%1$s

', $message ); + echo \wp_kses_post( $html_message ); + } + + /** + * Activation hook + */ + public function activation() : void { + \flush_rewrite_rules(); + } + + /** + * Deactivation hook + */ + public function deactivation() : void { + \flush_rewrite_rules(); + } +} diff --git a/init-custom-actions.php b/includes/init-custom-actions.php similarity index 67% rename from init-custom-actions.php rename to includes/init-custom-actions.php index b32829a..80bd091 100644 --- a/init-custom-actions.php +++ b/includes/init-custom-actions.php @@ -4,9 +4,17 @@ exit; } -use Eef\Includes\Register_Create_Post_Fields; -use Eef\Includes\Whatsapp_Action_After_Submit; -use Eef\Includes\Register_Post; +use EEF\Includes\Actions\Whatsapp_Redirect; +use EEF\Includes\Actions\Register_Post; +use EEF\Includes\Actions\Register_Post_Fields_Controls; + +/** + * Register custom field to form repeater + */ +include_once EEF_PLUGIN_PATH . '/includes/actions/class-register-post-fields-controls.php'; +$register_post_fields_controls = new Register_Post_Fields_Controls(); +$register_post_fields_controls->set_hooks(); + /** * Add new form action after form submission. @@ -16,22 +24,14 @@ * @return void */ function eef_register_custom_action( $form_actions_registrar ) { - include_once EEF_PLUGIN_PATH . '/includes/class-whatsapp-action.php'; - include_once EEF_PLUGIN_PATH . '/includes/class-register-post.php'; + include_once EEF_PLUGIN_PATH . '/includes/actions/class-whatsapp-redirect.php'; + include_once EEF_PLUGIN_PATH . '/includes/actions/class-register-post.php'; - $form_actions_registrar->register( new Whatsapp_Action_After_Submit() ); + $form_actions_registrar->register( new Whatsapp_Redirect() ); $form_actions_registrar->register( new Register_Post() ); } -add_action( 'elementor_pro/forms/actions/register', 'eef_register_custom_action' ); - -/** - * Register custom field to form repeater - * - * @since 2.0 - */ -include_once EEF_PLUGIN_PATH . '/includes/class-register-create-post-fields.php'; -new Register_Create_Post_Fields(); +add_action( 'elementor_pro/forms/actions/register', 'eef_register_custom_action', -10 ); /** * Register frontend assets From 004cdd4fece7ad1c7f26e4812ea3721baf4a9298 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Sun, 5 Jan 2025 22:28:28 -0300 Subject: [PATCH 6/7] feat: refactor how actions are created --- includes/actions/class-register-actions.php | 43 +++++++ .../class-register-post-fields-controls.php | 106 ------------------ includes/actions/class-register-post.php | 89 +++++++++++++++ includes/class-plugin.php | 37 +++++- includes/init-custom-actions.php | 58 ---------- src/js/admin/editor-scripts.js | 86 +++++++------- 6 files changed, 207 insertions(+), 212 deletions(-) create mode 100644 includes/actions/class-register-actions.php delete mode 100644 includes/actions/class-register-post-fields-controls.php delete mode 100644 includes/init-custom-actions.php diff --git a/includes/actions/class-register-actions.php b/includes/actions/class-register-actions.php new file mode 100644 index 0000000..56f564c --- /dev/null +++ b/includes/actions/class-register-actions.php @@ -0,0 +1,43 @@ +actions = $actions; + } + + public function set_hooks() : void { + \add_action( 'elementor_pro/forms/actions/register', array( $this, 'register' ), -10 ); + } + + /** + * Register form acitons to be used after subumitting the form. + * + * @since 2.0 + * @param ElementorPro\Modules\Forms\Registrars\Form_Actions_Registrar $form_actions_registrar + */ + public function register( $form_actions_registrar ) : void { + if ( empty( $this->actions ) ) { + return; + } + + foreach ( $this->actions as $action ) { + include_once EEF_PLUGIN_PATH . $action['relative_path'] ?? ''; + + $class_name = 'EEF\Includes\Actions\\' . $action['class_name']; + if ( class_exists( $class_name ) ) { + $form_actions_registrar->register( new $class_name() ); + } + } + } +} diff --git a/includes/actions/class-register-post-fields-controls.php b/includes/actions/class-register-post-fields-controls.php deleted file mode 100644 index a01f6b9..0000000 --- a/includes/actions/class-register-post-fields-controls.php +++ /dev/null @@ -1,106 +0,0 @@ -controls_manager->get_control_from_stack( $element->get_name(), 'form_fields' ); - - if ( is_wp_error( $control_data ) ) { - return; - } - - $new_control = [ - 'label' => \esc_html__( 'Field to Register', 'extensions-for-elementor-form' ), - 'type' => ElementorControls::SELECT, - 'tab' => 'content', - 'tabs_wrapper' => 'form_fields_tabs', - 'inner_tab' => 'form_fields_advanced_tab', - 'classes' => 'elementor-hidden-control', - 'description' => \esc_html__( 'Use this input to define what post field will receive this data when post is registered', 'extensions-for-elementor-form' ), - 'default' => 'select', - 'options' => [ - 'select' => \esc_html__( 'Select', 'extensions-for-elementor-form' ), - 'post_title' => \esc_html__( 'Post Title', 'extensions-for-elementor-form' ), - 'post_content' => \esc_html__( 'Post Content', 'extensions-for-elementor-form' ), - 'post_excerpt' => \esc_html__( 'Post Excerpt', 'extensions-for-elementor-form' ), - 'post_author' => \esc_html__( 'Post Author', 'extensions-for-elementor-form' ), - 'custom_field' => \esc_html__( 'Custom Field', 'extensions-for-elementor-form' ), - ], - ]; - - $new_control_2 = [ - 'label' => \esc_html__( 'Custom Field Name', 'extensions-for-elementor-form' ), - 'type' => ElementorControls::TEXT, - 'placeholder' => \esc_html__( 'custom_field_name', 'extensions-for-elementor-form' ), - 'tab' => 'content', - 'tabs_wrapper' => 'form_fields_tabs', - 'inner_tab' => 'form_fields_advanced_tab', - 'description' => \esc_html__( 'Add the Custom Field name here. You can use default fields or custom created with ACF or similars', 'extensions-for-elementor-form' ), - 'condition' => [ - 'eef-register-post-field' => 'custom_field', - ], - ]; - - $mask_control = new ElementorRepeater(); - $mask_control->add_control( 'eef-register-post-field', $new_control ); - $mask_control->add_control( 'eef-register-post-custom-field', $new_control_2 ); - $pattern_field = $mask_control->get_controls(); - - /** - * Register control in form advanced tab. - */ - $this->register_control_in_form_advanced_tab( $element, $control_data, $pattern_field ); - } - - /** - * Register control in form advanced tab - * - * @param object $element - * @param array $control_data - * @param array $pattern_field - * @return void - */ - public function register_control_in_form_advanced_tab( $element, $control_data, $pattern_field ) { - foreach( $pattern_field as $key => $control ) { - if( $key !== '_id' ) { - $new_order = []; - foreach ( $control_data['fields'] as $field_key => $field ) { - if ( 'field_value' === $field['name'] ) { - $new_order[$key] = $control; - } - $new_order[ $field_key ] = $field; - } - - $control_data['fields'] = $new_order; - } - } - - return $element->update_control( 'form_fields', $control_data ); - } -} diff --git a/includes/actions/class-register-post.php b/includes/actions/class-register-post.php index 8e919f9..71d545a 100644 --- a/includes/actions/class-register-post.php +++ b/includes/actions/class-register-post.php @@ -6,10 +6,17 @@ exit; } +use \Elementor\Plugin as ElementorPlugin; +use \Elementor\Controls_Manager as ElementorControls; +use \Elementor\Repeater as ElementorRepeater; + /** * Register post after form submit. */ class Register_Post extends \ElementorPro\Modules\Forms\Classes\Action_Base { + public function __construct() { + \add_action( 'elementor/element/form/section_form_fields/before_section_end', array( $this, 'add_control_fields' ), 100, 2 ); + } /** * Get Name * @@ -178,4 +185,86 @@ public function run( $record, $ajax_handler ) { } } } + + /** + * Add create post fields + * + * @param $element + * @param $args + */ + public function add_control_fields( $element, $args ) { + $elementor = ElementorPlugin::instance(); + $control_data = $elementor->controls_manager->get_control_from_stack( $element->get_name(), 'form_fields' ); + + if ( is_wp_error( $control_data ) ) { + return; + } + + $new_control = [ + 'label' => \esc_html__( 'Field to Register', 'extensions-for-elementor-form' ), + 'type' => ElementorControls::SELECT, + 'tab' => 'content', + 'tabs_wrapper' => 'form_fields_tabs', + 'inner_tab' => 'form_fields_advanced_tab', + 'classes' => 'elementor-hidden-control', + 'description' => \esc_html__( 'Use this input to define what post field will receive this data when post is registered', 'extensions-for-elementor-form' ), + 'default' => 'select', + 'options' => [ + 'select' => \esc_html__( 'Select', 'extensions-for-elementor-form' ), + 'post_title' => \esc_html__( 'Post Title', 'extensions-for-elementor-form' ), + 'post_content' => \esc_html__( 'Post Content', 'extensions-for-elementor-form' ), + 'post_excerpt' => \esc_html__( 'Post Excerpt', 'extensions-for-elementor-form' ), + 'post_author' => \esc_html__( 'Post Author', 'extensions-for-elementor-form' ), + 'custom_field' => \esc_html__( 'Custom Field', 'extensions-for-elementor-form' ), + ], + ]; + + $new_control_2 = [ + 'label' => \esc_html__( 'Custom Field Name', 'extensions-for-elementor-form' ), + 'type' => ElementorControls::TEXT, + 'placeholder' => \esc_html__( 'custom_field_name', 'extensions-for-elementor-form' ), + 'tab' => 'content', + 'tabs_wrapper' => 'form_fields_tabs', + 'inner_tab' => 'form_fields_advanced_tab', + 'description' => \esc_html__( 'Add the Custom Field name here. You can use default fields or custom created with ACF or similars', 'extensions-for-elementor-form' ), + 'condition' => [ + 'eef-register-post-field' => 'custom_field', + ], + ]; + + $mask_control = new ElementorRepeater(); + $mask_control->add_control( 'eef-register-post-field', $new_control ); + $mask_control->add_control( 'eef-register-post-custom-field', $new_control_2 ); + $pattern_field = $mask_control->get_controls(); + + /** + * Register control in form advanced tab. + */ + $this->register_control_in_form_advanced_tab( $element, $control_data, $pattern_field ); + } + + /** + * Register control in form advanced tab + * + * @param object $element + * @param array $control_data + * @param array $pattern_field + */ + public function register_control_in_form_advanced_tab( $element, $control_data, $pattern_field ) { + foreach( $pattern_field as $key => $control ) { + if( $key !== '_id' ) { + $new_order = []; + foreach ( $control_data['fields'] as $field_key => $field ) { + if ( 'field_value' === $field['name'] ) { + $new_order[$key] = $control; + } + $new_order[ $field_key ] = $field; + } + + $control_data['fields'] = $new_order; + } + } + + return $element->update_control( 'form_fields', $control_data ); + } } diff --git a/includes/class-plugin.php b/includes/class-plugin.php index b0fb2ea..2419674 100644 --- a/includes/class-plugin.php +++ b/includes/class-plugin.php @@ -6,6 +6,9 @@ exit; } +use EEF\Includes\Actions\Register_Actions; +use EEF\Includes\Custom_Success_Message; + /** * Plugin main class */ @@ -60,27 +63,51 @@ public function init() : void { $this->load_required_files(); + $actions = array( + 'whatsapp_redirect' => array( + 'relative_path' => '/includes/actions/class-whatsapp-redirect.php', + 'class_name' => 'Whatsapp_Redirect', + ), + 'register_post' => array( + 'relative_path' => '/includes/actions/class-register-post.php', + 'class_name' => 'Register_Post', + ), + ); + $regiser_actions = new Register_Actions( $actions ); + $regiser_actions->set_hooks(); + $custom_success_message = new Custom_Success_Message(); $custom_success_message->set_hooks(); + + \add_action( 'elementor/editor/after_enqueue_scripts', array( $this, 'register_editor_scripts') ); + \add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frondend_scripts' ) ); } /** * Load required files */ public function load_required_files() : void { - include_once EEF_PLUGIN_PATH . '/includes/init-custom-actions.php'; + include_once EEF_PLUGIN_PATH . '/includes/actions/class-register-actions.php'; include_once EEF_PLUGIN_PATH . '/includes/class-custom-success-message.php'; } /** - * Enqueu admin styles/scripts + * Enqueue front end styles/scripts */ - public function enqueue_admin_scripts() : void {} + public function enqueue_frondend_scripts() : void { + wp_enqueue_script( 'eef-frontend-script', EEF_PLUGN_URL . 'assets/js/frontend-scripts.min.js', array( 'jquery' ), EEF_VERSION ); + wp_enqueue_style( 'eef-frontend-style', EEF_PLUGN_URL . 'assets/css/style.min.css', array(), EEF_VERSION ); + } /** - * Enqueue front end styles/scripts + * Register custom scritps on Elementor editor + * + * @since 2.0 */ - public function enqueue_frondend_scripts() : void {} + function register_editor_scripts() : void { + wp_register_script( 'eef-editor-scripts', EEF_PLUGN_URL . 'assets/js/editor-scripts.min.js', array(), EEF_VERSION ); + wp_enqueue_script( 'eef-editor-scripts' ); + } /** * Check Elementor Pro loaded diff --git a/includes/init-custom-actions.php b/includes/init-custom-actions.php deleted file mode 100644 index 80bd091..0000000 --- a/includes/init-custom-actions.php +++ /dev/null @@ -1,58 +0,0 @@ -set_hooks(); - - -/** - * Add new form action after form submission. - * - * @since 2.0 - * @param ElementorPro\Modules\Forms\Registrars\Form_Actions_Registrar $form_actions_registrar - * @return void - */ -function eef_register_custom_action( $form_actions_registrar ) { - include_once EEF_PLUGIN_PATH . '/includes/actions/class-whatsapp-redirect.php'; - include_once EEF_PLUGIN_PATH . '/includes/actions/class-register-post.php'; - - $form_actions_registrar->register( new Whatsapp_Redirect() ); - $form_actions_registrar->register( new Register_Post() ); -} - -add_action( 'elementor_pro/forms/actions/register', 'eef_register_custom_action', -10 ); - -/** - * Register frontend assets - * - * @since 2.0 - */ -function eef_register_plugin_assets() { - wp_enqueue_script( 'eef-frontend-script', EEF_PLUGN_URL . 'assets/js/frontend-scripts.min.js', array( 'jquery' ), EEF_VERSION ); - wp_enqueue_style( 'eef-frontend-style', EEF_PLUGN_URL . 'assets/css/style.min.css', array(), EEF_VERSION ); -} - -add_action( 'wp_enqueue_scripts', 'eef_register_plugin_assets' ); - -/** - * Register custom scritps on Elementor editor - * - * @since 2.0 - */ -function eef_register_editor_scripts() { - wp_register_script( 'eef-editor-scripts', EEF_PLUGN_URL . 'assets/js/editor-scripts.min.js', array(), EEF_VERSION ); - wp_enqueue_script( 'eef-editor-scripts' ); -} - -add_action( 'elementor/editor/after_enqueue_scripts', 'eef_register_editor_scripts' ); diff --git a/src/js/admin/editor-scripts.js b/src/js/admin/editor-scripts.js index 6ec46e3..cc83c0a 100644 --- a/src/js/admin/editor-scripts.js +++ b/src/js/admin/editor-scripts.js @@ -1,45 +1,45 @@ -window.addEventListener('load', (e) => { - const eefElementorPanelEditor = document.getElementById('elementor-panel-content-wrapper'); - if (!eefElementorPanelEditor) { - return; - } +window.addEventListener('load', () => { + const eefElementorPanelEditor = document.getElementById('elementor-panel-content-wrapper'); + if (!eefElementorPanelEditor) { + return; + } - const observer = new MutationObserver((mutations, observer) => { - const actionRegisterPostSection = eefElementorPanelEditor.querySelector('.elementor-control-eef-register-post-section'); - if (!actionRegisterPostSection) { - return; - } - - if(!actionRegisterPostSection.classList.contains('elementor-hidden-control') ) { - const effActionControls = eefElementorPanelEditor.querySelectorAll('.elementor-control-eef-register-post-field'); - if (effActionControls.length > 0) { - effActionControls.forEach((item) => { - item.classList.remove('elementor-hidden-control'); - }); - } - } else { - const effActionControls = eefElementorPanelEditor.querySelectorAll('.elementor-control-eef-register-post-field'); - const effActionControls2 = eefElementorPanelEditor.querySelectorAll('.elementor-control-eef-register-post-custom-field'); - if (effActionControls.length > 0) { - effActionControls.forEach((item) => { - item.classList.add('elementor-hidden-control'); - }); - } + const observer = new MutationObserver((mutations, observer) => { + const actionRegisterPostSection = eefElementorPanelEditor.querySelector('.elementor-control-eef-register-post-section'); + if (!actionRegisterPostSection) { + return; + } - if (effActionControls2.length > 0) { - effActionControls2.forEach((item) => { - if (!item.classList.contains('elementor-hidden-control')) { - item.classList.add('elementor-hidden-control'); - } - }); - } - } - }); - - observer.observe(eefElementorPanelEditor, { - childList: true, - subtree: true, - attributes: false, - characterData: false, - }); -}); \ No newline at end of file + if(!actionRegisterPostSection.classList.contains('elementor-hidden-control') ) { + const effActionControls = eefElementorPanelEditor.querySelectorAll('.elementor-control-eef-register-post-field'); + if (effActionControls.length > 0) { + effActionControls.forEach((item) => { + item.classList.remove('elementor-hidden-control'); + }); + } + } else { + const effActionControls = eefElementorPanelEditor.querySelectorAll('.elementor-control-eef-register-post-field'); + const effActionControls2 = eefElementorPanelEditor.querySelectorAll('.elementor-control-eef-register-post-custom-field'); + if (effActionControls.length > 0) { + effActionControls.forEach((item) => { + item.classList.add('elementor-hidden-control'); + }); + } + + if (effActionControls2.length > 0) { + effActionControls2.forEach((item) => { + if (!item.classList.contains('elementor-hidden-control')) { + item.classList.add('elementor-hidden-control'); + } + }); + } + } + }); + + observer.observe(eefElementorPanelEditor, { + childList: true, + subtree: true, + attributes: false, + characterData: false, + }); +}); From 5f8ccf49b32ef804a3ca500f9f82ebde8ccba4b0 Mon Sep 17 00:00:00 2001 From: Eduardo Date: Sun, 5 Jan 2025 22:31:38 -0300 Subject: [PATCH 7/7] chore: set version --- README.md | 4 ++++ extensions-for-elementor-form.php | 4 ++-- readme.txt | 7 ++++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 21548d1..331b73a 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,10 @@ We’d love to hear from you! [plugins@eduardovillao.me](mailto:plugins@eduardov ## Changelog ``` += 2.3 = +* Changed: refactor code to support Pro version. +* Changed: improve assets build process. + = 2.2 = * Changed: code improvements. diff --git a/extensions-for-elementor-form.php b/extensions-for-elementor-form.php index 6594dfd..367ca91 100644 --- a/extensions-for-elementor-form.php +++ b/extensions-for-elementor-form.php @@ -6,7 +6,7 @@ * Author: EduardoVillao.me * Author URI: https://eduardovillao.me/ * Text Domain: extensions-for-elementor-form - * Version: 2.2 + * Version: 2.3 * Requires at least: 5.5 * Requires PHP: 7.4 * License: GPL-2.0+ @@ -19,7 +19,7 @@ define( 'EEF_PLUGIN_PATH', plugin_dir_path( __FILE__ ) ); define( 'EEF_PLUGN_URL', plugin_dir_url( __FILE__ ) ); -define( 'EEF_VERSION', '2.2' ); +define( 'EEF_VERSION', '2.3' ); define( 'EEF_PHP_MINIMUM_VERSION', '7.4' ); define( 'EEF_WP_MINIMUM_VERSION', '5.5' ); define( 'EEF_PLUGIN_MAIN_FILE', __FILE__ ); diff --git a/readme.txt b/readme.txt index a2a5ce0..8cf4a13 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Donate link: https://eduardovillao.me/ Tags: elementor, elementor form, register custom post type, whatsapp Requires at least: 5.5 Tested up to: 6.7 -Stable tag: 2.2 +Stable tag: 2.3 Requires PHP: 7.4 License: GPL-2.0+ URI:https://www.gnu.org/licenses/gpl-2.0.html @@ -66,6 +66,11 @@ No! This extensions will be work only with Elementor plugin. == Screenshots == == Changelog == + += 2.3 = +* Changed: refactor code to support Pro version. +* Changed: improve assets build process. + = 2.2 = * Changed: code improvements.