MC4WP_Tools

Class MC4WP_Tools.

Defined (1)

The class is defined in the following location(s).

/includes/class-tools.php  
  1. class MC4WP_Tools { 
  2.  
  3. /** 
  4. * @param array $merge_vars 
  5. * @return mixed 
  6. */ 
  7. public static function guess_merge_vars( array $merge_vars ) { 
  8.  
  9. // maybe guess first and last name 
  10. if ( isset( $merge_vars['NAME'] ) ) { 
  11. if( ! isset( $merge_vars['FNAME'] ) && ! isset( $merge_vars['LNAME'] ) ) { 
  12. $strpos = strpos( $merge_vars['NAME'], ' ' ); 
  13. if ( $strpos !== false ) { 
  14. $merge_vars['FNAME'] = trim( substr( $merge_vars['NAME'], 0, $strpos ) ); 
  15. $merge_vars['LNAME'] = trim( substr( $merge_vars['NAME'], $strpos ) ); 
  16. } else { 
  17. $merge_vars['FNAME'] = $merge_vars['NAME']; 
  18.  
  19. return $merge_vars; 
  20.  
  21. /** 
  22. * Returns text with {variables} replaced. 
  23. * @param string $string 
  24. * @param array $additional_replacements 
  25. * @param array Array of list ID's (needed if {subscriber_count} is set 
  26. * @return string $text The text with {variables} replaced. 
  27. * replaced. 
  28. */ 
  29. public static function replace_variables( $string, $additional_replacements = array(), $list_ids = array() ) { 
  30.  
  31. // replace general vars 
  32. $replacements = array( 
  33. '{ip}' => self::get_client_ip(),  
  34. '{current_url}' => mc4wp_get_current_url(),  
  35. '{current_path}' => ( ! empty( $_SERVER['REQUEST_URI'] ) ) ? esc_html( $_SERVER['REQUEST_URI'] ) : '',  
  36. '{date}' => date( 'm/d/Y' ),  
  37. '{time}' => date( 'H:i:s' ),  
  38. '{language}' => defined( 'ICL_LANGUAGE_CODE' ) ? ICL_LANGUAGE_CODE : get_locale(),  
  39. '{email}' => self::get_known_email(),  
  40. '{user_email}' => '',  
  41. '{user_firstname}' => '',  
  42. '{user_lastname}' => '',  
  43. '{user_name}' => '',  
  44. '{user_id}' => '',  
  45. ); 
  46.  
  47. // setup replacements for logged-in users 
  48. if ( is_user_logged_in() 
  49. && ( $user = wp_get_current_user() ) 
  50. && ( $user instanceof WP_User ) ) { 
  51.  
  52. // logged in user, replace vars by user vars 
  53. $replacements['{user_email}'] = $user->user_email; 
  54. $replacements['{user_firstname}'] = $user->first_name; 
  55. $replacements['{user_lastname}'] = $user->last_name; 
  56. $replacements['{user_name}'] = $user->display_name; 
  57. $replacements['{user_id}'] = $user->ID; 
  58.  
  59. // merge with additional replacements 
  60. $replacements = array_merge( $replacements, $additional_replacements ); 
  61.  
  62. // subscriber count? only fetch these if the tag is actually used 
  63. if ( stristr( $string, '{subscriber_count}' ) !== false ) { 
  64. $mailchimp = new MC4WP_MailChimp(); 
  65. $subscriber_count = $mailchimp->get_subscriber_count( $list_ids ); 
  66. $replacements['{subscriber_count}'] = $subscriber_count; 
  67.  
  68. // perform the replacement 
  69. $string = str_ireplace( array_keys( $replacements ), array_values( $replacements ), $string ); 
  70.  
  71. // replace dynamic variables 
  72. if( stristr( $string, '{data_' ) !== false ) { 
  73. $string = preg_replace_callback('/\{data_([\w-.]+)( default=\"([^"]*)\") {0, 1}\}/', array( 'MC4WP_Tools', 'replace_request_data_variables' ), $string ); 
  74.  
  75. return $string; 
  76.  
  77.  
  78. /** 
  79. * @param $matches 
  80. * @return string 
  81. */ 
  82. public static function replace_request_data_variables( $matches ) { 
  83.  
  84. $variable = strtoupper( $matches[1] ); 
  85. $default = ( ! empty( $matches[3] ) ) ? $matches[3] : ''; 
  86.  
  87. $request_data = array_change_key_case( $_REQUEST, CASE_UPPER ); 
  88.  
  89. if( isset( $request_data[ $variable ] ) && is_scalar( $request_data[ $variable ] ) ) { 
  90. return esc_html( $request_data[ $variable ] ); 
  91.  
  92. return $default; 
  93.  
  94. /** 
  95. * Returns the email address of the visitor if it is known to us 
  96. * @return string 
  97. */ 
  98. public static function get_known_email() { 
  99.  
  100. // case insensitive check in $_REQUEST 
  101. $request_data = array_change_key_case( $_REQUEST, CASE_LOWER ); 
  102.  
  103. if( isset( $request_data['email'] ) ) { 
  104. $email = $request_data['email']; 
  105. } elseif( isset( $request_data['mc4wp_email'] ) ) { 
  106. $email = $request_data['mc4wp_email']; 
  107. } elseif( isset( $_COOKIE['mc4wp_email'] ) ) { 
  108. $email = $_COOKIE['mc4wp_email']; 
  109. } else { 
  110. $email = ''; 
  111.  
  112. return strip_tags( $email ); 
  113.  
  114. /** 
  115. * Returns the IP address of the visitor, does not take proxies into account. 
  116. * @return string 
  117. */ 
  118. public static function get_client_ip() { 
  119.  
  120. $headers = ( function_exists( 'apache_request_headers' ) ) ? apache_request_headers() : $_SERVER; 
  121.  
  122. if ( array_key_exists( 'X-Forwarded-For', $headers ) && filter_var( $headers['X-Forwarded-For'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ) { 
  123. $ip = $headers['X-Forwarded-For']; 
  124. } elseif ( array_key_exists( 'HTTP_X_FORWARDED_FOR', $headers ) && filter_var( $headers['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ) ) { 
  125. $ip = $headers['HTTP_X_FORWARDED_FOR']; 
  126. } else { 
  127. $ip = filter_var( $_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 ); 
  128.  
  129. return $ip; 
  130.  
  131. /** 
  132. * @param $email 
  133. */ 
  134. public static function remember_email( $email ) { 
  135.  
  136. /** 
  137. * @filter `mc4wp_cookie_expiration_time` 
  138. * @expects timestamp 
  139. * @default timestamp for 90 days from now 
  140. * Timestamp indicating when the email cookie expires, defaults to 90 days 
  141. */ 
  142. $expiration_time = apply_filters( 'mc4wp_cookie_expiration_time', strtotime( '+90 days' ) ); 
  143.  
  144. setcookie( 'mc4wp_email', $email, $expiration_time, '/' ); 
  145.