Last active
September 30, 2021 21:17
-
-
Save richardW8k/9304480 to your computer and use it in GitHub Desktop.
Adds a new settings to Gravity Forms notifications allowing uploaded files to be attached to the notification and the notification format to be changed to text.
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: Gravity Forms - Notification Extras | |
* Author: Richard Wawrzyniak | |
* Description: Adds new settings to Gravity Forms Notifications enabling file uploads to be attached to notifications and the notification format to be changed to text. | |
* Version: 1.0 | |
* | |
* Last Modified: 17/10/2014 | |
* Updated attach_file() to allow for possibility that uploads folder was changed | |
* Added suppot for post_image fields | |
* Updated and incorporated changes by blueliquiddesigns: https://gist.github.com/blueliquiddesigns/9609093 | |
*/ | |
class RWNotificationExtras { | |
function __construct() { | |
if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) | |
return; | |
add_filter( 'gform_tooltips', array( $this, 'add_extra_tooltips') ); | |
add_filter( 'gform_notification_ui_settings', array( $this, 'notification_extra_settings' ), 10, 3 ); | |
add_action( 'gform_pre_notification_save', array( $this, 'save_extra_settings' ), 10, 2 ); | |
add_filter( 'gform_notification', array( $this, 'notification_extras' ), 5, 3 ); | |
} | |
function add_extra_tooltips( $tooltips ) { | |
$tooltips['enable_attachments'] = "<h6>" . __( "Attach uploaded files", "gravityforms" ) . "</h6>" . __( "When enabled, files uploaded via the forms file upload fields will be attached to the notification.", "gravityforms" ); | |
$tooltips['textFormat'] = "<h6>" . __( "Text format notifications", "gravityforms" ) . "</h6>" . __( "When enabled, the notification format will be changed to text instead of the default of HTML.", "gravityforms" ); | |
return $tooltips; | |
} | |
function notification_extra_settings( $ui_settings, $notification, $form ) { | |
$enable_attachments_checked = ( rgar( $notification, 'enableAttachments' ) ) ? 'checked="checked"' : ""; | |
$ui_settings['enableAttachments'] = ' | |
<tr> | |
<th>' . __( "Attachments", "gravityforms" ) . '</th> | |
<td> | |
<input type="checkbox" id="enable_attachments" name="enable_attachments" value="1" ' . $enable_attachments_checked . ' /> | |
<label for="enable_attachments">' . __( "Attach uploaded files", "gravityforms" ) . ' ' . gform_tooltip( "enable_attachments", "", true ) . '</label> | |
</td> | |
</tr>'; | |
$text_format_checked = ( rgar( $notification, 'textFormat' ) ) ? 'checked="checked"' : ""; | |
$ui_settings['textFormat'] = ' | |
<tr> | |
<th>' . __( "Format", "gravityforms" ) . '</th> | |
<td> | |
<input type="checkbox" id="text_format" name="text_format" value="1" ' . $text_format_checked . ' /> | |
<label for="text_format">' . __( "Text Format Notifications", "gravityforms" ) . ' ' . gform_tooltip( "text_format", "", true ) . '</label> | |
</td> | |
</tr>'; | |
return $ui_settings; | |
} | |
function save_extra_settings( $notification, $form ) { | |
$notification['enableAttachments'] = rgpost( 'enable_attachments' ); | |
$notification['textFormat'] = rgpost( 'text_format' ); | |
return $notification; | |
} | |
function notification_extras( $notification, $form, $entry ) { | |
if ( rgar( $notification, 'textFormat') ) { | |
$notification['message_format'] = 'text'; | |
GFCommon::log_debug( 'RWNotificationExtras() - changing message_format to text.' ); | |
} | |
if ( rgar( $notification, 'enableAttachments') ) { | |
$fileupload_fields = GFCommon::get_fields_by_type( $form, array( 'fileupload', 'post_image' ) ); | |
if ( ! is_array( $fileupload_fields ) ) { | |
return $notification; | |
} | |
foreach( $fileupload_fields as $field ) { | |
$url = $entry[$field['id']]; | |
if ( empty( $url ) ) { | |
continue; | |
} elseif ( rgar( $field, 'multipleFiles' ) ) { | |
$uploaded_files = json_decode( $url, true ); | |
foreach ( $uploaded_files as $uploaded_file ) { | |
$notification['attachments'][] = self::attach_file( $uploaded_file ); | |
} | |
} else { | |
if ( $field['type'] == 'post_image' ) { | |
$img = explode( '|:|', $url ); | |
$url = str_replace( ' ', '%20', $img[0] ); | |
} | |
$notification['attachments'][] = self::attach_file( $url ); | |
} | |
} | |
} | |
return $notification; | |
} | |
function attach_file( $url ) { | |
$attachment = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $url ); | |
GFCommon::log_debug( 'RWNotificationExtras() - attaching the file: ' . print_r( $attachment, true ) ); | |
return $attachment; | |
} | |
} | |
new RWNotificationExtras(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment