forked from hiddentao/wp-flickr-embed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-flickr-embed.php
349 lines (284 loc) · 11.8 KB
/
wp-flickr-embed.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
/*
Plugin Name: Wordpress Flickr Embed
Plugin URI: https://github.com/hiddentao/wp-flickr-embed
Description: Insert Flickr images into your post using an interactive popup, launched from the visual editor toolbar.
Author: Ramesh Nair
Version: 1.2.3
Author URI: http://hiddentao.com
*/
if (!defined('PHP_VERSION_ID')) {
$version = explode('.', PHP_VERSION);
define('PHP_VERSION_ID', ($version[0] * 10000 + $version[1] * 100 + $version[2]));
}
/**
* Class loader.
*/
function wp_flickr_embed_class_loader($class) {
if (0 !== stripos($class, 'WpFlickrEmbed')) {
return;
}
// class name is in form: WpFlickrEmbed_Xyz_Yyy
$shortClassName = str_replace('_', '-', substr($class, stripos($class, '_') + 1));
require(dirname(__FILE__).'/include/class.' . strtolower($shortClassName) . '.php');
}
spl_autoload_register('wp_flickr_embed_class_loader');
class WpFlickrEmbed implements WPFlickrEmbed_Constants {
private $_slug = 'wp-flickr-embed';
var $pluginURI = null;
var $settings = array();
var $default_settings = array(
self::FLICKR_USER_FULLNAME => '',
self::FLICKR_USER_NAME => '',
self::FLICKR_USER_NSID => '',
self::FLICKR_OAUTH_TOKEN => '',
self::FLICKR_OAUTH_TOKEN_SECRET => '',
self::OPTION_PHOTO_LINK => '0',
self::OPTION_LINK_REL => '',
self::OPTION_LINK_CLASS => '',
);
function WpFlickrEmbed() {
global $wp;
$this->includeDir = dirname(__FILE__). '/include';
$this->pagesDir = $this->includeDir . '/pages';
$this->settings = get_option(get_class($this));
$flush_settings = false;
foreach($this->default_settings as $key => $value) {
if(!isset($this->settings[$key])) {
$this->settings[$key] = $value;
$flush_settings = true;
}
}
if($flush_settings) {
$this->update_settings();
}
$this->pluginURI = plugin_dir_url(__FILE__);
// Avoid 'insecure content' loading errors.
if (is_ssl())
$this->pluginURI = str_replace('http:', 'https:', $this->pluginURI);
add_action('init', array(&$this, 'hook_init'));
add_action('template_redirect', array(&$this, 'hook_template_redirect'));
add_action('media_buttons', array(&$this, 'addMediaButton'), 20);
add_action('media_upload_flickr', array(&$this, 'media_upload_flickr'));
add_action('admin_menu', array(&$this, 'addAdminMenu'));
add_action('admin_print_scripts', array(&$this, 'adminPrintScripts'));
add_action('admin_print_styles', array(&$this, 'adminPrintStyles'));
// check that we can do use this plugin
if(!function_exists('curl_init') && !ini_get('allow_url_fopen')) {
$this->_disabled = self::DISABLED_REASON_CURL_FOPEN;
} else if (PHP_VERSION_ID < 50200) {
$this->_disabled = self::DISABLED_REASON_PHP_VERSION;
}
if ($this->isViewingOurAdminPage()) {
$this->_currentURL = admin_url(sprintf('options-general.php?%s', $_SERVER['QUERY_STRING']));
} else {
$this->_currentURL = null;
}
$this->flickrAPI = new WpFlickrEmbed_Flickr(
self::FLICKR_API_KEY,
self::FLICKR_SECRET,
$this->_currentURL
);
// are we authenticated with flickr? if so then set up auth params
if (!empty($this->settings[self::FLICKR_OAUTH_TOKEN])) {
$this->flickrAPI->useOAuthAccessCredentials(array(
WpFlickrEmbed_Flickr::USER_FULL_NAME => $this->settings[self::FLICKR_USER_FULLNAME],
WpFlickrEmbed_Flickr::USER_NAME => $this->settings[self::FLICKR_USER_NAME],
WpFlickrEmbed_Flickr::USER_NSID => $this->settings[self::FLICKR_USER_NSID],
WpFlickrEmbed_Flickr::OAUTH_ACCESS_TOKEN => $this->settings[self::FLICKR_OAUTH_TOKEN],
WpFlickrEmbed_Flickr::OAUTH_ACCESS_TOKEN_SECRET => $this->settings[self::FLICKR_OAUTH_TOKEN_SECRET],
));
}
}
/**
* Get whether this plugin is disabled.
*
* @return bool FALSE if not; otherwise the reason for being disabled.
*/
function isDisabled() {
return $this->_disabled ? $this->_disabled : FALSE;
}
/**
* Hook for 'init' action.
*/
function hook_init() {
global $wp;
$wp->add_query_var( self::SIGN_URL_PARAM_NAME );
$wp->add_query_var( self::FLICKR_AUTH_URL_PARAM_NAME );
}
/**
* Hook for 'template_redirect' action.
*/
function hook_template_redirect() {
// sign a flickr request
$wpfe_sign = get_query_var(self::SIGN_URL_PARAM_NAME);
if (!empty($wpfe_sign)) {
// parse JSON string
$json = @json_decode(stripslashes($wpfe_sign));
if (empty($json) || !is_object($json)) {
die('Invalid JSON input: ' . $wpfe_sign);
} else {
if (empty($json->method)) {
die('No Flickr API method specified: ' . print_r($json,true));
}
// api key
$json->api_key = self::FLICKR_API_KEY;
// get signed call
$ret = $this->flickrAPI->getSignedUrlParams($json->method, get_object_vars($json), array(
'use_secure_api' => TRUE
));
header("Content-type: application/json");
print json_encode($ret);
exit();
}
}
// flickr authorization process
$wpfe_flickr_auth = get_query_var(self::FLICKR_AUTH_URL_PARAM_NAME);
if (!empty($wpfe_flickr_auth)) {
// first time call?
$oauth_verifier = $_GET['oauth_verifier'];
if (empty($oauth_verifier)) {
// want flickr to come back here
$current_url = sprintf('%s?%s=%s',
trailingslashit($this->getSiteHomeUrl()), self::FLICKR_AUTH_URL_PARAM_NAME, urlencode($wpfe_flickr_auth));
$this->flickrAPI = new WpFlickrEmbed_Flickr(
self::FLICKR_API_KEY,
self::FLICKR_SECRET,
$current_url
);
// sign out and re-auth
$this->flickrAPI->signOut();
if (!$this->flickrAPI->authenticate('read')) {
header('Location: ' . $wpfe_flickr_auth . '&auth_result=fail');
}
} else {
// complete auth
if ($this->flickrAPI->authenticate('read')) {
$this->saveFlickrAuthentication();
header('Location: ' . $wpfe_flickr_auth . '&auth_result=success');
} else {
header('Location: ' . $wpfe_flickr_auth . '&auth_result=fail');
}
}
exit();
}
}
function addMediaButton() {
global $post_ID, $temp_ID;
$uploading_iframe_ID = (int) (0 == $post_ID ? $temp_ID : $post_ID);
$media_upload_iframe_src = "media-upload.php?post_id=$uploading_iframe_ID";
$wp_flickr_embed_iframe_src = apply_filters('wp_flickr_embed_iframe_src', "$media_upload_iframe_src&type=flickr&tab=flickr");
$wp_flickr_embed_title = __('Add Flickr photo', 'wp-flickr-embed');
echo "<a href=\"{$wp_flickr_embed_iframe_src}&TB_iframe=true&height=500&width=640\" class=\"thickbox\" title=\"$wp_flickr_embed_title\"><img src=\"{$this->pluginURI}/images/media-flickr.png\" alt=\"$wp_flickr_embed_title\" /></a>";
}
function modifyMediaTab($tabs) {
return array(
'flickr' => __('Flickr photo', 'wp-flickr-embed')
);
}
function addAdminMenu() {
if (function_exists('add_options_page')) {
add_options_page(__('WP Flickr Embed', 'wp-flickr-embed'), __('WP Flickr Embed', 'wp-flickr-embed'), 'manage_options', $this->pagesDir.'/admin.php');
}
}
/**
* Handler for 'admin_print_scripts' hook.
*/
function adminPrintScripts() {
if ($this->isViewingOurAdminPage()) {
wp_enqueue_script('postbox');
wp_enqueue_script('dashboard');
}
}
public function getSignRequestApiUrl() {
return sprintf('%s?%s=', trailingslashit($this->getSiteHomeUrl()), self::SIGN_URL_PARAM_NAME);
}
/**
* Get website home URL.
*
* @return string
*/
public function getSiteHomeUrl() {
// if we're in SSL then request SSL version
return home_url('', is_ssl() ? 'https' : 'http');
}
/**
* Handler for 'admin_print_styles' hook.
*/
public function adminPrintStyles() {
if ($this->isViewingOurAdminPage()) {
wp_enqueue_style('dashboard');
wp_enqueue_style('wp-flickr-embed-admin', $this->pluginURI . '/wp-flickr-embed-admin.css');
}
}
/**
* Get whether user is viewing our admin page.
*/
private function isViewingOurAdminPage() {
return (isset($_GET['page']) && 0 === stripos($_GET['page'], $this->_slug));
}
function media_upload_flickr() {
wp_iframe('media_upload_type_flickr');
}
/**
* Update settings.
* @param null $settings
*/
function update_settings($settings=null){
if($settings) {
foreach($settings as $key => $val) {
$this->settings[$key] = $val;
}
}
$_settings = array();
foreach($this->settings as $key => $value) {
if(isset($this->default_settings[$key])) {
$_settings[$key] = $value;
}
}
update_option(get_class($this), $_settings);
}
/**
* Clear Flickr authentication credentials.
*/
function clearFlickrAuthentication() {
$this->flickrAPI->signout();
$this->update_settings(array(
self::FLICKR_USER_FULLNAME => '',
self::FLICKR_USER_NAME => '',
self::FLICKR_USER_NSID => '',
self::FLICKR_OAUTH_TOKEN => '',
self::FLICKR_OAUTH_TOKEN_SECRET => '',
));
}
/**
* Save Flickr authentication data into settings.
*/
function saveFlickrAuthentication() {
$this->update_settings(array(
self::FLICKR_USER_FULLNAME => $this->flickrAPI->getOauthData(WpFlickrEmbed_Flickr::USER_FULL_NAME),
self::FLICKR_USER_NAME => $this->flickrAPI->getOauthData(WpFlickrEmbed_Flickr::USER_NAME),
self::FLICKR_USER_NSID => $this->flickrAPI->getOauthData(WpFlickrEmbed_Flickr::USER_NSID),
self::FLICKR_OAUTH_TOKEN => $this->flickrAPI->getOauthData(WpFlickrEmbed_Flickr::OAUTH_ACCESS_TOKEN),
self::FLICKR_OAUTH_TOKEN_SECRET => $this->flickrAPI->getOauthData(WpFlickrEmbed_Flickr::OAUTH_ACCESS_TOKEN_SECRET),
));
}
/**
* Get whether user has authenticated with Flickr.
*
* @return bool true if so; false otherwise.
*/
function authenticatedWithFlickr() {
return !empty($this->settings[self::FLICKR_OAUTH_TOKEN]);
}
}
/** Get the iFRAME contents */
function media_upload_type_flickr() {
global $wpFlickrEmbed;
if ($wpFlickrEmbed->isDisabled()) {
echo '<p>' . __('The Wordpress Flickr Embed plugin has been disabled. Please visit the plugin\'s options page.', 'wp-flickr-embed') . '</p>';
} else {
require(dirname(__FILE__) .'/wp-flickr-embed-upload-frame.php');
}
}
$wpFlickrEmbed = new WpFlickrEmbed;