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