wip prevent post publish if wrong coordinates

This commit is contained in:
lenovo
2022-11-08 21:00:05 +01:00
parent cfc747bad3
commit 73d988eb6a
4 changed files with 157 additions and 41 deletions

View File

@@ -138,18 +138,131 @@ add_shortcode('lejourduprof_map', 'mp_add_div');
/** /**
* when a post is published, check its coordinates * when a post is saved or published or updated,
* find its coordinates
*/ */
function post_published_coordinates($id, $post) { function post_published_coordinates($id, $post) {
$coordinates_valid = true;
$coordinates = mp_get_coordinates($id, $coordinates_valid); if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( !isset( $_POST['ep_eventposts_nonce'] ) )
return;
if ( !wp_verify_nonce( $_POST['ep_eventposts_nonce'], plugin_basename( __FILE__ ) ) )
return;
// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ) )
return;
$coordinates = mp_get_coordinates($id);
if ( ! add_post_meta( $id, 'coordinates', $coordinates, true ) ) if ( ! add_post_meta( $id, 'coordinates', $coordinates, true ) )
update_post_meta( $id, 'coordinates', $coordinates ); update_post_meta( $id, 'coordinates', $coordinates );
if ( ! add_post_meta( $id, 'coordinates_valid', $coordinates_valid, true ) )
update_post_meta( $id, 'coordinates_valid', $coordinates_valid );
} }
add_action( 'publish_post', 'post_published_coordinates', 10, 2 ); add_action( 'save_post', 'post_published_coordinates', 10, 2 );
// https://wordpress.stackexchange.com/questions/42013/prevent-post-from-being-published-if-custom-fields-not-filled
// https://developer.wordpress.org/reference/hooks/save_post/
function test1($id, $post, $update) {
// is doing autosaving ?
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// is post creation ?
if (!$update)
return;
// is the user allowed to edit the post or page ?
if ( !current_user_can( 'edit_post', $id ) )
return;
// is not being published ?
if ($post->post_status != "publish")
return;
$prevent_publish= true; // set to true if data was invalid
if ($prevent_publish) {
echo '<div class="notice notice-warning is-dismissible">
<p>my message</p>
</div>';
// unhook this function to prevent indefinite loop
remove_action('save_post', 'my_save_post');
// update the post to change post status
wp_update_post(array('ID' => $post_id, 'post_status' => 'draft'));
// re-hook this function again
add_action('save_post', 'my_save_post');
}
}
add_action( 'save_post_post', 'test1', 10, 3);
function my_notice() {
echo '<div class="notice notice-warning is-dismissible">
<p>my message</p>
</div>';
components-notice-list components-editor-notices__pinned
}
//add_action( 'admin_notices', 'my_notice');
function my_redirect_location($location,$post_id){
// if post was published...
if (isset($_POST['publish'])){
// obtain current post status
$status = get_post_status( $post_id );
// the post was 'published', but if it is still a draft, display draft message (10).
if($status=='draft')
$location = add_query_arg('message', 10, $location);
}
return $location;
}
//add_filter('redirect_post_location', 'my_redirect_location', 10, 2);
/*
modification rapide, publie -> brouillon : 1 - draft
modification rapide, brouillon -> brouillon : 1 - draft
modification rapide, brouillon -> publie : 1 - publish
add post : 1 - auto-draft
modification, publie -> brouillon : 2 - draft
modification, brouillon -> brouillon : 2 - draft
modification, brouillon -> publie : 2 - publish
*/
//function test2($id, $post) {
// mp_console_log("publish_post - " . $post->post_status);
//}
//add_action( 'publish_post', 'test2', 10, 2 );
/*
modification rapide, publie -> brouillon : /
modification rapide, brouillon -> brouillon : /
modification rapide, brouillon -> publie : 1 - publish
add post : /
modification, publie -> brouillon : /
modification, brouillon -> brouillon : /
modification, brouillon -> publie : 2 - publish
*/
//function save_post_coordinates($id, $post) {
// mp_console_log("coordinates is null");
//}
//add_action( 'publish_post', 'post_published_coordinates', 10, 2 );
?> ?>

View File

@@ -1,8 +1,25 @@
<?php <?php
// https://stackify.com/how-to-log-to-console-in-php/ // https://stackify.com/how-to-log-to-console-in-php/
function mp_console_log($output) { function mp_console_log($output) {
$js_code = 'console.log(' . json_encode($output, JSON_HEX_TAG) . ');'; $time = date('H:i');
$js_code = '<script>' . $js_code . '</script>'; $file = '/var/www/html/wp-debug.log';
echo $js_code; file_put_contents($file, $time . " " . $output . "\n", FILE_APPEND);
} }
// function mp_console_log($output) {
// $js_code = 'console.log(' . json_encode($output, JSON_HEX_TAG) . ');';
// $js_code = '<script>' . $js_code . '</script>';
// echo $js_code;
// }
// function mp_console_log($output, $with_script_tags = true) {
// $js_code = 'console.log(' . json_encode($output, JSON_HEX_TAG) .
// ');';
// if ($with_script_tags) {
// $js_code = '<script>' . $js_code . '</script>';
// }
// echo $js_code;
// }
?> ?>

View File

@@ -1,13 +1,12 @@
<?php <?php
function mp_get_coordinates($id, &$valid) { function mp_get_coordinates($id) {
global $mp_api_key; global $mp_api_key;
global $mp_coordinates_default;
$event = (object)[]; $event = (object)[];
$errors = 0;
$coordinates = null; $coordinates = null;
// get address // get address
$address = "";
$fields = array( $fields = array(
"adresse", "adresse",
"ville", "ville",
@@ -15,37 +14,18 @@ function mp_get_coordinates($id, &$valid) {
); );
foreach($fields as $field) { foreach($fields as $field) {
$address_section = get_field($field, $id); $address_section = get_field($field, $id);
$event->$field = $address_section; $address .= $address_section . ",";
if (strlen($address_section) == 0)
$errors++;
} }
// check errors
if ($errors > 0)
$valid = false;
else
$valid = true;
// concat as an address
$address = $event->adresse . ","
. $event->ville . ","
. $event->pays;
// get coordinates from google maps api // get coordinates from google maps api
if ($errors < 3) { $geolocation = 'https://maps.googleapis.com/maps/api/geocode/json'
$geolocation = 'https://maps.googleapis.com/maps/api/geocode/json' . '?address=' . urlencode($address)
. '?address=' . urlencode($address) . '&key=' . $mp_api_key;
. '&key=' . $mp_api_key; $jsoncontent = file_get_contents($geolocation);
$jsoncontent = file_get_contents($geolocation);
// extract coordinates from json // extract coordinates from json
$content = json_decode($jsoncontent); $content = json_decode($jsoncontent);
$coordinates = $content->results[0]->geometry->location; $coordinates = $content->results[0]->geometry->location;
}
// if null, set to default (by default, france)
if ($coordinates == null)
$coordinates = $mp_coordinates_default;
return $coordinates; return $coordinates;
} }

View File

@@ -127,7 +127,7 @@ function mp_get_published_posts() {
return $posts_published; return $posts_published;
} }
function mp_fill_fields_value($post, $id, &$pays) { function mp_fill_fields_value($id, &$pays) {
/* /*
* get_field is an ACF function * get_field is an ACF function
@@ -170,9 +170,15 @@ function mp_get_published_events(&$pays) {
$posts_list = mp_get_published_posts(); $posts_list = mp_get_published_posts();
$events = []; $events = [];
foreach ($posts_list as $post) { foreach ($posts_list as $post) {
$event = mp_fill_fields_value($post, $post->ID, $pays); $event = mp_fill_fields_value($post->ID, $pays);
$event->id = $post->ID; $event->id = $post->ID;
$event->title = $post->post_title; $event->title = $post->post_title;
// TEMP
if (strlen($event->pays) == 0) {
mp_console_log("pays nulle:");
mp_console_log($event);
}
//
array_push($events, $event); array_push($events, $event);
} }
mp_console_log("nombre de posts: " . count($events)); mp_console_log("nombre de posts: " . count($events));