Skip to content

Instantly share code, notes, and snippets.

@alfredo-wpmudev
Created April 28, 2024 05:12
Show Gist options
  • Save alfredo-wpmudev/997fe107706897ba1916e52f1ed6d17e to your computer and use it in GitHub Desktop.
Save alfredo-wpmudev/997fe107706897ba1916e52f1ed6d17e to your computer and use it in GitHub Desktop.
Form 2 Company Attendee List and Delete
{"type":"form","data":{"fields":[{"id":"select-1","element_id":"select-1","form_id":"wrapper-1015-7110","parent_group":"","type":"select","options":[{"label":"3M Thailand Limited.","value":597,"limit":"","key":"8606-4941"}],"cols":"12","conditions":[],"wrapper_id":"wrapper-1015-7110","hidden_behavior":"zero","value_type":"single","field_label":"Companies","options_bulk_editor":""}],"settings":{"pagination-header":"nav","paginationData":{"pagination-header-design":"show","pagination-header":"nav"},"formName":"Company Attendee List","version":"1.30.1","form-border-style":"none","form-padding":"","form-border":"","fields-style":"open","field-image-size":"custom","validation":"on_submit","akismet-protection":"0","form-style":"default","enable-ajax":"false","autoclose":"true","submission-indicator":"show","indicator-label":"Submitting...","form-type":"default","submission-behaviour":"behaviour-thankyou","thankyou-message":"Thank you for contacting us, we will be in touch shortly.","submitData":{"custom-submit-text":"Delete Attendee","custom-invalid-form-message":"Error: Your form is not valid, please fix the errors!"},"validation-inline":"1","form-expire":"no_expire","form-padding-top":"0","form-padding-right":"0","form-padding-bottom":"0","form-padding-left":"0","form-border-width":"0","form-border-radius":"0","cform-label-font-family":"Roboto","cform-label-custom-family":"","cform-label-font-size":"12","cform-label-font-weight":"bold","cform-title-font-family":"Roboto","cform-title-custom-family":"","cform-title-font-size":"45","cform-title-font-weight":"normal","cform-title-text-align":"left","cform-subtitle-font-family":"Roboto","cform-subtitle-custom-font":"","cform-subtitle-font-size":"18","cform-subtitle-font-weight":"normal","cform-subtitle-text-align":"left","cform-input-font-family":"Roboto","cform-input-custom-font":"","cform-input-font-size":"16","cform-input-font-weight":"normal","cform-radio-font-family":"Roboto","cform-radio-custom-font":"","cform-radio-font-size":"14","cform-radio-font-weight":"normal","cform-select-font-family":"Roboto","cform-select-custom-family":"","cform-select-font-size":"16","cform-select-font-weight":"normal","cform-multiselect-font-family":"Roboto","cform-multiselect-custom-font":"","cform-multiselect-font-size":"16","cform-multiselect-font-weight":"normal","cform-dropdown-font-family":"Roboto","cform-dropdown-custom-font":"","cform-dropdown-font-size":"16","cform-dropdown-font-weight":"normal","cform-calendar-font-family":"Roboto","cform-calendar-custom-font":"","cform-calendar-font-size":"13","cform-calendar-font-weight":"normal","cform-button-font-family":"Roboto","cform-button-custom-font":"","cform-button-font-size":"14","cform-button-font-weight":"500","cform-timeline-font-family":"Roboto","cform-timeline-custom-font":"","cform-timeline-font-size":"12","cform-timeline-font-weight":"normal","cform-pagination-font-family":"","cform-pagination-custom-font":"","cform-pagination-font-size":"16","cform-pagination-font-weight":"normal","payment_require_ssl":"","submission-file":"delete","geolocation_field":"","require_geolocation_field":"Oops! We need access to your location to submit the form. Please allow location access to continue.","store_submissions":"","form_name":"Company Attendee List","form_status":"publish","print_value":"true","sc_email_link":"1","sc_message":"<p>Your form has been saved as draft and a resume link has been generated so you can return to the form anytime within {retention_period} days from today. Copy and save the link or enter your email address below to have the link sent to your mail.</p><p>These fields weren't saved to your submission draft: Paypal, Stripe, Signature, Password, Captcha, and Upload. Kindly fill them out before submitting the form.</p>","notification_count":1,"previous_status":"publish"},"client_id":null,"integration_conditions":[],"behaviors":[{"slug":"behavior-1234-4567","label":"","autoclose-time":"5","autoclose":"true","newtab":"sametab","thankyou-message":"Thank you for contacting us, we will be in touch shortly.","email-thankyou-message":"","manual-thankyou-message":"","submission-behaviour":"behaviour-thankyou","redirect-url":"https://myblank.tempurl.host/companies-attendee-list/"}],"notifications":[{"slug":"notification-1234-4567","label":"Admin Email","email-recipients":"default","recipients":"alfredogalanoloyola@gmail.com","email-subject":"New Form Entry #{submission_id} for {form_name}","email-editor":"You have a new website form submission: <br /> {all_fields} <br />---<br /> This message was sent from {site_url}.","email-attachment":"true","type":"default"}]},"status":"publish","version":"1.30.1"}
<?php
/**
* Plugin Name: [Forminator Pro] - Load data to Select Field and Delete
* Plugin URI: https://premium.wpmudev.org/
* Description: Load data to Select Field and Delete entries based in the selection
* Author: Alessandro Kaounas @ WPMUDEV, Paweł Pela @ WPMUDEV, Alfredo Loyola @WPMUDEV
* Author URI: https://premium.wpmudev.org/
* 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_Bulk_Select_Options' ) ) {
class WPMUDEV_Forminator_Bulk_Select_Options {
// User defined settings
//Set your Form ID here
private $form_id = "1049419";
//Indicate the select field that will receive the data
private $field_id = 'select-1';
// User defined options
private $options = array();
private $options_data = array();
private static $_instance = null;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_Forminator_Bulk_Select_Options();
}
return self::$_instance;
}
private function __construct() {
/*Customize your data to load here */
/*Stop customizing */
if ( ! defined( 'FORMINATOR_VERSION' ) || FORMINATOR_VERSION < '1.12' ) {
return;
}
$this->init();
}
public function init(){
add_filter( 'forminator_before_form_render', array( $this, 'wpmudev_forminator_before_form_render' ) );
add_filter('forminator_custom_form_submit_field_data', array($this, 'wpmudev_set_field_data'), 10, 2);
}
public function wpmudev_forminator_before_form_render( $id ){
if( $this->form_id != $id ){
return;
}
add_filter( 'forminator_field_markup', array( $this, 'wpmudev_forminator_field_markup' ), 10, 2 );
}
public function get_select_options(){
$this->options_data['options'] =[];
$this->options =[];
global $wpdb;
$table_meta = $wpdb->prefix . 'frmt_form_entry_meta';
$table_entry = $wpdb->prefix . 'frmt_form_entry';
$user_id = get_current_user_id();
$sql = $wpdb->prepare("SELECT `entry_id`, `meta_value` FROM $table_meta WHERE `entry_id` IN(SELECT m.entry_id FROM $table_meta as m LEFT JOIN $table_entry as e ON m.entry_id = e.entry_id WHERE e.form_id=1049414 AND `meta_key`='hidden-1' AND meta_value=$user_id) AND `meta_key`='select-1';");
$companies = $wpdb->get_results($sql);
foreach($companies as $company) {
$this->options[$company->entry_id] = $company->meta_value;
}
}
public function wpmudev_forminator_field_markup( $html, $field ){
if( $field['element_id'] === $this->field_id ){
$markup = '';
$this->get_select_options();
$new_options = array();
foreach( $this->options as $key => $option ){
//$markup .= '<option value="' . $key . '" data-calculation="0">' . $option .'</option>';
$new_options[] = array(
'label' => $option,
'value' => $key,
'limit' => '',
'key' => forminator_unique_key(),
);
}
$this->options_data['options'] = $new_options;
$select_field = Forminator_API::get_form_field( $this->form_id, $field['element_id'], true );
if( $select_field ){
Forminator_API::update_form_field( $this->form_id, $field['element_id'], $this->options_data );
}
return str_replace( '</select>', $markup . '</select>', $html );
}
return $html;
}
public function wpmudev_set_field_data( $field_data_array, $form_id){
if($form_id != $this->form_id){
return $field_data_array;
}
global $wpdb;
$table_meta = $wpdb->prefix . 'frmt_form_entry_meta';
$table_entry = $wpdb->prefix . 'frmt_form_entry';
foreach ($field_data_array as $key => $field){
if ($field['name'] === $this->field_id) {
$meta = $wpdb->delete( $table_meta, array( 'entry_id' => $field['value'] ) );
$entry = $wpdb->delete( $table_entry, array( 'entry_id' => $field['value'] ) );
if($meta > 0 || $entry > 0){
$this->get_select_options();
}
break;
}
}
return $field_data_array;
}
}
add_action( 'plugins_loaded', function(){
return WPMUDEV_Forminator_Bulk_Select_Options::get_instance();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment