__NAMESPACE__ . '\load_assets', ) ); } add_action( 'init', __NAMESPACE__ . '\register_block' ); /** * Record a Tracks event every time the Map block is loaded on WordPress.com and Atomic. * * @param string $access_token_source The Mapbox API access token source. */ function wpcom_load_event( $access_token_source ) { if ( 'wpcom' !== $access_token_source ) { return; } $event_name = 'map_block_mapbox_wpcom_key_load'; if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { require_lib( 'tracks/client' ); tracks_record_event( wp_get_current_user(), $event_name ); } elseif ( ( new Host() )->is_woa_site() && Jetpack::is_connection_ready() ) { $tracking = new Tracking(); $tracking->record_user_event( $event_name ); } } /** * Function to determine which map provider to choose * * @param array $html The block's HTML - needed for the class name. * * @return string The name of the map provider. */ function get_map_provider( $html ) { $mapbox_styles = array( 'is-style-terrain' ); // return mapbox if html contains one of the mapbox styles foreach ( $mapbox_styles as $style ) { if ( str_contains( $html, $style ) ) { return 'mapbox'; } } // you can override the map provider with a cookie if ( isset( $_COOKIE['map_provider'] ) ) { return sanitize_text_field( wp_unslash( $_COOKIE['map_provider'] ) ); } // if we don't apply the filters & default to mapbox return apply_filters( 'wpcom_map_block_map_provider', 'mapbox' ); } /** * Map block registration/dependency declaration. * * @param array $attr Array containing the map block attributes. * @param string $content String containing the map block content. * * @return string */ function load_assets( $attr, $content ) { $access_token = Jetpack_Mapbox_Helper::get_access_token(); wpcom_load_event( $access_token['source'] ); if ( Blocks::is_amp_request() ) { static $map_block_counter = array(); $id = get_the_ID(); if ( ! isset( $map_block_counter[ $id ] ) ) { $map_block_counter[ $id ] = 0; } ++$map_block_counter[ $id ]; $iframe_url = add_query_arg( array( 'map-block-counter' => absint( $map_block_counter[ $id ] ), 'map-block-post-id' => $id, ), get_permalink() ); $placeholder = preg_replace( '/(?<=%s', esc_url( $iframe_url ), 4, 3, $placeholder ); } Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); $map_provider = get_map_provider( $content ); if ( $map_provider === 'mapkit' ) { return preg_replace( '/
post_content ); /* Suppress warnings */ libxml_use_internal_errors( true ); @$post_html->loadHTML( $content ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged libxml_use_internal_errors( false ); $xpath = new \DOMXPath( $post_html ); $container = $xpath->query( '//div[ contains( @class, "wp-block-jetpack-map" ) ]' )->item( $map_block_counter - 1 ); /* Check that we have a block matching the counter position */ if ( ! $container ) { return; } /* Compile scripts and styles */ ob_start(); add_filter( 'jetpack_is_amp_request', '__return_false' ); Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); wp_scripts()->do_items(); wp_styles()->do_items(); add_filter( 'jetpack_is_amp_request', '__return_true' ); $head_content = ob_get_clean(); /* Put together a new complete document containing only the requested block markup and the scripts/styles needed to render it */ $block_markup = $post_html->saveHTML( $container ); $access_token = Jetpack_Mapbox_Helper::get_access_token(); $page_html = sprintf( '%s%s', $head_content, preg_replace( '/(?<= $points, 'zoom' => 8, 'mapCenter' => array( 'lng' => $points[0]['coordinates']['longitude'], 'lat' => $points[0]['coordinates']['latitude'], ), ); $list_items = array_map( function ( $point ) { $link = add_query_arg( array( 'api' => 1, 'query' => $point['coordinates']['latitude'] . ',' . $point['coordinates']['longitude'], ), 'https://www.google.com/maps/search/' ); return sprintf( '
  • %s
  • ', esc_url( $link ), $point['title'] ); }, $points ); $map_block = '' . PHP_EOL; $map_block .= sprintf( '
    ', esc_html( wp_json_encode( $map_block_data['points'] ) ), (int) $map_block_data['zoom'], esc_html( wp_json_encode( $map_block_data['mapCenter'] ) ) ); $map_block .= '
      ' . implode( "\n", $list_items ) . '
    '; $map_block .= '
    ' . PHP_EOL; $map_block .= ''; return $map_block; }