269 lines
7.1 KiB
PHP
269 lines
7.1 KiB
PHP
<?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__) . '/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');
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
* global variables :
|
|
*/
|
|
|
|
$mp_icon_base_url = '/wp-content/plugins/map_prof/images/';
|
|
$mp_icon_url = $mp_icon_base_url . $mp_icon_file;
|
|
$mp_icon_cluster_url = $mp_icon_base_url . $mp_icon_cluster_file;
|
|
|
|
$mp_icon_size = [40, 40];
|
|
if (isset($mp_settings_icon_size)) {
|
|
if (is_array($mp_settings_icon_size)) {
|
|
if (count($mp_settings_icon_size) === 2) {
|
|
if ( is_numeric($mp_settings_icon_size[0]) && is_numeric($mp_settings_icon_size[1]) ) {
|
|
$mp_icon_size = $mp_settings_icon_size;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$mp_icon_color = "#ba197a";
|
|
if (isset($mp_settings_icon_color))
|
|
$mp_icon_color = $mp_settings_icon_color;
|
|
|
|
$mp_icon_size_factor = 2.5;
|
|
if (isset($mp_settings_icon_size_factor))
|
|
$mp_icon_size_factor = $mp_settings_icon_size_factor;
|
|
|
|
$mp_coordinates_default = (object)["lat" => 46.227638, "lng" => 2.213749]; // france
|
|
if (isset($mp_settings_coordinates_default))
|
|
$mp_coordinates_default = $mp_settings_coordinates_default;
|
|
|
|
$mp_icon_color = "#ba197a";
|
|
$mp_icon_color_back = "#ffffff99";
|
|
if (isset($mp_settings_icon_color))
|
|
$mp_icon_color = $mp_settings_icon_color;
|
|
if (isset($mp_settings_icon_color_back))
|
|
$mp_icon_color_back = $mp_settings_icon_color_back;
|
|
|
|
$mp_icon_stroke_width = 6;
|
|
if (isset($mp_settings_icon_stroke_width))
|
|
$mp_icon_stroke_width = $mp_settings_icon_stroke_width;
|
|
|
|
|
|
/**
|
|
* to add a tag, like defer or async, to a script
|
|
*/
|
|
|
|
function mp_tag_scripts($tag, $handle) {
|
|
if ('mp_googe_api' === $handle)
|
|
return str_replace(' src="', ' async src="', $tag);
|
|
return $tag;
|
|
}
|
|
add_filter('script_loader_tag', 'mp_tag_scripts', 10, 2);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
* when 'shortcode' found in page, enqueue scripts and styles,
|
|
* run php script, and replace shortcode by return value
|
|
*/
|
|
|
|
function mp_add_div() {
|
|
|
|
// https://developers.google.com/maps/documentation/javascript/marker-clustering
|
|
$marker_clusterer = "https://unpkg.com/@googlemaps/markerclusterer/dist/index.min.js";
|
|
|
|
wp_enqueue_style('mp_style', plugins_url('styles/mp_style.css', __FILE__), '', '', false);
|
|
wp_enqueue_style('mp_style_info_windows', plugins_url('styles/mp_info_windows.css', __FILE__), '', '', false);
|
|
wp_enqueue_style('mp_style_zoom', plugins_url('styles/mp_zoom.css', __FILE__), '', '', false);
|
|
|
|
wp_enqueue_script('mp_info_window', plugins_url('scripts/mp_info_window.js', __FILE__), '', '', false);
|
|
wp_enqueue_script('mp_create_markers', plugins_url('scripts/mp_create_markers.js', __FILE__), '', '', false);
|
|
wp_enqueue_script('mp_draw_clusters', plugins_url('scripts/mp_draw_clusters.js', __FILE__), '', '', false);
|
|
wp_enqueue_script('mp_marker_clusterer', $marker_clusterer, '', '', true);
|
|
wp_enqueue_script('mp_init_map', plugins_url('scripts/mp_init_map.js', __FILE__), ['mp_marker_clusterer'],'', true);
|
|
wp_enqueue_script('mp_google_api', mp_url_api(), ['mp_init_map'], '', true);
|
|
|
|
/*
|
|
pays
|
|
ville
|
|
categorie
|
|
*/
|
|
$pays = array();
|
|
mp_add_to_scripts($pays);
|
|
|
|
$mp_api_script = '
|
|
<div id="ljdp_map_wrapper">
|
|
<div id="ljdp_map"></div>
|
|
<div id="ljdp_map_filters">
|
|
';
|
|
foreach ($pays as $loc) {
|
|
$mp_api_script .= '<p>';
|
|
$mp_api_script .= $loc;
|
|
$mp_api_script .= '</p>';
|
|
}
|
|
$mp_api_script .= '
|
|
</div>
|
|
</div>
|
|
';
|
|
|
|
return $mp_api_script;
|
|
}
|
|
add_shortcode('lejourduprof_map', 'mp_add_div');
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
* when a post is saved or published or updated,
|
|
* find its coordinates
|
|
*/
|
|
function post_published_coordinates($id, $post) {
|
|
|
|
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 ) )
|
|
update_post_meta( $id, 'coordinates', $coordinates );
|
|
|
|
}
|
|
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 );
|
|
|
|
?>
|