<?php
/*
Plugin Name: map_prof
Plugin URI:
Description: add/remove locations on map at publication/deletion of posts
Author: hugogogo
Version: 1.0.0
Author URI:
*/





/**
 * inclusions :
 */

include_once(dirname(__FILE__) . '/utils/mp_console_log.php');

require_once(dirname(__FILE__) . '/mp_enqueue.php');

require_once(dirname(__FILE__) . '/settings/mp_required.php');
require_once(dirname(__FILE__) . '/settings/mp_optionnals.php');
require_once(dirname(__FILE__) . '/settings/mp_globals.php');
require_once(dirname(__FILE__) . '/settings/mp_url_api.php');

require_once(dirname(__FILE__) . '/srcs/errors/mp_address_errors.php');

require_once(dirname(__FILE__) . '/srcs/map/mp_add_to_scripts.php');
require_once(dirname(__FILE__) . '/srcs/map/mp_create_div.php');
require_once(dirname(__FILE__) . '/srcs/map/mp_get_events.php');
require_once(dirname(__FILE__) . '/srcs/map/mp_get_filters.php');
require_once(dirname(__FILE__) . '/srcs/map/mp_get_locations.php');

require_once(dirname(__FILE__) . '/srcs/map_posts/mp_post_events_pages.php');

require_once(dirname(__FILE__) . '/srcs/publish/mp_get_coordinates.php');
require_once(dirname(__FILE__) . '/srcs/publish/mp_update_publish.php');







/**
 * when 'shortcode' found in page, enqueue scripts and styles,
 * run php script, and replace shortcode by return value
 */

function mp_ljdp_map() {

	mp_enqueue_scripts_and_styles();

	$events = mp_get_published_events();
	$locations = mp_sort_events($events);
	$filters = mp_get_filters($events);
	// if post event instead of map page, change coordinate and zoom
	mp_post_event_pages_setting();

	mp_add_to_scripts(array(
		"locations" => $locations,
		"filters" => $filters,
	));

	return mp_create_div($filters);

}
add_shortcode('lejourduprof_map', 'mp_ljdp_map');





/**
 * re-publish posts
 */

function mp_add_update_button() {
	return mp_create_republish_button();
}
add_shortcode('ljdp_update_publish', 'mp_add_update_button');




/**
 * errors map
 */

function mp_errors_map() {
	return mp_find_address_errors();
}
add_shortcode('ljdp_errors_map', 'mp_errors_map');





/**
 * when a post is saved or published or updated,
 * find its coordinates
 */

function post_published_coordinates($id, $post) {

	$location = mp_get_coordinates($id);

	if ( ! add_post_meta( $id, 'location', $location, true ) )
		update_post_meta( $id, 'location', $location );

}
add_action( 'publish_post', 'post_published_coordinates', 10, 2 );




/**
 * trying to prevent publication with message if bad country address
 */

//	// notice on post edit :
//	//   https://developer.wordpress.org/block-editor/how-to-guides/notices/
//	//   https://developer.wordpress.org/block-editor/how-to-guides/javascript/loading-javascript/
//	//   https://developer.wordpress.org/block-editor/reference-guides/components/notice/https://developer.wordpress.org/block-editor/reference-guides/components/notice/
//	// modals : https://developer.wordpress.org/block-editor/reference-guides/components/modal/
//
//	// https://www.wpbeginner.com/wp-tutorials/how-to-add-admin-notices-in-wordpress/
//	// https://digwp.com/2016/05/wordpress-admin-notices/
//	function my_notice() {
//		//echo '<div class="notice notice-warning is-dismissible">
//		//echo '<div class="notice notice-info is-dismissible">
//		//echo '<div class="notice notice-success is-dismissible">
//		//echo '<div class="components-notice is-error is-dismissible">
//	//	echo '
//	//		<div class="notice notice-error is-dismissible">
//	//			<p>my message</p>
//	//		</div>
//	//	';
//	
//		mp_console_log("\n       get values:");
//		foreach ($_GET as $key => $value) {
//			mp_console_log("  " . $key . ": " . $value);
//		}
//		$screen = get_current_screen();
//		mp_console_log("\n       screen values:");
//		foreach ($screen as $key => $value) {
//			mp_console_log("  " . $key . ": " . $value);
//		}
//	}
//	add_action( 'admin_notices', 'my_notice');
//	
//	// 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 my_publish_check($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', 'my_publish_check', 10, 3);
//	
//	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');
//	
//	// 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);
//	
//	//function test1($id, $post) {
//	//	mp_console_log("save_post - " . $post->post_status);
//	//}
//	//add_action( 'save_post', 'test1', 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
//	*/


?>
