Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active July 19, 2023 22:04
Show Gist options
  • Save wpmudev-sls/f96179bab8501ee50801f5cce0da9ed0 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/f96179bab8501ee50801f5cce0da9ed0 to your computer and use it in GitHub Desktop.
Removes header row values from the google sheet
<?php
/**
* Plugin Name: [Forminator] - Remove field IDs from header row values
* Plugin URI: https://premium.wpmudev.org/
* Description: Removes field IDs from header row values of googlesheet
* Task: SLS-4106
* Author: Prashant Singh @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( defined( 'WP_CLI' ) && WP_CLI ) {
return;
}
if ( ! class_exists( 'WPMUDEV_Forminator_Remove_Gsheet_Rows' ) ) {
class WPMUDEV_Forminator_Remove_Gsheet_Rows {
private static $_instance = null;
public static function get_instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new WPMUDEV_Forminator_Remove_Gsheet_Rows();
}
return self::$_instance;
}
private function __construct() {
if ( ! class_exists( 'Forminator_Form_Model' ) ) {
return;
}
add_action( 'forminator_addon_googlesheet_after_prepare_sheet_headers', array( $this, 'wpmudev_remove_field_labels_googlesheets' ), 10, 6 );
add_action( 'forminator_addon_googlesheet_before_prepare_sheet_headers', array( $this, 'wpmudev_add_field_labels_googlesheets' ), 10, 5 );
}
public function wpmudev_add_field_labels_googlesheets( $connection_settings, $form_id, $submitted_data, $form_entry_fields, $form_settings_instance ) {
$form_ids = array( 90, 130, 245 ); //Please use your form IDs here
if ( ! in_array( $form_id, $form_ids ) ) {
return;
}
$file_id = $connection_settings['file_id'];
$cls = new Forminator_Addon_Googlesheet();
$google_client = $cls->get_google_client();
$spreadsheet_service = new Forminator_Google_Service_Sheets( $google_client );
$google_client->setAccessToken( $cls->get_client_access_token() );
$form_fields = $form_settings_instance->get_form_fields();
$update_bodies = array();
$sheet_values = array();
foreach ( $form_fields as $form_field ) {
$sheet_values[] = '|' . $form_field['element_id'];
}
foreach ( $form_fields as $form_field ) {
$update_body = new Forminator_Google_Service_Sheets_ValueRange();
$update_body->setRange( '1:1' );
$update_body->setValues( array( $sheet_values ) );
$update_bodies[] = $update_body;
}
if ( ! empty( $update_bodies ) ) {
$request_body = new Forminator_Google_Service_Sheets_BatchUpdateValuesRequest();
$request_body->setData( $update_bodies );
$request_body->setValueInputOption( 'RAW' );
$spreadsheet_service->spreadsheets_values->batchUpdate( $file_id, $request_body );
}
}
public function wpmudev_remove_field_labels_googlesheets( $header_fields, $connection_settings, $form_id, $submitted_data, $form_entry_fields, $form_settings_instance ) {
$form_ids = array( 90, 130, 245 ); //Please use your form IDs here
if ( ! in_array( $form_id, $form_ids ) ) {
return;
}
$file_id = $connection_settings['file_id'];
$cls = new Forminator_Addon_Googlesheet();
$google_client = $cls->get_google_client();
$spreadsheet_service = new Forminator_Google_Service_Sheets( $google_client );
$google_client->setAccessToken( $cls->get_client_access_token() );
$update_bodies = array();
$sheet_values = array();
$custom_form_model = Forminator_Form_Model::model()->load( $form_id );
$form_fields = array();
$fields = $custom_form_model->get_real_fields();
$allowed_field_types = forminator_get_allowed_field_types_for_addon();
foreach ( $fields as $field ) {
$field_as_array = $field->to_formatted_array();
// check non label fields.
if ( empty( $field_as_array['field_label'] ) ) {
$field_as_array['field_label'] = $field_as_array['type'];
}
// handle multiple.
$multi_fields = forminator_addon_flatten_mutiple_field( $field_as_array );
if ( false === $multi_fields ) {
if ( ! in_array( $field_as_array['type'], $allowed_field_types, true ) ) {
continue;
}
$field_as_array['field_type'] = $field_as_array['type'];
$form_fields[] = $field_as_array;
} else {
foreach ( $multi_fields as $multi_field ) {
if ( ! in_array( $multi_field['type'], $allowed_field_types, true ) ) {
continue;
}
$multi_field['field_type'] = $field_as_array['type'];
$form_fields[] = $multi_field;
}
}
}
foreach ( $form_fields as $form_field ) {
$sheet_values[] = $form_field['field_label'];
}
foreach ( $form_fields as $form_field ) {
$update_body = new Forminator_Google_Service_Sheets_ValueRange();
$update_body->setRange( '1:1' );
$update_body->setValues( array( $sheet_values ) );
$update_bodies[] = $update_body;
}
if ( ! empty( $update_bodies ) ) {
$request_body = new Forminator_Google_Service_Sheets_BatchUpdateValuesRequest();
$request_body->setData( $update_bodies );
$request_body->setValueInputOption( 'RAW' );
$spreadsheet_service->spreadsheets_values->batchUpdate( $file_id, $request_body );
}
}
}
if ( ! function_exists( 'wpmudev_forminator_remove_gsheet_rows' ) ) {
function wpmudev_forminator_remove_gsheet_rows() {
return WPMUDEV_Forminator_Remove_Gsheet_Rows::get_instance();
};
add_action( 'plugins_loaded', 'wpmudev_forminator_remove_gsheet_rows', 99 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment