PluginProbe
Photonic Gallery & Lightbox for Flickr, SmugMug & Others / 3.33
Photonic Gallery & Lightbox for Flickr, SmugMug & Others v3.33
3.36 3.35 3.34 3.33 2.19 2.20 2.21 2.22 2.23 2.24 2.25 2.26 2.27 2.28 2.29 2.30 2.31 2.32 2.33 2.34 2.40 2.41 2.42 2.43 2.44 All 141 releases
photonic / Platforms / Base.php

Base.php in Photonic Gallery & Lightbox for Flickr, SmugMug & Others 3.33, at Platforms/Base.php

276 lines 9.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Photonic_Plugin\Platforms;
4
5 use Photonic_Plugin\Components\Stack_Trace;
6 use Photonic_Plugin\Core\Photonic;
7 use WP_Error;
8
9 require_once PHOTONIC_PATH . '/Components/Printable.php';
10 require_once PHOTONIC_PATH . '/Components/Stack_Trace.php';
11 require_once PHOTONIC_PATH . '/Components/Header.php';
12 require_once PHOTONIC_PATH . '/Components/Error.php';
13
14 /**
15 * Gallery processor class to be extended by individual processors. This class has an abstract method called <code>get_gallery_images</code>
16 * that has to be defined by each inheriting processor.
17 *
18 * This is also where the OAuth support is implemented. The URLs are defined using abstract functions, while a handful of utility functions are defined.
19 * Most utility functions have been adapted from the OAuth PHP package distributed here: https://code.google.com/p/oauth-php/.
20 */
21 abstract class Base {
22 public $api_key, $api_secret, $provider, $user_agent, $nonce, $oauth_timestamp, $signature_parameters, $link_lightbox_title,
23 $oauth_version, $oauth_done, $show_more_link, $gallery_index, $common_parameters,
24 $doc_links, $password_protected, $token, $token_secret, $show_buy_link, $stack_trace;
25
26 protected function __construct() {
27 global $photonic_enable_popup, $photonic_thumbnail_style;
28
29 $this->nonce = self::nonce();
30 $this->user_agent = 'PhotonicWP/' . PHOTONIC_VERSION . '; ' . get_home_url();
31 $this->oauth_timestamp = time();
32 $this->oauth_version = '1.0';
33 $this->show_more_link = false;
34 $this->gallery_index = 0;
35
36 $bypass_popup = empty($photonic_enable_popup) || 'off' === $photonic_enable_popup || 'hide' === $photonic_enable_popup;
37 $this->common_parameters = [
38 'columns' => 'auto',
39 'layout' => !empty($photonic_thumbnail_style) ? $photonic_thumbnail_style : 'square',
40 'display' => 'local', // local: on the same page, lightbox: in a lightbox, modal: in a modal popup, template: using the page template
41 'popup' => $bypass_popup ? 'hide' : $photonic_enable_popup, // While popups are not possible for slideshows, this shortcode value is needed in Platform sub-classes, to determine whether a `gallery_url` should be associated with an album thumbnail.
42 'filter' => '',
43 'filter_type' => 'include',
44 'more' => '',
45 'panel' => '',
46 'custom_classes' => '',
47 'alignment' => '',
48 ];
49 $this->common_parameters['photo_layout'] = $this->common_parameters['layout'];
50
51 $this->doc_links = [];
52 $this->password_protected = esc_html__('This album is password-protected. Please provide a valid password.', 'photonic');
53 $this->show_buy_link = false;
54 $this->stack_trace = [];
55
56 $this->add_hooks();
57 }
58
59 final public static function get_instance() {
60 static $instances = array();
61 $called_class = get_called_class();
62
63 if (!isset($instances[$called_class])) {
64 $instances[$called_class] = new $called_class();
65 }
66 return $instances[$called_class];
67 }
68
69 /**
70 * Main function that fetches the images associated with the shortcode. This is implemented by all sub-classes.
71 *
72 * @abstract
73 * @param array $attr
74 * @return array
75 */
76 abstract public function get_gallery_images($attr = []): array;
77
78 /**
79 * Generates a nonce for use in signing calls.
80 *
81 * @static
82 * @return string
83 */
84 public static function nonce(): string {
85 $mt = microtime();
86 $rand = mt_rand();
87 return md5($mt . $rand);
88 }
89
90 /**
91 * Takes a string of parameters in an HTML encoded string, then returns an array of name-value pairs, with the parameter
92 * name and the associated value.
93 *
94 * @static
95 * @param $input
96 * @return array
97 */
98 public static function parse_parameters($input): array {
99 if (!isset($input) || !$input) {
100 return [];
101 }
102
103 $pairs = explode('&', $input);
104
105 $parsed_parameters = [];
106 foreach ($pairs as $pair) {
107 $split = explode('=', $pair, 2);
108 $parameter = sanitize_text_field(urldecode($split[0]));
109 $value = isset($split[1]) ? sanitize_text_field(urldecode($split[1])) : '';
110
111 if (isset($parsed_parameters[$parameter])) {
112 // We have already recieved parameter(s) with this name, so add to the list
113 // of parameters with this name
114 if (is_scalar($parsed_parameters[$parameter])) {
115 // This is the first duplicate, so transform scalar (string) into an array
116 // so we can add the duplicates
117 $parsed_parameters[$parameter] = [$parsed_parameters[$parameter]];
118 }
119
120 $parsed_parameters[$parameter][] = $value;
121 }
122 else {
123 $parsed_parameters[$parameter] = $value;
124 }
125 }
126 return $parsed_parameters;
127 }
128
129 public function get_header_display($args): array {
130 if (!isset($args['headers'])) {
131 return [
132 'thumbnail' => 'inherit',
133 'title' => 'inherit',
134 'counter' => 'inherit',
135 ];
136 }
137 elseif (empty($args['headers'])) {
138 return [
139 'thumbnail' => 'none',
140 'title' => 'none',
141 'counter' => 'none',
142 ];
143 }
144 else {
145 $header_array = explode(',', $args['headers']);
146 return [
147 'thumbnail' => in_array('thumbnail', $header_array, true) ? 'show' : 'none',
148 'title' => in_array('title', $header_array, true) ? 'show' : 'none',
149 'counter' => in_array('counter', $header_array, true) ? 'show' : 'none',
150 ];
151 }
152 }
153
154 public function get_hidden_headers($arg_headers, $setting_headers): array {
155 return [
156 'thumbnail' => 'inherit' === $arg_headers['thumbnail'] ? $setting_headers['thumbnail'] : 'none' === $arg_headers['thumbnail'],
157 'title' => 'inherit' === $arg_headers['title'] ? $setting_headers['title'] : 'none' === $arg_headers['title'],
158 'counter' => 'inherit' === $arg_headers['counter'] ? $setting_headers['counter'] : 'none' === $arg_headers['counter'],
159 ];
160 }
161
162 /**
163 * Wraps an error message in appropriately-styled markup for display in the front-end
164 *
165 * @param $message
166 * @return string
167 */
168 public function error($message): string {
169 return "<div class='photonic-error photonic-{$this->provider}-error' id='photonic-{$this->provider}-error-{$this->gallery_index}'>\n\t<span class='photonic-error-icon photonic-icon'>&nbsp;</span>\n\t<div class='photonic-message'>\n\t\t$message\n\t</div>\n</div>\n";
170 }
171
172 /**
173 * Retrieves the error messages from a WP_Response object and formats them in a display-ready markup.
174 *
175 * @param WP_Error $response
176 * @param bool $server_msg
177 * @return string
178 */
179 public function wp_error_message($response, $server_msg = true): string {
180 $ret = '';
181 if ($server_msg) {
182 $ret = $this->get_server_error() . "<br/>\n";
183 }
184 if (is_wp_error($response)) {
185 $messages = $response->get_error_messages();
186 $ret .= '<strong>' . esc_html(sprintf(_n('%s Message:', '%s Messages:', count($messages), 'photonic'), count($messages))) . "</strong><br/>\n";
187 foreach ($messages as $message) {
188 $ret .= $message . "<br>\n";
189 }
190 }
191 return $ret;
192 }
193
194 public function push_to_stack($event) {
195 global $photonic_performance_logging;
196 if (empty($photonic_performance_logging)) {
197 return;
198 }
199
200 if (!isset($this->stack_trace[$this->gallery_index])) {
201 $stack_trace = new Stack_Trace();
202 }
203 else {
204 $stack_trace = $this->stack_trace[$this->gallery_index];
205 }
206
207 $stack_trace->add_to_first_open_event($event);
208 $this->stack_trace[$this->gallery_index] = $stack_trace;
209 }
210
211 public function pop_from_stack() {
212 global $photonic_performance_logging;
213 if (empty($photonic_performance_logging)) {
214 return;
215 }
216
217 if (isset($this->stack_trace[$this->gallery_index])) {
218 /** @var Stack_Trace $stack_trace */
219 $stack_trace = $this->stack_trace[$this->gallery_index];
220 $stack_trace->pop_from_first_open_event();
221 $this->stack_trace[$this->gallery_index] = $stack_trace;
222 }
223 }
224
225 protected function get_gallery_url($short_code, $meta): string {
226 global $photonic_alternative_shortcode, $photonic_gallery_template_page;
227
228 $shortcode_tag = $photonic_alternative_shortcode ?: 'gallery';
229 $shortcode_parts = [];
230 foreach ($short_code as $attr => $value) {
231 if (is_array($value)) {
232 continue;
233 }
234 $shortcode_parts[] = $attr . '="' . esc_attr($value) . '"';
235 }
236 $raw_shortcode = '[' . $shortcode_tag . ' ' . implode(' ', $shortcode_parts) . ']';
237
238 $gallery_url = add_query_arg(
239 [
240 'photonic_gallery' => base64_encode($raw_shortcode), // Encode the shortcode to avoid issues in passing the data around in URLs. This is decoded in Template.php
241 'photonic_gallery_title' => rawurlencode($meta['title']),
242 ],
243 get_page_link($photonic_gallery_template_page)
244 );
245 return $gallery_url;
246 }
247
248 public function ssl_verify_peer(&$handle) {
249 // Photonic mostly uses wp_remote_get, but for bulk requests, it needs to use the 'WpOrg\Requests\Hooks' class, which needs 'curl.before_multi_add'.
250 curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); // phpcs:ignore WordPress.WP.AlternativeFunctions
251 }
252
253 public function get_server_error(): string {
254 return sprintf(esc_html__('There was an error connecting to %s. Please try again later.', 'photonic'), $this->provider);
255 }
256
257 /**
258 * Helper execution, implemented by child classes
259 *
260 * @param array $args
261 * @return string
262 */
263 public function execute_helper($args = []): string { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
264 // Blank method, to be overridden by child classes
265 return '';
266 }
267
268 public function add_hooks() {
269 // Blank method, implemented by child classes, if required
270 }
271
272 public function increment_gallery_index() {
273 $this->gallery_index++;
274 }
275 }
276