1 | <?php |
||
40 | class MailChimp_Subscriber_Popup { |
||
41 | |||
42 | /** |
||
43 | * Regular expressions to reverse script tags to shortcodes. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | static $reversal_regexes = array( |
||
|
|||
48 | /* raw examplejs */ |
||
49 | '/<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', |
||
50 | /* visual editor */ |
||
51 | '/<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', |
||
52 | ); |
||
53 | |||
54 | /** |
||
55 | * Allowed configuration attributes. Used in reversal when checking allowed attributes. |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | static $allowed_config = array( |
||
60 | 'usePlainJson' => 'true', |
||
61 | 'isDebug' => 'false', |
||
62 | ); |
||
63 | |||
64 | /** |
||
65 | * Allowed JS variables. Used in reversal to whitelist variables. |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | static $allowed_js_vars = array( |
||
70 | 'baseUrl', |
||
71 | 'uuid', |
||
72 | 'lid', |
||
73 | ); |
||
74 | |||
75 | /** |
||
76 | * Runs the whole reversal. |
||
77 | * |
||
78 | * @since 4.5.0 |
||
79 | * |
||
80 | * @param string $content Post Content |
||
81 | * |
||
82 | * @return string Content with embeds replaced |
||
83 | */ |
||
84 | static function reversal( $content ) { |
||
85 | // Bail without the js src |
||
86 | if ( ! is_string( $content ) || false === stripos( $content, 'downloads.mailchimp.com/js/signup-forms/popup/embed.js' ) ) { |
||
87 | return $content; |
||
88 | } |
||
89 | |||
90 | require_once( ABSPATH . WPINC . '/class-json.php' ); |
||
91 | $wp_json = new Services_JSON(); |
||
92 | |||
93 | // loop through our rules and find valid embeds |
||
94 | foreach ( self::$reversal_regexes as $regex ) { |
||
95 | |||
96 | if ( ! preg_match_all( $regex, $content, $matches ) ) { |
||
97 | continue; |
||
98 | } |
||
99 | |||
100 | foreach ( $matches[3] as $index => $js_vars ) { |
||
101 | // the regex rule for a specific embed |
||
102 | $replace_regex = sprintf( '#\s*%s\s*#', preg_quote( $matches[0][$index], '#' ) ); |
||
103 | |||
104 | $attrs = $wp_json->decode( '{' . $js_vars . '}' ); |
||
105 | |||
106 | if ( $matches[2][$index] ) { |
||
107 | $config_attrs = $wp_json->decode( '{' . $matches[2][$index] . '}' ); |
||
108 | foreach ( $config_attrs as $key => $value ) { |
||
109 | $attrs->$key = ( 1 == $value ) ? 'true' : 'false'; |
||
110 | } |
||
111 | } |
||
112 | |||
113 | $shortcode = self::build_shortcode_from_reversal_attrs( $attrs ); |
||
114 | |||
115 | $content = preg_replace( $replace_regex, "\n\n$shortcode\n\n", $content ); |
||
116 | |||
117 | /** This action is documented in modules/widgets/social-media-icons.php */ |
||
118 | do_action( 'jetpack_bump_stats_extras', 'html_to_shortcode', 'mailchimp_subscriber_popup' ); |
||
119 | } |
||
120 | } |
||
121 | |||
122 | return $content; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Builds the actual shortcode based on passed in attributes. |
||
127 | * |
||
128 | * @since 4.5.0 |
||
129 | * |
||
130 | * @param array $attrs A valid list of attributes (gets matched against self::$allowed_config and self::$allowed_js_vars) |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | static function build_shortcode_from_reversal_attrs( $attrs ) { |
||
148 | |||
149 | /** |
||
150 | * Parses the shortcode back out to embedded information. |
||
151 | * |
||
152 | * @since 4.5.0 |
||
153 | * |
||
154 | * @param array $lcase_attrs |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | static function shortcode( $lcase_attrs ) { |
||
204 | } |
||
205 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.