coordinates now added when published

This commit is contained in:
lenovo
2022-11-05 11:25:02 +01:00
parent 66aa25c4b0
commit 79ed319b8f
3 changed files with 100 additions and 100 deletions

View File

@@ -17,6 +17,7 @@ Author URI:
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');
require_once(dirname(__FILE__) . '/mp_get_coordinates.php');
require_once(dirname(__FILE__) . '/settings/mp_required.php');
require_once(dirname(__FILE__) . '/settings/mp_optionnals.php');
@@ -90,8 +91,9 @@ add_shortcode('lejourduprof_map', 'mp_add_div');
* when a post is published, check its coordinates
*/
function post_published_coordinates($id, $post) {
if ( ! add_post_meta( $id, 'coordinates', 'banana', true ) ) {
update_post_meta( $id, 'coordinates', 'banana' );
$coordinates = mp_get_coordinates($id);
if ( ! add_post_meta( $id, 'coordinates', $coordinates, true ) ) {
update_post_meta( $id, 'coordinates', $coordinates );
}
}
add_action( 'publish_post', 'post_published_coordinates', 10, 2 );

View File

@@ -0,0 +1,34 @@
<?php
function mp_get_coordinates($id) {
global $mp_api_key;
// get address informations
$fields = array(
"adresse",
"ville",
"pays",
);
$event = (object)[];
foreach($fields as $field) {
$event->$field = get_field($field, $id);
}
// concat as an address
$address = $event->adresse . ","
. $event->ville . ","
. $event->pays;
// get coordinates from google maps api
$geolocation = 'https://maps.googleapis.com/maps/api/geocode/json'
. '?address=' . urlencode($address)
. '&key=' . $mp_api_key;
$jsoncontent = file_get_contents($geolocation);
// extract coordinates from json
$content = json_decode($jsoncontent);
$coordinates = $content->results[0]->geometry->location;
return $coordinates;
}
?>

View File

@@ -42,85 +42,9 @@
--- 36: "nom"
*/
// FUNCTION TO RETRIEVE FORMS FIELDS
// not used anymore
//
//function mp_get_form_fields() {
// $get_form_args = array(
// 'numberposts' => -1,
// 'post_status' => 'publish',
// 'post_type' => 'acf-field',
// );
// $forms = get_posts($get_form_args);
// $fields = [];
// foreach ($forms as $form) {
// $field = $form->post_excerpt;
// array_push($fields, $field);
// }
// return $fields;
//}
function mp_get_published_posts() {
// TESTS
// script to publish or unpublish posts
//
// $post_args = array(
// 'numberposts' => -1,
// 'post_status' => 'draft',
// //'post_status' => 'publish',
// 'post_type' => 'post',
// );
// $post_list = get_posts($post_args);
// foreach ($post_list as $post) {
// wp_update_post(array(
// 'ID' => $post->ID,
// //'post_status' => 'draft',
// 'post_status' => 'publish',
// ));
// }
$get_posts_args = array(
'numberposts' => -1,
//'post_status' => 'publish',
'post_status' => 'draft',
'post_type' => 'post',
);
$posts_published = get_posts($get_posts_args);
// // "get_post"
// // https://developer.wordpress.org/reference/functions/get_posts/
// $post = $posts_published[0];
// mp_console_log("get post:");
// mp_console_log($post);
// // https://developer.wordpress.org/reference/functions/get_post_meta/
// $post_meta = get_post_meta($post->ID);
// mp_console_log("get post meta:");
// mp_console_log($post_meta);
// // same as "get_post_meta", but cannot specify a key
// // and is optimized for cache
// // https://developer.wordpress.org/reference/functions/get_post_custom/
// $post_custom = get_post_custom($post->ID);
// mp_console_log("get post custom:");
// mp_console_log($post_custom);
// // get fiels 'coordinates'
// $field_coordinates = get_field('coordinates', $post->ID);
// mp_console_log("get field coordinates:");
// mp_console_log($field_coordinates);
// // get fiels 'ID'
// $field_id = get_field('ID', $post->ID);
// mp_console_log("get field ID:");
// mp_console_log($field_id);
// // get fiels 'post_title'
// $field_title = get_field('post_title', $post->ID);
// mp_console_log("get field title:");
// mp_console_log($field_title);
return $posts_published;
}
/*
GET POST :
GET POST EXEMPLE :
ID: 29693
comment_count: "0"
comment_status: "closed"
@@ -147,11 +71,64 @@ function mp_get_published_posts() {
to_ping: ""
*/
function mp_fill_fields_value($id) {
// get_field is an ACF function
// in "pure" worpdress use :
// get_post_meta or get_post_custom_values
// FUNCTION TO RETRIEVE FORMS FIELDS
// not used anymore
//
//function mp_get_form_fields() {
// $get_form_args = array(
// 'numberposts' => -1,
// 'post_status' => 'publish',
// 'post_type' => 'acf-field',
// );
// $forms = get_posts($get_form_args);
// $fields = [];
// foreach ($forms as $form) {
// $field = $form->post_excerpt;
// array_push($fields, $field);
// }
// return $fields;
//}
function mp_get_published_posts() {
// FOR TESTS
// script to publish or unpublish posts
//
// $post_args = array(
// 'numberposts' => -1,
// 'post_status' => 'draft',
// //'post_status' => 'publish',
// 'post_type' => 'post',
// );
// $post_list = get_posts($post_args);
// foreach ($post_list as $post) {
// wp_update_post(array(
// 'ID' => $post->ID,
// //'post_status' => 'draft',
// 'post_status' => 'publish',
// ));
// }
$get_posts_args = array(
'numberposts' => -1,
'post_status' => 'publish',
//'post_status' => 'draft',
'post_type' => 'post',
);
$posts_published = get_posts($get_posts_args);
return $posts_published;
}
function mp_fill_fields_value($id) {
/*
* get_field is an ACF function
* in "pure" worpdress use :
* get_post_meta or get_post_custom_values
*/
// add fields
$fields = array(
"heure_de_debut",
"heure_de_fin",
@@ -168,28 +145,16 @@ function mp_fill_fields_value($id) {
foreach($fields as $field) {
$event->$field = get_field($field, $id);
}
// add mode irl or online (irl: true | false)
$presentiel = get_field("mode", $id);
$event->irl = false;
if ($presentiel[0] === "En présentiel")
$event->irl = true;
return $event;
}
function mp_get_coordinates(&$marker) {
global $mp_api_key;
$address = $marker->adresse . ","
. $marker->ville . ","
. $marker->pays;
$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_get_published_events() {
$posts_list = mp_get_published_posts();
$events = [];
@@ -197,7 +162,6 @@ function mp_get_published_events() {
$event = mp_fill_fields_value($post->ID);
$event->id = $post->ID;
$event->title = $post->post_title;
// $event->coordinates = mp_get_coordinates($event);
array_push($events, $event);
}
mp_console_log($events);