PageRenderTime 74ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 2ms

/wp-content/plugins/gravityforms/common.php

https://github.com/webgefrickel/frckl-init-wordpress
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

  1. <?php
  2. class GFCommon{
  3. public static $version = "1.7.9";
  4. public static $tab_index = 1;
  5. public static $errors = array();
  6. private static $messages = array();
  7. public static function get_selection_fields($form, $selected_field_id){
  8. $str = "";
  9. foreach($form["fields"] as $field){
  10. $input_type = RGFormsModel::get_input_type($field);
  11. $field_label = RGFormsModel::get_label($field);
  12. if($input_type == "checkbox" || $input_type == "radio" || $input_type == "select"){
  13. $selected = $field["id"] == $selected_field_id ? "selected='selected'" : "";
  14. $str .= "<option value='" . $field["id"] . "' " . $selected . ">" . $field_label . "</option>";
  15. }
  16. }
  17. return $str;
  18. }
  19. public static function is_numeric($value, $number_format=""){
  20. switch($number_format){
  21. case "decimal_dot" :
  22. return preg_match("/^(-?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]+)?)$/", $value);
  23. break;
  24. case "decimal_comma" :
  25. return preg_match("/^(-?[0-9]{1,3}(?:\.?[0-9]{3})*(?:,[0-9]+)?)$/", $value);
  26. break;
  27. default :
  28. 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);
  29. }
  30. }
  31. public static function trim_all($text){
  32. $text = trim($text);
  33. do{
  34. $prev_text = $text;
  35. $text = str_replace(" ", " ", $text);
  36. }
  37. while($text != $prev_text);
  38. return $text;
  39. }
  40. public static function format_number($number, $number_format){
  41. if(!is_numeric($number))
  42. return $number;
  43. //replacing commas with dots and dots with commas
  44. if($number_format == "decimal_comma"){
  45. $number = str_replace(",", "|", $number);
  46. $number = str_replace(".", ",", $number);
  47. $number = str_replace("|", ".", $number);
  48. }
  49. return $number;
  50. }
  51. public static function recursive_add_index_file($dir) {
  52. if(!is_dir($dir))
  53. return;
  54. if(!($dp = opendir($dir)))
  55. return;
  56. //ignores all errors
  57. set_error_handler(create_function("", "return 0;"), E_ALL);
  58. //creates an empty index.html file
  59. if($f = fopen($dir . "/index.html", 'w'))
  60. fclose($f);
  61. //restores error handler
  62. restore_error_handler();
  63. while((false !== $file = readdir($dp))){
  64. if(is_dir("$dir/$file") && $file != '.' && $file !='..')
  65. self::recursive_add_index_file("$dir/$file");
  66. }
  67. closedir($dp);
  68. }
  69. public static function clean_number($number, $number_format=""){
  70. if(rgblank($number))
  71. return $number;
  72. $decimal_char = "";
  73. if($number_format == "decimal_dot")
  74. $decimal_char = ".";
  75. else if($number_format == "decimal_comma")
  76. $decimal_char = ",";
  77. $float_number = "";
  78. $clean_number = "";
  79. $is_negative = false;
  80. //Removing all non-numeric characters
  81. $array = str_split($number);
  82. foreach($array as $char){
  83. if (($char >= '0' && $char <= '9') || $char=="," || $char==".")
  84. $clean_number .= $char;
  85. else if($char == '-')
  86. $is_negative = true;
  87. }
  88. //Removing thousand separators but keeping decimal point
  89. $array = str_split($clean_number);
  90. for($i=0, $count = sizeof($array); $i<$count; $i++)
  91. {
  92. $char = $array[$i];
  93. if ($char >= '0' && $char <= '9')
  94. $float_number .= $char;
  95. else if(empty($decimal_char) && ($char == "." || $char == ",") && strlen($clean_number) - $i <= 3)
  96. $float_number .= ".";
  97. else if($decimal_char == $char)
  98. $float_number .= ".";
  99. }
  100. if($is_negative)
  101. $float_number = "-" . $float_number;
  102. return $float_number;
  103. }
  104. public static function json_encode($value){
  105. return json_encode($value);
  106. }
  107. public static function json_decode($str, $is_assoc=true){
  108. return json_decode($str, $is_assoc);
  109. }
  110. //Returns the url of the plugin's root folder
  111. public static function get_base_url(){
  112. $folder = basename(dirname(__FILE__));
  113. return plugins_url($folder);
  114. }
  115. //Returns the physical path of the plugin's root folder
  116. public static function get_base_path(){
  117. $folder = basename(dirname(__FILE__));
  118. return WP_PLUGIN_DIR . "/" . $folder;
  119. }
  120. public static function get_email_fields($form){
  121. $fields = array();
  122. foreach($form["fields"] as $field){
  123. if(RGForms::get("type", $field) == "email" || RGForms::get("inputType", $field) == "email")
  124. $fields[] = $field;
  125. }
  126. return $fields;
  127. }
  128. public static function truncate_middle($text, $max_length){
  129. if(strlen($text) <= $max_length)
  130. return $text;
  131. $middle = intval($max_length / 2);
  132. return substr($text, 0, $middle) . "..." . substr($text, strlen($text) - $middle, $middle);
  133. }
  134. public static function is_invalid_or_empty_email($email){
  135. return empty($email) || !self::is_valid_email($email);
  136. }
  137. public static function is_valid_url($url){
  138. return preg_match('!^(http|https)://([\w-]+\.?)+[\w-]+(:\d+)?(/[\w- ./?~%&=+\']*)?$!', $url);
  139. }
  140. public static function is_valid_email($email){
  141. return preg_match('/^(([a-zA-Z0-9_.\-+!#$&\'*+=?^`{|}~])+\@((([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+|localhost) *,? *)+$/', $email);
  142. }
  143. public static function get_label($field, $input_id = 0, $input_only = false){
  144. return RGFormsModel::get_label($field, $input_id, $input_only);
  145. }
  146. public static function get_input($field, $id){
  147. return RGFormsModel::get_input($field, $id);
  148. }
  149. public static function insert_variables($fields, $element_id, $hide_all_fields=false, $callback="", $onchange="", $max_label_size=40, $exclude = null, $args="", $class_name=""){
  150. if($fields == null)
  151. $fields = array();
  152. if($exclude == null)
  153. $exclude = array();
  154. $exclude = apply_filters("gform_merge_tag_list_exclude", $exclude, $element_id, $fields);
  155. $merge_tags = self::get_merge_tags($fields, $element_id, $hide_all_fields, $exclude, $args);
  156. $onchange = empty($onchange) ? "InsertVariable('{$element_id}', '{$callback}');" : $onchange;
  157. $class = trim($class_name . " gform_merge_tags");
  158. ?>
  159. <select id="<?php echo $element_id?>_variable_select" onchange="<?php echo $onchange ?>" class="<?php echo esc_attr($class)?>">
  160. <option value=''><?php _e("Insert Merge Tag", "gravityforms"); ?></option>
  161. <?php foreach($merge_tags as $group => $group_tags) {
  162. $group_label = rgar($group_tags, 'label');
  163. $tags = rgar($group_tags, 'tags');
  164. if(empty($group_tags['tags']))
  165. continue;
  166. if($group_label) { ?>
  167. <optgroup label="<?php echo $group_label; ?>">
  168. <?php } ?>
  169. <?php foreach($tags as $tag) { ?>
  170. <option value="<?php echo $tag['tag']; ?>"><?php echo $tag['label']; ?></option>
  171. <?php }
  172. if($group_label) { ?>
  173. </optgroup>
  174. <?php }
  175. } ?>
  176. </select>
  177. <?php
  178. }
  179. public static function OLD_insert_variables($fields, $element_id, $hide_all_fields=false, $callback="", $onchange="", $max_label_size=40, $exclude = null, $args="", $class_name=""){
  180. if($fields == null)
  181. $fields = array();
  182. if($exclude == null)
  183. $exclude = array();
  184. $exclude = apply_filters("gform_merge_tag_list_exclude", $exclude, $element_id, $fields);
  185. $onchange = empty($onchange) ? "InsertVariable('{$element_id}', '{$callback}');" : $onchange;
  186. $class = trim($class_name . " gform_merge_tags");
  187. ?>
  188. <select id="<?php echo $element_id?>_variable_select" onchange="<?php echo $onchange ?>" class="<?php echo esc_attr($class)?>">
  189. <option value=''><?php _e("Insert Merge Tag", "gravityforms"); ?></option>
  190. <?php
  191. if(!$hide_all_fields){
  192. ?>
  193. <option value='{all_fields}'><?php _e("All Submitted Fields", "gravityforms"); ?></option>
  194. <?php
  195. }
  196. $required_fields = array();
  197. $optional_fields = array();
  198. $pricing_fields = array();
  199. foreach($fields as $field){
  200. if(rgget("displayOnly", $field))
  201. continue;
  202. $input_type = RGFormsModel::get_input_type($field);
  203. //skip field types that should be excluded
  204. if(is_array($exclude) && in_array($input_type, $exclude))
  205. continue;
  206. if($field["isRequired"]){
  207. switch($input_type){
  208. case "name" :
  209. if($field["nameFormat"] == "extended"){
  210. $prefix = GFCommon::get_input($field, $field["id"] + 0.2);
  211. $suffix = GFCommon::get_input($field, $field["id"] + 0.8);
  212. $optional_field = $field;
  213. $optional_field["inputs"] = array($prefix, $suffix);
  214. //Add optional name fields to the optional list
  215. $optional_fields[] = $optional_field;
  216. //Remove optional name field from required list
  217. unset($field["inputs"][0]);
  218. unset($field["inputs"][3]);
  219. }
  220. $required_fields[] = $field;
  221. break;
  222. default:
  223. $required_fields[] = $field;
  224. }
  225. }
  226. else{
  227. $optional_fields[] = $field;
  228. }
  229. if(self::is_pricing_field($field["type"])){
  230. $pricing_fields[] = $field;
  231. }
  232. }
  233. if(!empty($required_fields)){
  234. ?>
  235. <optgroup label="<?php _e("Required form fields", "gravityforms"); ?>">
  236. <?php
  237. foreach($required_fields as $field){
  238. self::insert_field_variable($field, $max_label_size, $args);
  239. }
  240. ?>
  241. </optgroup>
  242. <?php
  243. }
  244. if(!empty($optional_fields)){
  245. ?>
  246. <optgroup label="<?php _e("Optional form fields", "gravityforms"); ?>">
  247. <?php
  248. foreach($optional_fields as $field){
  249. self::insert_field_variable($field, $max_label_size, $args);
  250. }
  251. ?>
  252. </optgroup>
  253. <?php
  254. }
  255. if(!empty($pricing_fields)){
  256. ?>
  257. <optgroup label="<?php _e("Pricing form fields", "gravityforms"); ?>">
  258. <?php
  259. if(!$hide_all_fields){
  260. ?>
  261. <option value='{pricing_fields}'><?php _e("All Pricing Fields", "gravityforms"); ?></option>
  262. <?php
  263. }?>
  264. <?php
  265. foreach($pricing_fields as $field){
  266. self::insert_field_variable($field, $max_label_size, $args);
  267. }
  268. ?>
  269. </optgroup>
  270. <?php
  271. }
  272. ?>
  273. <optgroup label="<?php _e("Other", "gravityforms"); ?>">
  274. <option value='{ip}'><?php _e("Client IP Address", "gravityforms"); ?></option>
  275. <option value='{date_mdy}'><?php _e("Date", "gravityforms"); ?> (mm/dd/yyyy)</option>
  276. <option value='{date_dmy}'><?php _e("Date", "gravityforms"); ?> (dd/mm/yyyy)</option>
  277. <option value='{embed_post:ID}'><?php _e("Embed Post/Page Id", "gravityforms"); ?></option>
  278. <option value='{embed_post:post_title}'><?php _e("Embed Post/Page Title", "gravityforms"); ?></option>
  279. <option value='{embed_url}'><?php _e("Embed URL", "gravityforms"); ?></option>
  280. <option value='{entry_id}'><?php _e("Entry Id", "gravityforms"); ?></option>
  281. <option value='{entry_url}'><?php _e("Entry URL", "gravityforms"); ?></option>
  282. <option value='{form_id}'><?php _e("Form Id", "gravityforms"); ?></option>
  283. <option value='{form_title}'><?php _e("Form Title", "gravityforms"); ?></option>
  284. <option value='{referer}'><?php _e("HTTP Referer URL", "gravityforms"); ?></option>
  285. <option value='{user_agent}'><?php _e("HTTP User Agent", "gravityforms"); ?></option>
  286. <?php if(self::has_post_field($fields)){ ?>
  287. <option value='{post_id}'><?php _e("Post Id", "gravityforms"); ?></option>
  288. <option value='{post_edit_url}'><?php _e("Post Edit URL", "gravityforms"); ?></option>
  289. <?php } ?>
  290. <option value='{user:display_name}'><?php _e("User Display Name", "gravityforms"); ?></option>
  291. <option value='{user:user_email}'><?php _e("User Email", "gravityforms"); ?></option>
  292. <option value='{user:user_login}'><?php _e("User Login", "gravityforms"); ?></option>
  293. </optgroup>
  294. <?php
  295. $custom_merge_tags = apply_filters('gform_custom_merge_tags', array(), rgars($fields, '0/formId'), $fields, $element_id);
  296. if(is_array($custom_merge_tags) && !empty($custom_merge_tags)) { ?>
  297. <optgroup label="<?php _e("Custom", "gravityforms"); ?>">
  298. <?php foreach($custom_merge_tags as $custom_merge_tag) { ?>
  299. <option value='<?php echo rgar($custom_merge_tag, 'tag'); ?>'><?php echo rgar($custom_merge_tag, 'label'); ?></option>
  300. <?php } ?>
  301. </optgroup>
  302. <?php } ?>
  303. </select>
  304. <?php
  305. }
  306. /**
  307. * This function is used by the gfMergeTags JS object to get the localized label for non-field merge tags as well as
  308. * for backwards compatability with the gform_custom_merge_tags hook. Lastly, this plugin is used by the soon-to-be
  309. * deprecrated insert_variables() function as the new gfMergeTags object has not yet been applied to the Post Content
  310. * Template setting.
  311. *
  312. * @param $fields
  313. * @param $element_id
  314. * @param bool $hide_all_fields
  315. * @param array $exclude_field_types
  316. * @param string $option
  317. * @return array
  318. */
  319. public static function get_merge_tags($fields, $element_id, $hide_all_fields = false, $exclude_field_types = array(), $option = '') {
  320. if($fields == null)
  321. $fields = array();
  322. if($exclude_field_types == null)
  323. $exclude_field_types = array();
  324. $required_fields = $optional_fields = $pricing_fields = array();
  325. $ungrouped = $required_group = $optional_group = $pricing_group = $other_group = array();
  326. if(!$hide_all_fields)
  327. $ungrouped[] = array('tag' => '{all_fields}', 'label' => __('All Submitted Fields', 'gravityforms'));
  328. // group fields by required, optional, and pricing
  329. foreach($fields as $field) {
  330. if(rgget("displayOnly", $field))
  331. continue;
  332. $input_type = RGFormsModel::get_input_type($field);
  333. // skip field types that should be excluded
  334. if(is_array($exclude_field_types) && in_array($input_type, $exclude_field_types))
  335. continue;
  336. if(rgar($field, 'isRequired')) {
  337. switch($input_type) {
  338. case "name" :
  339. if(rgar($field,"nameFormat") == "extended") {
  340. $prefix = GFCommon::get_input($field, $field["id"] + 0.2);
  341. $suffix = GFCommon::get_input($field, $field["id"] + 0.8);
  342. $optional_field = $field;
  343. $optional_field["inputs"] = array($prefix, $suffix);
  344. //Add optional name fields to the optional list
  345. $optional_fields[] = $optional_field;
  346. //Remove optional name field from required list
  347. unset($field["inputs"][0]);
  348. unset($field["inputs"][3]);
  349. }
  350. $required_fields[] = $field;
  351. break;
  352. default:
  353. $required_fields[] = $field;
  354. }
  355. }
  356. else {
  357. $optional_fields[] = $field;
  358. }
  359. if(self::is_pricing_field($field["type"])){
  360. $pricing_fields[] = $field;
  361. }
  362. }
  363. if(!empty($required_fields)){
  364. foreach($required_fields as $field){
  365. $required_group = array_merge($required_group, self::get_field_merge_tags($field, $option));
  366. }
  367. }
  368. if(!empty($optional_fields)){
  369. foreach($optional_fields as $field){
  370. $optional_group = array_merge($optional_group, self::get_field_merge_tags($field, $option));
  371. }
  372. }
  373. if(!empty($pricing_fields)){
  374. if(!$hide_all_fields)
  375. $pricing_group[] = array('tag' => '{pricing_fields}', 'label' => __("All Pricing Fields", "gravityforms"));
  376. foreach($pricing_fields as $field){
  377. $pricing_group = array_merge($pricing_group, self::get_field_merge_tags($field, $option));
  378. }
  379. }
  380. $other_group[] = array('tag' => '{ip}', 'label' => __("Client IP Address", "gravityforms"));
  381. $other_group[] = array('tag' => '{date_mdy}', 'label' => __("Date", "gravityforms") . ' (mm/dd/yyyy)');
  382. $other_group[] = array('tag' => '{date_dmy}', 'label' => __("Date", "gravityforms") . ' (dd/mm/yyyy)');
  383. $other_group[] = array('tag' => '{embed_post:ID}', 'label' => __("Embed Post/Page Id", "gravityforms"));
  384. $other_group[] = array('tag' => '{embed_post:post_title}', 'label' => __("Embed Post/Page Title", "gravityforms"));
  385. $other_group[] = array('tag' => '{embed_url}', 'label' => __("Embed URL", "gravityforms"));
  386. $other_group[] = array('tag' => '{entry_id}', 'label' => __("Entry Id", "gravityforms"));
  387. $other_group[] = array('tag' => '{entry_url}', 'label' => __("Entry URL", "gravityforms"));
  388. $other_group[] = array('tag' => '{form_id}', 'label' => __("Form Id", "gravityforms"));
  389. $other_group[] = array('tag' => '{form_title}', 'label' => __("Form Title", "gravityforms"));
  390. $other_group[] = array('tag' => '{user_agent}', 'label' => __("HTTP User Agent", "gravityforms"));
  391. $other_group[] = array('tag' => '{referer}', 'label' => __("HTTP Referer URL", "gravityforms"));
  392. if(self::has_post_field($fields)) {
  393. $other_group[] = array('tag' => '{post_id}', 'label' => __("Post Id", "gravityforms"));
  394. $other_group[] = array('tag' => '{post_edit_url}', 'label' => __("Post Edit URL", "gravityforms"));
  395. }
  396. $other_group[] = array('tag' => '{user:display_name}', 'label' => __("User Display Name", "gravityforms"));
  397. $other_group[] = array('tag' => '{user:user_email}', 'label' => __("User Email", "gravityforms"));
  398. $other_group[] = array('tag' => '{user:user_login}', 'label' => __("User Login", "gravityforms"));
  399. $custom_group = apply_filters('gform_custom_merge_tags', array(), rgars($fields, '0/formId'), $fields, $element_id);
  400. $merge_tags = array(
  401. 'ungrouped' => array(
  402. 'label' => false,
  403. 'tags' => $ungrouped
  404. ),
  405. 'required' => array(
  406. 'label' => __("Required form fields", "gravityforms"),
  407. 'tags' => $required_group
  408. ),
  409. 'optional' => array(
  410. 'label' => __("Optional form fields", "gravityforms"),
  411. 'tags' => $optional_group
  412. ),
  413. 'pricing' => array(
  414. 'label' => __("Pricing form fields", "gravityforms"),
  415. 'tags' => $pricing_group
  416. ),
  417. 'other' => array(
  418. 'label' => __("Other", "gravityforms"),
  419. 'tags' => $other_group
  420. ),
  421. 'custom' => array(
  422. 'label' => __("Custom", "gravityforms"),
  423. 'tags' => $custom_group
  424. )
  425. );
  426. return $merge_tags;
  427. }
  428. public static function get_field_merge_tags($field, $option="") {
  429. $merge_tags = array();
  430. $tag_args = RGFormsModel::get_input_type($field) == "list" ? ":{$option}" : ""; //args currently only supported by list field
  431. if(isset($field["inputs"]) && is_array($field["inputs"])) {
  432. if(RGFormsModel::get_input_type($field) == "checkbox") {
  433. $value = "{" . esc_html(GFCommon::get_label($field, $field["id"])) . ":" . $field["id"] . "{$tag_args}}";
  434. $merge_tags[] = array( 'tag' => $value, 'label' => esc_html(GFCommon::get_label($field, $field["id"])) );
  435. }
  436. foreach($field["inputs"] as $input) {
  437. $value = "{" . esc_html(GFCommon::get_label($field, $input["id"])) . ":" . $input["id"] . "{$tag_args}}";
  438. $merge_tags[] = array( 'tag' => $value, 'label' => esc_html(GFCommon::get_label($field, $input["id"])) );
  439. }
  440. }
  441. else {
  442. $value = "{" . esc_html(GFCommon::get_label($field)) . ":" . $field["id"] . "{$tag_args}}";
  443. $merge_tags[] = array( 'tag' => $value, 'label' => esc_html(GFCommon::get_label($field)) );
  444. }
  445. return $merge_tags;
  446. }
  447. public static function insert_field_variable($field, $max_label_size=40, $args=""){
  448. $tag_args = RGFormsModel::get_input_type($field) == "list" ? ":{$args}" : ""; //args currently only supported by list field
  449. if(is_array($field["inputs"]))
  450. {
  451. if(RGFormsModel::get_input_type($field) == "checkbox"){
  452. ?>
  453. <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>
  454. <?php
  455. }
  456. foreach($field["inputs"] as $input){
  457. ?>
  458. <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>
  459. <?php
  460. }
  461. }
  462. else{
  463. ?>
  464. <option value='<?php echo "{" . esc_html(GFCommon::get_label($field)) . ":" . $field["id"] . "{$tag_args}}" ?>'><?php echo esc_html(GFCommon::get_label($field)) ?></option>
  465. <?php
  466. }
  467. }
  468. public static function insert_post_content_variables($fields, $element_id, $callback, $max_label_size=25){
  469. // TODO: replace with class-powered merge tags
  470. self::insert_variables($fields, $element_id, true, "", "InsertPostContentVariable('{$element_id}', '{$callback}');", $max_label_size, null, "", "gform_content_template_merge_tags");
  471. ?>
  472. &nbsp;&nbsp;
  473. <select id="<?php echo $element_id?>_image_size_select" onchange="InsertPostImageVariable('<?php echo $element_id ?>', '<?php echo $element_id ?>'); SetCustomFieldTemplate();" style="display:none;">
  474. <option value=""><?php _e("Select image size", "gravityforms") ?></option>
  475. <option value="thumbnail"><?php _e("Thumbnail", "gravityforms") ?></option>
  476. <option value="thumbnail:left"><?php _e("Thumbnail - Left Aligned", "gravityforms") ?></option>
  477. <option value="thumbnail:center"><?php _e("Thumbnail - Centered", "gravityforms") ?></option>
  478. <option value="thumbnail:right"><?php _e("Thumbnail - Right Aligned", "gravityforms") ?></option>
  479. <option value="medium"><?php _e("Medium", "gravityforms") ?></option>
  480. <option value="medium:left"><?php _e("Medium - Left Aligned", "gravityforms") ?></option>
  481. <option value="medium:center"><?php _e("Medium - Centered", "gravityforms") ?></option>
  482. <option value="medium:right"><?php _e("Medium - Right Aligned", "gravityforms") ?></option>
  483. <option value="large"><?php _e("Large", "gravityforms") ?></option>
  484. <option value="large:left"><?php _e("Large - Left Aligned", "gravityforms") ?></option>
  485. <option value="large:center"><?php _e("Large - Centered", "gravityforms") ?></option>
  486. <option value="large:right"><?php _e("Large - Right Aligned", "gravityforms") ?></option>
  487. <option value="full"><?php _e("Full Size", "gravityforms") ?></option>
  488. <option value="full:left"><?php _e("Full Size - Left Aligned", "gravityforms") ?></option>
  489. <option value="full:center"><?php _e("Full Size - Centered", "gravityforms") ?></option>
  490. <option value="full:right"><?php _e("Full Size - Right Aligned", "gravityforms") ?></option>
  491. </select>
  492. <?php
  493. }
  494. public static function insert_calculation_variables($fields, $element_id, $onchange = '', $callback = '', $max_label_size=40) {
  495. if($fields == null)
  496. $fields = array();
  497. $onchange = empty($onchange) ? "InsertVariable('{$element_id}', '{$callback}');" : $onchange;
  498. $class = 'gform_merge_tags';
  499. ?>
  500. <select id="<?php echo $element_id?>_variable_select" onchange="<?php echo $onchange ?>" class="<?php echo esc_attr($class)?>">
  501. <option value=''><?php _e("Insert Merge Tag", "gravityforms"); ?></option>
  502. <optgroup label="<?php _e("Allowable form fields", "gravityforms"); ?>">
  503. <?php
  504. foreach($fields as $field) {
  505. if(!self::is_valid_for_calcuation($field))
  506. continue;
  507. if(RGFormsModel::get_input_type($field) == 'checkbox') {
  508. foreach($field["inputs"] as $input){
  509. ?>
  510. <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>
  511. <?php
  512. }
  513. } else {
  514. self::insert_field_variable($field, $max_label_size);
  515. }
  516. }
  517. ?>
  518. </optgroup>
  519. <?php
  520. $custom_merge_tags = apply_filters('gform_custom_merge_tags', array(), rgars($fields, '0/formId'), $fields, $element_id);
  521. if(is_array($custom_merge_tags) && !empty($custom_merge_tags)) { ?>
  522. <optgroup label="<?php _e("Custom", "gravityforms"); ?>">
  523. <?php foreach($custom_merge_tags as $custom_merge_tag) { ?>
  524. <option value='<?php echo rgar($custom_merge_tag, 'tag'); ?>'><?php echo rgar($custom_merge_tag, 'label'); ?></option>
  525. <?php } ?>
  526. </optgroup>
  527. <?php } ?>
  528. </select>
  529. <?php
  530. }
  531. private static function get_post_image_variable($media_id, $arg1, $arg2, $is_url = false){
  532. if($is_url){
  533. $image = wp_get_attachment_image_src($media_id, $arg1);
  534. if ( $image )
  535. list($src, $width, $height) = $image;
  536. return $src;
  537. }
  538. switch($arg1){
  539. case "title" :
  540. $media = get_post($media_id);
  541. return $media->post_title;
  542. case "caption" :
  543. $media = get_post($media_id);
  544. return $media->post_excerpt;
  545. case "description" :
  546. $media = get_post($media_id);
  547. return $media->post_content;
  548. default :
  549. $img = wp_get_attachment_image($media_id, $arg1, false, array("class" => "size-{$arg1} align{$arg2} wp-image-{$media_id}"));
  550. return $img;
  551. }
  552. }
  553. public static function replace_variables_post_image($text, $post_images, $lead){
  554. preg_match_all('/{[^{]*?:(\d+)(:([^:]*?))?(:([^:]*?))?(:url)?}/mi', $text, $matches, PREG_SET_ORDER);
  555. if(is_array($matches))
  556. {
  557. foreach($matches as $match){
  558. $input_id = $match[1];
  559. //ignore fields that are not post images
  560. if(!isset($post_images[$input_id]))
  561. continue;
  562. //Reading alignment and "url" parameters.
  563. //Format could be {image:5:medium:left:url} or {image:5:medium:url}
  564. $size_meta = empty($match[3]) ? "full" : $match[3];
  565. $align = empty($match[5]) ? "none" : $match[5];
  566. if($align == "url"){
  567. $align = "none";
  568. $is_url = true;
  569. }
  570. else{
  571. $is_url = rgar($match,6) == ":url";
  572. }
  573. $media_id = $post_images[$input_id];
  574. $value = is_wp_error($media_id) ? "" : self::get_post_image_variable($media_id, $size_meta, $align, $is_url);
  575. $text = str_replace($match[0], $value , $text);
  576. }
  577. }
  578. return $text;
  579. }
  580. public static function implode_non_blank($separator, $array){
  581. if(!is_array($array))
  582. return "";
  583. $ary = array();
  584. foreach($array as $item){
  585. if(!rgblank($item))
  586. $ary[] = $item;
  587. }
  588. return implode($separator, $ary);
  589. }
  590. private static function format_variable_value($value, $url_encode, $esc_html, $format){
  591. if($esc_html)
  592. $value = esc_html($value);
  593. if($format == "html")
  594. $value = nl2br($value);
  595. if($url_encode)
  596. $value = urlencode($value);
  597. return $value;
  598. }
  599. public static function replace_variables($text, $form, $lead, $url_encode = false, $esc_html=true, $nl2br = true, $format="html"){
  600. $text = $nl2br ? nl2br($text) : $text;
  601. //Replacing field variables: {FIELD_LABEL:FIELD_ID} {My Field:2}
  602. preg_match_all('/{[^{]*?:(\d+(\.\d+)?)(:(.*?))?}/mi', $text, $matches, PREG_SET_ORDER);
  603. if(is_array($matches))
  604. {
  605. foreach($matches as $match){
  606. $input_id = $match[1];
  607. $field = RGFormsModel::get_field($form,$input_id);
  608. $value = RGFormsModel::get_lead_field_value($lead, $field);
  609. $raw_value = $value;
  610. if(is_array($value))
  611. $value = rgar($value, $input_id);
  612. $value = self::format_variable_value($value, $url_encode, $esc_html, $format);
  613. switch(RGFormsModel::get_input_type($field)){
  614. case "fileupload" :
  615. $value = str_replace(" ", "%20", $value);
  616. break;
  617. case "post_image" :
  618. list($url, $title, $caption, $description) = explode("|:|", $value);
  619. $value = str_replace(" ", "%20", $url);
  620. break;
  621. case "checkbox" :
  622. case "select" :
  623. case "radio" :
  624. $use_value = rgar($match,4) == "value";
  625. $use_price = in_array(rgar($match,4), array("price", "currency"));
  626. $format_currency = rgar($match,4) == "currency";
  627. if(is_array($raw_value) && (string)intval($input_id) != $input_id){
  628. $items = array($input_id => $value); //float input Ids. (i.e. 4.1 ). Used when targeting specific checkbox items
  629. }
  630. else if(is_array($raw_value)){
  631. $items = $raw_value;
  632. }
  633. else{
  634. $items = array($input_id => $raw_value);
  635. }
  636. $ary = array();
  637. foreach($items as $input_id => $item){
  638. if($use_value){
  639. list($val, $price) = rgexplode("|", $item, 2);
  640. }
  641. else if($use_price){
  642. list($name, $val) = rgexplode("|", $item, 2);
  643. if($format_currency)
  644. $val = GFCommon::to_money($val, rgar($lead, "currency"));
  645. }
  646. else if($field["type"] == "post_category"){
  647. $use_id = strtolower(rgar($match,4)) == "id";
  648. $item_value = self::format_post_category($item, $use_id);
  649. $val = RGFormsModel::is_field_hidden($form, $field, array(), $lead) ? "" : $item_value;
  650. }
  651. else{
  652. $val = RGFormsModel::is_field_hidden($form, $field, array(), $lead) ? "" : RGFormsModel::get_choice_text($field, $raw_value, $input_id);
  653. }
  654. $ary[] = self::format_variable_value($val, $url_encode, $esc_html, $format);
  655. }
  656. $value = self::implode_non_blank(", ", $ary);
  657. break;
  658. case "multiselect" :
  659. if($field["type"] == "post_category"){
  660. $use_id = strtolower(rgar($match,4)) == "id";
  661. $items = explode(",", $value);
  662. if(is_array($items)){
  663. $cats = array();
  664. foreach($items as $item){
  665. $cat = self::format_post_category($item, $use_id);
  666. $cats[] = self::format_variable_value($cat, $url_encode, $esc_html, $format);
  667. }
  668. $value = self::implode_non_blank(", ", $cats);
  669. }
  670. }
  671. break;
  672. case "date" :
  673. $value = self::date_display($value, rgar($field,"dateFormat"));
  674. break;
  675. case "total" :
  676. $format_numeric = rgar($match,4) == "price";
  677. $value = $format_numeric ? GFCommon::to_number($value) : GFCommon::to_money($value);
  678. $value = self::format_variable_value($value, $url_encode, $esc_html, $format);
  679. break;
  680. case "post_category" :
  681. $use_id = strtolower(rgar($match,4)) == "id";
  682. $value = self::format_post_category($value, $use_id);
  683. $value = self::format_variable_value($value, $url_encode, $esc_html, $format);
  684. break;
  685. case "list" :
  686. $output_format = in_array(rgar($match,4), array("text", "html", "url")) ? rgar($match,4) : $format;
  687. $value = self::get_lead_field_display($field, $raw_value, $lead["currency"], true, $output_format);
  688. break;
  689. }
  690. if(rgar($match,4) == "label"){
  691. $value = empty($value) ? "" : rgar($field, "label");
  692. }
  693. else if(rgar($match,4) == "qty" && $field["type"] == "product"){
  694. //getting quantity associated with product field
  695. $products = self::get_product_fields($form, $lead, false, false);
  696. $value = 0;
  697. foreach($products["products"] as $product_id => $product)
  698. {
  699. if($product_id == $field["id"])
  700. $value = $product["quantity"];
  701. }
  702. }
  703. //filter can change merge code variable
  704. $value = apply_filters("gform_merge_tag_filter", $value, $input_id, rgar($match,4), $field, $raw_value);
  705. if($value === false)
  706. $value = "";
  707. $text = str_replace($match[0], $value , $text);
  708. }
  709. }
  710. //replacing global variables
  711. //form title
  712. $text = str_replace("{form_title}", $url_encode ? urlencode($form["title"]) : $form["title"], $text);
  713. $matches = array();
  714. preg_match_all("/{all_fields(:(.*?))?}/", $text, $matches, PREG_SET_ORDER);
  715. foreach($matches as $match){
  716. $options = explode(",", rgar($match,2));
  717. $use_value = in_array("value", $options);
  718. $display_empty = in_array("empty", $options);
  719. $use_admin_label = in_array("admin", $options);
  720. //all submitted fields using text
  721. if (strpos($text, $match[0]) !== false){
  722. $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);
  723. }
  724. }
  725. //all submitted fields including empty fields
  726. if (strpos($text, "{all_fields_display_empty}") !== false){
  727. $text = str_replace("{all_fields_display_empty}", self::get_submitted_fields($form, $lead, true, true, $format, false, "all_fields_display_empty"), $text);
  728. }
  729. //pricing fields
  730. if (strpos($text, "{pricing_fields}") !== false){
  731. $text = str_replace("{pricing_fields}", self::get_submitted_pricing_fields($form, $lead, $format), $text);
  732. }
  733. //form id
  734. $text = str_replace("{form_id}", $url_encode ? urlencode($form["id"]) : $form["id"], $text);
  735. //entry id
  736. $text = str_replace("{entry_id}", $url_encode ? urlencode(rgar($lead, "id")) : rgar($lead, "id"), $text);
  737. //entry url
  738. $entry_url = get_bloginfo("wpurl") . "/wp-admin/admin.php?page=gf_entries&view=entry&id=" . $form["id"] . "&lid=" . rgar($lead,"id");
  739. $text = str_replace("{entry_url}", $url_encode ? urlencode($entry_url) : $entry_url, $text);
  740. //post id
  741. $text = str_replace("{post_id}", $url_encode ? urlencode($lead["post_id"]) : $lead["post_id"], $text);
  742. //admin email
  743. $wp_email = get_bloginfo("admin_email");
  744. $text = str_replace("{admin_email}", $url_encode ? urlencode($wp_email) : $wp_email, $text);
  745. //post edit url
  746. $post_url = get_bloginfo("wpurl") . "/wp-admin/post.php?action=edit&post=" . $lead["post_id"];
  747. $text = str_replace("{post_edit_url}", $url_encode ? urlencode($post_url) : $post_url, $text);
  748. $text = self::replace_variables_prepopulate($text, $url_encode);
  749. // hook allows for custom merge tags
  750. $text = apply_filters('gform_replace_merge_tags', $text, $form, $lead, $url_encode, $esc_html, $nl2br, $format);
  751. // TODO: Deprecate the 'gform_replace_merge_tags' and replace it with a call to the 'gform_merge_tag_filter'
  752. //$text = apply_filters('gform_merge_tag_filter', $text, false, false, false );
  753. return $text;
  754. }
  755. public static function format_post_category($value, $use_id){
  756. list($item_value, $item_id) = rgexplode(":", $value, 2);
  757. if($use_id && !empty($item_id))
  758. $item_value = $item_id;
  759. return $item_value;
  760. }
  761. public static function get_embed_post(){
  762. global $embed_post, $post, $wp_query;
  763. if($embed_post){
  764. return $embed_post;
  765. }
  766. if(!rgempty("gform_embed_post")){
  767. $post_id = absint(rgpost("gform_embed_post"));
  768. $embed_post = get_post($post_id);
  769. }
  770. else if($wp_query->is_in_loop){
  771. $embed_post = $post;
  772. }
  773. else{
  774. $embed_post = array();
  775. }
  776. }
  777. public static function replace_variables_prepopulate($text, $url_encode=false){
  778. //embed url
  779. $text = str_replace("{embed_url}", $url_encode ? urlencode(RGFormsModel::get_current_page_url()) : RGFormsModel::get_current_page_url(), $text);
  780. $local_timestamp = self::get_local_timestamp(time());
  781. //date (mm/dd/yyyy)
  782. $local_date_mdy = date_i18n("m/d/Y", $local_timestamp, true);
  783. $text = str_replace("{date_mdy}", $url_encode ? urlencode($local_date_mdy) : $local_date_mdy, $text);
  784. //date (dd/mm/yyyy)
  785. $local_date_dmy = date_i18n("d/m/Y", $local_timestamp, true);
  786. $text = str_replace("{date_dmy}", $url_encode ? urlencode($local_date_dmy) : $local_date_dmy, $text);
  787. //ip
  788. $text = str_replace("{ip}", $url_encode ? urlencode($_SERVER['REMOTE_ADDR']) : $_SERVER['REMOTE_ADDR'], $text);
  789. global $post;
  790. $post_array = self::object_to_array($post);
  791. preg_match_all("/\{embed_post:(.*?)\}/", $text, $matches, PREG_SET_ORDER);
  792. foreach($matches as $match){
  793. $full_tag = $match[0];
  794. $property = $match[1];
  795. $text = str_replace($full_tag, $url_encode ? urlencode($post_array[$property]) : $post_array[$property], $text);
  796. }
  797. //embed post custom fields
  798. preg_match_all("/\{custom_field:(.*?)\}/", $text, $matches, PREG_SET_ORDER);
  799. foreach($matches as $match){
  800. $full_tag = $match[0];
  801. $custom_field_name = $match[1];
  802. $custom_field_value = !empty($post_array["ID"]) ? get_post_meta($post_array["ID"], $custom_field_name, true) : "";
  803. $text = str_replace($full_tag, $url_encode ? urlencode($custom_field_value) : $custom_field_value, $text);
  804. }
  805. //user agent
  806. $text = str_replace("{user_agent}", $url_encode ? urlencode(RGForms::get("HTTP_USER_AGENT", $_SERVER)) : RGForms::get("HTTP_USER_AGENT", $_SERVER), $text);
  807. //referrer
  808. $text = str_replace("{referer}", $url_encode ? urlencode(RGForms::get("HTTP_REFERER", $_SERVER)) : RGForms::get("HTTP_REFERER", $_SERVER), $text);
  809. //logged in user info
  810. global $userdata, $wp_version, $current_user;
  811. $user_array = self::object_to_array($userdata);
  812. preg_match_all("/\{user:(.*?)\}/", $text, $matches, PREG_SET_ORDER);
  813. foreach($matches as $match){
  814. $full_tag = $match[0];
  815. $property = $match[1];
  816. $value = version_compare($wp_version, '3.3', '>=') ? $current_user->get($property) : $user_array[$property];
  817. $value = $url_encode ? urlencode($value) : $value;
  818. $text = str_replace($full_tag, $value, $text);
  819. }
  820. return $text;
  821. }
  822. public static function object_to_array($object){
  823. $array=array();
  824. if(!empty($object)){
  825. foreach($object as $member=>$data)
  826. $array[$member]=$data;
  827. }
  828. return $array;
  829. }
  830. public static function is_empty_array($val){
  831. if(!is_array($val))
  832. $val = array($val);
  833. $ary = array_values($val);
  834. foreach($ary as $item){
  835. if(!rgblank($item))
  836. return false;
  837. }
  838. return true;
  839. }
  840. public static function get_submitted_fields($form, $lead, $display_empty=false, $use_text=false, $format="html", $use_admin_label=false, $merge_tag="", $options=""){
  841. $field_data = "";
  842. if($format == "html"){
  843. $field_data = '<table width="99%" border="0" cellpadding="1" cellspacing="0" bgcolor="#EAEAEA"><tr><td>
  844. <table width="100%" border="0" cellpadding="5" cellspacing="0" bgcolor="#FFFFFF">
  845. ';
  846. }
  847. $options_array = explode(",", $options);
  848. $no_admin = in_array("noadmin", $options_array);
  849. $no_hidden = in_array("nohidden", $options_array);
  850. $has_product_fields = false;
  851. foreach($form["fields"] as $field){
  852. $field_value = "";
  853. $field_label = $use_admin_label && !rgempty("adminLabel", $field) ? rgar($field, "adminLabel") : esc_html(GFCommon::get_label($field));
  854. switch($field["type"]){
  855. case "captcha" :
  856. break;
  857. case "section" :
  858. if((!GFCommon::is_section_empty($field, $form, $lead) || $display_empty) && !rgar($field, "adminOnly")){
  859. switch($format){
  860. case "text" :
  861. $field_value = "--------------------------------\n{$field_label}\n\n";
  862. break;
  863. default:
  864. $field_value = sprintf('<tr>
  865. <td colspan="2" style="font-size:14px; font-weight:bold; background-color:#EEE; border-bottom:1px solid #DFDFDF; padding:7px 7px">%s</td>
  866. </tr>
  867. ', $field_label);
  868. break;
  869. }
  870. }
  871. $field_value = apply_filters("gform_merge_tag_filter", $field_value, $merge_tag, $options, $field, $field_label);
  872. $field_data .= $field_value;
  873. break;
  874. case "password" :
  875. //ignore password fields
  876. break;
  877. default :
  878. //ignore product fields as they will be grouped together at the end of the grid
  879. if(self::is_product_field($field["type"])){
  880. $has_product_fields = true;
  881. continue;
  882. }
  883. else if(RGFormsModel::is_field_hidden($form, $field, array(), $lead)){
  884. //ignore fields hidden by conditional logic
  885. continue;
  886. }
  887. $raw_field_value = RGFormsModel::get_lead_field_value($lead, $field);
  888. $field_value = GFCommon::get_lead_field_display($field, $raw_field_value, rgar($lead,"currency"), $use_text, $format, "email");
  889. $display_field = true;
  890. //depending on parameters, don't display adminOnly or hidden fields
  891. if($no_admin && rgar($field, "adminOnly"))
  892. $display_field = false;
  893. else if($no_hidden && RGFormsModel::get_input_type($field) == "hidden")
  894. $display_field = false;
  895. //if field is not supposed to be displayed, pass false to filter. otherwise, pass field's value
  896. if(!$display_field)
  897. $field_value = false;
  898. $field_value = apply_filters("gform_merge_tag_filter", $field_value, $merge_tag, $options, $field, $raw_field_value);
  899. if($field_value === false)
  900. continue;
  901. if( !empty($field_value) || strlen($field_value) > 0 || $display_empty){
  902. switch($format){
  903. case "text" :
  904. $field_data .= "{$field_label}: {$field_value}\n\n";
  905. break;
  906. default:
  907. $field_data .= sprintf('<tr bgcolor="#EAF2FA">
  908. <td colspan="2">
  909. <font style="font-family: sans-serif; font-size:12px;"><strong>%s</strong></font>
  910. </td>
  911. </tr>
  912. <tr bgcolor="#FFFFFF">
  913. <td width="20">&nbsp;</td>
  914. <td>
  915. <font style="font-family: sans-serif; font-size:12px;">%s</font>
  916. </td>
  917. </tr>
  918. ', $field_label, empty($field_value) && strlen($field_value) == 0 ? "&nbsp;" : $field_value);
  919. break;
  920. }
  921. }
  922. }
  923. }
  924. if($has_product_fields)
  925. $field_data .= self::get_submitted_pricing_fields($form, $lead, $format, $use_text, $use_admin_label);
  926. if($format == "html"){
  927. $field_data .='</table>
  928. </td>
  929. </tr>
  930. </table>';
  931. }
  932. return $field_data;
  933. }
  934. public static function get_submitted_pricing_fields($form, $lead, $format, $use_text=true, $use_admin_label=false){
  935. $form_id = $form["id"];
  936. $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