| @@ -9,8 +9,12 @@ | ||
| 9 | 9 | * |
| 10 | 10 | * @package automattic/jetpack |
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 14 | + exit( 0 ); | |
| 15 | +} | |
| 16 | + | |
| 13 | 17 | /** |
| 14 | 18 | * Google maps iframe - transforms code that looks like that: |
| 15 | 19 | * <iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps?f=q&source=s_q&hl=bg&geocode=&q=%D0%9C%D0%BB%D0%B0%D0%B4%D0%BE%D1%81%D1%82+1,+%D0%A1%D0%BE%D1%84%D0%B8%D1%8F,+%D0%91%D1%8A%D0%BB%D0%B3%D0%B0%D1%80%D0%B8%D1%8F&sll=37.0625,-95.677068&sspn=40.545434,79.013672&ie=UTF8&hq=&hnear=%D0%9C%D0%BB%D0%B0%D0%B4%D0%BE%D1%81%D1%82+1&ll=42.654446,23.372061&spn=0.036864,0.077162&t=h&z=14&output=embed"></iframe><br /><small><a href="http://maps.google.com/maps?f=q&source=embed&hl=bg&geocode=&q=%D0%9C%D0%BB%D0%B0%D0%B4%D0%BE%D1%81%D1%82+1,+%D0%A1%D0%BE%D1%84%D0%B8%D1%8F,+%D0%91%D1%8A%D0%BB%D0%B3%D0%B0%D1%80%D0%B8%D1%8F&sll=37.0625,-95.677068&sspn=40.545434,79.013672&ie=UTF8&hq=&hnear=%D0%9C%D0%BB%D0%B0%D0%B4%D0%BE%D1%81%D1%82+1&ll=42.654446,23.372061&spn=0.036864,0.077162&t=h&z=14" style="color:#0000FF;text-align:left">Вижте по-голяма карта</a></small> |
| 16 | 20 | * into the [googlemaps http://...] shortcode format |
| @@ -67,9 +71,11 @@ | ||
| 67 | 71 | |
| 68 | 72 | return "[googlemaps $url]"; |
| 69 | 73 | } |
| 70 | 74 | |
| 71 | -add_filter( 'pre_kses', 'jetpack_googlemaps_embed_to_short_code' ); | |
| 75 | +if ( jetpack_shortcodes_should_hook_pre_kses() ) { | |
| 76 | + add_filter( 'pre_kses', 'jetpack_googlemaps_embed_to_short_code' ); | |
| 77 | +} | |
| 72 | 78 | |
| 73 | 79 | /** |
| 74 | 80 | * Display the [googlemaps] shortcode |
| 75 | 81 | * |
| @@ -85,30 +91,54 @@ | ||
| 85 | 91 | $width = 425; |
| 86 | 92 | $height = 350; |
| 87 | 93 | |
| 88 | 94 | if ( preg_match( '!^https?://(www|maps|mapsengine)\.google(\.co|\.com)?(\.[a-z]+)?/.*?(\?.+)!i', $params, $match ) ) { |
| 89 | - $params = str_replace( '&amp;', '&', $params ); | |
| 90 | - $params = str_replace( '&', '&', $params ); | |
| 91 | - parse_str( $params, $arg ); | |
| 95 | + $url_parts = wp_parse_url( $params ); | |
| 96 | + if ( ! is_array( $url_parts ) || empty( $url_parts['host'] ) ) { | |
| 97 | + return ''; | |
| 98 | + } | |
| 92 | 99 | |
| 93 | - if ( isset( $arg['hq'] ) ) { | |
| 94 | - unset( $arg['hq'] ); | |
| 100 | + $base_url = ( $url_parts['scheme'] ?? 'https' ) . '://' . $url_parts['host'] . ( $url_parts['path'] ?? '' ); | |
| 101 | + $query_string = $url_parts['query'] ?? ''; | |
| 102 | + | |
| 103 | + // Convert separator-position `&` (and `&amp;` etc.) to `&` so parse_str() can split parameters, | |
| 104 | + // but leave entity-encoded ampersands inside values alone — those are handled after parse_str(). | |
| 105 | + $query_string = preg_replace( '/&(?:amp;)+(?=[a-zA-Z_][a-zA-Z0-9_]*=)/', '&', $query_string ); | |
| 106 | + | |
| 107 | + // Any `&` left at this point sits inside a value. Encode the leading `&` so parse_str() does not | |
| 108 | + // split on it; the trailing `amp;` is decoded back to `&` after parse_str() runs. | |
| 109 | + $query_string = str_replace( '&', '%26amp;', $query_string ); | |
| 110 | + | |
| 111 | + parse_str( $query_string, $arg ); | |
| 112 | + | |
| 113 | + unset( $arg['hq'] ); | |
| 114 | + | |
| 115 | + if ( isset( $arg['w'] ) ) { | |
| 116 | + $w_value = (string) $arg['w']; | |
| 117 | + $percent = str_ends_with( $w_value, '%' ) ? '%' : ''; | |
| 118 | + $width = (int) $w_value . $percent; | |
| 119 | + unset( $arg['w'] ); | |
| 95 | 120 | } |
| 96 | 121 | |
| 97 | - $url = ''; | |
| 98 | - foreach ( (array) $arg as $key => $value ) { | |
| 99 | - if ( 'w' === $key ) { | |
| 100 | - $percent = ( str_ends_with( $value, '%' ) ) ? '%' : ''; | |
| 101 | - $width = (int) $value . $percent; | |
| 102 | - } elseif ( 'h' === $key ) { | |
| 103 | - $height = (int) $value; | |
| 104 | - } else { | |
| 105 | - $key = str_replace( '_', '.', $key ); | |
| 106 | - $url .= esc_attr( "$key=$value&" ); | |
| 122 | + if ( isset( $arg['h'] ) ) { | |
| 123 | + $height = (int) $arg['h']; | |
| 124 | + unset( $arg['h'] ); | |
| 125 | + } | |
| 126 | + | |
| 127 | + // Restore parse_str()'s underscore-mangled keys (e.g. `f.q` → `f_q` → `f.q`) and decode any | |
| 128 | + // HTML entities that survived inside values, so http_build_query() encodes the real characters. | |
| 129 | + $rebuilt = array(); | |
| 130 | + foreach ( $arg as $key => $value ) { | |
| 131 | + $key = str_replace( '_', '.', (string) $key ); | |
| 132 | + if ( is_string( $value ) ) { | |
| 133 | + $value = preg_replace( '/&(?:amp;)+/', '&', $value ); | |
| 107 | 134 | } |
| 135 | + $rebuilt[ $key ] = $value; | |
| 108 | 136 | } |
| 109 | - $url = substr( $url, 0, -5 ); | |
| 110 | 137 | |
| 138 | + $query = http_build_query( $rebuilt, '', '&', PHP_QUERY_RFC3986 ); | |
| 139 | + | |
| 140 | + $url = $base_url . ( '' !== $query ? '?' . $query : '' ); | |
| 111 | 141 | $url = str_replace( 'http://', 'https://', $url ); |
| 112 | 142 | |
| 113 | 143 | $css_class = 'googlemaps'; |
| 114 | 144 | |