posts automatically add markers yeeeah
This commit is contained in:
1
srcs/plugins/google_map/google_map
Symbolic link
1
srcs/plugins/google_map/google_map
Symbolic link
@@ -0,0 +1 @@
|
||||
/home/www-data/google_map
|
||||
@@ -9,11 +9,26 @@ Author URI:
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* global variables :
|
||||
*/
|
||||
|
||||
$mp_api_key = 'AIzaSyCvdGV2ssD4ov4a9CuIlQhoJyz5gWWiSvE';
|
||||
$mp_marker_icon = '/wp-content/plugins/map_prof/marker.png';
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* inclusions :
|
||||
*/
|
||||
|
||||
include_once(dirname(__FILE__) . '/mp_console_log.php');
|
||||
require_once(dirname(__FILE__) . '/mp_url_api.php');
|
||||
require_once(dirname(__FILE__) . '/mp_add_to_scripts.php');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* add scripts and styles to the header or the footer
|
||||
*/
|
||||
|
||||
@@ -1,64 +1,27 @@
|
||||
<?php
|
||||
|
||||
// // TESTS : print posts full content
|
||||
// //
|
||||
// //$posts_list = get_posts($args);
|
||||
// //$content .= "<p>";
|
||||
// //$content .= "nb posts published : ";
|
||||
// //$content .= count($posts_list);
|
||||
// //$content .= "</p>";
|
||||
// //foreach ($posts_list as $post_key => $post_value) {
|
||||
// // $content .= "<p>";
|
||||
// // $content .= "post content : ";
|
||||
// // $content .= $post_key;
|
||||
// // $content .= " : ";
|
||||
// // $content .= "<br>";
|
||||
// // foreach ($post_value as $key => $value) {
|
||||
// // $content .= "- [";
|
||||
// // $content .= $key;
|
||||
// // $content .= "]: [";
|
||||
// // $content .= $value;
|
||||
// // $content .= "]<br>";
|
||||
// // }
|
||||
// // $content .= "</p>";
|
||||
// //}
|
||||
require_once(dirname(__FILE__) . '/mp_coordinates_from_posts.php');
|
||||
|
||||
function mp_get_locations() {
|
||||
|
||||
// https://developer.wordpress.org/reference/functions/get_posts/
|
||||
$get_posts_args = array(
|
||||
'numberposts' => -1,
|
||||
'post_status' => 'publish',
|
||||
);
|
||||
$posts_list = get_posts($get_posts_args);
|
||||
$locs = "";
|
||||
foreach ($posts_list as $post_key => $post_value) {
|
||||
$locs .= "\n"
|
||||
. 'post_key: '
|
||||
. $post_key . "\n"
|
||||
. 'post_value: ' . "\n";
|
||||
foreach ($post_value as $key => $value) {
|
||||
$locs .= " "
|
||||
. $key
|
||||
. ': '
|
||||
. $value . "\n"
|
||||
;
|
||||
};
|
||||
};
|
||||
mp_console_log("locs : \n" . $locs);
|
||||
$posts_list = mp_get_published_posts();
|
||||
$locs = mp_retrieve_address($posts_list);
|
||||
|
||||
$locations = 'let locations = [';
|
||||
foreach ($locs as $value) {
|
||||
$locations .= json_encode($value);
|
||||
$locations .= ',';
|
||||
};
|
||||
$locations .= '];';
|
||||
|
||||
$locations =
|
||||
'let locations = ['
|
||||
. '{lat: 38.8833, lng: -77.0167}' . ','
|
||||
. '{lat: 39.8833, lng: -76.0167}' . ','
|
||||
. '];'
|
||||
;
|
||||
wp_add_inline_script('mp_init_map', $locations, 'before');
|
||||
}
|
||||
|
||||
function mp_get_markers() {
|
||||
global $mp_marker_icon;
|
||||
$markers =
|
||||
'let icon_url = "'
|
||||
. '/wp-content/plugins/map_prof/marker.png'
|
||||
. $mp_marker_icon
|
||||
. '";'
|
||||
;
|
||||
wp_add_inline_script('mp_init_map', $markers, 'before');
|
||||
|
||||
43
srcs/plugins/map_prof/mp_coordinates_from_posts.php
Normal file
43
srcs/plugins/map_prof/mp_coordinates_from_posts.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
function mp_get_published_posts() {
|
||||
$get_posts_args = array(
|
||||
'numberposts' => -1,
|
||||
'post_status' => 'publish',
|
||||
);
|
||||
return get_posts($get_posts_args);
|
||||
}
|
||||
|
||||
function mp_extract_address(&$content) {
|
||||
$begin = "<p>adresse: ";
|
||||
$end = "</p>";
|
||||
$len = strlen($begin);
|
||||
$address = substr($content, strpos($content, $begin)+$len);
|
||||
$address = substr($content, 0, strpos($content, $end));
|
||||
return $address;
|
||||
}
|
||||
|
||||
function mp_convert_coordinates(&$address) {
|
||||
global $mp_api_key;
|
||||
$geolocation = 'https://maps.googleapis.com/maps/api/geocode/json'
|
||||
. '?address=' . urlencode($address)
|
||||
. '&key=' . $mp_api_key;
|
||||
|
||||
$jsoncontent = file_get_contents($geolocation);
|
||||
$content = json_decode($jsoncontent);
|
||||
$coordinates = $content->results[0]->geometry->location;
|
||||
return $coordinates;
|
||||
}
|
||||
|
||||
function mp_retrieve_address(&$posts_list) {
|
||||
$locs = array();
|
||||
foreach ($posts_list as $post_value) {
|
||||
$content = $post_value->post_content;
|
||||
$address = mp_extract_address($content);
|
||||
$lat_lng = mp_convert_coordinates($address);
|
||||
array_push($locs, $lat_lng);
|
||||
};
|
||||
return $locs;
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -1,8 +1,10 @@
|
||||
<?php
|
||||
|
||||
function mp_url_api() {
|
||||
global $mp_api_key;
|
||||
$mp_url = array(
|
||||
'src' => 'https://maps.googleapis.com/maps/api/js',
|
||||
'key' => 'AIzaSyCvdGV2ssD4ov4a9CuIlQhoJyz5gWWiSvE',
|
||||
'key' => $mp_api_key,
|
||||
'callback' => 'mp_init_map',
|
||||
);
|
||||
$mp_src = "";
|
||||
|
||||
Reference in New Issue
Block a user