forked from AdamWills/flickr-picturefill
/
class-flickr-picturefill.php
238 lines (186 loc) · 5.52 KB
/
class-flickr-picturefill.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
<?php
/**
* @package Flickr Picturefill
* @author Adam Wills <adam@adamwills.com>
* @license MIT/GPLv2
* @link http://adamwills.com
* @copyright 2013 Adam Wills
*/
/**
* Plugin class.
*
* @package Flickr_Picturefill
* @author Your Name <email@example.com>
*/
class Flickr_Picturefill {
/**
* Plugin version, used for cache-busting of style and script file references.
*
* @since 1.0.0
*
* @const string
*/
const VERSION = '2.0.0';
/**
* Unique identifier for your plugin.
*
* Use this value (not the variable name) as the text domain when internationalizing strings of text. It should
* match the Text Domain file header in the main plugin file.
*
* @since 1.0.0
*
* @var string
*/
protected $plugin_slug = 'flickr-picturefill';
/**
* Instance of this class.
*
* @since 1.0.0
*
* @var object
*/
protected static $instance = null;
/**
* Slug of the plugin screen.
*
* @since 1.0.0
*
* @var string
*/
protected $plugin_screen_hook_suffix = null;
/**
* Initialize the plugin by setting localization, filters, and administration functions.
*
* @since 1.0.0
*/
private function __construct() {
// Load plugin text domain
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
// Add the options page and menu item.
add_action( 'admin_menu', array( $this, 'add_plugin_admin_menu' ) );
// Add an action link pointing to the options page.
$plugin_basename = plugin_basename( plugin_dir_path( __FILE__ ) . 'flickr-picturefill.php' );
add_filter( 'plugin_action_links_' . $plugin_basename, array( $this, 'add_action_links' ) );
// Load public-facing JavaScript.
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_shortcode( 'picturefill', array( $this, 'create_shortcode' ) );
}
/**
* Return an instance of this class.
*
* @since 1.0.0
*
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Load the plugin text domain for translation.
*
* @since 1.0.0
*/
public function load_plugin_textdomain() {
$domain = $this->plugin_slug;
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
load_textdomain( $domain, WP_LANG_DIR . '/' . $domain . '/' . $domain . '-' . $locale . '.mo' );
load_plugin_textdomain( $domain, FALSE, basename( dirname( __FILE__ ) ) . '/lang/' );
}
/**
* Register and enqueues public-facing JavaScript files.
*
* @since 1.0.0
*/
public function enqueue_scripts() {
wp_register_script( $this->plugin_slug . '-plugin-script', plugins_url( 'js/picturefill.min.js', __FILE__ ) );
wp_enqueue_script( $this->plugin_slug . '-plugin-script' );
}
/**
* Register the administration menu for this plugin into the WordPress Dashboard menu.
*
* @since 1.0.0
*/
public function add_plugin_admin_menu() {
$this->plugin_screen_hook_suffix = add_options_page(
__( 'Flickr Picturefill', $this->plugin_slug ),
__( 'Flickr Picturefill Settings', $this->plugin_slug ),
'read',
$this->plugin_slug,
array( $this, 'display_plugin_admin_page' )
);
}
/**
* Render the settings page for this plugin.
*
* @since 1.0.0
*/
public function display_plugin_admin_page() {
include_once( 'views/admin.php' );
}
/**
* Add settings action link to the plugins page.
*
* @since 1.0.0
*/
public function add_action_links( $links ) {
return array_merge(
array(
'settings' => '<a href="' . admin_url( 'plugins.php?page=flickr-picturefill' ) . '">' . __( 'Settings', $this->plugin_slug ) . '</a>'
),
$links
);
}
/**
* Add settings action link to the plugins page.
*
* @since 1.0.0
*/
public function create_shortcode( $atts ) {
extract( shortcode_atts( array(
'id' => '0',
'alt' => '',
), $atts ) );
$trans_name = 'flickrpf-'. $atts['id'];
if ( false === ( $output = get_transient( $trans_name ) ) ) {
$params = array(
'api_key' => get_option('flickrpf_api_key'),
'method' => 'flickr.photos.getSizes',
'photo_id' => $atts['id'],
'format' => 'php_serial',
);
$encoded_params = array();
foreach ($params as $k => $v){
$encoded_params[] = urlencode($k).'='.urlencode($v);
}
$url = "https://api.flickr.com/services/rest/?".implode('&', $encoded_params);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
$rsp = curl_exec($curl);
if(curl_errno($curl)) {
return curl_error($curl);
}
curl_close($curl);
$rsp_obj = unserialize($rsp);
if ( $rsp_obj['stat'] == 'ok' ) {
$sizes = $rsp_obj['sizes']['size'];
$output = '<picture>';
$output.= '<!--[if IE 9]><video style="display: none;"><![endif]-->'; // IE9 support
$output.= '<source srcset="'.$sizes[9]['source'].'" media="(min-width: 1000px)">';
$output.= '<source srcset="'.$sizes[8]['source'].'" media="(min-width: 800px)">';
$output.= '<!--[if IE 9]></video><![endif]-->'; // IE9 support
$output.= '<img srcset="' . $sizes[4]['source'] . '" alt="' . $atts['alt'] . '">';
$output.= '</picture>';
set_transient( $trans_name, $output, 2 * 24 * HOUR_IN_SECONDS );
}
}
return $output;
}
}