Skip to content

Commit

Permalink
feat: refactor how actions are created
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardovillao committed Jan 6, 2025
1 parent 31b5453 commit 004cdd4
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 212 deletions.
43 changes: 43 additions & 0 deletions includes/actions/class-register-actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace EEF\Includes\Actions;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

class Register_Actions {
/**
* Actions
*/
private array $actions;

public function __construct( $actions ) {
$this->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() );
}
}
}
}
106 changes: 0 additions & 106 deletions includes/actions/class-register-post-fields-controls.php

This file was deleted.

89 changes: 89 additions & 0 deletions includes/actions/class-register-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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 );
}
}
37 changes: 32 additions & 5 deletions includes/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
exit;
}

use EEF\Includes\Actions\Register_Actions;
use EEF\Includes\Custom_Success_Message;

/**
* Plugin main class
*/
Expand Down Expand Up @@ -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
Expand Down
58 changes: 0 additions & 58 deletions includes/init-custom-actions.php

This file was deleted.

Loading

0 comments on commit 004cdd4

Please sign in to comment.