Skip to content

Commit a256fb4

Browse files
committed
initial commit
0 parents  commit a256fb4

File tree

5 files changed

+294
-0
lines changed

5 files changed

+294
-0
lines changed

WP_Places.php

+193
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
<?php
2+
/**
3+
* Plugin Name: WP_Places
4+
* Version: 0.1-alpha
5+
* Description: Given location name saved with a Post search Google Places API Web Service and displays address, hours, phone number and link to website
6+
* Author: Gary Kovar
7+
* Author URI: http://binarygary.com
8+
* Plugin URI: http://www.binarygary.com/plugins/wp_places-plugin/
9+
* Text Domain: WP_PlacesReviews
10+
* @package WP_Places
11+
* License: GPL v3
12+
*
13+
* This program is free software: you can redistribute it and/or modify
14+
* it under the terms of the GNU General Public License as published by
15+
* the Free Software Foundation, either version 3 of the License, or
16+
* (at your option) any later version.
17+
*
18+
* This program is distributed in the hope that it will be useful,
19+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+
* GNU General Public License for more details.
22+
*
23+
* You should have received a copy of the GNU General Public License
24+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
25+
*/
26+
27+
require_once(dirname(__FILE__) . "/includes/googlePlaces.php");
28+
29+
30+
//Get the users google places key
31+
function WP_Places_add_settings_field() {
32+
register_setting('general', 'WP_Places_Google_Id_Setting', 'esc_attr');
33+
add_settings_field('WP_Places_Google_Id_Setting', '<label for="WP_Places_Google_Id_Setting">'.__('Google ID' , 'WP_Places_Google_Id_Setting' ).'</label>' , 'print_custom_field', 'general');
34+
35+
}
36+
function print_custom_field()
37+
{
38+
$value = get_option( 'WP_Places_Google_Id_Setting', '' );
39+
echo '<input type="text" id="WP_Places_Google_Id_Setting" name="WP_Places_Google_Id_Setting" value="' . $value . '" />';
40+
}
41+
add_action ( 'admin_init', 'WP_Places_add_settings_field' );
42+
43+
44+
/**
45+
* Adds a box to the main column on the Post and Page edit screens.
46+
*/
47+
function WP_Places_add_meta_box() {
48+
49+
$screens = array( 'post', 'page' );
50+
51+
foreach ( $screens as $screen ) {
52+
53+
add_meta_box(
54+
'WP_Places_sectionid',
55+
__( 'WP Places', 'WP_Places_textdomain' ),
56+
'WP_Places_meta_box_callback',
57+
$screen
58+
);
59+
}
60+
}
61+
add_action( 'add_meta_boxes', 'WP_Places_add_meta_box' );
62+
63+
/**
64+
* Prints the box content.
65+
*/
66+
function WP_Places_meta_box_callback( $post ) {
67+
68+
// Add a nonce field so we can check for it later.
69+
wp_nonce_field( 'WP_Places_save_meta_box_data', 'WP_Places_meta_box_nonce' );
70+
71+
/*
72+
* Use get_post_meta() to retrieve an existing value
73+
* from the database and use the value for the form.
74+
*/
75+
$value = get_post_meta( $post->ID, '_WP_Places_meta_value_key', true );
76+
77+
echo '<label for="WP_Places_new_field">';
78+
_e( 'Name and Address', 'WP_Places_textdomain' );
79+
echo '</label> ';
80+
echo '<input type="text" id="WP_Places_new_field" name="WP_Places_new_field" value="' . esc_attr( $value ) . '" size="25" />';
81+
}
82+
83+
/**
84+
* When the post is saved, saves our custom data.
85+
*
86+
* @param int $post_id The ID of the post being saved.
87+
*/
88+
function WP_Places_save_meta_box_data( $post_id ) {
89+
90+
/*
91+
* We need to verify this came from our screen and with proper authorization,
92+
* because the save_post action can be triggered at other times.
93+
*/
94+
95+
// Check if our nonce is set.
96+
if ( ! isset( $_POST['WP_Places_meta_box_nonce'] ) ) {
97+
return;
98+
}
99+
100+
// Verify that the nonce is valid.
101+
if ( ! wp_verify_nonce( $_POST['WP_Places_meta_box_nonce'], 'WP_Places_save_meta_box_data' ) ) {
102+
return;
103+
}
104+
105+
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
106+
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
107+
return;
108+
}
109+
110+
// Check the user's permissions.
111+
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
112+
113+
if ( ! current_user_can( 'edit_page', $post_id ) ) {
114+
return;
115+
}
116+
117+
} else {
118+
119+
if ( ! current_user_can( 'edit_post', $post_id ) ) {
120+
return;
121+
}
122+
}
123+
124+
/* OK, it's safe for us to save the data now. */
125+
126+
// Make sure that it is set.
127+
if ( ! isset( $_POST['WP_Places_new_field'] ) ) {
128+
return;
129+
}
130+
131+
// Sanitize user input.
132+
$my_data = sanitize_text_field( $_POST['WP_Places_new_field'] );
133+
134+
// Update the meta field in the database.
135+
update_post_meta( $post_id, '_WP_Places_meta_value_key', $my_data );
136+
137+
//Check with the Google and grab the meta
138+
//_WP_Places_meta_places_id, _WP_Places_meta_hours, _WP_Places_meta_reviews, _WP_Places_meta_closed, _WP_Places_meta_lat, _WP_Places_meta_lon,
139+
$result=search($my_data);
140+
//print_r($result);
141+
update_post_meta( $post_id, '_WP_Places_meta_Google_response', $result);
142+
143+
144+
145+
}
146+
add_action( 'save_post', 'WP_Places_save_meta_box_data' );
147+
148+
149+
150+
function WP_Places_add_before_content($content) {
151+
$locationPlace=get_post_meta(get_the_ID(),'_WP_Places_meta_Google_response', true);
152+
//print_r($array);
153+
//let's go ahead and cache this
154+
155+
if ( false === ( $placeArray = get_transient( '_Wp_Places_$locationPlace' ) ) ) {
156+
$placeArray = placeDetails($locationPlace);
157+
set_transient( "_Wp_Places_$locationPlace", $placeArray, DAY_IN_SECONDS );
158+
}
159+
160+
161+
if(!NULL==$placeArray[name]) {
162+
$WpPlaces.="<DIV style=\"float: right; border: 1px black solid; bgcolor=#f1f1f1; padding: 10px; background-color: #cccccc; font-size: 12px; max-width: 250px; margin: auto;\">";
163+
if (isset($placeArray[permanentlyClosed])) {
164+
$WpPlaces.="<span style=\"color: red;\">PERMANENTLY CLOSED</SPAN><BR>";
165+
}
166+
if (isset($placeArray[name])) {
167+
$WpPlaces.="<B>$placeArray[name]</B><BR>";
168+
}
169+
if (isset($placeArray[formattedAddress])) {
170+
$WpPlaces.="$placeArray[formattedAddress]<BR>";
171+
}
172+
if (isset($placeArray[phoneNumber])) {
173+
$WpPlaces.="$placeArray[phoneNumber]<BR>";
174+
}
175+
if (isset($placeArray[hours])) {
176+
//the hell happened with open now?
177+
foreach ($placeArray[hours] as $day) {
178+
$WpPlaces.="$day<BR>";
179+
}
180+
}
181+
if (isset($placeArray[website])) {
182+
$WpPlaces.="<a href=$placeArray[website]>website</a><BR>";
183+
}
184+
$WpPlaces.="<img src=".WP_PLUGIN_URL."/WP_Places/img/powered_by_google_on_white.png />";
185+
$WpPlaces.="</DIV>";
186+
}
187+
if (is_single()) {
188+
return $WpPlaces.$content;
189+
} else {
190+
return $content;
191+
}
192+
}
193+
add_filter('the_content', 'WP_Places_add_before_content');

WP_Places.zip

8.8 KB
Binary file not shown.

img/powered_by_google_on_white.png

2.08 KB
Loading

includes/googlePlaces.php

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
4+
5+
function search($location) {
6+
$apiKey = get_option( 'WP_Places_Google_Id_Setting', '' );
7+
$location=urlencode(trim(preg_replace("/[^0-9a-zA-Z -]/", "", $location)));
8+
$ch = curl_init();
9+
curl_setopt($ch, CURLOPT_URL, "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=$location&key=$apiKey");
10+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
11+
curl_setopt($ch, CURLOPT_VERBOSE, 0);
12+
curl_setopt($ch, CURLOPT_HEADER, 0);
13+
$response=curl_exec($ch);
14+
$response=json_decode(stripslashes($response),true);
15+
16+
unset($ch);
17+
18+
if ('ZERO_RESULTS'==$response['status'] || 'INVALID_REQUEST'==$response['status']) {
19+
20+
} else {
21+
$placeId=$response['predictions'][0]['place_id'];
22+
return($placeId);
23+
}
24+
}
25+
26+
27+
function placeDetails($placeId) {
28+
if (!NULL==$placeId) {
29+
$apiKey = get_option( 'WP_Places_Google_Id_Setting', '' );
30+
$ch = curl_init();
31+
curl_setopt($ch, CURLOPT_URL, "https://maps.googleapis.com/maps/api/place/details/json?placeid=$placeId&key=$apiKey");
32+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
33+
curl_setopt($ch, CURLOPT_VERBOSE, 0);
34+
curl_setopt($ch, CURLOPT_HEADER, 0);
35+
$gp[placeId]=$placeId;
36+
$response=curl_exec($ch);
37+
unset($ch);
38+
for ($i = 0; $i <= 31; ++$i) {
39+
$response = str_replace(chr($i), "", $response);
40+
}
41+
$response = str_replace(chr(127), "", $response);
42+
$response = str_replace("–","-",$response);
43+
44+
// This is the most common part
45+
// Some file begins with 'efbbbf' to mark the beginning of the file. (binary level)
46+
// here we detect it and we remove it, basically it's the first 3 characters
47+
if (0 === strpos(bin2hex($response), 'efbbbf')) {
48+
$response = substr($response, 3);
49+
}
50+
$response=json_decode($response,true);
51+
//print_r($response);
52+
//$this->openNow=$response['result']['opening_hours']['open_now'];
53+
$gp[hours]=$response['result']['opening_hours']['weekday_text'];//
54+
$gp[openNow]=$response['result']['opening_hours']['open_now'];
55+
$gp[priceLevel]=$response['result']['price_level'];
56+
$gp[name]=$response['result']['name'];//
57+
$gp[rating]=$response['result']['rating'];
58+
$gp[phoneNumber]=$response['result']['formatted_phone_number'];//
59+
$gp[website]=$response['result']['website'];//
60+
$gp[lat]=$response['result']['geometry']['location']['lat'];
61+
$gp[lng]=$response['result']['geometry']['location']['lng'];
62+
$gp[formattedAddress]=$response['result']['formatted_address'];//
63+
$gp[permanentlyClosed]=$response['result']['permanently_closed'];
64+
//print_r($gp);
65+
return($gp);
66+
} else {
67+
return;
68+
}
69+
}

readme.txt

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=== Plugin Name ===
2+
Contributors: binarygary
3+
Tags: Google Places, Business Information, Location, Google Places API Web Services
4+
Stable Tag: Trunk
5+
Requires at least: 4.0
6+
Tested up to: 4.4
7+
License: GPLv3 or later
8+
License URI: https://www.gnu.org/licenses/gpl.html
9+
10+
Given location name saved with a Post search Google Places API Web Service and displays address, hours, phone number and link to website
11+
12+
== Description ==
13+
14+
If you find you regularly write blog posts about (local?) businesses you might want to provide info such as hours, phone number, address to your users. However, this can be difficult to keep current.
15+
16+
Fortunately Google offers an API called Google Places API Web Service. Google Place API Web Service allows you to Add up-to-date information about millions of locations.
17+
18+
WP_Places Plugin requires a Google Places API Web Service Key. However, at the time of writing the API key is free and provides up to 1,000 requests per 24 hour period. If you verify your identity (by providing Google a Credit Card) they will increase your daily request per 24 hours to 150,000.
19+
20+
Once Installed, WP_Places takes name and location and displays a DIV containing Business Name, Address, Hours, Phone Number, Website.
21+
22+
== Installation ==
23+
24+
1. Upload the plugin files to the `/wp-content/plugins/WP_Places` directory, or install the plugin through the WordPress plugins screen directly.
25+
2. Activate the plugin through the 'Plugins' screen in WordPress
26+
3. Use the Settings->General screen to add your Google Places API Web Services Key
27+
4. When writing a post about a business, add the business name and address to the WP_Places field.
28+
29+
== Changelog ==
30+
31+
= 1.0 =
32+
* Initial Launch!

0 commit comments

Comments
 (0)