Skip to content

Commit

Permalink
beta-v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardovillao committed Feb 16, 2023
1 parent 5487437 commit 1c80297
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 51 deletions.
46 changes: 46 additions & 0 deletions assets/editor-scripts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
window.addEventListener('load', (e) => {
const eefElementorPanelEditor = document.getElementById('elementor-panel-content-wrapper');
if (!eefElementorPanelEditor) {
return;
}

const observer = new MutationObserver((mutations, observer) => {
console.log('mudou');
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');
});
}

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,
});
});
File renamed without changes.
12 changes: 0 additions & 12 deletions extensions-for-elementor-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,6 @@ 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';

add_action( 'wp_enqueue_scripts', 'eef_register_plugin_assets' );
}

/**
* Register style (temp. code)
*
* @since 2.0
*/
function eef_register_plugin_assets() {
wp_enqueue_script( 'eef-frontend-script', EEF_PLUGN_URL . 'assets/script.js', array( 'jquery' ), EEF_VERSION );
wp_enqueue_style( 'eef-frontend-style', EEF_PLUGN_URL . 'assets/style.css', array(), EEF_VERSION );
}

/**
Expand Down
7 changes: 3 additions & 4 deletions includes/class-register-create-post-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,17 @@ public function add_control_fields( $element, $args ) {
'tab' => 'content',
'tabs_wrapper' => 'form_fields_tabs',
'inner_tab' => 'form_fields_advanced_tab',
'classes' => 'elementor-hidden-control',
'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' ),
'post_excerpt' => __( 'Post Excerpt', 'extensions-for-elementor-form' ),
'post_author' => __( 'Post Author', 'extensions-for-elementor-form' ),
'custom_field' => __( 'Custom Field', 'extensions-for-elementor-form' ),
],
// 'condition' => [
// 'submit_actions' => 'eef-register-post',
// ],
];

$new_control_2 = [
Expand Down
88 changes: 54 additions & 34 deletions includes/class-register-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ public function get_label() {
return 'Register Post/Custom Post';
}

/**
* Get public post types
*/
private function get_registered_post_types() {
$registered_post_types = get_post_types( array( 'public' => true ), 'objects' );
$post_type_options = array();
foreach ( $registered_post_types as $post_type ) {
$post_type_options[ $post_type->name ] = $post_type->label;
}

return $post_type_options;
}

/**
* Register Settings Section
*
Expand Down Expand Up @@ -63,10 +76,7 @@ public function register_settings_section( $widget ) {
'label' => __( 'Post Type', 'extensions-for-elementor-form' ),
'type' => \Elementor\Controls_Manager::SELECT,
'default' => 'post',
'options' => [
'post' => esc_html__( 'Post', 'extensions-for-elementor-form' ),
'page' => esc_html__( 'Page', 'extensions-for-elementor-form' ),
],
'options' => $this->get_registered_post_types(),
]
);

Expand All @@ -87,7 +97,7 @@ public function register_settings_section( $widget ) {
$widget->add_control(
'eef-register-post-user-permission',
[
'label' => esc_html__( 'Run only to logged in users', 'extensions-for-elementor-form' ),
'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' ),
Expand All @@ -107,12 +117,13 @@ public function register_settings_section( $widget ) {
* @param array $element
*/
public function on_export( $element ) {
// unset(
// $element['settings']['whatsapp_to'],
// $element['settings']['whatsapp_message']
// );
unset(
$element['settings']['eef-register-post-post-type'],
$element['settings']['eef-register-post-post-status'],
$element['settings']['eef-register-post-user-permission'],
);

// return $element;
return $element;
}

/**
Expand All @@ -122,6 +133,13 @@ public function on_export( $element ) {
* @param \ElementorPro\Modules\Forms\Classes\Ajax_Handler $ajax_handler
*/
public function run( $record, $ajax_handler ) {
$new_post_data = array(
'post_type' => $record->get_form_settings( 'eef-register-post-post-type' ),
'post_status' => $record->get_form_settings( 'eef-register-post-post-status' ),
'post_title' => $record->get_form_settings( 'form_name' ),
'post_content' => $record->get_form_settings( 'form_name' ),
);

$form_fields = $record->get( 'fields' );
$fields_settings = $record->get_form_settings( 'form_fields' );
$formated_fields = array();
Expand All @@ -131,31 +149,33 @@ public function run( $record, $ajax_handler ) {
$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';
$custom_fields_to_register = array();
foreach ( $formated_fields as $field ) {
if ( $field['field-to-register'] !== 'custom_field' ) {
$new_post_data[ $field['field-to-register'] ] = $field['value'];
} else {
$custom_fields_to_register[ $field['custom-field-to-register'] ] = $field['value'];
}
}

$is_restrict_to_loggedin_users = $record->get_form_settings( 'eef-register-post-user-permission' );
if ( $is_restrict_to_loggedin_users !== 'yes' ) {
$post_id = wp_insert_post( $new_post_data, true );
if ( ! is_wp_error( $post_id ) ) {
foreach ( $custom_fields_to_register as $meta_key => $meta_value ) {
add_post_meta( $post_id, $meta_key, $meta_value );
}
}
return;
}
//sanitize_post();

// $whatsapp_to = $record->get_form_settings( 'whatsapp_to' );
// $whatsapp_message = $record->get_form_settings( 'whatsapp_message' );

// $whatsapp_message = str_replace('%break%', '%0D%0A', $whatsapp_message);

// $whatsapp_to = 'https://wa.me/'.$whatsapp_to.'?text='.$whatsapp_message.'';
// $whatsapp_to = $record->replace_setting_shortcodes( $whatsapp_to, true );

// if ( ! empty( $whatsapp_to ) ) {
// $ajax_handler->add_response_data( 'redirect_url', $whatsapp_to );
// }
if ( is_user_logged_in() ) {
$post_id = wp_insert_post( $new_post_data, true );
if ( ! is_wp_error( $post_id ) ) {
foreach ( $custom_fields_to_register as $meta_key => $meta_value ) {
add_post_meta( $post_id, $meta_key, $meta_value );
}
}
}
}
}
28 changes: 27 additions & 1 deletion init-custom-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,32 @@ function eef_register_custom_action( $form_actions_registrar ) {

/**
* 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();
new Register_Create_Post_Fields();

/**
* Register frontend assets
*
* @since 2.0
*/
function eef_register_plugin_assets() {
wp_enqueue_script( 'eef-frontend-script', EEF_PLUGN_URL . 'assets/frontend-scripts.js', array( 'jquery' ), EEF_VERSION );
wp_enqueue_style( 'eef-frontend-style', EEF_PLUGN_URL . 'assets/style.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/editor-scripts.js', array(), EEF_VERSION );
wp_enqueue_script( 'eef-editor-scripts' );
}

add_action( 'elementor/editor/after_enqueue_scripts', 'eef_register_editor_scripts' );

0 comments on commit 1c80297

Please sign in to comment.