__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( '/(?<=