map.php
249 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Map block. |
| 4 | * |
| 5 | * @since 6.8.0 |
| 6 | * |
| 7 | * @package automattic/jetpack |
| 8 | */ |
| 9 | |
| 10 | namespace Automattic\Jetpack\Extensions\Map; |
| 11 | |
| 12 | use Automattic\Jetpack\Blocks; |
| 13 | use Automattic\Jetpack\Status\Host; |
| 14 | use Automattic\Jetpack\Tracking; |
| 15 | use Jetpack; |
| 16 | use Jetpack_Gutenberg; |
| 17 | use Jetpack_Mapbox_Helper; |
| 18 | |
| 19 | if ( ! defined( 'ABSPATH' ) ) { |
| 20 | exit( 0 ); |
| 21 | } |
| 22 | |
| 23 | if ( ! class_exists( 'Jetpack_Mapbox_Helper' ) ) { |
| 24 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-mapbox-helper.php'; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Registers the block for use in Gutenberg |
| 29 | * This is done via an action so that we can disable |
| 30 | * registration if we need to. |
| 31 | */ |
| 32 | function register_block() { |
| 33 | Blocks::jetpack_register_block( |
| 34 | __DIR__, |
| 35 | array( |
| 36 | 'render_callback' => __NAMESPACE__ . '\load_assets', |
| 37 | ) |
| 38 | ); |
| 39 | } |
| 40 | add_action( 'init', __NAMESPACE__ . '\register_block' ); |
| 41 | |
| 42 | /** |
| 43 | * Record a Tracks event every time the Map block is loaded on WordPress.com and Atomic. |
| 44 | * |
| 45 | * @param string $access_token_source The Mapbox API access token source. |
| 46 | */ |
| 47 | function wpcom_load_event( $access_token_source ) { |
| 48 | if ( 'wpcom' !== $access_token_source ) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | $event_name = 'map_block_mapbox_wpcom_key_load'; |
| 53 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 54 | require_lib( 'tracks/client' ); |
| 55 | tracks_record_event( wp_get_current_user(), $event_name ); |
| 56 | } elseif ( ( new Host() )->is_woa_site() && Jetpack::is_connection_ready() ) { |
| 57 | $tracking = new Tracking(); |
| 58 | $tracking->record_user_event( $event_name ); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Function to determine which map provider to choose |
| 64 | * |
| 65 | * @param string $html The block's HTML - needed for the class name. |
| 66 | * |
| 67 | * @return string The name of the map provider. |
| 68 | */ |
| 69 | function get_map_provider( $html ) { |
| 70 | $mapbox_styles = array( 'is-style-terrain' ); |
| 71 | // return mapbox if html contains one of the mapbox styles |
| 72 | foreach ( $mapbox_styles as $style ) { |
| 73 | if ( str_contains( $html, $style ) ) { |
| 74 | return 'mapbox'; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // you can override the map provider with a cookie |
| 79 | if ( isset( $_COOKIE['map_provider'] ) ) { |
| 80 | return sanitize_text_field( wp_unslash( $_COOKIE['map_provider'] ) ); |
| 81 | } |
| 82 | |
| 83 | // if we don't apply the filters & default to mapbox |
| 84 | return apply_filters( 'wpcom_map_block_map_provider', 'mapbox' ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Map block registration/dependency declaration. |
| 89 | * |
| 90 | * @param array $attr Array containing the map block attributes. |
| 91 | * @param string $content String containing the map block content. |
| 92 | * |
| 93 | * @return string |
| 94 | */ |
| 95 | function load_assets( $attr, $content ) { |
| 96 | $access_token = Jetpack_Mapbox_Helper::get_access_token(); |
| 97 | wpcom_load_event( $access_token['source'] ); |
| 98 | |
| 99 | if ( Blocks::is_amp_request() ) { |
| 100 | static $map_block_counter = array(); |
| 101 | |
| 102 | $id = get_the_ID(); |
| 103 | if ( ! isset( $map_block_counter[ $id ] ) ) { |
| 104 | $map_block_counter[ $id ] = 0; |
| 105 | } |
| 106 | ++$map_block_counter[ $id ]; |
| 107 | |
| 108 | $iframe_url = add_query_arg( |
| 109 | array( |
| 110 | 'map-block-counter' => absint( $map_block_counter[ $id ] ), |
| 111 | 'map-block-post-id' => $id, |
| 112 | ), |
| 113 | get_permalink() |
| 114 | ); |
| 115 | |
| 116 | $placeholder = preg_replace( '/(?<=<div\s)/', 'placeholder ', $content ); |
| 117 | |
| 118 | return sprintf( |
| 119 | '<amp-iframe src="%s" width="%d" height="%d" layout="responsive" allowfullscreen sandbox="allow-scripts">%s</amp-iframe>', |
| 120 | esc_url( $iframe_url ), |
| 121 | 4, |
| 122 | 3, |
| 123 | $placeholder |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 128 | |
| 129 | $map_provider = get_map_provider( $content ); |
| 130 | if ( $map_provider === 'mapkit' ) { |
| 131 | return preg_replace( '/<div /', '<div data-map-provider="mapkit" data-api-key="' . esc_attr( $access_token['key'] ) . '" data-blog-id="' . \Jetpack_Options::get_option( 'id' ) . '" ', $content, 1 ); |
| 132 | } |
| 133 | |
| 134 | return preg_replace( '/<div /', '<div data-map-provider="mapbox" data-api-key="' . esc_attr( $access_token['key'] ) . '" ', $content, 1 ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Render a page containing only a single Map block. |
| 139 | */ |
| 140 | function render_single_block_page() { |
| 141 | // phpcs:ignore WordPress.Security.NonceVerification |
| 142 | $map_block_counter = isset( $_GET['map-block-counter'] ) ? absint( $_GET['map-block-counter'] ) : null; |
| 143 | // phpcs:ignore WordPress.Security.NonceVerification |
| 144 | $map_block_post_id = isset( $_GET['map-block-post-id'] ) ? absint( $_GET['map-block-post-id'] ) : null; |
| 145 | |
| 146 | if ( ! $map_block_counter || ! $map_block_post_id ) { |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | /* Create an array of all root-level DIVs that are Map Blocks */ |
| 151 | $post = get_post( $map_block_post_id ); |
| 152 | |
| 153 | if ( ! class_exists( 'DOMDocument' ) ) { |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | $post_html = new \DOMDocument(); |
| 158 | /** This filter is already documented in core/wp-includes/post-template.php */ |
| 159 | $content = apply_filters( 'the_content', $post->post_content ); |
| 160 | |
| 161 | // Return early if empty to prevent DOMDocument::loadHTML fatal. |
| 162 | if ( empty( $content ) ) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | /* Suppress warnings */ |
| 167 | libxml_use_internal_errors( true ); |
| 168 | @$post_html->loadHTML( $content ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 169 | libxml_use_internal_errors( false ); |
| 170 | |
| 171 | $xpath = new \DOMXPath( $post_html ); |
| 172 | $container = $xpath->query( '//div[ contains( @class, "wp-block-jetpack-map" ) ]' )->item( $map_block_counter - 1 ); |
| 173 | |
| 174 | /* Check that we have a block matching the counter position */ |
| 175 | if ( ! $container ) { |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | /* Compile scripts and styles */ |
| 180 | ob_start(); |
| 181 | |
| 182 | add_filter( 'jetpack_is_amp_request', '__return_false' ); |
| 183 | |
| 184 | Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); |
| 185 | wp_scripts()->do_items(); |
| 186 | wp_styles()->do_items(); |
| 187 | |
| 188 | add_filter( 'jetpack_is_amp_request', '__return_true' ); |
| 189 | |
| 190 | $head_content = ob_get_clean(); |
| 191 | |
| 192 | /* Put together a new complete document containing only the requested block markup and the scripts/styles needed to render it */ |
| 193 | $block_markup = $post_html->saveHTML( $container ); |
| 194 | $access_token = Jetpack_Mapbox_Helper::get_access_token(); |
| 195 | $page_html = sprintf( |
| 196 | '<!DOCTYPE html><head><style>html, body { margin: 0; padding: 0; }</style>%s</head><body>%s</body>', |
| 197 | $head_content, |
| 198 | preg_replace( '/(?<=<div\s)/', 'data-api-key="' . esc_attr( $access_token['key'] ) . '" ', $block_markup, 1 ) |
| 199 | ); |
| 200 | echo $page_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 201 | exit( 0 ); |
| 202 | } |
| 203 | add_action( 'wp', __NAMESPACE__ . '\render_single_block_page' ); |
| 204 | |
| 205 | /** |
| 206 | * Helper function to generate the markup of the block in PHP. |
| 207 | * |
| 208 | * @param Array $points - Array containing geo location points. |
| 209 | * |
| 210 | * @return string Markup for the jetpack/map block. |
| 211 | */ |
| 212 | function map_block_from_geo_points( $points ) { |
| 213 | $map_block_data = array( |
| 214 | 'points' => $points, |
| 215 | 'zoom' => 8, |
| 216 | 'mapCenter' => array( |
| 217 | 'lng' => $points[0]['coordinates']['longitude'], |
| 218 | 'lat' => $points[0]['coordinates']['latitude'], |
| 219 | ), |
| 220 | ); |
| 221 | |
| 222 | $list_items = array_map( |
| 223 | function ( $point ) { |
| 224 | $link = add_query_arg( |
| 225 | array( |
| 226 | 'api' => 1, |
| 227 | 'query' => $point['coordinates']['latitude'] . ',' . $point['coordinates']['longitude'], |
| 228 | ), |
| 229 | 'https://www.google.com/maps/search/' |
| 230 | ); |
| 231 | return sprintf( '<li><a href="%s">%s</a></li>', esc_url( $link ), $point['title'] ); |
| 232 | }, |
| 233 | $points |
| 234 | ); |
| 235 | |
| 236 | $map_block = '<!-- wp:jetpack/map ' . wp_json_encode( $map_block_data, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ' -->' . PHP_EOL; |
| 237 | $map_block .= sprintf( |
| 238 | '<div class="wp-block-jetpack-map" data-map-style="default" data-map-details="true" data-points="%1$s" data-zoom="%2$d" data-map-center="%3$s" data-marker-color="red" data-show-fullscreen-button="true">', |
| 239 | esc_attr( wp_json_encode( $map_block_data['points'], JSON_HEX_AMP | JSON_UNESCAPED_SLASHES ) ), |
| 240 | $map_block_data['zoom'], |
| 241 | esc_attr( wp_json_encode( $map_block_data['mapCenter'], JSON_HEX_AMP | JSON_UNESCAPED_SLASHES ) ) |
| 242 | ); |
| 243 | $map_block .= '<ul>' . implode( "\n", $list_items ) . '</ul>'; |
| 244 | $map_block .= '</div>' . PHP_EOL; |
| 245 | $map_block .= '<!-- /wp:jetpack/map -->'; |
| 246 | |
| 247 | return $map_block; |
| 248 | } |
| 249 |