From d326b6f51ea3eb76a273e25a6e6d90ac9a5ecbe5 Mon Sep 17 00:00:00 2001 From: lenovo Date: Wed, 9 Nov 2022 09:30:49 +0100 Subject: [PATCH] abort preventiont of publication if address country invalid --- srcs/plugins/map_prof/map_prof_hooks.php | 125 +++++++++--------- .../map_prof/scripts/mp_publish_error.js | 21 +++ 2 files changed, 87 insertions(+), 59 deletions(-) create mode 100644 srcs/plugins/map_prof/scripts/mp_publish_error.js diff --git a/srcs/plugins/map_prof/map_prof_hooks.php b/srcs/plugins/map_prof/map_prof_hooks.php index 976a3e3..3e4d838 100644 --- a/srcs/plugins/map_prof/map_prof_hooks.php +++ b/srcs/plugins/map_prof/map_prof_hooks.php @@ -167,69 +167,76 @@ 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; +// // 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; +// +// // if this is a revision, get real post ID +// if ( $parent_id = wp_is_post_revision( $id ) ) +// $id = $parent_id; +// +// // 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) { +// +// // 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' => $id, 'post_status' => 'draft')); +// +// // re-hook this function again +// add_action('save_post', 'my_save_post'); +// } +// } +// add_action( 'save_post_post', 'test1', 10, 3); - // is the user allowed to edit the post or page ? - if ( !current_user_can( 'edit_post', $id ) ) - return; +// function publish_error() { +// wp_enqueue_script('mp_publish_error', +// plugins_url('scripts/mp_publish_error.js', __FILE__), +// array( 'wp-data' ), +// '', +// true +// ); +// } +// add_action( 'enqueue_block_editor_assets', 'publish_error'); - // is not being published ? - if ($post->post_status != "publish") - return; - - $prevent_publish= true; // set to true if data was invalid - if ($prevent_publish) { - - echo '
-

my message

-
'; - - // 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 '
-

my message

-
'; - 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); +// // https://wordpress.stackexchange.com/questions/42013/prevent-post-from-being-published-if-custom-fields-not-filled +// // https://wordpress.stackexchange.com/questions/15546/dont-publish-custom-post-type-post-if-a-meta-data-field-isnt-valid +// 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 diff --git a/srcs/plugins/map_prof/scripts/mp_publish_error.js b/srcs/plugins/map_prof/scripts/mp_publish_error.js new file mode 100644 index 0000000..5d1270e --- /dev/null +++ b/srcs/plugins/map_prof/scripts/mp_publish_error.js @@ -0,0 +1,21 @@ + +console.log("publish error"); + +wp.data.dispatch( 'core/notices' ).createNotice( + 'error', // Can be one of: success, info, warning, error. + 'impossible de publier : le pays est invalide', // Text string to display. + { + isDismissible: true, // Whether the user can dismiss the notice. + } +); + +//( function ( wp ) { +// console.log("publish error"); +// wp.data.dispatch( 'core/notices' ).createNotice( +// 'error', // Can be one of: success, info, warning, error. +// 'impossible de publie : le pays est invalide', // Text string to display. +// { +// isDismissible: true, // Whether the user can dismiss the notice. +// } +// ); +//} )( window.wp );