MailChimp_Subscriber_Popup

Class MailChimp_Subscriber_Popup.

Defined (1)

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

/modules/shortcodes/mailchimp.php  
  1. class MailChimp_Subscriber_Popup { 
  2.  
  3. /** 
  4. * Regular expressions to reverse script tags to shortcodes. 
  5. * @var array 
  6. */ 
  7. static $reversal_regexes = array( 
  8. /** raw examplejs */ 
  9. '/<script type="text\/javascript" src="(https?:)?\/\/s3\.amazonaws.com\/downloads\.mailchimp\.com\/js\/signup-forms\/popup\/embed\.js" data-dojo-config="([^"]*?)"><\/script><script type="text\/javascript">require\(\["mojo\/signup-forms\/Loader"\]\, function\(L\) { L\.start\({([^}]*?)}\) }\)<\/script>/s',  
  10. /** visual editor */ 
  11. '/<script type="text\/javascript" src="(https?:)?\/\/s3\.amazonaws.com\/downloads\.mailchimp\.com\/js\/signup-forms\/popup\/embed\.js" data-dojo-config="([^"]*?)"><\/script><script type="text\/javascript">require\(\["mojo\/signup-forms\/Loader"]\, function\(L\) { L\.start\({([^}]*?)}\) }\)<\/script>/s',  
  12. ); 
  13.  
  14. /** 
  15. * Allowed configuration attributes. Used in reversal when checking allowed attributes. 
  16. * @var array 
  17. */ 
  18. static $allowed_config = array( 
  19. 'usePlainJson' => 'true',  
  20. 'isDebug' => 'false',  
  21. ); 
  22.  
  23. /** 
  24. * Allowed JS variables. Used in reversal to whitelist variables. 
  25. * @var array 
  26. */ 
  27. static $allowed_js_vars = array( 
  28. 'baseUrl',  
  29. 'uuid',  
  30. 'lid',  
  31. ); 
  32.  
  33. /** 
  34. * Runs the whole reversal. 
  35. * @since 4.5.0 
  36. * @param string $content Post Content 
  37. * @return string Content with embeds replaced 
  38. */ 
  39. static function reversal( $content ) { 
  40. // Bail without the js src 
  41. if ( ! is_string( $content ) || false === stripos( $content, 'downloads.mailchimp.com/js/signup-forms/popup/embed.js' ) ) { 
  42. return $content; 
  43.  
  44. require_once( ABSPATH . WPINC . '/class-json.php' ); 
  45. $wp_json = new Services_JSON(); 
  46.  
  47. // loop through our rules and find valid embeds 
  48. foreach ( self::$reversal_regexes as $regex ) { 
  49.  
  50. if ( ! preg_match_all( $regex, $content, $matches ) ) { 
  51. continue; 
  52.  
  53. foreach ( $matches[3] as $index => $js_vars ) { 
  54. // the regex rule for a specific embed 
  55. $replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $matches[0][$index], '#' ) ); 
  56.  
  57. $attrs = $wp_json->decode( '{' . $js_vars . '}' ); 
  58.  
  59. if ( $matches[2][$index] ) { 
  60. $config_attrs = $wp_json->decode( '{' . $matches[2][$index] . '}' ); 
  61. foreach ( $config_attrs as $key => $value ) { 
  62. $attrs->$key = ( 1 == $value ) ? 'true' : 'false'; 
  63.  
  64. $shortcode = self::build_shortcode_from_reversal_attrs( $attrs ); 
  65.  
  66. $content = preg_replace( $replace_regex, "\n\n$shortcode\n\n", $content ); 
  67.  
  68. /** This action is documented in modules/widgets/social-media-icons.php */ 
  69. do_action( 'jetpack_bump_stats_extras', 'html_to_shortcode', 'mailchimp_subscriber_popup' ); 
  70.  
  71. return $content; 
  72.  
  73. /** 
  74. * Builds the actual shortcode based on passed in attributes. 
  75. * @since 4.5.0 
  76. * @param array $attrs A valid list of attributes (gets matched against self::$allowed_config and self::$allowed_js_vars) 
  77. * @return string 
  78. */ 
  79. static function build_shortcode_from_reversal_attrs( $attrs ) { 
  80. $shortcode = '[mailchimp_subscriber_popup '; 
  81.  
  82. foreach ( $attrs as $key => $value ) { 
  83. // skip unsupported keys 
  84. if ( ! in_array( $key, array_keys( self::$allowed_config ) ) && ! in_array( $key, self::$allowed_js_vars ) ) { 
  85. continue; 
  86.  
  87. $value = esc_attr( $value ); 
  88. $shortcode .= "$key='$value' "; 
  89. return trim( $shortcode ) . ']'; 
  90.  
  91. /** 
  92. * Parses the shortcode back out to embedded information. 
  93. * @since 4.5.0 
  94. * @param array $lcase_attrs 
  95. * @return string 
  96. */ 
  97. static function shortcode( $lcase_attrs ) { 
  98. static $displayed_once = false; 
  99.  
  100. // Limit to one form per page load 
  101. if ( $displayed_once ) { 
  102. return ''; 
  103.  
  104. if ( empty( $lcase_attrs ) ) { 
  105. return '<!-- Missing MailChimp baseUrl, uuid or lid -->'; 
  106.  
  107. $defaults = array_fill_keys( self::$allowed_js_vars, '' ); 
  108. $defaults = array_merge( $defaults, self::$allowed_config ); 
  109.  
  110. // Convert $attrs back to proper casing since they come through in all lowercase 
  111. $attrs = array(); 
  112. foreach ( $defaults as $key => $value ) { 
  113. if ( array_key_exists( strtolower( $key ), $lcase_attrs ) ) { 
  114. $attrs[ $key ] = $lcase_attrs[ strtolower( $key ) ]; 
  115. $attrs = array_map( 'esc_js', array_filter( shortcode_atts( $defaults, $attrs ) ) ); 
  116.  
  117. // Split config & js vars 
  118. $config_vars = $js_vars = array(); 
  119. foreach ( $attrs as $key => $value ) { 
  120. if ( in_array( $key, self::$allowed_js_vars ) ) { 
  121. $js_vars[ $key ] = $value; 
  122. } else { 
  123. $config_vars[] = "$key: $value"; 
  124.  
  125. // If one of these parameters is missing we can't render the form so exist. 
  126. if ( empty( $js_vars['baseUrl'] ) || empty( $js_vars['uuid'] ) || empty( $js_vars['lid'] ) ) { 
  127. return '<!-- Missing MailChimp baseUrl, uuid or lid -->'; 
  128.  
  129. /** This action is already documented in modules/widgets/gravatar-profile.php */ 
  130. do_action( 'jetpack_stats_extra', 'mailchimp_subscriber_popup', 'view' ); 
  131.  
  132. $displayed_once = true; 
  133.  
  134. return "\n\n" . '<script type="text/javascript" data-dojo-config="' . esc_attr( implode( ', ', $config_vars ) ) . '">jQuery.getScript( "//s3.amazonaws.com/downloads.mailchimp.com/js/signup-forms/popup/embed.js", function( data, textStatus, jqxhr ) { require(["mojo/signup-forms/Loader"], function(L) { L.start(' . wp_json_encode( $js_vars ) . ') }); } );</script>' . "\n\n";