PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.1.5
Jetpack – WP Security, Backup, Speed, & Growth v7.1.5
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / shortcodes / mailchimp.php

mailchimp.php in Jetpack – WP Security, Backup, Speed, & Growth 7.1.5, at modules/shortcodes/mailchimp.php

214 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MailChimp Subscriber Popup Form shortcode
4 *
5 * Example:
6 * [mailchimp_subscriber_popup baseUrl="mc.us11.list-manage.com" uuid="1ca7856462585a934b8674c71" lid="2d24f1898b"]
7 *
8 * Embed code example:
9 * <script type="text/javascript" src="//downloads.mailchimp.com/js/signup-forms/popup/unique-methods/embed.js" data-dojo-config="usePlainJson: true, isDebug: false"></script><script type="text/javascript">window.dojoRequire(["mojo/signup-forms/Loader"], function(L) { L.start({"baseUrl":"mc.us11.list-manage.com","uuid":"1ca7856462585a934b8674c71","lid":"2d24f1898b","uniqueMethods":true}) })</script>
10 *
11 */
12
13 /**
14 * Register [mailchimp_subscriber_popup] shortcode and add a filter to 'pre_kses' queue to reverse MailChimp embed to shortcode.
15 *
16 * @since 4.5.0
17 */
18 function jetpack_mailchimp_subscriber_popup() {
19 add_shortcode(
20 'mailchimp_subscriber_popup',
21 array(
22 'MailChimp_Subscriber_Popup',
23 'shortcode',
24 )
25 );
26 add_filter(
27 'pre_kses',
28 array(
29 'MailChimp_Subscriber_Popup',
30 'reversal',
31 )
32 );
33 }
34
35 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
36 add_action( 'init', 'jetpack_mailchimp_subscriber_popup' );
37 } else {
38 jetpack_mailchimp_subscriber_popup();
39 }
40
41 /**
42 * Class MailChimp_Subscriber_Popup
43 *
44 * @since 4.5.0
45 */
46 class MailChimp_Subscriber_Popup {
47
48 /**
49 * Regular expressions to reverse script tags to shortcodes.
50 *
51 * @var array
52 */
53 static $reversal_regexes = array(
54 /* raw examplejs */
55 '/<script type="text\/javascript" src="(https?:)?\/\/downloads\.mailchimp\.com\/js\/signup-forms\/popup\/unique-methods\/embed\.js" data-dojo-config="([^"]*?)"><\/script><script type="text\/javascript">window.dojoRequire\(\["mojo\/signup-forms\/Loader"\]\, function\(L\) { L\.start\({([^}]*?)}\) }\)<\/script>/s',
56 /* visual editor */
57 '/&lt;script type="text\/javascript" src="(https?:)?\/\/downloads\.mailchimp\.com\/js\/signup-forms\/popup\/unique-methods\/embed\.js" data-dojo-config="([^"]*?)"&gt;&lt;\/script&gt;&lt;script type="text\/javascript"&gt;window.dojoRequire\(\["mojo\/signup-forms\/Loader"]\, function\(L\) { L\.start\({([^}]*?)}\) }\)&lt;\/script&gt;/s',
58 );
59
60 /**
61 * Allowed configuration attributes. Used in reversal when checking allowed attributes.
62 *
63 * @var array
64 */
65 static $allowed_config = array(
66 'usePlainJson' => 'true',
67 'isDebug' => 'false',
68 );
69
70 /**
71 * Allowed JS variables. Used in reversal to whitelist variables.
72 *
73 * @var array
74 */
75 static $allowed_js_vars = array(
76 'baseUrl',
77 'uuid',
78 'lid',
79 );
80
81 /**
82 * Runs the whole reversal.
83 *
84 * @since 4.5.0
85 *
86 * @param string $content Post Content
87 *
88 * @return string Content with embeds replaced
89 */
90 static function reversal( $content ) {
91 // Bail without the js src
92 if ( ! is_string( $content ) || false === stripos( $content, 'downloads.mailchimp.com/js/signup-forms/popup/unique-methods/embed.js' ) ) {
93 return $content;
94 }
95
96 require_once ABSPATH . WPINC . '/class-json.php';
97 $wp_json = new Services_JSON();
98
99 // loop through our rules and find valid embeds
100 foreach ( self::$reversal_regexes as $regex ) {
101
102 if ( ! preg_match_all( $regex, $content, $matches ) ) {
103 continue;
104 }
105
106 foreach ( $matches[3] as $index => $js_vars ) {
107 // the regex rule for a specific embed
108 $replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $matches[0][ $index ], '#' ) );
109
110 $attrs = $wp_json->decode( '{' . $js_vars . '}' );
111
112 if ( $matches[2][ $index ] ) {
113 $config_attrs = $wp_json->decode( '{' . $matches[2][ $index ] . '}' );
114 foreach ( $config_attrs as $key => $value ) {
115 $attrs->$key = ( 1 == $value ) ? 'true' : 'false';
116 }
117 }
118
119 $shortcode = self::build_shortcode_from_reversal_attrs( $attrs );
120
121 $content = preg_replace( $replace_regex, "\n\n$shortcode\n\n", $content );
122
123 /** This action is documented in modules/widgets/social-media-icons.php */
124 do_action( 'jetpack_bump_stats_extras', 'html_to_shortcode', 'mailchimp_subscriber_popup' );
125 }
126 }
127
128 return $content;
129 }
130
131 /**
132 * Builds the actual shortcode based on passed in attributes.
133 *
134 * @since 4.5.0
135 *
136 * @param array $attrs A valid list of attributes (gets matched against self::$allowed_config and self::$allowed_js_vars)
137 *
138 * @return string
139 */
140 static function build_shortcode_from_reversal_attrs( $attrs ) {
141 $shortcode = '[mailchimp_subscriber_popup ';
142
143 foreach ( $attrs as $key => $value ) {
144 // skip unsupported keys
145 if ( ! in_array( $key, array_keys( self::$allowed_config ) ) && ! in_array( $key, self::$allowed_js_vars ) ) {
146 continue;
147 }
148
149 $value = esc_attr( $value );
150 $shortcode .= "$key='$value' ";
151 }
152 return trim( $shortcode ) . ']';
153 }
154
155 /**
156 * Parses the shortcode back out to embedded information.
157 *
158 * @since 4.5.0
159 *
160 * @param array $lcase_attrs
161 *
162 * @return string
163 */
164 static function shortcode( $lcase_attrs ) {
165 static $displayed_once = false;
166
167 // Limit to one form per page load
168 if ( $displayed_once ) {
169 return '';
170 }
171
172 if ( empty( $lcase_attrs ) ) {
173 return '<!-- Missing MailChimp baseUrl, uuid or lid -->';
174 }
175
176 $defaults = array_fill_keys( self::$allowed_js_vars, '' );
177 $defaults = array_merge( $defaults, self::$allowed_config );
178
179 // Convert $attrs back to proper casing since they come through in all lowercase
180 $attrs = array();
181 foreach ( $defaults as $key => $value ) {
182 if ( array_key_exists( strtolower( $key ), $lcase_attrs ) ) {
183 $attrs[ $key ] = $lcase_attrs[ strtolower( $key ) ];
184 }
185 }
186 $attrs = array_map( 'esc_js', array_filter( shortcode_atts( $defaults, $attrs ) ) );
187
188 // Split config & js vars
189 $config_vars = $js_vars = array();
190 foreach ( $attrs as $key => $value ) {
191 if ( in_array( $key, self::$allowed_js_vars ) ) {
192 $js_vars[ $key ] = $value;
193 } else {
194 $config_vars[] = "$key: $value";
195 }
196 }
197
198 // If one of these parameters is missing we can't render the form so exist.
199 if ( empty( $js_vars['baseUrl'] ) || empty( $js_vars['uuid'] ) || empty( $js_vars['lid'] ) ) {
200 return '<!-- Missing MailChimp baseUrl, uuid or lid -->';
201 }
202
203 // Add a uniqueMethods parameter if it is missing from the data we got from the embed code.
204 $js_vars['uniqueMethods'] = true;
205
206 /** This action is already documented in modules/widgets/gravatar-profile.php */
207 do_action( 'jetpack_stats_extra', 'mailchimp_subscriber_popup', 'view' );
208
209 $displayed_once = true;
210
211 return "\n\n" . '<script type="text/javascript" data-dojo-config="' . esc_attr( implode( ', ', $config_vars ) ) . '">jQuery.getScript( "//downloads.mailchimp.com/js/signup-forms/popup/unique-methods/embed.js", function( data, textStatus, jqxhr ) { window.dojoRequire(["mojo/signup-forms/Loader"], function(L) { L.start(' . wp_json_encode( $js_vars ) . ') });} );</script>' . "\n\n";
212 }
213 }
214