boolean, * 'latitude' => float, * 'longitude' => float, * 'label' => string, * 'is_populated' => boolean * ) * * Add your filter with: * * add_filter( 'jetpack_geo_location_display', 'your_filter_function_name', 10, 2); */ class Jetpack_Geo_Location { private static $instance; /** * Whether dashicons are enqueued. * * @since 6.6.0 * * @var bool */ private static $style_enqueued = false; public static function init() { if ( is_null( self::$instance ) ) { self::$instance = new Jetpack_Geo_Location(); } return self::$instance; } /** * This is mostly just used for testing purposes. */ public static function reset_instance() { self::$instance = null; } public function __construct() { add_action( 'init', array( $this, 'wordpress_init' ) ); add_action( 'wp_head', array( $this, 'wp_head' ) ); add_filter( 'the_content', array( $this, 'the_content_microformat' ) ); $this->register_rss_hooks(); } /** * Register support for the geo-location feature on pages and posts. Register the meta * fields managed by this plugin so that they are properly sanitized during save. */ public function wordpress_init() { // Only render location label after post content, if the theme claims to support "geo-location". if ( current_theme_supports( 'jetpack-geo-location' ) ) { add_filter( 'the_content', array( $this, 'the_content_location_display' ), 15, 1 ); } add_post_type_support( 'post', 'geo-location' ); add_post_type_support( 'page', 'geo-location' ); register_meta( 'post', 'geo_public', array( 'sanitize_callback' => array( $this, 'sanitize_public' ), 'type' => 'boolean', 'single' => true, ) ); register_meta( 'post', 'geo_latitude', array( 'sanitize_callback' => array( $this, 'sanitize_coordinate' ), 'type' => 'float', 'single' => true, ) ); register_meta( 'post', 'geo_longitude', array( 'sanitize_callback' => array( $this, 'sanitize_coordinate' ), 'type' => 'float', 'single' => true, ) ); register_meta( 'post', 'geo_address', array( 'sanitize_callback' => 'sanitize_text_field', 'type' => 'string', 'single' => true, ) ); } /** * Filter "public" input to always be either 1 or 0. * * @param mixed $public * * @return int */ public function sanitize_public( $public ) { return absint( $public ) ? 1 : 0; } /** * Filter geo coordinates and normalize them to floats with 7 digits of precision. * * @param mixed $coordinate * * @return float|null */ public function sanitize_coordinate( $coordinate ) { if ( ! $coordinate ) { return null; } return round( (float) $coordinate, 7 ); } /** * Render geo.position and ICBM meta tags with public geo meta values when rendering * a single post. */ public function wp_head() { if ( ! is_single() ) { return; } $meta_values = $this->get_meta_values( $this->get_post_id() ); if ( ! $meta_values['is_public'] ) { return; } if ( ! self::$style_enqueued ) { // only enqueue scripts and styles when needed. self::enqueue_scripts(); self::$style_enqueued = true; } echo "\n\n"; if ( $meta_values['label'] ) { printf( '', esc_attr( $meta_values['label'] ) ); } printf( '' . PHP_EOL, esc_attr( $meta_values['latitude'] ), esc_attr( $meta_values['longitude'] ) ); printf( '' . PHP_EOL, esc_attr( $meta_values['latitude'] ), esc_attr( $meta_values['longitude'] ) ); echo "\n\n"; } /** * Append public meta values in the Geo microformat (https://en.wikipedia.org/wiki/Geo_(microformat) * to the supplied content. * * Note that we cannot render the microformat in the context of an excerpt because tags are stripped * in that context, making our microformat data visible. * * @param string $content * * @return string */ public function the_content_microformat( $content ) { if ( is_feed() || $this->is_currently_excerpt_filter() ) { return $content; } $meta_values = $this->get_meta_values( $this->get_post_id() ); if ( ! $meta_values['is_public'] ) { return $content; } $microformat = sprintf( '
'; return $content . $microformat; } /** * Register a range of hooks for integrating geo data with various feeds. */ public function register_rss_hooks() { add_action( 'rss2_ns', array( $this, 'rss_namespace' ) ); add_action( 'atom_ns', array( $this, 'rss_namespace' ) ); add_action( 'rdf_ns', array( $this, 'rss_namespace' ) ); add_action( 'rss_item', array( $this, 'rss_item' ) ); add_action( 'rss2_item', array( $this, 'rss_item' ) ); add_action( 'atom_entry', array( $this, 'rss_item' ) ); add_action( 'rdf_item', array( $this, 'rss_item' ) ); } /** * Add the georss namespace during RSS generation. */ public function rss_namespace() { echo PHP_EOL . 'xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"' . PHP_EOL; } /** * Output georss data for RSS items, assuming we have data for the currently rendered post and * that data as marked as public. */ public function rss_item() { $meta_values = $this->get_meta_values( $this->get_post_id() ); if ( ! $meta_values['is_public'] ) { return; } printf( "\t