/wp-content/plugins/gravityforms/common.php
PHP | 5422 lines | 3928 code | 1070 blank | 424 comment | 838 complexity | d1de7789de1cdea7028e24d86ce5bc5a MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-2.0
Large files files are truncated, but you can click here to view the full file
- <?php
- class GFCommon{
- public static $version = "1.7.9";
- public static $tab_index = 1;
- public static $errors = array();
- private static $messages = array();
- public static function get_selection_fields($form, $selected_field_id){
- $str = "";
- foreach($form["fields"] as $field){
- $input_type = RGFormsModel::get_input_type($field);
- $field_label = RGFormsModel::get_label($field);
- if($input_type == "checkbox" || $input_type == "radio" || $input_type == "select"){
- $selected = $field["id"] == $selected_field_id ? "selected='selected'" : "";
- $str .= "<option value='" . $field["id"] . "' " . $selected . ">" . $field_label . "</option>";
- }
- }
- return $str;
- }
- public static function is_numeric($value, $number_format=""){
- switch($number_format){
- case "decimal_dot" :
- return preg_match("/^(-?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]+)?)$/", $value);
- break;
- case "decimal_comma" :
- return preg_match("/^(-?[0-9]{1,3}(?:\.?[0-9]{3})*(?:,[0-9]+)?)$/", $value);
- break;
- default :
- return preg_match("/^(-?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?)$/", $value) || preg_match("/^(-?[0-9]{1,3}(?:\.?[0-9]{3})*(?:,[0-9]{2})?)$/", $value);
- }
- }
- public static function trim_all($text){
- $text = trim($text);
- do{
- $prev_text = $text;
- $text = str_replace(" ", " ", $text);
- }
- while($text != $prev_text);
- return $text;
- }
- public static function format_number($number, $number_format){
- if(!is_numeric($number))
- return $number;
- //replacing commas with dots and dots with commas
- if($number_format == "decimal_comma"){
- $number = str_replace(",", "|", $number);
- $number = str_replace(".", ",", $number);
- $number = str_replace("|", ".", $number);
- }
- return $number;
- }
- public static function recursive_add_index_file($dir) {
- if(!is_dir($dir))
- return;
- if(!($dp = opendir($dir)))
- return;
- //ignores all errors
- set_error_handler(create_function("", "return 0;"), E_ALL);
- //creates an empty index.html file
- if($f = fopen($dir . "/index.html", 'w'))
- fclose($f);
- //restores error handler
- restore_error_handler();
- while((false !== $file = readdir($dp))){
- if(is_dir("$dir/$file") && $file != '.' && $file !='..')
- self::recursive_add_index_file("$dir/$file");
- }
- closedir($dp);
- }
- public static function clean_number($number, $number_format=""){
- if(rgblank($number))
- return $number;
- $decimal_char = "";
- if($number_format == "decimal_dot")
- $decimal_char = ".";
- else if($number_format == "decimal_comma")
- $decimal_char = ",";
- $float_number = "";
- $clean_number = "";
- $is_negative = false;
- //Removing all non-numeric characters
- $array = str_split($number);
- foreach($array as $char){
- if (($char >= '0' && $char <= '9') || $char=="," || $char==".")
- $clean_number .= $char;
- else if($char == '-')
- $is_negative = true;
- }
- //Removing thousand separators but keeping decimal point
- $array = str_split($clean_number);
- for($i=0, $count = sizeof($array); $i<$count; $i++)
- {
- $char = $array[$i];
- if ($char >= '0' && $char <= '9')
- $float_number .= $char;
- else if(empty($decimal_char) && ($char == "." || $char == ",") && strlen($clean_number) - $i <= 3)
- $float_number .= ".";
- else if($decimal_char == $char)
- $float_number .= ".";
- }
- if($is_negative)
- $float_number = "-" . $float_number;
- return $float_number;
- }
- public static function json_encode($value){
- return json_encode($value);
- }
- public static function json_decode($str, $is_assoc=true){
- return json_decode($str, $is_assoc);
- }
- //Returns the url of the plugin's root folder
- public static function get_base_url(){
- $folder = basename(dirname(__FILE__));
- return plugins_url($folder);
- }
- //Returns the physical path of the plugin's root folder
- public static function get_base_path(){
- $folder = basename(dirname(__FILE__));
- return WP_PLUGIN_DIR . "/" . $folder;
- }
- public static function get_email_fields($form){
- $fields = array();
- foreach($form["fields"] as $field){
- if(RGForms::get("type", $field) == "email" || RGForms::get("inputType", $field) == "email")
- $fields[] = $field;
- }
- return $fields;
- }
- public static function truncate_middle($text, $max_length){
- if(strlen($text) <= $max_length)
- return $text;
- $middle = intval($max_length / 2);
- return substr($text, 0, $middle) . "..." . substr($text, strlen($text) - $middle, $middle);
- }
- public static function is_invalid_or_empty_email($email){
- return empty($email) || !self::is_valid_email($email);
- }
- public static function is_valid_url($url){
- return preg_match('!^(http|https)://([\w-]+\.?)+[\w-]+(:\d+)?(/[\w- ./?~%&=+\']*)?$!', $url);
- }
- public static function is_valid_email($email){
- return preg_match('/^(([a-zA-Z0-9_.\-+!#$&\'*+=?^`{|}~])+\@((([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+|localhost) *,? *)+$/', $email);
- }
- public static function get_label($field, $input_id = 0, $input_only = false){
- return RGFormsModel::get_label($field, $input_id, $input_only);
- }
- public static function get_input($field, $id){
- return RGFormsModel::get_input($field, $id);
- }
- public static function insert_variables($fields, $element_id, $hide_all_fields=false, $callback="", $onchange="", $max_label_size=40, $exclude = null, $args="", $class_name=""){
- if($fields == null)
- $fields = array();
- if($exclude == null)
- $exclude = array();
- $exclude = apply_filters("gform_merge_tag_list_exclude", $exclude, $element_id, $fields);
- $merge_tags = self::get_merge_tags($fields, $element_id, $hide_all_fields, $exclude, $args);
- $onchange = empty($onchange) ? "InsertVariable('{$element_id}', '{$callback}');" : $onchange;
- $class = trim($class_name . " gform_merge_tags");
- ?>
- <select id="<?php echo $element_id?>_variable_select" onchange="<?php echo $onchange ?>" class="<?php echo esc_attr($class)?>">
- <option value=''><?php _e("Insert Merge Tag", "gravityforms"); ?></option>
- <?php foreach($merge_tags as $group => $group_tags) {
- $group_label = rgar($group_tags, 'label');
- $tags = rgar($group_tags, 'tags');
- if(empty($group_tags['tags']))
- continue;
- if($group_label) { ?>
- <optgroup label="<?php echo $group_label; ?>">
- <?php } ?>
- <?php foreach($tags as $tag) { ?>
- <option value="<?php echo $tag['tag']; ?>"><?php echo $tag['label']; ?></option>
- <?php }
- if($group_label) { ?>
- </optgroup>
- <?php }
- } ?>
- </select>
- <?php
- }
- public static function OLD_insert_variables($fields, $element_id, $hide_all_fields=false, $callback="", $onchange="", $max_label_size=40, $exclude = null, $args="", $class_name=""){
- if($fields == null)
- $fields = array();
- if($exclude == null)
- $exclude = array();
- $exclude = apply_filters("gform_merge_tag_list_exclude", $exclude, $element_id, $fields);
- $onchange = empty($onchange) ? "InsertVariable('{$element_id}', '{$callback}');" : $onchange;
- $class = trim($class_name . " gform_merge_tags");
- ?>
- <select id="<?php echo $element_id?>_variable_select" onchange="<?php echo $onchange ?>" class="<?php echo esc_attr($class)?>">
- <option value=''><?php _e("Insert Merge Tag", "gravityforms"); ?></option>
- <?php
- if(!$hide_all_fields){
- ?>
- <option value='{all_fields}'><?php _e("All Submitted Fields", "gravityforms"); ?></option>
- <?php
- }
- $required_fields = array();
- $optional_fields = array();
- $pricing_fields = array();
- foreach($fields as $field){
- if(rgget("displayOnly", $field))
- continue;
- $input_type = RGFormsModel::get_input_type($field);
- //skip field types that should be excluded
- if(is_array($exclude) && in_array($input_type, $exclude))
- continue;
- if($field["isRequired"]){
- switch($input_type){
- case "name" :
- if($field["nameFormat"] == "extended"){
- $prefix = GFCommon::get_input($field, $field["id"] + 0.2);
- $suffix = GFCommon::get_input($field, $field["id"] + 0.8);
- $optional_field = $field;
- $optional_field["inputs"] = array($prefix, $suffix);
- //Add optional name fields to the optional list
- $optional_fields[] = $optional_field;
- //Remove optional name field from required list
- unset($field["inputs"][0]);
- unset($field["inputs"][3]);
- }
- $required_fields[] = $field;
- break;
- default:
- $required_fields[] = $field;
- }
- }
- else{
- $optional_fields[] = $field;
- }
- if(self::is_pricing_field($field["type"])){
- $pricing_fields[] = $field;
- }
- }
- if(!empty($required_fields)){
- ?>
- <optgroup label="<?php _e("Required form fields", "gravityforms"); ?>">
- <?php
- foreach($required_fields as $field){
- self::insert_field_variable($field, $max_label_size, $args);
- }
- ?>
- </optgroup>
- <?php
- }
- if(!empty($optional_fields)){
- ?>
- <optgroup label="<?php _e("Optional form fields", "gravityforms"); ?>">
- <?php
- foreach($optional_fields as $field){
- self::insert_field_variable($field, $max_label_size, $args);
- }
- ?>
- </optgroup>
- <?php
- }
- if(!empty($pricing_fields)){
- ?>
- <optgroup label="<?php _e("Pricing form fields", "gravityforms"); ?>">
- <?php
- if(!$hide_all_fields){
- ?>
- <option value='{pricing_fields}'><?php _e("All Pricing Fields", "gravityforms"); ?></option>
- <?php
- }?>
- <?php
- foreach($pricing_fields as $field){
- self::insert_field_variable($field, $max_label_size, $args);
- }
- ?>
- </optgroup>
- <?php
- }
- ?>
- <optgroup label="<?php _e("Other", "gravityforms"); ?>">
- <option value='{ip}'><?php _e("Client IP Address", "gravityforms"); ?></option>
- <option value='{date_mdy}'><?php _e("Date", "gravityforms"); ?> (mm/dd/yyyy)</option>
- <option value='{date_dmy}'><?php _e("Date", "gravityforms"); ?> (dd/mm/yyyy)</option>
- <option value='{embed_post:ID}'><?php _e("Embed Post/Page Id", "gravityforms"); ?></option>
- <option value='{embed_post:post_title}'><?php _e("Embed Post/Page Title", "gravityforms"); ?></option>
- <option value='{embed_url}'><?php _e("Embed URL", "gravityforms"); ?></option>
- <option value='{entry_id}'><?php _e("Entry Id", "gravityforms"); ?></option>
- <option value='{entry_url}'><?php _e("Entry URL", "gravityforms"); ?></option>
- <option value='{form_id}'><?php _e("Form Id", "gravityforms"); ?></option>
- <option value='{form_title}'><?php _e("Form Title", "gravityforms"); ?></option>
- <option value='{referer}'><?php _e("HTTP Referer URL", "gravityforms"); ?></option>
- <option value='{user_agent}'><?php _e("HTTP User Agent", "gravityforms"); ?></option>
- <?php if(self::has_post_field($fields)){ ?>
- <option value='{post_id}'><?php _e("Post Id", "gravityforms"); ?></option>
- <option value='{post_edit_url}'><?php _e("Post Edit URL", "gravityforms"); ?></option>
- <?php } ?>
- <option value='{user:display_name}'><?php _e("User Display Name", "gravityforms"); ?></option>
- <option value='{user:user_email}'><?php _e("User Email", "gravityforms"); ?></option>
- <option value='{user:user_login}'><?php _e("User Login", "gravityforms"); ?></option>
- </optgroup>
- <?php
- $custom_merge_tags = apply_filters('gform_custom_merge_tags', array(), rgars($fields, '0/formId'), $fields, $element_id);
- if(is_array($custom_merge_tags) && !empty($custom_merge_tags)) { ?>
- <optgroup label="<?php _e("Custom", "gravityforms"); ?>">
- <?php foreach($custom_merge_tags as $custom_merge_tag) { ?>
- <option value='<?php echo rgar($custom_merge_tag, 'tag'); ?>'><?php echo rgar($custom_merge_tag, 'label'); ?></option>
- <?php } ?>
- </optgroup>
- <?php } ?>
- </select>
- <?php
- }
- /**
- * This function is used by the gfMergeTags JS object to get the localized label for non-field merge tags as well as
- * for backwards compatability with the gform_custom_merge_tags hook. Lastly, this plugin is used by the soon-to-be
- * deprecrated insert_variables() function as the new gfMergeTags object has not yet been applied to the Post Content
- * Template setting.
- *
- * @param $fields
- * @param $element_id
- * @param bool $hide_all_fields
- * @param array $exclude_field_types
- * @param string $option
- * @return array
- */
- public static function get_merge_tags($fields, $element_id, $hide_all_fields = false, $exclude_field_types = array(), $option = '') {
- if($fields == null)
- $fields = array();
- if($exclude_field_types == null)
- $exclude_field_types = array();
- $required_fields = $optional_fields = $pricing_fields = array();
- $ungrouped = $required_group = $optional_group = $pricing_group = $other_group = array();
- if(!$hide_all_fields)
- $ungrouped[] = array('tag' => '{all_fields}', 'label' => __('All Submitted Fields', 'gravityforms'));
- // group fields by required, optional, and pricing
- foreach($fields as $field) {
- if(rgget("displayOnly", $field))
- continue;
- $input_type = RGFormsModel::get_input_type($field);
- // skip field types that should be excluded
- if(is_array($exclude_field_types) && in_array($input_type, $exclude_field_types))
- continue;
- if(rgar($field, 'isRequired')) {
- switch($input_type) {
- case "name" :
- if(rgar($field,"nameFormat") == "extended") {
- $prefix = GFCommon::get_input($field, $field["id"] + 0.2);
- $suffix = GFCommon::get_input($field, $field["id"] + 0.8);
- $optional_field = $field;
- $optional_field["inputs"] = array($prefix, $suffix);
- //Add optional name fields to the optional list
- $optional_fields[] = $optional_field;
- //Remove optional name field from required list
- unset($field["inputs"][0]);
- unset($field["inputs"][3]);
- }
- $required_fields[] = $field;
- break;
- default:
- $required_fields[] = $field;
- }
- }
- else {
- $optional_fields[] = $field;
- }
- if(self::is_pricing_field($field["type"])){
- $pricing_fields[] = $field;
- }
- }
- if(!empty($required_fields)){
- foreach($required_fields as $field){
- $required_group = array_merge($required_group, self::get_field_merge_tags($field, $option));
- }
- }
- if(!empty($optional_fields)){
- foreach($optional_fields as $field){
- $optional_group = array_merge($optional_group, self::get_field_merge_tags($field, $option));
- }
- }
- if(!empty($pricing_fields)){
- if(!$hide_all_fields)
- $pricing_group[] = array('tag' => '{pricing_fields}', 'label' => __("All Pricing Fields", "gravityforms"));
- foreach($pricing_fields as $field){
- $pricing_group = array_merge($pricing_group, self::get_field_merge_tags($field, $option));
- }
- }
- $other_group[] = array('tag' => '{ip}', 'label' => __("Client IP Address", "gravityforms"));
- $other_group[] = array('tag' => '{date_mdy}', 'label' => __("Date", "gravityforms") . ' (mm/dd/yyyy)');
- $other_group[] = array('tag' => '{date_dmy}', 'label' => __("Date", "gravityforms") . ' (dd/mm/yyyy)');
- $other_group[] = array('tag' => '{embed_post:ID}', 'label' => __("Embed Post/Page Id", "gravityforms"));
- $other_group[] = array('tag' => '{embed_post:post_title}', 'label' => __("Embed Post/Page Title", "gravityforms"));
- $other_group[] = array('tag' => '{embed_url}', 'label' => __("Embed URL", "gravityforms"));
- $other_group[] = array('tag' => '{entry_id}', 'label' => __("Entry Id", "gravityforms"));
- $other_group[] = array('tag' => '{entry_url}', 'label' => __("Entry URL", "gravityforms"));
- $other_group[] = array('tag' => '{form_id}', 'label' => __("Form Id", "gravityforms"));
- $other_group[] = array('tag' => '{form_title}', 'label' => __("Form Title", "gravityforms"));
- $other_group[] = array('tag' => '{user_agent}', 'label' => __("HTTP User Agent", "gravityforms"));
- $other_group[] = array('tag' => '{referer}', 'label' => __("HTTP Referer URL", "gravityforms"));
- if(self::has_post_field($fields)) {
- $other_group[] = array('tag' => '{post_id}', 'label' => __("Post Id", "gravityforms"));
- $other_group[] = array('tag' => '{post_edit_url}', 'label' => __("Post Edit URL", "gravityforms"));
- }
- $other_group[] = array('tag' => '{user:display_name}', 'label' => __("User Display Name", "gravityforms"));
- $other_group[] = array('tag' => '{user:user_email}', 'label' => __("User Email", "gravityforms"));
- $other_group[] = array('tag' => '{user:user_login}', 'label' => __("User Login", "gravityforms"));
- $custom_group = apply_filters('gform_custom_merge_tags', array(), rgars($fields, '0/formId'), $fields, $element_id);
- $merge_tags = array(
- 'ungrouped' => array(
- 'label' => false,
- 'tags' => $ungrouped
- ),
- 'required' => array(
- 'label' => __("Required form fields", "gravityforms"),
- 'tags' => $required_group
- ),
- 'optional' => array(
- 'label' => __("Optional form fields", "gravityforms"),
- 'tags' => $optional_group
- ),
- 'pricing' => array(
- 'label' => __("Pricing form fields", "gravityforms"),
- 'tags' => $pricing_group
- ),
- 'other' => array(
- 'label' => __("Other", "gravityforms"),
- 'tags' => $other_group
- ),
- 'custom' => array(
- 'label' => __("Custom", "gravityforms"),
- 'tags' => $custom_group
- )
- );
- return $merge_tags;
- }
- public static function get_field_merge_tags($field, $option="") {
- $merge_tags = array();
- $tag_args = RGFormsModel::get_input_type($field) == "list" ? ":{$option}" : ""; //args currently only supported by list field
- if(isset($field["inputs"]) && is_array($field["inputs"])) {
- if(RGFormsModel::get_input_type($field) == "checkbox") {
- $value = "{" . esc_html(GFCommon::get_label($field, $field["id"])) . ":" . $field["id"] . "{$tag_args}}";
- $merge_tags[] = array( 'tag' => $value, 'label' => esc_html(GFCommon::get_label($field, $field["id"])) );
- }
- foreach($field["inputs"] as $input) {
- $value = "{" . esc_html(GFCommon::get_label($field, $input["id"])) . ":" . $input["id"] . "{$tag_args}}";
- $merge_tags[] = array( 'tag' => $value, 'label' => esc_html(GFCommon::get_label($field, $input["id"])) );
- }
- }
- else {
- $value = "{" . esc_html(GFCommon::get_label($field)) . ":" . $field["id"] . "{$tag_args}}";
- $merge_tags[] = array( 'tag' => $value, 'label' => esc_html(GFCommon::get_label($field)) );
- }
- return $merge_tags;
- }
- public static function insert_field_variable($field, $max_label_size=40, $args=""){
- $tag_args = RGFormsModel::get_input_type($field) == "list" ? ":{$args}" : ""; //args currently only supported by list field
- if(is_array($field["inputs"]))
- {
- if(RGFormsModel::get_input_type($field) == "checkbox"){
- ?>
- <option value='<?php echo "{" . esc_html(GFCommon::get_label($field, $field["id"])) . ":" . $field["id"] . "{$tag_args}}" ?>'><?php echo esc_html(GFCommon::get_label($field, $field["id"])) ?></option>
- <?php
- }
- foreach($field["inputs"] as $input){
- ?>
- <option value='<?php echo "{" . esc_html(GFCommon::get_label($field, $input["id"])) . ":" . $input["id"] . "{$tag_args}}" ?>'><?php echo esc_html(GFCommon::get_label($field, $input["id"])) ?></option>
- <?php
- }
- }
- else{
- ?>
- <option value='<?php echo "{" . esc_html(GFCommon::get_label($field)) . ":" . $field["id"] . "{$tag_args}}" ?>'><?php echo esc_html(GFCommon::get_label($field)) ?></option>
- <?php
- }
- }
- public static function insert_post_content_variables($fields, $element_id, $callback, $max_label_size=25){
- // TODO: replace with class-powered merge tags
- self::insert_variables($fields, $element_id, true, "", "InsertPostContentVariable('{$element_id}', '{$callback}');", $max_label_size, null, "", "gform_content_template_merge_tags");
- ?>
-
- <select id="<?php echo $element_id?>_image_size_select" onchange="InsertPostImageVariable('<?php echo $element_id ?>', '<?php echo $element_id ?>'); SetCustomFieldTemplate();" style="display:none;">
- <option value=""><?php _e("Select image size", "gravityforms") ?></option>
- <option value="thumbnail"><?php _e("Thumbnail", "gravityforms") ?></option>
- <option value="thumbnail:left"><?php _e("Thumbnail - Left Aligned", "gravityforms") ?></option>
- <option value="thumbnail:center"><?php _e("Thumbnail - Centered", "gravityforms") ?></option>
- <option value="thumbnail:right"><?php _e("Thumbnail - Right Aligned", "gravityforms") ?></option>
- <option value="medium"><?php _e("Medium", "gravityforms") ?></option>
- <option value="medium:left"><?php _e("Medium - Left Aligned", "gravityforms") ?></option>
- <option value="medium:center"><?php _e("Medium - Centered", "gravityforms") ?></option>
- <option value="medium:right"><?php _e("Medium - Right Aligned", "gravityforms") ?></option>
- <option value="large"><?php _e("Large", "gravityforms") ?></option>
- <option value="large:left"><?php _e("Large - Left Aligned", "gravityforms") ?></option>
- <option value="large:center"><?php _e("Large - Centered", "gravityforms") ?></option>
- <option value="large:right"><?php _e("Large - Right Aligned", "gravityforms") ?></option>
- <option value="full"><?php _e("Full Size", "gravityforms") ?></option>
- <option value="full:left"><?php _e("Full Size - Left Aligned", "gravityforms") ?></option>
- <option value="full:center"><?php _e("Full Size - Centered", "gravityforms") ?></option>
- <option value="full:right"><?php _e("Full Size - Right Aligned", "gravityforms") ?></option>
- </select>
- <?php
- }
- public static function insert_calculation_variables($fields, $element_id, $onchange = '', $callback = '', $max_label_size=40) {
- if($fields == null)
- $fields = array();
- $onchange = empty($onchange) ? "InsertVariable('{$element_id}', '{$callback}');" : $onchange;
- $class = 'gform_merge_tags';
- ?>
- <select id="<?php echo $element_id?>_variable_select" onchange="<?php echo $onchange ?>" class="<?php echo esc_attr($class)?>">
- <option value=''><?php _e("Insert Merge Tag", "gravityforms"); ?></option>
- <optgroup label="<?php _e("Allowable form fields", "gravityforms"); ?>">
- <?php
- foreach($fields as $field) {
- if(!self::is_valid_for_calcuation($field))
- continue;
- if(RGFormsModel::get_input_type($field) == 'checkbox') {
- foreach($field["inputs"] as $input){
- ?>
- <option value='<?php echo "{" . esc_html(GFCommon::get_label($field, $input["id"])) . ":" . $input["id"] . "}" ?>'><?php echo esc_html(GFCommon::get_label($field, $input["id"])) ?></option>
- <?php
- }
- } else {
- self::insert_field_variable($field, $max_label_size);
- }
- }
- ?>
- </optgroup>
- <?php
- $custom_merge_tags = apply_filters('gform_custom_merge_tags', array(), rgars($fields, '0/formId'), $fields, $element_id);
- if(is_array($custom_merge_tags) && !empty($custom_merge_tags)) { ?>
- <optgroup label="<?php _e("Custom", "gravityforms"); ?>">
- <?php foreach($custom_merge_tags as $custom_merge_tag) { ?>
- <option value='<?php echo rgar($custom_merge_tag, 'tag'); ?>'><?php echo rgar($custom_merge_tag, 'label'); ?></option>
- <?php } ?>
- </optgroup>
- <?php } ?>
- </select>
- <?php
- }
- private static function get_post_image_variable($media_id, $arg1, $arg2, $is_url = false){
- if($is_url){
- $image = wp_get_attachment_image_src($media_id, $arg1);
- if ( $image )
- list($src, $width, $height) = $image;
- return $src;
- }
- switch($arg1){
- case "title" :
- $media = get_post($media_id);
- return $media->post_title;
- case "caption" :
- $media = get_post($media_id);
- return $media->post_excerpt;
- case "description" :
- $media = get_post($media_id);
- return $media->post_content;
- default :
- $img = wp_get_attachment_image($media_id, $arg1, false, array("class" => "size-{$arg1} align{$arg2} wp-image-{$media_id}"));
- return $img;
- }
- }
- public static function replace_variables_post_image($text, $post_images, $lead){
- preg_match_all('/{[^{]*?:(\d+)(:([^:]*?))?(:([^:]*?))?(:url)?}/mi', $text, $matches, PREG_SET_ORDER);
- if(is_array($matches))
- {
- foreach($matches as $match){
- $input_id = $match[1];
- //ignore fields that are not post images
- if(!isset($post_images[$input_id]))
- continue;
- //Reading alignment and "url" parameters.
- //Format could be {image:5:medium:left:url} or {image:5:medium:url}
- $size_meta = empty($match[3]) ? "full" : $match[3];
- $align = empty($match[5]) ? "none" : $match[5];
- if($align == "url"){
- $align = "none";
- $is_url = true;
- }
- else{
- $is_url = rgar($match,6) == ":url";
- }
- $media_id = $post_images[$input_id];
- $value = is_wp_error($media_id) ? "" : self::get_post_image_variable($media_id, $size_meta, $align, $is_url);
- $text = str_replace($match[0], $value , $text);
- }
- }
- return $text;
- }
- public static function implode_non_blank($separator, $array){
- if(!is_array($array))
- return "";
- $ary = array();
- foreach($array as $item){
- if(!rgblank($item))
- $ary[] = $item;
- }
- return implode($separator, $ary);
- }
- private static function format_variable_value($value, $url_encode, $esc_html, $format){
- if($esc_html)
- $value = esc_html($value);
- if($format == "html")
- $value = nl2br($value);
- if($url_encode)
- $value = urlencode($value);
- return $value;
- }
- public static function replace_variables($text, $form, $lead, $url_encode = false, $esc_html=true, $nl2br = true, $format="html"){
- $text = $nl2br ? nl2br($text) : $text;
- //Replacing field variables: {FIELD_LABEL:FIELD_ID} {My Field:2}
- preg_match_all('/{[^{]*?:(\d+(\.\d+)?)(:(.*?))?}/mi', $text, $matches, PREG_SET_ORDER);
- if(is_array($matches))
- {
- foreach($matches as $match){
- $input_id = $match[1];
- $field = RGFormsModel::get_field($form,$input_id);
- $value = RGFormsModel::get_lead_field_value($lead, $field);
- $raw_value = $value;
- if(is_array($value))
- $value = rgar($value, $input_id);
- $value = self::format_variable_value($value, $url_encode, $esc_html, $format);
- switch(RGFormsModel::get_input_type($field)){
- case "fileupload" :
- $value = str_replace(" ", "%20", $value);
- break;
- case "post_image" :
- list($url, $title, $caption, $description) = explode("|:|", $value);
- $value = str_replace(" ", "%20", $url);
- break;
- case "checkbox" :
- case "select" :
- case "radio" :
- $use_value = rgar($match,4) == "value";
- $use_price = in_array(rgar($match,4), array("price", "currency"));
- $format_currency = rgar($match,4) == "currency";
- if(is_array($raw_value) && (string)intval($input_id) != $input_id){
- $items = array($input_id => $value); //float input Ids. (i.e. 4.1 ). Used when targeting specific checkbox items
- }
- else if(is_array($raw_value)){
- $items = $raw_value;
- }
- else{
- $items = array($input_id => $raw_value);
- }
- $ary = array();
- foreach($items as $input_id => $item){
- if($use_value){
- list($val, $price) = rgexplode("|", $item, 2);
- }
- else if($use_price){
- list($name, $val) = rgexplode("|", $item, 2);
- if($format_currency)
- $val = GFCommon::to_money($val, rgar($lead, "currency"));
- }
- else if($field["type"] == "post_category"){
- $use_id = strtolower(rgar($match,4)) == "id";
- $item_value = self::format_post_category($item, $use_id);
- $val = RGFormsModel::is_field_hidden($form, $field, array(), $lead) ? "" : $item_value;
- }
- else{
- $val = RGFormsModel::is_field_hidden($form, $field, array(), $lead) ? "" : RGFormsModel::get_choice_text($field, $raw_value, $input_id);
- }
- $ary[] = self::format_variable_value($val, $url_encode, $esc_html, $format);
- }
- $value = self::implode_non_blank(", ", $ary);
- break;
- case "multiselect" :
- if($field["type"] == "post_category"){
- $use_id = strtolower(rgar($match,4)) == "id";
- $items = explode(",", $value);
- if(is_array($items)){
- $cats = array();
- foreach($items as $item){
- $cat = self::format_post_category($item, $use_id);
- $cats[] = self::format_variable_value($cat, $url_encode, $esc_html, $format);
- }
- $value = self::implode_non_blank(", ", $cats);
- }
- }
- break;
- case "date" :
- $value = self::date_display($value, rgar($field,"dateFormat"));
- break;
- case "total" :
- $format_numeric = rgar($match,4) == "price";
- $value = $format_numeric ? GFCommon::to_number($value) : GFCommon::to_money($value);
- $value = self::format_variable_value($value, $url_encode, $esc_html, $format);
- break;
- case "post_category" :
- $use_id = strtolower(rgar($match,4)) == "id";
- $value = self::format_post_category($value, $use_id);
- $value = self::format_variable_value($value, $url_encode, $esc_html, $format);
- break;
- case "list" :
- $output_format = in_array(rgar($match,4), array("text", "html", "url")) ? rgar($match,4) : $format;
- $value = self::get_lead_field_display($field, $raw_value, $lead["currency"], true, $output_format);
- break;
- }
- if(rgar($match,4) == "label"){
- $value = empty($value) ? "" : rgar($field, "label");
- }
- else if(rgar($match,4) == "qty" && $field["type"] == "product"){
- //getting quantity associated with product field
- $products = self::get_product_fields($form, $lead, false, false);
- $value = 0;
- foreach($products["products"] as $product_id => $product)
- {
- if($product_id == $field["id"])
- $value = $product["quantity"];
- }
- }
- //filter can change merge code variable
- $value = apply_filters("gform_merge_tag_filter", $value, $input_id, rgar($match,4), $field, $raw_value);
- if($value === false)
- $value = "";
- $text = str_replace($match[0], $value , $text);
- }
- }
- //replacing global variables
- //form title
- $text = str_replace("{form_title}", $url_encode ? urlencode($form["title"]) : $form["title"], $text);
- $matches = array();
- preg_match_all("/{all_fields(:(.*?))?}/", $text, $matches, PREG_SET_ORDER);
- foreach($matches as $match){
- $options = explode(",", rgar($match,2));
- $use_value = in_array("value", $options);
- $display_empty = in_array("empty", $options);
- $use_admin_label = in_array("admin", $options);
- //all submitted fields using text
- if (strpos($text, $match[0]) !== false){
- $text = str_replace($match[0], self::get_submitted_fields($form, $lead, $display_empty, !$use_value, $format, $use_admin_label, "all_fields", rgar($match,2)), $text);
- }
- }
- //all submitted fields including empty fields
- if (strpos($text, "{all_fields_display_empty}") !== false){
- $text = str_replace("{all_fields_display_empty}", self::get_submitted_fields($form, $lead, true, true, $format, false, "all_fields_display_empty"), $text);
- }
- //pricing fields
- if (strpos($text, "{pricing_fields}") !== false){
- $text = str_replace("{pricing_fields}", self::get_submitted_pricing_fields($form, $lead, $format), $text);
- }
- //form id
- $text = str_replace("{form_id}", $url_encode ? urlencode($form["id"]) : $form["id"], $text);
- //entry id
- $text = str_replace("{entry_id}", $url_encode ? urlencode(rgar($lead, "id")) : rgar($lead, "id"), $text);
- //entry url
- $entry_url = get_bloginfo("wpurl") . "/wp-admin/admin.php?page=gf_entries&view=entry&id=" . $form["id"] . "&lid=" . rgar($lead,"id");
- $text = str_replace("{entry_url}", $url_encode ? urlencode($entry_url) : $entry_url, $text);
- //post id
- $text = str_replace("{post_id}", $url_encode ? urlencode($lead["post_id"]) : $lead["post_id"], $text);
- //admin email
- $wp_email = get_bloginfo("admin_email");
- $text = str_replace("{admin_email}", $url_encode ? urlencode($wp_email) : $wp_email, $text);
- //post edit url
- $post_url = get_bloginfo("wpurl") . "/wp-admin/post.php?action=edit&post=" . $lead["post_id"];
- $text = str_replace("{post_edit_url}", $url_encode ? urlencode($post_url) : $post_url, $text);
- $text = self::replace_variables_prepopulate($text, $url_encode);
- // hook allows for custom merge tags
- $text = apply_filters('gform_replace_merge_tags', $text, $form, $lead, $url_encode, $esc_html, $nl2br, $format);
- // TODO: Deprecate the 'gform_replace_merge_tags' and replace it with a call to the 'gform_merge_tag_filter'
- //$text = apply_filters('gform_merge_tag_filter', $text, false, false, false );
- return $text;
- }
- public static function format_post_category($value, $use_id){
- list($item_value, $item_id) = rgexplode(":", $value, 2);
- if($use_id && !empty($item_id))
- $item_value = $item_id;
- return $item_value;
- }
- public static function get_embed_post(){
- global $embed_post, $post, $wp_query;
- if($embed_post){
- return $embed_post;
- }
- if(!rgempty("gform_embed_post")){
- $post_id = absint(rgpost("gform_embed_post"));
- $embed_post = get_post($post_id);
- }
- else if($wp_query->is_in_loop){
- $embed_post = $post;
- }
- else{
- $embed_post = array();
- }
- }
- public static function replace_variables_prepopulate($text, $url_encode=false){
- //embed url
- $text = str_replace("{embed_url}", $url_encode ? urlencode(RGFormsModel::get_current_page_url()) : RGFormsModel::get_current_page_url(), $text);
- $local_timestamp = self::get_local_timestamp(time());
- //date (mm/dd/yyyy)
- $local_date_mdy = date_i18n("m/d/Y", $local_timestamp, true);
- $text = str_replace("{date_mdy}", $url_encode ? urlencode($local_date_mdy) : $local_date_mdy, $text);
- //date (dd/mm/yyyy)
- $local_date_dmy = date_i18n("d/m/Y", $local_timestamp, true);
- $text = str_replace("{date_dmy}", $url_encode ? urlencode($local_date_dmy) : $local_date_dmy, $text);
- //ip
- $text = str_replace("{ip}", $url_encode ? urlencode($_SERVER['REMOTE_ADDR']) : $_SERVER['REMOTE_ADDR'], $text);
- global $post;
- $post_array = self::object_to_array($post);
- preg_match_all("/\{embed_post:(.*?)\}/", $text, $matches, PREG_SET_ORDER);
- foreach($matches as $match){
- $full_tag = $match[0];
- $property = $match[1];
- $text = str_replace($full_tag, $url_encode ? urlencode($post_array[$property]) : $post_array[$property], $text);
- }
- //embed post custom fields
- preg_match_all("/\{custom_field:(.*?)\}/", $text, $matches, PREG_SET_ORDER);
- foreach($matches as $match){
- $full_tag = $match[0];
- $custom_field_name = $match[1];
- $custom_field_value = !empty($post_array["ID"]) ? get_post_meta($post_array["ID"], $custom_field_name, true) : "";
- $text = str_replace($full_tag, $url_encode ? urlencode($custom_field_value) : $custom_field_value, $text);
- }
- //user agent
- $text = str_replace("{user_agent}", $url_encode ? urlencode(RGForms::get("HTTP_USER_AGENT", $_SERVER)) : RGForms::get("HTTP_USER_AGENT", $_SERVER), $text);
- //referrer
- $text = str_replace("{referer}", $url_encode ? urlencode(RGForms::get("HTTP_REFERER", $_SERVER)) : RGForms::get("HTTP_REFERER", $_SERVER), $text);
- //logged in user info
- global $userdata, $wp_version, $current_user;
- $user_array = self::object_to_array($userdata);
- preg_match_all("/\{user:(.*?)\}/", $text, $matches, PREG_SET_ORDER);
- foreach($matches as $match){
- $full_tag = $match[0];
- $property = $match[1];
- $value = version_compare($wp_version, '3.3', '>=') ? $current_user->get($property) : $user_array[$property];
- $value = $url_encode ? urlencode($value) : $value;
- $text = str_replace($full_tag, $value, $text);
- }
- return $text;
- }
- public static function object_to_array($object){
- $array=array();
- if(!empty($object)){
- foreach($object as $member=>$data)
- $array[$member]=$data;
- }
- return $array;
- }
- public static function is_empty_array($val){
- if(!is_array($val))
- $val = array($val);
- $ary = array_values($val);
- foreach($ary as $item){
- if(!rgblank($item))
- return false;
- }
- return true;
- }
- public static function get_submitted_fields($form, $lead, $display_empty=false, $use_text=false, $format="html", $use_admin_label=false, $merge_tag="", $options=""){
- $field_data = "";
- if($format == "html"){
- $field_data = '<table width="99%" border="0" cellpadding="1" cellspacing="0" bgcolor="#EAEAEA"><tr><td>
- <table width="100%" border="0" cellpadding="5" cellspacing="0" bgcolor="#FFFFFF">
- ';
- }
- $options_array = explode(",", $options);
- $no_admin = in_array("noadmin", $options_array);
- $no_hidden = in_array("nohidden", $options_array);
- $has_product_fields = false;
- foreach($form["fields"] as $field){
- $field_value = "";
- $field_label = $use_admin_label && !rgempty("adminLabel", $field) ? rgar($field, "adminLabel") : esc_html(GFCommon::get_label($field));
- switch($field["type"]){
- case "captcha" :
- break;
- case "section" :
- if((!GFCommon::is_section_empty($field, $form, $lead) || $display_empty) && !rgar($field, "adminOnly")){
- switch($format){
- case "text" :
- $field_value = "--------------------------------\n{$field_label}\n\n";
- break;
- default:
- $field_value = sprintf('<tr>
- <td colspan="2" style="font-size:14px; font-weight:bold; background-color:#EEE; border-bottom:1px solid #DFDFDF; padding:7px 7px">%s</td>
- </tr>
- ', $field_label);
- break;
- }
- }
- $field_value = apply_filters("gform_merge_tag_filter", $field_value, $merge_tag, $options, $field, $field_label);
- $field_data .= $field_value;
- break;
- case "password" :
- //ignore password fields
- break;
- default :
- //ignore product fields as they will be grouped together at the end of the grid
- if(self::is_product_field($field["type"])){
- $has_product_fields = true;
- continue;
- }
- else if(RGFormsModel::is_field_hidden($form, $field, array(), $lead)){
- //ignore fields hidden by conditional logic
- continue;
- }
- $raw_field_value = RGFormsModel::get_lead_field_value($lead, $field);
- $field_value = GFCommon::get_lead_field_display($field, $raw_field_value, rgar($lead,"currency"), $use_text, $format, "email");
- $display_field = true;
- //depending on parameters, don't display adminOnly or hidden fields
- if($no_admin && rgar($field, "adminOnly"))
- $display_field = false;
- else if($no_hidden && RGFormsModel::get_input_type($field) == "hidden")
- $display_field = false;
- //if field is not supposed to be displayed, pass false to filter. otherwise, pass field's value
- if(!$display_field)
- $field_value = false;
- $field_value = apply_filters("gform_merge_tag_filter", $field_value, $merge_tag, $options, $field, $raw_field_value);
- if($field_value === false)
- continue;
- if( !empty($field_value) || strlen($field_value) > 0 || $display_empty){
- switch($format){
- case "text" :
- $field_data .= "{$field_label}: {$field_value}\n\n";
- break;
- default:
- $field_data .= sprintf('<tr bgcolor="#EAF2FA">
- <td colspan="2">
- <font style="font-family: sans-serif; font-size:12px;"><strong>%s</strong></font>
- </td>
- </tr>
- <tr bgcolor="#FFFFFF">
- <td width="20"> </td>
- <td>
- <font style="font-family: sans-serif; font-size:12px;">%s</font>
- </td>
- </tr>
- ', $field_label, empty($field_value) && strlen($field_value) == 0 ? " " : $field_value);
- break;
- }
- }
- }
- }
- if($has_product_fields)
- $field_data .= self::get_submitted_pricing_fields($form, $lead, $format, $use_text, $use_admin_label);
- if($format == "html"){
- $field_data .='</table>
- </td>
- </tr>
- </table>';
- }
- return $field_data;
- }
- public static function get_submitted_pricing_fields($form, $lead, $format, $use_text=true, $use_admin_label=false){
- $form_id = $form["id"];
- $order_label = apply_filters("gform_order_label_{$form["id"]}", apply_filters("gform_order_label", __("Order", "gravityforms"), $form["id"])…
Large files files are truncated, but you can click here to view the full file