Skip to content

Commit

Permalink
WIP: register post after submit
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardovillao committed Feb 15, 2023
1 parent bbe019c commit 5487437
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 21 deletions.
5 changes: 4 additions & 1 deletion extensions-for-elementor-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
exit;
}

use ElementorPro\Plugin;
use ElementorPro\Modules\ThemeBuilder\Module;

define( 'EEF_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
define( 'EEF_PLUGN_URL', plugin_dir_url( __FILE__ ) );
define( 'EEF_VERSION', '2.0' );
Expand Down Expand Up @@ -49,7 +52,7 @@ function eef_init_plugin() {
return;
}

include_once EEF_PLUGIN_PATH . '/init-whatsapp-action.php';
include_once EEF_PLUGIN_PATH . '/init-custom-actions.php';
include_once EEF_PLUGIN_PATH . '/includes/class-show-content-after-submit.php';

add_action( 'wp_enqueue_scripts', 'eef_register_plugin_assets' );
Expand Down
114 changes: 114 additions & 0 deletions includes/class-register-create-post-fields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace Eef\Includes;

use \Elementor\Plugin as ElementorPlugin;
use \Elementor\Controls_Manager as ElementorControls;
use \Elementor\Repeater as ElementorRepeater;

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

/**
* Register custom field inside a form repeater (advanced tab) if action is enabled.
*
* @since 2.0
*/
class Register_Create_Post_Fields {
/**
* Contructor...
*/
public function __construct() {
add_action( 'elementor/element/form/section_form_fields/before_section_end', [ $this, 'add_control_fields' ], 100, 2 );
}

/**
* Add create post fields
*
* @since 2.0
* @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' => __( 'Field to Register', 'extensions-for-elementor-form' ),
'type' => ElementorControls::SELECT,
'tab' => 'content',
'tabs_wrapper' => 'form_fields_tabs',
'inner_tab' => 'form_fields_advanced_tab',
'description' => __( 'Use this input to define what post field will receive this data when post is registered', 'extensions-for-elementor-form' ),
'default' => 'select',
'options' => [
'select' => __( 'Select', 'extensions-for-elementor-form' ),
'post_title' => __( 'Post Title', 'extensions-for-elementor-form' ),
'post_content' => __( 'Post Content', 'extensions-for-elementor-form' ),
'post_excerpt' => __( 'Post Excerpt', 'extensions-for-elementor-form' ),
'custom_field' => __( 'Custom Field', 'extensions-for-elementor-form' ),
],
// 'condition' => [
// 'submit_actions' => 'eef-register-post',
// ],
];

$new_control_2 = [
'label' => __( 'Custom Field Name', 'extensions-for-elementor-form' ),
'type' => ElementorControls::TEXT,
'placeholder' => __( 'custom_field_name', 'extensions-for-elementor-form' ),
'tab' => 'content',
'tabs_wrapper' => 'form_fields_tabs',
'inner_tab' => 'form_fields_advanced_tab',
'description' => __( '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.
*
* @since 2.0
*/
$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
*
* @since 2.0
*/
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 );
}
}
72 changes: 55 additions & 17 deletions includes/class-register-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Register_Post extends \ElementorPro\Modules\Forms\Classes\Action_Base {
* @return string
*/
public function get_name() {
return 'register-post';
return 'eef-register-post';
}

/**
Expand Down Expand Up @@ -51,22 +51,12 @@ public function register_settings_section( $widget ) {
);

/**
* 1. Post status.
* 2. Add or edit existent post.
* 3. Post terms (taxonomy).
* 4. Redirect to post after submit/created?.
* 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-note',
// [
// 'label' => esc_html__( 'Important', 'extensions-for-elementor-form' ),
// 'type' => \Elementor\Controls_Manager::RAW_HTML,
// 'raw' => esc_html__( 'For security reasons this action will works only to logged in users.', 'extensions-for-elementor-form' ),
// 'content_classes' => 'your-class',
// ]
// );

$widget->add_control(
'eef-register-post-post-type',
[
Expand All @@ -77,7 +67,33 @@ public function register_settings_section( $widget ) {
'post' => esc_html__( 'Post', 'extensions-for-elementor-form' ),
'page' => esc_html__( 'Page', 'extensions-for-elementor-form' ),
],
'description' => __( 'Select the Post Type to receive the form data.', 'extensions-for-elementor-form' ),
]
);

$widget->add_control(
'eef-register-post-post-status',
[
'label' => __( 'Post Status', 'extensions-for-elementor-form' ),
'type' => \Elementor\Controls_Manager::SELECT,
'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' ),
],
]
);

$widget->add_control(
'eef-register-post-user-permission',
[
'label' => esc_html__( 'Run only to logged in users', 'extensions-for-elementor-form' ),
'type' => \Elementor\Controls_Manager::SWITCHER,
'label_on' => esc_html__( 'Yes', 'extensions-for-elementor-form' ),
'label_off' => esc_html__( 'No', 'extensions-for-elementor-form' ),
'return_value' => 'yes',
'default' => 'yes',
'description' => __( 'Warning: Save data from not logged in users can be a security risk', 'extensions-for-elementor-form' ),
]
);

Expand Down Expand Up @@ -106,7 +122,29 @@ public function on_export( $element ) {
* @param \ElementorPro\Modules\Forms\Classes\Ajax_Handler $ajax_handler
*/
public function run( $record, $ajax_handler ) {
$form_fields = $record->get( 'fields' );
$fields_settings = $record->get_form_settings( 'form_fields' );
$formated_fields = array();
foreach ( $fields_settings as $key => $field ) {
$formated_fields[ $key ] = $form_fields[ $field['custom_id'] ];
$formated_fields[ $key ]['field-to-register'] = $field['eef-register-post-field'];
$formated_fields[ $key ]['custom-field-to-register'] = $field['eef-register-post-custom-field'];
}

print_r( $formated_fields );
return;

$post_data = array(
'post_title' => '123',
'post_content' => '456',
'post_status' => 'draft'
);

$post_id = wp_insert_post( $post_data );
if ( is_wp_error( $post_id ) ) {
return 'WP Error';
}
//sanitize_post();

// $whatsapp_to = $record->get_form_settings( 'whatsapp_to' );
// $whatsapp_message = $record->get_form_settings( 'whatsapp_message' );
Expand Down
16 changes: 13 additions & 3 deletions init-whatsapp-action.php → init-custom-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
exit;
}

use Eef\Includes\Register_Create_Post_Fields;
use Eef\Includes\Whatsapp_Action_After_Submit;
use Eef\Includes\Register_Post;

/**
* Add new form action after form submission.
*
Expand All @@ -15,8 +19,14 @@ 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';

$form_actions_registrar->register( new Eef\Includes\Whatsapp_Action_After_Submit() );
$form_actions_registrar->register( new \Eef\Includes\Register_Post() );
$form_actions_registrar->register( new Whatsapp_Action_After_Submit() );
$form_actions_registrar->register( new Register_Post() );
}

add_action( 'elementor_pro/forms/actions/register', 'eef_register_custom_action' );
add_action( 'elementor_pro/forms/actions/register', 'eef_register_custom_action' );

/**
* Register custom field to form repeater
*/
include_once EEF_PLUGIN_PATH . '/includes/class-register-create-post-fields.php';
new Register_Create_Post_Fields();

0 comments on commit 5487437

Please sign in to comment.