Skip to content

Instantly share code, notes, and snippets.

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/ca6b44982fe3ac1d953ebbf860168458 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/ca6b44982fe3ac1d953ebbf860168458 to your computer and use it in GitHub Desktop.
[Forminator] Sends files URL to Google Sheets
<?php
/**
* Plugin Name: [Forminator] Sends files URL to Google Sheets
* Plugin URI: https://premium.wpmudev.org/
* Description: This plugin fixes a problem on the Forminator that avoid the files URL be sent to the Google Sheets - works with upload fields (even with multi-upload enabled) and also with the featured image of the postdata field.
* Author: Glauber Silva @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* Task: SLS-378
* License: GPLv2 or later
*
* @package WPMUDEV_Forminator_Sends_Files_URL_to_Google_Sheets
*/
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'WPMUDEV_Forminator_Sends_Files_URL_To_Google_Sheets' ) ) {
/**
* Main Class
*/
class WPMUDEV_Forminator_Sends_Files_URL_To_Google_Sheets {
/**
* Stores the main instance of the class.
*
* @var Property
*/
private static $instance = null;
/**
* Returns the main instance of the class.
*
* @return WPMUDEV_Forminator_Sends_Files_URL_To_Google_Sheets
*/
public static function get_instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new WPMUDEV_Forminator_Sends_Files_URL_To_Google_Sheets();
}
return self::$instance;
}
/**
* Constructor of the class.
*/
private function __construct() {
$this->init();
}
/**
* Loads the functions of the class in the apropriete hooks.
*/
public function init() {
add_filter( 'forminator_addon_googlesheet_form_submitted_data', array( $this, 'wpmudev_intercept_postdata_featured_image' ), 10, 2 );
add_filter( 'forminator_entry_meta_value_to_string', array( $this, 'wpmudev_intercept_upload_fields' ), 10, 5 );
}
/**
* Filter Google Sheets submitted form data to be processed
*
* @since 1.2
*
* @param array $submitted_data
* @param array $form_entry_fields
* @param int $form_id current Form ID
* @param Forminator_Addon_Googlesheet_Form_Settings $form_settings_instance Google Sheets Addon Form Settings instance
*/
public function wpmudev_intercept_postdata_featured_image( $submitted_data, $form_entry_fields ) {
foreach ( $form_entry_fields as $field ) {
if ( strpos( $field['name'], 'postdata' ) !== false ) {
$field_name = $field['name'] . '-post-image';
$file_url = $field['value']['value']['post-image']['uploaded_file'][0];
if ( isset( $file_url ) && ! empty( $file_url ) && is_array( $submitted_data[ $field_name ] ) ) {
$submitted_data[ $field_name ] = esc_url( $file_url );
}
}
}
return $submitted_data;
}
/**
* Filter string value of meta entry
*
* @since 1.7
*
* @param string $string_value
* @param string $field_type
* @param array $meta_value
* @param boolean $allow_html
* @param int $truncate
*
* @return string
*/
public function wpmudev_intercept_upload_fields( $string_value, $field_type, $meta_value, $allow_html, $truncate ) {
if ( empty( $string_value ) && 'upload' === $field_type && isset( $meta_value['file'] ) ) {
$file = $meta_value['file'];
if ( ! empty( $file ) && is_array( $file ) && isset( $file['file_url'] ) && ! empty( $file['file_url'] ) ) {
$file_urls = is_array( $file['file_url'] ) ? $file['file_url'] : array( $file['file_url'] );
$length = count( $file_urls );
foreach ( $file_urls as $key => $file_url ) {
$string_value .= esc_url( $file_url );
if ( $length > 1 && $key !== $length - 1 ) {
$string_value .= PHP_EOL;
}
}
}
}
return $string_value;
}
}
add_action(
'plugins_loaded',
function() {
return WPMUDEV_Forminator_Sends_Files_URL_To_Google_Sheets::get_instance();
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment