Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Basic Google Maps Placemarksの拡張プラグイン
<?php
/*
Plugin Name: Basic Google Maps Placemarks Extension
Version: 0.1-alpha
Description: Basic Google Maps Placemarksの拡張プラグイン
Author: kurozumi
Author URI: http://a-zuim.net
Plugin URI: http://a-zuim.net
Text Domain: basic-google-maps-placemarks-extension
Domain Path: /languages
*/
$basic_google_maps_placemarks_extension = new BasicGoogleMapsPlacemarks_Extension;
$basic_google_maps_placemarks_extension->register();
class BasicGoogleMapsPlacemarks_Extension
{
private $search_id;
private $search_name;
private $plugin_file = "basic-google-maps-placemarks/basic-google-maps-placemarks.php";
public function register()
{
register_activation_hook( __FILE__, array( $this, 'register_activation_hook' ) );
add_action('plugins_loaded', array($this, 'plugins_loaded'));
}
public function register_activation_hook()
{
if(!$this->is_plugin_active())
wp_redirect( self_admin_url("plugins.php?deactivate=true") );
}
public function plugins_loaded()
{
if(!$this->is_plugin_active())
wp_redirect( self_admin_url("plugins.php?deactivate=true") );
$this->search_id = BasicGoogleMapsPlacemarks::PREFIX . "map-search";
$this->search_name = BasicGoogleMapsPlacemarks::PREFIX . "search-address";
add_action(BasicGoogleMapsPlacemarks::PREFIX . 'shortcode-bgmp-map-after', array($this, 'shortcode_bgmp_map_after'));
add_action('really_simple_csv_importer_post_saved', array($this, 'really_simple_csv_importer_post_saved'));
add_action('really_simple_csv_importer_save_meta', array($this, 'really_simple_csv_importer_save_meta'), 99, 3);
add_filter(BasicGoogleMapsPlacemarks::PREFIX . 'get-placemarks-return', array($this, 'get_placemarks_return'));
add_filter(BasicGoogleMapsPlacemarks::PREFIX . 'map-options', array($this, 'map_options'));
add_filter(BasicGoogleMapsPlacemarks::PREFIX . 'post-type-params', array($this, 'post_type_params'));
}
/**
* プラグインの有効化チェック
* @return boolean
*/
public function is_plugin_active()
{
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
// Basic GoogleMaps Placemarksプラグインが有効化されているかチェック
if(!is_plugin_active($this->plugin_file)) {
// プラグインを停止
deactivate_plugins( __FILE__ );
return false;
}
return true;
}
/**
* 吹き出しの内容を変更
* @param type $placemarks
* @return type
*/
public function get_placemarks_return($placemarks)
{
foreach ($placemarks as &$placemark)
{
$meta = get_post_custom($placemark['id']);
$placemark['details'] = sprintf('〒%s', $meta[BasicGoogleMapsPlacemarks::PREFIX . 'zipcode'][0]) . "<br />" .
$meta[BasicGoogleMapsPlacemarks::PREFIX . 'address'][0] . "<br />" .
sprintf("TEL:%s", $meta[BasicGoogleMapsPlacemarks::PREFIX . 'tel'][0])
;
}
return $placemarks;
}
/**
* 住所検索に対応させる
* @global type $bgmp
* @param type $options
* @return type
*/
public function map_options($options)
{
global $bgmp;
$haveCoordinates = true;
if (isset($_REQUEST[$this->search_name])) {
if (empty($_REQUEST[$this->search_name]))
$haveCoordinates = false;
else {
$coordinates = $bgmp->geocode($_REQUEST[$this->search_name]);
if (!$coordinates)
$haveCoordinates = false;
}
if ($haveCoordinates) {
$options['latitude'] = $coordinates['latitude'];
$options['longitude'] = $coordinates['longitude'];
}
}
return $options;
}
/**
* 地図の下に住所検索フォームを設置
*/
public function shortcode_bgmp_map_after()
{
$id = $this->search_id;
$name = $this->search_name;
$value = esc_attr($_REQUEST[$name]);
echo <<< __HTML__
<div id="{$id}">
<form action="./" method="POST">
<input type="text" name="{$name}" value="{$value}" />
<input type="submit" value="検索" />
</form>
</div>
__HTML__;
}
public function post_type_params($postTypeParams)
{
$postTypeParams['supports'] = array('title', 'editor', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields');
return $postTypeParams;
}
/**
* Really Simple CSV Importerに対応
*
* bgmp_addressカラムを追加して住所を入力すると、
* 自動でBasic Google Maps Placemarks用の経緯度が登録されます
*
* @global type $bgmp
* @param type $post
* @return type
*/
public function really_simple_csv_importer_post_saved($post)
{
global $bgmp;
$coordinates = false;
if (!$post || $post->post_type != BasicGoogleMapsPlacemarks::POST_TYPE || !current_user_can('edit_posts'))
return;
if (( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) || $post->post_status == 'auto-draft')
return;
// 住所に変更がないか確認
if(get_transient('bgmp-extension-check-address'))
return;
// 住所を取得して経緯度を登録する
$address = get_post_meta($post->ID, BasicGoogleMapsPlacemarks::PREFIX . 'address', true);
if ($address)
$coordinates = $bgmp->geocode($address);
if ($coordinates) {
update_post_meta($post->ID, BasicGoogleMapsPlacemarks::PREFIX . 'latitude', $coordinates['latitude']);
update_post_meta($post->ID, BasicGoogleMapsPlacemarks::PREFIX . 'longitude', $coordinates['longitude']);
} else {
update_post_meta($post->ID, BasicGoogleMapsPlacemarks::PREFIX . 'latitude', '');
update_post_meta($post->ID, BasicGoogleMapsPlacemarks::PREFIX . 'longitude', '');
}
}
/**
* 住所に変更がないか確認
*
* @global type $bgmp
* @param type $meta
* @param type $post
* @param type $is_update
* @return type
*/
public function really_simple_csv_importer_save_meta($meta, $post, $is_update)
{
global $bgmp;
if($is_update === false)
return $meta;
if($post['post_type'] != BasicGoogleMapsPlacemarks::POST_TYPE)
return $meta;
$old_address = get_post_meta($post['ID'], BasicGoogleMapsPlacemarks::PREFIX . 'address', true);
$new_address = $meta[BasicGoogleMapsPlacemarks::PREFIX . 'address'];
if($old_address == $new_address)
set_transient('bgmp-extension-check-address', 1, 10);
else
set_transient('bgmp-extension-check-address', 0, 10);
return $meta;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment