Forked from wpmudev-sls/wpmudev-forminator-api-select-field-import.php
Last active
November 6, 2022 00:23
-
-
Save patrickfreitasdev/818fab56b6fafc740a98a1b458a18101 to your computer and use it in GitHub Desktop.
[Forminator Pro] - Create form via API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Forminator Pro] - Create form via API | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Create a select along with name and email fields depending on selection (as of 1.12.1.1) | |
* Author: Alessandro Kaounas @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* Task: FOR-448 | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
// No need to do anything if the request is via WP-CLI. | |
if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
return; | |
} | |
if ( ! class_exists( 'WPMUDEV_Forminator_API_Select_Field_Import' ) ) { | |
class WPMUDEV_Forminator_API_Select_Field_Import { | |
private $guests = 10; | |
private static $instance = null; | |
public static function get_instance() { | |
if ( is_null( self::$instance ) ) { | |
self::$instance = new self(); | |
} | |
return self::$instance; | |
} | |
public function __construct(){ | |
if ( ! defined( 'FORMINATOR_VERSION' ) || FORMINATOR_VERSION < '1.12' ) { | |
return; | |
} | |
$this->init(); | |
} | |
public function init(){ | |
if( ! is_admin() || ! isset( $_GET['page'] ) || $_GET['page'] !== 'forminator-cform' | |
|| ! isset( $_GET['action'] ) || $_GET['action'] !== 'create' ){ | |
return; | |
} | |
$wrappers = $options = $conditions = array(); | |
for ( $i=1; $i <= $this->guests ; $i++ ) { | |
$options[] = array( | |
'label' => __( $i, 'forminator' ), | |
'value' => "{$i}", | |
'limit' => '', | |
'key' => forminator_unique_key(), | |
); | |
} | |
$wrappers[] = array( | |
'wrapper_id' => 'wrapper-' . forminator_unique_key(), | |
'fields' => array( | |
array( | |
"element_id" => "select-1", | |
"type" => "select", | |
"cols" => "12", | |
"required" => "true", | |
"value_type" => "single", | |
"field_label" => __( "Guests", 'forminator' ), | |
"placeholder" => __( "Select number of guests", 'forminator' ), | |
"options" => $options, | |
) | |
) | |
); | |
for ( $i=1; $i <= $this->guests ; $i++ ) { | |
if( $i === 1 ){ | |
$conditions[] = array( | |
"element_id" => "select-1", | |
"rule" => "is", | |
"value" => "", | |
); | |
} | |
$wrappers[] = array( | |
'wrapper_id' => 'wrapper-' . forminator_unique_key(), | |
'fields' => array( | |
array( | |
"element_id" => "name-{$i}", | |
"type" => "name", | |
"cols" => "6", | |
"required" => "true", | |
"field_label" => __( "First Name", 'forminator' ), | |
"placeholder" => __( "E.g. John", 'forminator' ), | |
"prefix_label" => __( "Prefix", 'forminator' ), | |
"fname_label" => __( "First Name", 'forminator' ), | |
"fname_placeholder" => __( "E.g. John", 'forminator' ), | |
"mname_label" => __( "Middle Name", 'forminator' ), | |
"mname_placeholder" => __( "E.g. Smith", 'forminator' ), | |
"lname_label" => __( "Last Name", 'forminator' ), | |
"lname_placeholder" => __( "E.g. Doe", 'forminator' ), | |
"conditions" => $conditions, | |
"condition_action" => "hide", | |
"condition_rule" => "any" | |
), | |
array( | |
"element_id" => "email-{$i}", | |
"type" => "email", | |
"cols" => "6", | |
"required" => "true", | |
"field_label" => __( "Email Address", 'forminator' ), | |
"placeholder" => __( "E.g. [email protected]", 'forminator' ), | |
"validation" => true, | |
"validation_text" => "", | |
"conditions" => $conditions, | |
"condition_action" => "hide", | |
"condition_rule" => "any" | |
) | |
), | |
); | |
$conditions[] = array( | |
"element_id" => "select-1", | |
"rule" => "is", | |
"value" => "$i", | |
); | |
} | |
$settings = array( | |
"thankyou" => "true", | |
"thankyou-message" => __( "Thank you for contacting us, we will be in touch shortly.", 'forminator' ), | |
"use-custom-submit" => "true", | |
"custom-submit-text" => __( "Send Message", 'forminator' ), | |
"use-custom-invalid-form" => "true", | |
"custom-invalid-form-message" => __( "Error: Your form is not valid, please fix the errors!", 'forminator' ), | |
"enable-ajax" => "true", | |
"validation" => "on_submit", | |
"validation-inline" => true, | |
"fields-style" => "open", | |
"form-expire" => "no_expire", | |
); | |
$response = Forminator_API::add_form( | |
'Party Time', | |
$wrappers, | |
$settings | |
); | |
wp_die( | |
! is_wp_error( $response ) ? 'Your form have been sucessfully created!' : 'There was a problem creating the form!', | |
'Create form via API', | |
array( | |
'response' => 200, | |
'link_text' => 'Go back', | |
'link_url' => get_admin_url( '', 'admin.php?page=forminator-cform' ), | |
) | |
); | |
} | |
} | |
add_action( 'init', function(){ | |
return WPMUDEV_Forminator_API_Select_Field_Import::get_instance(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment