Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active September 17, 2021 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wpmudev-sls/e54e1ac62f30ae2c15c755092832c796 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/e54e1ac62f30ae2c15c755092832c796 to your computer and use it in GitHub Desktop.
[Forminator Pro] - Create form via API
<?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::DOMAIN ),
'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::DOMAIN ),
"placeholder" => __( "Select number of guests", Forminator::DOMAIN ),
"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::DOMAIN ),
"placeholder" => __( "E.g. John", Forminator::DOMAIN ),
"prefix_label" => __( "Prefix", Forminator::DOMAIN ),
"fname_label" => __( "First Name", Forminator::DOMAIN ),
"fname_placeholder" => __( "E.g. John", Forminator::DOMAIN ),
"mname_label" => __( "Middle Name", Forminator::DOMAIN ),
"mname_placeholder" => __( "E.g. Smith", Forminator::DOMAIN ),
"lname_label" => __( "Last Name", Forminator::DOMAIN ),
"lname_placeholder" => __( "E.g. Doe", Forminator::DOMAIN ),
"conditions" => $conditions,
"condition_action" => "hide",
"condition_rule" => "any"
),
array(
"element_id" => "email-{$i}",
"type" => "email",
"cols" => "6",
"required" => "true",
"field_label" => __( "Email Address", Forminator::DOMAIN ),
"placeholder" => __( "E.g. john@doe.com", Forminator::DOMAIN ),
"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::DOMAIN ),
"use-custom-submit" => "true",
"custom-submit-text" => __( "Send Message", Forminator::DOMAIN ),
"use-custom-invalid-form" => "true",
"custom-invalid-form-message" => __( "Error: Your form is not valid, please fix the errors!", Forminator::DOMAIN ),
"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