diff --git a/assets/editor-scripts.js b/assets/editor-scripts.js new file mode 100644 index 0000000..c31eeb2 --- /dev/null +++ b/assets/editor-scripts.js @@ -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, + }); +}); \ No newline at end of file diff --git a/assets/script.js b/assets/frontend-scripts.js similarity index 100% rename from assets/script.js rename to assets/frontend-scripts.js diff --git a/extensions-for-elementor-form.php b/extensions-for-elementor-form.php index 7d88742..d1a97c7 100644 --- a/extensions-for-elementor-form.php +++ b/extensions-for-elementor-form.php @@ -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 ); } /** diff --git a/includes/class-register-create-post-fields.php b/includes/class-register-create-post-fields.php index 019dac9..e675bf3 100644 --- a/includes/class-register-create-post-fields.php +++ b/includes/class-register-create-post-fields.php @@ -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 = [ diff --git a/includes/class-register-post.php b/includes/class-register-post.php index d48e366..05ce6a4 100644 --- a/includes/class-register-post.php +++ b/includes/class-register-post.php @@ -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 * @@ -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(), ] ); @@ -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' ), @@ -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; } /** @@ -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(); @@ -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 ); + } + } + } } } diff --git a/init-custom-actions.php b/init-custom-actions.php index 80e709b..0be93c2 100644 --- a/init-custom-actions.php +++ b/init-custom-actions.php @@ -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(); \ No newline at end of file +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' ); \ No newline at end of file