Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkstylestudio/4689805 to your computer and use it in GitHub Desktop.
Save thinkstylestudio/4689805 to your computer and use it in GitHub Desktop.
<?php
/**
Plugin Name: Gravity Forms - Orange44 Customizations
Plugin URI:
Description:
Version: 0.1
Author: Think Style Studio
Author URI: http://thinkstylestudio.com
License: GPL2
------------------------------------------------------------------------
Copyright 2012 Think Style Studio
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/****************************************
* Global Variables
*****************************************/
/****************************************
* Includes
*****************************************/
include_once __DIR__ . '/packages/autoload.php';
if (!class_exists( 'RGForms' )) {
@include_once( WP_PLUGIN_DIR . '/gravityforms/gravityforms.php' ); // this is gravity forms main class
}
if (!class_exists( 'RGFormsModel' )) {
@include_once( WP_PLUGIN_DIR . '/gravityforms/forms_model.php' ); // this is for retreiving all forms
}
if (!class_exists( 'GFCommon' )) {
@include_once( WP_PLUGIN_DIR . '/gravityforms/common.php' );
}
if (!class_exists( 'GFExport' )) {
@include_once( WP_PLUGIN_DIR . '/gravityforms/export.php' ); // this exports entries
}
@include_once(
WP_PLUGIN_DIR . '/gravityforms/tooltips.php' ); // this isso the tool tips work outside of the gravity form plugin
if (!class_exists( 'GF_Orange44_Customizations' )) {
class GF_Orange44_Customizations
{
private static $orange44_options;
private static $slug_to_email = "orange44_settings_to_email";
private static $slug_export = "orange44_settings_export";
private static $version = "1.3";
private static $min_gravityforms_version = "1.3.9";
private static $plugin_id = "orange44";
function __construct()
{
add_action( 'admin_init', 'register_settings' );
//load plugin settings
if (!self::is_gravityforms_supported()) {
return;
}
add_action(
"gform_notification_email",
array( "GF_Orange44_Customizations", "change_notification_email" ),
10,
2
);
//set the priority so this is called after the menu had been built
if (is_admin()) {
self::$orange44_options = get_option( self::$plugin_id . '_settings' );
wp_enqueue_script( "sack" );
add_filter(
"gform_addon_navigation",
array( 'GF_Orange44_Customizations', 'create_export_page_menu' )
);
add_filter(
"gform_addon_navigation",
array( 'GF_Orange44_Customizations', 'create_to_email_page_menu' )
);
add_action( 'admin_menu', 'remove_admin_sub_menu' );
register_activation_hook(
__FILE__,
array( "GF_Orange44_Customizations", "set_roles_and_capabilities" )
);
}
}
public static function create_to_email_page_menu( $menus )
{
// Adding submenu if user has access
$permission = self::has_access( "orange44_email_to_overide" ); // TODO: Add Capability
if (!empty( $permission )) {
$menus[ ] = array(
"name" => "orange44_settings_email_to",
"label" => __( "Email To", "gravityformscustomized" ),
"callback" => array( "GF_Orange44_Customizations", "orange44_email_to_page" ),
"permission" => $permission
);
}
return $menus;
}
//Creates Orange44 left nav menu under Forms
public static function create_export_page_menu( $menus )
{
// Adding submenu if user has access
$permission = self::has_access( "orange44_export" ); // TODO: Add Capability
if (!empty( $permission )) {
$menus[ ] = array(
"name" => "orange44_settings_export",
"label" => __( "Export Entries", "gravityformscustomized" ),
"callback" => array( "GF_Orange44_Customizations", "export_page" ),
"permission" => $permission
);
}
return $menus;
}
public static function orange44_email_to_page()
{
$forms = RGFormsModel::get_forms( null, "title" );
$array_directory = "orange44_settings";
$array_label = "orange44_to_email";
if (isset( $_POST[ 'my_submit' ] )) {
if (is_array( $forms )) {
foreach ( $forms as $form ) {
$form_id = absint( $form->id );
$field_label = $array_label . "_" . $form_id;
$notification_emails = $_POST[ $field_label ];
if (empty( $notification_emails )) {
self::update_notification_form_meta(
$form_id,
$array_label,
$notification_emails,
$array_directory
);
} else if (self::is_valid_notification_email( $notification_emails )) {
$notification_emails = sanitize_text_field( $notification_emails );
self::update_notification_form_meta(
$form_id,
$array_label,
$notification_emails,
$array_directory
);
}
}
} else {
echo "error loading forms";
}
}
?>
<div class="wrap">
<h2>Orange44 Email Notification Settings</h2>
<?php if (isset( $_POST[ 'my_submit' ] )) : ?>
<div id="message" class="updated fade">
<p>
<?php _e( 'Settings Saved', 'my' ) ?>
</p>
</div>
<?php endif; ?>
<form method="post" action="">
<?php settings_fields( self::$slug_to_email );
foreach ( $forms as $form ) {
$form_id = absint( $form->id );
$field_label = "orange44_to_email_" . $form_id; ?>
<hr>
<h3 style="text-transform: capitalize; "><?php echo esc_html( $form->title ) . " Form" ?>
<!-- --><?php //echo " ID: " . $form_id ?>
</h3>
<?php do_settings_sections( self::$slug_to_email );
$form_meta_value = RGFormsModel::get_form_meta( $form_id );
$field_value = $form_meta_value[ $array_directory ][ $array_label ];
?>
<p>
<?php if ($form_meta_value[ "notification" ][ "to" ]) { ?>
Currently Notification is set to:
<strong><?php echo $form_meta_value[ "notification" ][ "to" ]; ?> </strong>
<?php } else { ?>
Currently Notification is not set.
<?php } ?>
</p>
<p>
<?php echo "<label for='$field_label'>Email To:</label>"; ?>
<?php echo "<input style='width:75%;' value='$field_value' name='$field_label' type='text'>"; ?>
</p>
<?php
}
?>
<p>
<input name="my_submit" type="submit" class="button-primary" value="<?php _e(
'Save Changes'
) ?>" />
</p>
</form>
</div><!-- /.wrap -->
<?php
}
private static function update_notification_form_meta(
$form_id,
$array_label,
$notification_emails,
$array_directory
) {
$form_meta = RGFormsModel::get_form_meta( $form_id );
$form_meta[ $array_directory ] = array( $array_label => $notification_emails );
//Add more orange44_form_meta[FIELDNAME] to form for more fields
RGFormsModel::update_form_meta( $form_id, $form_meta );
}
protected function export_page()
{
?>
<div class="wrap">
<h2>Orange44 Email Notification Settings</h2>
<?php self::export_lead_page(); ?>
</div><!-- /.wrap -->
<?php
}
//Returns true if the current page is an Feed pages. Returns false if not
private static function is_custom_export_entries_page()
{
$current_page = trim( strtolower( rgget( "page" ) ) );
$orange44_setting_pages = array( "orange44_settings_export", "orange44_settings_email_to" );
return in_array( $current_page, $orange44_setting_pages );
}
private static function is_gravityforms_supported()
{
if (class_exists( "GFCommon" )) {
$is_correct_version = version_compare( GFCommon::$version, self::$min_gravityforms_version, ">=" );
return $is_correct_version;
} else {
return false;
}
}
protected static function has_access( $required_permission )
{
$has_members_plugin = function_exists( 'members_get_capabilities' );
// TODO: Check what this is spitting out.
$has_access = $has_members_plugin
? current_user_can( $required_permission )
: current_user_can(
"level_7"
);
if ($has_access) {
return $has_members_plugin ? $required_permission
: "level_7"; // if members plugin exists send required permission otherwise send level_7
} else {
return false;
}
}
protected static function register_settings()
{
register_setting( 'orange44_settings_group', self::$slug_export );
register_setting( 'orange44_settings_group', self::$slug_to_email );
}
function change_notification_email( $email, $entry )
{
$form = RGFormsModel::get_form_meta( $entry[ 'form_id' ] );
if (!empty( $form[ 'orange44_settings' ][ 'orange44_to_email' ] )) {
$email = $form[ 'orange44_settings' ][ 'orange44_to_email' ];
}
return $email;
}
public static function export_lead_page()
{
?>
<script type='text/javascript' src='<?php echo GFCommon::get_base_url(
)?>/js/jquery-ui/ui.datepicker.js?ver=<?php echo GFCommon::$version ?>'></script>
<script type="text/javascript">
function SelectExportForm(formId) {
if (!formId)
return;
var mysack = new sack("<?php echo admin_url( "admin-ajax.php" )?>");
mysack.execute = 1;
mysack.method = 'POST';
mysack.setVar("action", "rg_select_export_form");
mysack.setVar("rg_select_export_form", "<?php echo wp_create_nonce( "rg_select_export_form" ) ?>");
mysack.setVar("form_id", formId);
mysack.onError = function () {
alert('<?php echo esc_js( __( "Ajax error while selecting a form", "gravityforms" ) ) ?>')
};
mysack.runAJAX();
return true;
}
function EndSelectExportForm(aryFields) {
if (aryFields.length == 0) {
jQuery("#export_field_container, #export_date_container, #export_submit_container").hide()
return;
}
var fieldList = "<li><input type='checkbox' onclick=\"jQuery('.gform_export_field').attr('checked', this.checked); jQuery('#gform_export_check_all').html(this.checked ? '<strong><?php _e(
"Deselect All",
"gravityforms"
) ?></strong>' : '<strong><?php _e(
"Select All",
"gravityforms"
) ?></strong>'); \"> <label id='gform_export_check_all'><strong><?php _e(
"Select All",
"gravityforms"
) ?></strong></label></li>";
for (var i = 0; i < aryFields.length; i++) {
fieldList += "<li><input type='checkbox' id='export_field_" + i + "' name='export_field[]' value='" + aryFields[i][0] + "' class='gform_export_field'> <label for='export_field_" + i + "'>" + aryFields[i][1] + "</label></li>";
}
jQuery("#export_field_list").html(fieldList);
jQuery("#export_date_start, #export_date_end").datepicker({dateFormat:'yy-mm-dd'});
jQuery("#export_field_container, #export_date_container, #export_submit_container").hide().show();
}
</script>
<link rel="stylesheet" href="<?php echo GFCommon::get_base_url()?>/css/admin.css" />
<div class="wrap">
<div class="icon32" id="gravity-export-icon"><br></div>
<p class="textleft"><?php _e(
"Select a form below to export entries. Once you have selected a form you may select the fields you would like to export and an optional date range. When you click the download button below, It will create a CSV file for you to save to your computer.",
"gravityforms"
); ?></p>
<div class="hr-divider"></div>
<form method="post" style="margin-top:10px;">
<?php echo wp_nonce_field( "rg_start_export", "rg_start_export_nonce" ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="export_form"><?php _e(
"Select A Form",
"gravityforms"
); ?></label> <?php gform_tooltip( "export_select_form" ) ?></th>
<td>
<select id="export_form" name="export_form" onchange="SelectExportForm(jQuery(this).val());">
<option value=""><?php _e( "Select a form", "gravityforms" ); ?></option>
<?php
$forms = RGFormsModel::get_forms( null, "title" );
foreach ( $forms as $form ) {
?>
<option value="<?php echo absint( $form->id ) ?>"><?php echo esc_html(
$form->title
) ?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr id="export_field_container" valign="top" style="display: none;">
<th scope="row"><label for="export_fields"><?php _e(
"Select Fields",
"gravityforms"
); ?></label> <?php gform_tooltip( "export_select_fields" ) ?></th>
<td>
<ul id="export_field_list">
</ul>
</td>
</tr>
<tr id="export_date_container" valign="top" style="display: none;">
<th scope="row"><label for="export_date"><?php _e(
"Select Date Range",
"gravityforms"
); ?></label> <?php gform_tooltip( "export_date_range" ) ?></th>
<td>
<div>
<span style="width:150px; float:left; ">
<input type="text" id="export_date_start" name="export_date_start" style="width:90%" />
<strong><label for="export_date_start" style="display:block;"><?php _e(
"Start",
"gravityforms"
); ?></label></strong>
</span>
<span style="width:150px; float:left;">
<input type="text" id="export_date_end" name="export_date_end" style="width:90%" />
<strong><label for="export_date_end" style="display:block;"><?php _e(
"End",
"gravityforms"
); ?></label></strong>
</span>
<div style="clear: both;"></div>
<?php _e(
"Date Range is optional, if no date range is selected all entries will be exported.",
"gravityforms"
); ?>
</div>
</td>
</tr>
</table>
<ul>
<li id="export_submit_container" style="display:none; clear:both;">
<br /><br />
<input type="submit" name="export_lead" value="<?php _e(
"Download Export File",
"gravityforms"
); ?>" class="button-primary" />
<span id="please_wait_container" style="display:none; margin-left:15px;">
<img src="<?php echo GFCommon::get_base_url()?>/images/loading.gif"> <?php _e(
"Exporting entries. Please wait...",
"gravityforms"
); ?>
</span>
<iframe id="export_frame" width="1" height="1" src="about:blank"></iframe>
</li>
</ul>
</form>
</div>
<?php
}
private static function is_valid_notification_email( $text )
{
if (empty( $text )) {
return false;
}
$emails = explode( ",", $text );
foreach ( $emails as $email ) {
$email = trim( $email );
$invalid_email = GFCommon::is_invalid_or_empty_email( $email );
$invalid_variable = !preg_match( '/^({[^{]*?:(\d+(\.\d+)?)(:(.*?))?},? *)+$/', $email );
if ($invalid_email && $invalid_variable) {
return false;
}
}
return true;
}
public static function set_roles_and_capabilities()
{
$roles = new WP_Roles();
$roles->add_cap( 'clientexport', 'orange44_export' );
$roles->add_cap( 'clientexport', 'orange44_email_to_overide' );
$roles->add_cap( 'clientexport', 'gravityforms_view_entries' );
$roles->add_cap( 'clientexport', 'read' );
}
function remove_admin_sub_menu()
{
//the first parameter is the parent name of the first item in the Forms menu
remove_submenu_page( "gf_edit_forms", "gf_help" );
}
}
}
$GFOrange44Custom = new GF_Orange44_Customizations;
if (!class_exists( 'OF' )) {
class OF
{
public static function vd( $var_dump )
{
echo "<pre>";
print_r( $var_dump );
echo "</pre>";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment