Skip to content

Instantly share code, notes, and snippets.

@MogulChris
Created March 15, 2023 02:56
Show Gist options
  • Save MogulChris/c78c4b74cc9345e5398ffb192f8acc33 to your computer and use it in GitHub Desktop.
Save MogulChris/c78c4b74cc9345e5398ffb192f8acc33 to your computer and use it in GitHub Desktop.
Custom 'Save submission' action for Jupiter X forms
<?php
/*
This is a quick a dirty way of saving JupiterX form submissions to your database, similar to how you can save the Elementor Pro ones.
I like the Jupiter X form widget because I work with its Hubspot action a lot, which Elementor Pro doesn't have. However the lack of local save sucks.
It seemed easier to implement saving submissions in the JX widget than Hubspot in the EPro one.
*/
//Include the action class. You can save it where you want and adjust the path below
include_once(get_stylesheet_directory() . '/inc/localsave.php');
//I will save submissions as a custom post type.
function create_jx_form_submission_post_type() {
register_post_type( 'jx_form_submission',
// CPT Options
array(
'labels' => array(
'name' => __( 'Form Submissions' ),
'singular_name' => __( 'Form submissoin' )
),
'public' => false,
'has_archive' => false,
'show_in_rest' => false,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
)
);
}
add_action( 'init', 'create_jx_form_submission_post_type' );
?>
<?php
/* This started life as a copy of /wp-content/plugins/jupiterx-core/includes/extensions/raven/includes/modules/forms/actions/webhook.php */
/* Note this registers itself at the bottom - there are no tidy hooks to use for this, but happily there is a register_custom_action() method we can use */
namespace JupiterX_Core\Raven\Modules\Forms\Actions;
use Elementor\Controls_Manager;
defined( 'ABSPATH' ) || die();
/**
* Localsave Action.
*
* Initializing the Localsave action by extending action base.
*
* @since 1.3.0
*/
class LocalSave extends Action_Base {
/**
* Get name.
*
* @since 1.19.0
* @access public
*/
public function get_name() {
return 'localsave';
}
/**
* Get title.
*
* @since 1.19.0
* @access public
*/
public function get_title() {
return __( 'Local save', 'jupiterx-core' );
}
/**
* Is private.
*
* @since 2.0.0
* @access public
*/
public function is_private() {
return false;
}
/**
* Exclude form fields in the localsave.
*
* @access private
*
* @var array
*/
private static $exclude_fields = [ 'recaptcha', 'recaptcha_v3' ];
/**
* Update controls.
*
* Localsave setting section.
*
* @since 1.3.0
* @access public
*
* @param object $widget Widget instance.
*/
public function update_controls( $widget ) {
// I don't need settings
// $widget->start_controls_section(
// 'section_localsave',
// [
// 'label' => __( 'Localsave', 'jupiterx-core' ),
// 'condition' => [
// 'actions' => 'localsave',
// ],
// ]
// );
// $widget->add_control(
// 'localsave_url',
// [
// 'label' => __( 'Localsave URL', 'jupiterx-core' ),
// 'type' => Controls_Manager::TEXT,
// 'placeholder' => 'http://localsave-endpoint.com',
// 'description' => __( 'Enter the localsave URL where you want to send your Form data after submit e.g. ', 'jupiterx-core' ) . sprintf( '<a href="%s" target="_blank">%s</a>.', 'https://zapier.com/apps/localsave/integrations', __( 'Integrate with Zapier Localsave', 'jupiterx-core' ) ),
// ]
// );
// $widget->end_controls_section();
}
/**
* Run action.
*
* Send form data to localsave URL.
*
* @since 1.3.0
* @access public
* @static
*
* @param object $ajax_handler Ajax handler instance.
*
* @return void
*/
public static function run( $ajax_handler ) {
$settings = $ajax_handler->form['settings'];
$body = self::get_form_data( $ajax_handler, $settings );
//do the save
$sub = [
'post_title' => 'Form submission ' . date('Y-m-d H:ia'),
'post_type' => 'jx_form_submission',
'post_status' => 'private',
'post_content' => json_encode($body),
];
wp_insert_post($sub);
}
/**
* Get form fields data.
*
* @since 1.3.0
* @access private
* @static
*
* @param object $ajax_handler Ajax handler instance.
* @param array $settings Form settings.
*
* @return array
*/
private static function get_form_data( $ajax_handler, $settings ) {
$fields = [];
foreach ( $settings['fields'] as $field ) {
if ( \in_array( $field['type'], self::$exclude_fields, true ) ) {
continue;
}
$field_value = $ajax_handler->record['fields'][ $field['_id'] ];
if ( 'acceptance' === $field['type'] ) {
$field_value = empty( $field_value ) ? __( 'No', 'jupiterx-core' ) : __( 'Yes', 'jupiterx-core' );
}
if ( empty( $field['label'] ) ) {
$fields[ __( 'No Label', 'jupiterx-core' ) . ' ' . $field['_id'] ] = $field_value;
} else {
$fields[ $field['label'] ] = $field_value;
}
}
return $fields;
}
}
namespace JupiterX_Core\Raven\Modules\Forms;
if(class_exists('Module')) Module::register_custom_action(new Actions\LocalSave()); //see wp-content/plugins/jupiterx-core/includes/extensions/raven/includes/modules/forms/module.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment