simple-location/simple-location.php

106 lines
2.9 KiB
PHP
Raw Normal View History

2014-01-02 14:41:00 +00:00
<?php
/*
Plugin Name: Simple Location
Plugin URI: http://www.danballard.com/
Description: Auto tags posts with city, country info
Version: 0.1
Author: Dan Ballard
Author URI: http://www.danballard.com
Minimum WordPress Version Required: 3.8
Tested up to: 3.8
*/
2014-01-07 13:52:48 +00:00
// register all handlers
2014-01-02 14:41:00 +00:00
if ( is_admin() ) {
add_action( 'admin_menu', 'add_options_menu' );
add_action( 'admin_init', 'reg_settings' );
2014-01-07 01:26:41 +00:00
add_action( 'admin_menu', 'add_meta_to_edit' );
add_action( 'edit_attachment', 'simple_location_post_save' );
add_action( 'save_post', 'simple_location_post_save' );
2014-01-02 14:41:00 +00:00
}
2014-01-07 13:52:48 +00:00
// resgister the SL options page in the WP settings menu
2014-01-02 14:41:00 +00:00
function add_options_menu() {
add_options_page('Simple Location Options', 'Simple Location', 'manage_options', 'simple-location', 'options_page');
}
2014-01-07 13:52:48 +00:00
// Display the SL admin options page
function options_page() {
include( plugin_dir_path(__FILE__) . 'options.php');
}
// Register the options page options that WP should store
function reg_settings() {
register_setting( 'simple-location-options', 'simple-location-api-key' );
}
// Register the SL meta edit box on WP pist edit pages
2014-01-07 01:26:41 +00:00
function add_meta_to_edit() {
add_meta_box('simple-location-meta', 'Simple Location', 'simple_location_meta', 'post', 'normal');
}
2014-01-07 13:52:48 +00:00
// display the post edit page SL meta edit box
function simple_location_meta() {
include( plugin_dir_path(__FILE__) . 'post-form.php');
}
// Hook for posts being saved.
// If he city or country field is blank, autopopulate from ipinfodb
2014-01-07 01:26:41 +00:00
function simple_location_post_save($post_id) {
if ($_POST['city'] == '' || $_POST['country'] == '') {
$key = get_option('simple-location-api-key');
if ($key != '') {
include('ip2locationlite.class.php');
$ipLite = new ip2location_lite;
$ipLite->setKey($key);
$locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']);
2014-01-07 01:59:19 +00:00
}
2014-01-07 01:26:41 +00:00
}
2014-01-07 13:52:48 +00:00
2014-01-07 01:26:41 +00:00
if ($_POST['city'] == '') {
2014-01-07 04:06:37 +00:00
$_POST['city'] = ucfirst(strtolower($locations['cityName']));
2014-01-07 01:26:41 +00:00
}
update_post_meta($post_id, '_simple_location_city', sanitize_text_field($_POST['city']));
2014-01-07 13:52:48 +00:00
2014-01-07 01:26:41 +00:00
if ($_POST['country'] == '') {
2014-01-07 04:06:37 +00:00
$_POST['country'] = ucfirst(strtolower($locations['countryName']));
2014-01-07 01:26:41 +00:00
}
update_post_meta($post_id, '_simple_location_country', sanitize_text_field($_POST['country']));
}
2014-01-07 13:52:48 +00:00
function get_city() {
global $post;
return get_post_meta($post->ID, '_simple_location_city', true);
2014-01-07 01:26:41 +00:00
}
2014-01-07 13:52:48 +00:00
function get_country() {
global $post;
return get_post_meta($post->ID, '_simple_location_country', true);
2014-01-02 14:41:00 +00:00
}
2014-01-07 13:52:48 +00:00
// City shortcode
function shortcode_city( $attrs ) {
return get_city();
2014-01-02 14:41:00 +00:00
}
2014-01-07 01:26:41 +00:00
2014-01-07 13:52:48 +00:00
// Country shortcode
function shortcode_country( $attrs ) {
return get_country();
}
// non admin registering of shortcodes
add_shortcode( 'city', 'shortcode_city' );
add_shortcode( 'country', 'shortcode_country' );
// Tempalte function for city
function simple_location_city() {
echo get_city();
}
// Template function for country
function simple_location_country() {
echo get_country();
}