pins = $pins;
$this->args = $this->parse_args( $args );
add_action( ‘wp_enqueue_scripts’, array( $this, ‘scripts’ ) );
if ( $this->args[‘hook’] ) {
add_action( $this->args[‘hook’], array( $this, ‘do_map’ ), $this->args[‘priority’] );
} elseif ( $this->args[‘filter’] ) {
add_filter( $this->args[‘filter’], array( $this, ‘insert_map’ ), $this->args[‘priority’] );
}
}
/**
* Make sure the args have something.
* @param $args
*
* @return array
*/
private function parse_args( $args ) {
$defaults = array(
‘hook’ => ‘genesis_entry_header’,
‘filter’ => false,
‘priority’ => 20,
);
return wp_parse_args( $args, $defaults );
}
/**
* Enqueue the scripts needed for our map, if the API key is present.
*/
public function scripts() {
$api_key = $this->get_api_key();
if ( ! $api_key ) {
return;
}
$minify = defined( ‘SCRIPT_DEBUG’ ) && SCRIPT_DEBUG ? ” : ‘.min’;
wp_register_script( ‘google-maps’, ‘https://maps.googleapis.com/maps/api/js?key=’ . $api_key . ‘&libraries=places’, array(), ‘3.0’, true );
$dependencies = array( ‘jquery’, ‘google-maps’ );
$localize = array(
‘hue’ => ‘#333’,
‘url’ => get_stylesheet_directory_uri() . ‘/images/icon.png’,
‘style’ => $this->get_json_data(),
);
$cluster = false;
if ( $cluster ) {
wp_register_script( ‘google-maps-cluster’, get_stylesheet_directory_uri() . “/js/markerclusterer{$minify}.js”, array( ‘google-maps’ ), ‘3.0’, true );
$dependencies[] = ‘google-maps-cluster’;
$localize[‘cluster’] = get_stylesheet_directory_uri() . ‘/images/m’;
}
wp_enqueue_script(
‘leaven-maps’,
get_stylesheet_directory_uri() . “/js/maps{$minify}.js”,
$dependencies,
$this->version,
true
);
wp_localize_script( ‘leaven-maps’, ‘LeavenMaps’, $localize );
}
/**
* Get the style data for the map.
*/
private function get_json_data() {
$request = wp_remote_get( get_stylesheet_directory_uri() . ‘/js/style.json’ );
if ( is_wp_error( $request ) ) {
return json_decode( include get_stylesheet_directory() . ‘/js/style.php’ );
}
$body = wp_remote_retrieve_body( $request );
return json_decode( $body );
}
/**
* Output the archive map, multiple markers
*/
public function do_map() {
$map = ‘‘;
return $marker;
}
/**
* Get our API key.
* @return bool
*/
protected function get_api_key() {
$setting = get_option( ‘sixtenpress’, array() );
if ( empty( $setting[‘google_api’] ) ) {
return false;
}
return $setting[‘google_api’];
}
}
‘;
$map .= $this->do_map_primary();
$map .= $this->do_map_search();
$map .= ‘
‘;
if ( $this->args[‘hook’] ) {
echo $map;
}
return $map;
}
/**
* Insert the map where the placeholder is.
* @param $content
*
* @return mixed
*/
public function insert_map( $content ) {
return str_replace( ‘##MAP##
‘, $this->do_map(), $content ); } /** * Output the primary map box with pins. */ protected function do_map_primary() { $primary = ‘‘;
foreach ( $this->pins as $pin ) {
if ( empty( $pin[‘geocode’] ) ) {
continue;
}
$primary .= $this->get_markers( $pin );
}
$primary .= ‘
‘;
return $primary;
}
/**
* If it’s enabled, output the map search box.
*/
protected function do_map_search() {
$setting = get_option( ‘sixtenpress’, array() );
if ( empty( $setting[‘google_search’] ) ) {
return ”;
}
$search = ‘‘;
$search .= ‘‘;
$search .= ‘‘;
$search .= ‘
‘;
return $search;
}
/**
* Get the markers/HTML for the map and info boxes.
*
* @param $location_data
*
* @return string
*/
protected function get_markers( $location_data ) {
$marker = sprintf( ‘‘, $location_data[‘geocode’][‘lat’], $location_data[‘geocode’][‘lng’], $location_data[‘id’] );
$marker .= sprintf( ‘%s‘, wp_kses_post( $location_data[‘title’] ) ) . ‘
‘;
$marker .= sprintf( ‘%s