From 7c6e480d1afa7db2fd5cf7693160622008cdb7f3 Mon Sep 17 00:00:00 2001
From: lenovo
Date: Mon, 31 Oct 2022 21:07:44 +0100
Subject: [PATCH] posts automatically add markers yeeeah
---
srcs/plugins/google_map/google_map | 1 +
srcs/plugins/map_prof/map_prof_hooks.php | 15 +++++
srcs/plugins/map_prof/mp_add_to_scripts.php | 63 ++++---------------
.../map_prof/mp_coordinates_from_posts.php | 43 +++++++++++++
srcs/plugins/map_prof/mp_url_api.php | 4 +-
5 files changed, 75 insertions(+), 51 deletions(-)
create mode 120000 srcs/plugins/google_map/google_map
create mode 100644 srcs/plugins/map_prof/mp_coordinates_from_posts.php
diff --git a/srcs/plugins/google_map/google_map b/srcs/plugins/google_map/google_map
new file mode 120000
index 0000000..d4d132a
--- /dev/null
+++ b/srcs/plugins/google_map/google_map
@@ -0,0 +1 @@
+/home/www-data/google_map
\ No newline at end of file
diff --git a/srcs/plugins/map_prof/map_prof_hooks.php b/srcs/plugins/map_prof/map_prof_hooks.php
index 7dcd655..edfe1c0 100644
--- a/srcs/plugins/map_prof/map_prof_hooks.php
+++ b/srcs/plugins/map_prof/map_prof_hooks.php
@@ -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
*/
diff --git a/srcs/plugins/map_prof/mp_add_to_scripts.php b/srcs/plugins/map_prof/mp_add_to_scripts.php
index 3434c0a..939ad67 100644
--- a/srcs/plugins/map_prof/mp_add_to_scripts.php
+++ b/srcs/plugins/map_prof/mp_add_to_scripts.php
@@ -1,64 +1,27 @@
";
-// //$content .= "nb posts published : ";
-// //$content .= count($posts_list);
-// //$content .= "
";
-// //foreach ($posts_list as $post_key => $post_value) {
-// // $content .= "";
-// // $content .= "post content : ";
-// // $content .= $post_key;
-// // $content .= " : ";
-// // $content .= "
";
-// // foreach ($post_value as $key => $value) {
-// // $content .= "- [";
-// // $content .= $key;
-// // $content .= "]: [";
-// // $content .= $value;
-// // $content .= "]
";
-// // }
-// // $content .= "
";
-// //}
+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');
diff --git a/srcs/plugins/map_prof/mp_coordinates_from_posts.php b/srcs/plugins/map_prof/mp_coordinates_from_posts.php
new file mode 100644
index 0000000..b18364c
--- /dev/null
+++ b/srcs/plugins/map_prof/mp_coordinates_from_posts.php
@@ -0,0 +1,43 @@
+ -1,
+ 'post_status' => 'publish',
+ );
+ return get_posts($get_posts_args);
+}
+
+function mp_extract_address(&$content) {
+ $begin = "adresse: ";
+ $end = "
";
+ $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;
+}
+
+?>
diff --git a/srcs/plugins/map_prof/mp_url_api.php b/srcs/plugins/map_prof/mp_url_api.php
index f77eec1..bfb5e9d 100644
--- a/srcs/plugins/map_prof/mp_url_api.php
+++ b/srcs/plugins/map_prof/mp_url_api.php
@@ -1,8 +1,10 @@
'https://maps.googleapis.com/maps/api/js',
- 'key' => 'AIzaSyCvdGV2ssD4ov4a9CuIlQhoJyz5gWWiSvE',
+ 'key' => $mp_api_key,
'callback' => 'mp_init_map',
);
$mp_src = "";