resolve some errors in php logic, like strlen with str containing space that is interpreted as array instead of string
This commit is contained in:
80
plug/map_prof/srcs/publish/mp_get_coordinates.php
Normal file
80
plug/map_prof/srcs/publish/mp_get_coordinates.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
function mp_get_address($id) {
|
||||
$presentiel = get_field("mode", $id);
|
||||
|
||||
if (empty($presentiel))
|
||||
return null;
|
||||
|
||||
// irl or online
|
||||
if ($presentiel[0] === "En présentiel")
|
||||
$address = get_field("adresse", $id);
|
||||
else
|
||||
$address = get_field("pays", $id);
|
||||
|
||||
return $address;
|
||||
}
|
||||
|
||||
function mp_get_coordinates($id) {
|
||||
global $mp_api_key_geo;
|
||||
$event = (object)[];
|
||||
$location = (object)[];
|
||||
$location->coordinates = null;
|
||||
$location->street = "";
|
||||
$location->city = "";
|
||||
$location->country = "";
|
||||
$location->address = "";
|
||||
$location->approximate = false;
|
||||
|
||||
$address = mp_get_address($id);
|
||||
//mp_console_log("adresse: " . $address);
|
||||
|
||||
// get coordinates from google maps api
|
||||
$geolocation = 'https://maps.googleapis.com/maps/api/geocode/json'
|
||||
. '?language=fr'
|
||||
. '&address=' . urlencode($address)
|
||||
. '&key=' . $mp_api_key_geo;
|
||||
//mp_console_log("geolocation:");
|
||||
//mp_console_log($geolocation);
|
||||
$jsoncontent = file_get_contents($geolocation);
|
||||
mp_console_log("jsoncontent:");
|
||||
mp_console_log($jsoncontent);
|
||||
|
||||
// extract coordinates from json
|
||||
// https://developers.google.com/maps/documentation/geocoding/requests-geocoding#Types
|
||||
$content = json_decode($jsoncontent);
|
||||
$location->coordinates = $content->results[0]->geometry->location;
|
||||
$location->address = $content->results[0]->formatted_address;
|
||||
foreach ($content->results[0]->address_components as $component) {
|
||||
if (in_array("street_number", $component->types))
|
||||
$location->street = $component->long_name;
|
||||
else if (in_array("route", $component->types)) {
|
||||
if (strlen($location->street) != 0)
|
||||
$location->street .= " ";
|
||||
$location->street .= $component->long_name;
|
||||
}
|
||||
else if (in_array("locality", $component->types))
|
||||
$location->city = $component->long_name;
|
||||
else if (in_array("postal_town", $component->types)) {
|
||||
if (strlen($location->city) != 0)
|
||||
$location->city .= "/";
|
||||
$location->city .= $component->long_name;
|
||||
}
|
||||
else if (in_array("country", $component->types))
|
||||
$location->country = $component->long_name;
|
||||
}
|
||||
if ($content->results[0]->geometry->location_type == "APPROXIMATE")
|
||||
$location->approximate = true;
|
||||
|
||||
// clean strings
|
||||
foreach ($location as $value) {
|
||||
if (gettype($value) != "string")
|
||||
continue;
|
||||
$value = trim($value, " ");
|
||||
}
|
||||
|
||||
return $location;
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
48
plug/map_prof/srcs/publish/mp_update_publish.php
Normal file
48
plug/map_prof/srcs/publish/mp_update_publish.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
|
||||
function mp_update_publish() {
|
||||
$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',
|
||||
));
|
||||
};
|
||||
}
|
||||
add_action( 'admin_post_update_publish', 'mp_update_publish' );
|
||||
add_action( 'admin_post_nopriv_update_publish', 'mp_update_publish' );
|
||||
|
||||
// https://developer.wordpress.org/reference/hooks/admin_post_action/
|
||||
// https://wordpress.stackexchange.com/questions/309440/wordpress-plugin-how-to-run-function-when-button-is-clicked
|
||||
function mp_create_republish_button() {
|
||||
$content = '
|
||||
<br />
|
||||
<div style="border:1px solid black;padding:20px;">
|
||||
<h2>mettre a jour les publications</h2>
|
||||
<p>
|
||||
cliquez sur ce bouton pour mettre a jour toutes les publications
|
||||
<br />
|
||||
une nouvelle page vide va s\'ouvrir dans un nouvel onglet, vous pouvez la fermer
|
||||
</p>
|
||||
<form action="'.admin_url('admin-post.php').'" method="post" target="_blank">
|
||||
<input type="hidden" name="action" value="update_publish">
|
||||
<input type="submit" value="mettre a jour">
|
||||
</form>
|
||||
</div>
|
||||
<br />
|
||||
';
|
||||
return $content;
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user