/wp-content/plugins/mailchimp-for-wp/includes/forms/class-form-tags.php

https://gitlab.com/bhargavi_dcw/dflocal · PHP · 263 lines · 131 code · 45 blank · 87 comment · 6 complexity · 018edbb7d73abd4d5c1b96033584aea7 MD5 · raw file

  1. <?php
  2. /**
  3. * Class MC4WP_Form_Tags
  4. *
  5. * @access private
  6. * @ignore
  7. */
  8. class MC4WP_Form_Tags {
  9. /**
  10. * @var MC4WP_Dynamic_Content_Tags
  11. */
  12. protected $tags;
  13. /**
  14. * @var MC4WP_Form
  15. */
  16. protected $form;
  17. /**
  18. * @var MC4WP_Form_Element
  19. */
  20. protected $form_element;
  21. /**
  22. * Constructor
  23. */
  24. public function __construct() {
  25. $this->tags = new MC4WP_Dynamic_Content_Tags( 'form' );
  26. }
  27. public function add_hooks() {
  28. add_filter( 'mc4wp_dynamic_content_tags_form', array( $this, 'register' ) );
  29. add_filter( 'mc4wp_form_response_html', array( $this, 'replace' ), 10, 2 );
  30. add_filter( 'mc4wp_form_content', array( $this, 'replace' ), 10, 3 );
  31. add_filter( 'mc4wp_form_redirect_url', array( $this, 'replace_in_url' ), 10, 2 );
  32. }
  33. /**
  34. * @return array
  35. */
  36. public function get() {
  37. return $this->tags->all();
  38. }
  39. /**
  40. * @param array $tags
  41. * @return array
  42. */
  43. public function register( array $tags ) {
  44. /**
  45. * @var MC4WP_Request
  46. */
  47. $request = mc4wp('request');
  48. $tags['response'] = array(
  49. 'description' => __( 'Replaced with the form response (error or success messages).', 'mailchimp-for-wp' ),
  50. 'callback' => array( $this, 'get_form_response' )
  51. );
  52. $tags['data'] = array(
  53. 'description' => sprintf( __( "Data from the URL or a submitted form.", 'mailchimp-for-wp' ) ),
  54. 'callback' => array( $this, 'get_data' ),
  55. 'example' => "data key='UTM_SOURCE' default='Default Source'"
  56. );
  57. $tags['subscriber_count'] = array(
  58. 'description' => __( 'Replaced with the number of subscribers on the selected list(s)', 'mailchimp-for-wp' ),
  59. 'callback' => array( $this, 'get_subscriber_count' )
  60. );
  61. $tags['email'] = array(
  62. 'description' => __( 'The email address of the current visitor (if known).', 'mailchimp-for-wp' ),
  63. 'callback' => array( $this, 'get_email' ),
  64. );
  65. $tags['current_url'] = array(
  66. 'description' => __( 'The URL of the page.', 'mailchimp-for-wp' ),
  67. 'callback' => 'mc4wp_get_current_url',
  68. );
  69. $tags['current_path'] = array(
  70. 'description' => __( 'The path of the page.', 'mailchimp-for-wp' ),
  71. 'callback' => array( $request, 'get_url' ),
  72. );
  73. $tags['date'] = array(
  74. 'description' => sprintf( __( 'The current date. Example: %s.', 'mailchimp-for-wp' ), '<strong>' . date( 'Y/m/d' ) . '</strong>' ),
  75. 'replacement' => date( 'Y/m/d' )
  76. );
  77. $tags['time'] = array(
  78. 'description' => sprintf( __( 'The current time. Example: %s.', 'mailchimp-for-wp' ), '<strong>' . date( 'H:i:s' ) . '</strong>'),
  79. 'replacement' => date( 'H:i:s' )
  80. );
  81. $tags['language'] = array(
  82. 'description' => sprintf( __( 'The site\'s language. Example: %s.', 'mailchimp-for-wp' ), '<strong>' . get_locale() . '</strong>' ),
  83. 'callback' => 'get_locale',
  84. );
  85. $tags['ip'] = array(
  86. 'description' => sprintf( __( 'The visitor\'s IP address. Example: %s.', 'mailchimp-for-wp' ), '<strong>' . mc4wp('request')->get_client_ip() . '</strong>' ),
  87. 'callback' => array( $request, 'get_client_ip' )
  88. );
  89. $tags['user'] = array(
  90. 'description' => sprintf( __( "The property of the currently logged-in user.", 'mailchimp-for-wp' ) ),
  91. 'callback' => array( $this, 'get_user_property' ),
  92. 'example' => "user property='user_email'"
  93. );
  94. $tags['post'] = array(
  95. 'description' => sprintf( __( "Property of the current page or post.", 'mailchimp-for-wp' ) ),
  96. 'callback' => array( $this, 'get_post_property' ),
  97. 'example' => "post property='ID'"
  98. );
  99. return $tags;
  100. }
  101. /**
  102. * Replaces the registered tags in the given string
  103. *
  104. * @hooked `mc4wp_form_message_html`
  105. * @hooked `mc4wp_form_content`
  106. *
  107. * @param string $string
  108. * @param MC4WP_Form $form
  109. * @param MC4WP_Form_Element $element
  110. *
  111. * @return string
  112. */
  113. public function replace( $string, MC4WP_Form $form, MC4WP_Form_Element $element = null ) {
  114. $this->form = $form;
  115. $this->form_element = $element;
  116. $string = $this->tags->replace( $string );
  117. return $string;
  118. }
  119. /**
  120. * @hooked `mc4wp_form_redirect_url`
  121. *
  122. * @param $string
  123. * @param MC4WP_Form $form
  124. *
  125. * @return string
  126. */
  127. public function replace_in_url( $string, MC4WP_Form $form ) {
  128. $this->form = $form;
  129. $string = $this->tags->replace_in_url( $string );
  130. return $string;
  131. }
  132. /**
  133. * Returns the number of subscribers on the selected lists (for the form context)
  134. *
  135. * @return int
  136. */
  137. public function get_subscriber_count() {
  138. $mailchimp = new MC4WP_MailChimp();
  139. $count = $mailchimp->get_subscriber_count( $this->form->get_lists() );
  140. return number_format( $count );
  141. }
  142. /**
  143. * Returns the form response
  144. *
  145. * @return string
  146. */
  147. public function get_form_response() {
  148. if( $this->form_element instanceof MC4WP_Form_Element ) {
  149. return $this->form_element->get_response_html();
  150. }
  151. return '';
  152. }
  153. /**
  154. *
  155. * @param $args
  156. *
  157. * @return string
  158. */
  159. public function get_data( $args = array() ) {
  160. if( empty( $args['key'] ) ) {
  161. return '';
  162. }
  163. $default = isset( $args['default'] ) ? $args['default'] : '';
  164. /**
  165. * @var MC4WP_Request $request
  166. */
  167. $request = mc4wp('request');
  168. return esc_html( $request->params->get( $args['key'], $default ) );
  169. }
  170. /*
  171. * Get property of currently logged-in user
  172. *
  173. * @param array $args
  174. *
  175. * @return string
  176. */
  177. public function get_user_property( $args = array() ) {
  178. $property = empty( $args['property'] ) ? 'user_email' : $args['property'];
  179. $user = wp_get_current_user();
  180. if( $user instanceof WP_User ) {
  181. return $user->{$property};
  182. }
  183. return '';
  184. }
  185. /*
  186. * Get property of viewed post
  187. *
  188. * @param array $args
  189. *
  190. * @return string
  191. */
  192. public function get_post_property( $args = array() ) {
  193. $property = empty( $args['property'] ) ? 'ID' : $args['property'];
  194. global $post;
  195. if( $post instanceof WP_Post ) {
  196. return $post->{$property};
  197. }
  198. return '';
  199. }
  200. /**
  201. * @return string
  202. */
  203. public function get_email() {
  204. // first, try request
  205. $request = mc4wp('request');
  206. $email = $request->params->get( 'EMAIL', '' );
  207. if( $email ) {
  208. return $email;
  209. }
  210. // then , try logged-in user
  211. if( is_user_logged_in() ) {
  212. $user = wp_get_current_user();
  213. return $user->user_email;
  214. }
  215. // TODO: Read from cookie? Or add $_COOKIE support to {data} tag?
  216. return '';
  217. }
  218. }