mailchimp.php
| 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/embed.js" data-dojo-config="usePlainJson: true, isDebug: false"></script><script type="text/javascript">require(["mojo/signup-forms/Loader"], function(L) { L.start({"baseUrl":"mc.us11.list-manage.com","uuid":"1ca7856462585a934b8674c71","lid":"2d24f1898b"}) })</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( 'mailchimp_subscriber_popup', array( |
| 20 | 'MailChimp_Subscriber_Popup', |
| 21 | 'shortcode' |
| 22 | ) ); |
| 23 | add_filter( 'pre_kses', array( |
| 24 | 'MailChimp_Subscriber_Popup', |
| 25 | 'reversal' |
| 26 | ) ); |
| 27 | } |
| 28 | |
| 29 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 30 | add_action( 'init', 'jetpack_mailchimp_subscriber_popup' ); |
| 31 | } else { |
| 32 | jetpack_mailchimp_subscriber_popup(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Class MailChimp_Subscriber_Popup |
| 37 | * |
| 38 | * @since 4.5.0 |
| 39 | */ |
| 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?:)?\/\/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?:)?\/\/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 ) { |
| 135 | $shortcode = '[mailchimp_subscriber_popup '; |
| 136 | |
| 137 | foreach ( $attrs as $key => $value ) { |
| 138 | // skip unsupported keys |
| 139 | if ( ! in_array( $key, array_keys( self::$allowed_config ) ) && ! in_array( $key, self::$allowed_js_vars ) ) { |
| 140 | continue; |
| 141 | } |
| 142 | |
| 143 | $value = esc_attr( $value ); |
| 144 | $shortcode .= "$key='$value' "; |
| 145 | } |
| 146 | return trim( $shortcode ) . ']'; |
| 147 | } |
| 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 ) { |
| 159 | static $displayed_once = false; |
| 160 | |
| 161 | // Limit to one form per page load |
| 162 | if ( $displayed_once ) { |
| 163 | return ''; |
| 164 | } |
| 165 | |
| 166 | if ( empty( $lcase_attrs ) ) { |
| 167 | return '<!-- Missing MailChimp baseUrl, uuid or lid -->'; |
| 168 | } |
| 169 | |
| 170 | $defaults = array_fill_keys( self::$allowed_js_vars, '' ); |
| 171 | $defaults = array_merge( $defaults, self::$allowed_config ); |
| 172 | |
| 173 | // Convert $attrs back to proper casing since they come through in all lowercase |
| 174 | $attrs = array(); |
| 175 | foreach ( $defaults as $key => $value ) { |
| 176 | if ( array_key_exists( strtolower( $key ), $lcase_attrs ) ) { |
| 177 | $attrs[ $key ] = $lcase_attrs[ strtolower( $key ) ]; |
| 178 | } |
| 179 | } |
| 180 | $attrs = array_map( 'esc_js', array_filter( shortcode_atts( $defaults, $attrs ) ) ); |
| 181 | |
| 182 | // Split config & js vars |
| 183 | $config_vars = $js_vars = array(); |
| 184 | foreach ( $attrs as $key => $value ) { |
| 185 | if ( in_array( $key, self::$allowed_js_vars ) ) { |
| 186 | $js_vars[ $key ] = $value; |
| 187 | } else { |
| 188 | $config_vars[] = "$key: $value"; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // If one of these parameters is missing we can't render the form so exist. |
| 193 | if ( empty( $js_vars['baseUrl'] ) || empty( $js_vars['uuid'] ) || empty( $js_vars['lid'] ) ) { |
| 194 | return '<!-- Missing MailChimp baseUrl, uuid or lid -->'; |
| 195 | } |
| 196 | |
| 197 | /** This action is already documented in modules/widgets/gravatar-profile.php */ |
| 198 | do_action( 'jetpack_stats_extra', 'mailchimp_subscriber_popup', 'view' ); |
| 199 | |
| 200 | $displayed_once = true; |
| 201 | |
| 202 | return "\n\n" . '<script type="text/javascript" data-dojo-config="' . esc_attr( implode( ', ', $config_vars ) ) . '">jQuery.getScript( "//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 ) . ') }); window.define.amd = undefined; } );</script>' . "\n\n"; |
| 203 | } |
| 204 | } |
| 205 |