PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/Avada/includes/class-avada-gravity-forms-tags-merger.php

https://bitbucket.org/senadir/innovate
PHP | 355 lines | 163 code | 68 blank | 124 comment | 40 complexity | 13f9f8dfa8837af49db75c427c3474af MD5 | raw file
Possible License(s): Apache-2.0, MIT, GPL-2.0, GPL-3.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * The Gravity Forms tags merger class.
  4. *
  5. * @author ThemeFusion
  6. * @copyright (c) Copyright by ThemeFusion
  7. * @link http://theme-fusion.com
  8. * @package Avada
  9. * @subpackage Core
  10. * @since 5.1.0
  11. */
  12. // Do not allow directly accessing this file.
  13. if ( ! defined( 'ABSPATH' ) ) {
  14. exit( 'Direct script access denied.' );
  15. }
  16. /**
  17. * The Gravity Forms tags merger class.
  18. */
  19. class Avada_Gravity_Forms_Tags_Merger {
  20. /**
  21. * The Gravity Form entry name.
  22. *
  23. * @static
  24. * @access public
  25. * @since 5.1
  26. * @var string
  27. */
  28. public static $_entry = null;
  29. /**
  30. * The object.
  31. *
  32. * @static
  33. * @access private
  34. * @since 5.1
  35. * @var Object
  36. */
  37. private static $instance = false;
  38. /**
  39. * The class constructor.
  40. *
  41. * @access public
  42. * @param array $args Array of bool auto_append_eid and encrypt_eid.
  43. */
  44. public function __construct( $args ) {
  45. if ( ! class_exists( 'GFForms' ) ) {
  46. return;
  47. }
  48. $this->_args = wp_parse_args(
  49. $args, array(
  50. 'auto_append_eid' => true, // Boolean or array of form IDs.
  51. 'encrypt_eid' => true,
  52. )
  53. );
  54. add_filter( 'the_content', array( $this, 'replace_merge_tags' ), 1 );
  55. add_filter( 'gform_replace_merge_tags', array( $this, 'replace_encrypt_entry_id_merge_tag' ), 10, 3 );
  56. if ( ! empty( $this->_args['auto_append_eid'] ) ) {
  57. add_filter( 'gform_confirmation', array( $this, 'append_eid_parameter' ), 20, 3 );
  58. }
  59. }
  60. /**
  61. * Creates or returns an instance of this class.
  62. *
  63. * @static
  64. * @access public
  65. * @since 5.1
  66. * @param array $args Array of bool auto_append_eid and encrypt_eid.
  67. * @return object $instance Instance of this class.
  68. */
  69. public static function get_instance( $args = array() ) {
  70. if ( null == self::$instance ) {
  71. self::$instance = new self( $args );
  72. }
  73. return self::$instance;
  74. }
  75. /**
  76. * Replaces the merged tags.
  77. *
  78. * @access public
  79. * @since 5.1
  80. * @param string $post_content Content of the post.
  81. * @return string $post_content Content of the post with variables replaced.
  82. */
  83. public function replace_merge_tags( $post_content ) {
  84. $entry = $this->get_entry();
  85. if ( ! $entry ) {
  86. return $post_content;
  87. }
  88. $form = GFFormsModel::get_form_meta( $entry['form_id'] );
  89. $post_content = $this->replace_field_label_merge_tags( $post_content, $form );
  90. $post_content = GFCommon::replace_variables( $post_content, $form, $entry, false, false, false );
  91. return $post_content;
  92. }
  93. /**
  94. * Replaces field labels in text.
  95. *
  96. * @access public
  97. * @since 5.1
  98. * @param string $text Content of the post.
  99. * @param string $form Content of the form.
  100. * @return string $text Updated content of the post.
  101. */
  102. public function replace_field_label_merge_tags( $text, $form ) {
  103. preg_match_all( '/{([^:]+?)}/', $text, $matches, PREG_SET_ORDER );
  104. if ( empty( $matches ) ) {
  105. return $text;
  106. }
  107. foreach ( $matches as $match ) {
  108. list( $search, $field_label ) = $match;
  109. foreach ( $form['fields'] as $field ) {
  110. $full_input_id = false;
  111. $matches_admin_label = rgar( $field, 'adminLabel' ) == $field_label;
  112. $matches_field_label = false;
  113. if ( is_array( $field['inputs'] ) ) {
  114. foreach ( $field['inputs'] as $input ) {
  115. if ( GFFormsModel::get_label( $field, $input['id'] ) == $field_label ) {
  116. $matches_field_label = true;
  117. $input_id = $input['id'];
  118. break;
  119. }
  120. }
  121. } else {
  122. $matches_field_label = GFFormsModel::get_label( $field ) == $field_label;
  123. $input_id = $field['id'];
  124. }
  125. if ( ! $matches_admin_label && ! $matches_field_label ) {
  126. continue;
  127. }
  128. $replace = sprintf( '{%s:%s}', $field_label, (string) $input_id );
  129. $text = str_replace( $search, $replace, $text );
  130. break;
  131. }
  132. } // End foreach().
  133. return $text;
  134. }
  135. /**
  136. * Replaces encrypted entry id.
  137. *
  138. * @access public
  139. * @since 5.1
  140. * @param string $text Content of the post.
  141. * @param string $form Content of the form.
  142. * @param object $entry Form entry object.
  143. * @return string $text Entry content.
  144. */
  145. public function replace_encrypt_entry_id_merge_tag( $text, $form, $entry ) {
  146. if ( false === strpos( $text, '{encrypted_entry_id}' ) ) {
  147. return $text;
  148. }
  149. // $entry is not always a "full" entry.
  150. $entry_id = rgar( $entry, 'id' );
  151. if ( $entry_id ) {
  152. $entry_id = $this->prepare_eid( $entry['id'], true );
  153. }
  154. return str_replace( '{encrypted_entry_id}', $entry_id, $text );
  155. }
  156. /**
  157. * Appends eid parameter if enabled.
  158. *
  159. * @access public
  160. * @since 5.1
  161. * @param string|array $confirmation Message or content.
  162. * @param string $form Content of the form.
  163. * @param object $entry Form entry object.
  164. * @return string|array $confirmation Confirmation content.
  165. */
  166. public function append_eid_parameter( $confirmation, $form, $entry ) {
  167. $is_ajax_redirect = is_string( $confirmation ) && strpos( $confirmation, 'gformRedirect' );
  168. $is_redirect = is_array( $confirmation ) && isset( $confirmation['redirect'] );
  169. if ( ! $this->is_auto_eid_enabled( $form ) || ! ( $is_ajax_redirect || $is_redirect ) ) {
  170. return $confirmation;
  171. }
  172. $eid = $this->prepare_eid( $entry['id'] );
  173. if ( $is_ajax_redirect ) {
  174. preg_match_all( '/gformRedirect.+?(http.+?)(?=\'|")/', $confirmation, $matches, PREG_SET_ORDER );
  175. list( $full_match, $url ) = $matches[0];
  176. $redirect_url = add_query_arg(
  177. array(
  178. 'eid' => $eid,
  179. ), $url
  180. );
  181. $confirmation = str_replace( $url, $redirect_url, $confirmation );
  182. } else {
  183. $redirect_url = add_query_arg(
  184. array(
  185. 'eid' => $eid,
  186. ), $confirmation['redirect']
  187. );
  188. $confirmation['redirect'] = $redirect_url;
  189. }
  190. return $confirmation;
  191. }
  192. /**
  193. * Prepares eid if enabled.
  194. *
  195. * @access public
  196. * @since 5.1
  197. * @param string $entry_id ID of the form entry.
  198. * @param bool $force_encrypt Flag for encryption enforcement.
  199. * @return string $eid updated Form entry ID.
  200. */
  201. public function prepare_eid( $entry_id, $force_encrypt = false ) {
  202. $eid = $entry_id;
  203. $do_encrypt = $force_encrypt || $this->_args['encrypt_eid'];
  204. if ( $do_encrypt && is_callable( array( 'GFCommon', 'encrypt' ) ) ) {
  205. $eid = rawurlencode( GFCommon::encrypt( $eid ) );
  206. }
  207. return $eid;
  208. }
  209. /**
  210. * Gets form entry.
  211. *
  212. * @access public
  213. * @since 5.1
  214. * @return bool|object $_entryEntry Object and false if none available.
  215. */
  216. public function get_entry() {
  217. if ( ! self::$_entry ) {
  218. $entry_id = $this->get_entry_id();
  219. if ( ! $entry_id ) {
  220. return false;
  221. }
  222. $entry = GFFormsModel::get_lead( $entry_id );
  223. if ( empty( $entry ) ) {
  224. return false;
  225. }
  226. self::$_entry = $entry;
  227. }
  228. return self::$_entry;
  229. }
  230. /**
  231. * Gets ID of form entry.
  232. *
  233. * @access public
  234. * @since 5.1
  235. * @return string|bool $entry_id ID of the entry or false.
  236. */
  237. public function get_entry_id() {
  238. $entry_id = rgget( 'eid' );
  239. if ( $entry_id ) {
  240. return $this->maybe_decrypt_entry_id( $entry_id );
  241. }
  242. $post = get_post();
  243. if ( $post ) {
  244. $entry_id = get_post_meta( $post->ID, '_gform-entry-id', true );
  245. }
  246. return $entry_id ? $entry_id : false;
  247. }
  248. /**
  249. * Decrypts entry ID of form if encryption is enabled.
  250. *
  251. * @access public
  252. * @since 5.1
  253. * @param string $entry_id ID of the form entry.
  254. * @return int|string $entry_id Decrypted ID of the entry.
  255. */
  256. public function maybe_decrypt_entry_id( $entry_id ) {
  257. // if encryption is enabled, 'eid' parameter MUST be encrypted.
  258. $do_encrypt = $this->_args['encrypt_eid'];
  259. if ( ! $entry_id ) {
  260. return null;
  261. } elseif ( ! $do_encrypt && is_numeric( $entry_id ) && intval( $entry_id ) > 0 ) {
  262. return $entry_id;
  263. } else {
  264. $entry_id = is_callable( array( 'GFCommon', 'decrypt' ) ) ? GFCommon::decrypt( $entry_id ) : $entry_id;
  265. return intval( $entry_id );
  266. }
  267. }
  268. /**
  269. * Checks if auto entry ID is enabled.
  270. *
  271. * @access public
  272. * @since 5.1
  273. * @param string $form Content of the form.
  274. * @return bool
  275. */
  276. public function is_auto_eid_enabled( $form ) {
  277. $auto_append_eid = $this->_args['auto_append_eid'];
  278. if ( is_bool( $auto_append_eid ) && true === $auto_append_eid ) {
  279. return true;
  280. }
  281. if ( is_array( $auto_append_eid ) && in_array( $form['id'], $auto_append_eid ) ) {
  282. return true;
  283. }
  284. return false;
  285. }
  286. }
  287. /* Omit closing PHP tag to avoid "Headers already sent" issues. */