PluginProbe
Sharing Image / 3.2
Sharing Image v3.2
3.10 trunk 2.0 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.16 2.0.17 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 3.0 3.1 3.2 3.3 All 29 releases
sharing-image / classes / class-config.php

class-config.php in Sharing Image 3.2, at classes/class-config.php

408 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 * Handle config tab on settings page and provide static methods.
4 *
5 * @package sharing-image
6 * @author Anton Lukin
7 */
8
9 namespace Sharing_Image;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 die;
13 }
14
15 /**
16 * Config class.
17 *
18 * @class Config
19 */
20 class Config {
21 /**
22 * Sharing Image config options name.
23 *
24 * @var string
25 */
26 const OPTION_CONFIG = 'sharing_image_config';
27
28 /**
29 * Init class actions and filters.
30 */
31 public static function init() {
32 add_action( 'admin_init', array( __CLASS__, 'handle_requests' ) );
33 }
34
35 /**
36 * Handle config requests from admin-side.
37 */
38 public static function handle_requests() {
39 add_action( 'admin_post_sharing_image_save_config', array( __CLASS__, 'save_config_tab' ) );
40 }
41
42 /**
43 * Save config fields on plugin settings page.
44 */
45 public static function save_config_tab() {
46 check_admin_referer( Settings::SETTINGS_NONCE, 'sharing_image_nonce' );
47
48 if ( ! current_user_can( 'manage_options' ) ) {
49 wp_die( esc_html__( 'Sorry, you do not have permission to manage options for this site.', 'sharing-image' ) );
50 }
51
52 $redirect = Settings::get_tab_link( 'config' );
53
54 if ( ! isset( $_POST['sharing_image_config'] ) ) {
55 Settings::redirect_with_message( $redirect, 5 );
56 }
57
58 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
59 $config = self::sanitize_config( wp_unslash( $_POST['sharing_image_config'] ) );
60
61 self::update_config( $config );
62
63 // Redirect with success message.
64 Settings::redirect_with_message( $redirect, 1 );
65 }
66
67 /**
68 * Get plugin config settings.
69 *
70 * @return array List of plugin config settings.
71 */
72 public static function get_config() {
73 $config = get_option( self::OPTION_CONFIG );
74
75 if ( empty( $config ) ) {
76 $config = array();
77 }
78
79 /**
80 * Filters settings config.
81 *
82 * @param array List of plugin config settings.
83 */
84 return apply_filters( 'sharing_image_get_config', $config );
85 }
86
87 /**
88 * Update config settings.
89 *
90 * @param array $config Updated config data.
91 */
92 public static function update_config( $config ) {
93 /**
94 * Filters config options before their update in database.
95 *
96 * @param array $config Settings config data.
97 */
98 $config = apply_filters( 'sharing_image_update_config', $config );
99
100 update_option( self::OPTION_CONFIG, $config );
101 }
102
103 /**
104 * Get path to directory with uploaded posters.
105 *
106 * @return array Path and url to upload directory.
107 */
108 public static function get_upload_dir() {
109 $config = self::get_config();
110
111 if ( ! isset( $config['uploads'] ) ) {
112 $config['uploads'] = 'default';
113 }
114
115 $directory = wp_upload_dir();
116
117 // Create default data.
118 $uploads = array( $directory['path'], $directory['url'] );
119
120 // Create custom upload directory.
121 if ( isset( $config['storage'] ) && 'custom' === $config['uploads'] ) {
122 $uploads = self::create_upload_dir( $config['storage'] );
123 }
124
125 /**
126 * Filters upload directory.
127 *
128 * @param array $dir Path and url to upload directory.
129 */
130 return apply_filters( 'sharing_image_upload_dir', $uploads );
131 }
132
133 /**
134 * Get generated image file format.
135 *
136 * @param string $format Optional. Default image format.
137
138 * @return string Image file format.
139 */
140 public static function get_file_format( $format = 'jpg' ) {
141 $config = self::get_config();
142
143 if ( isset( $config['format'] ) ) {
144 $format = $config['format'];
145 }
146
147 /**
148 * Filters Image file format.
149 *
150 * @param string $format Image file format.
151 */
152 return apply_filters( 'sharing_image_file_format', $format );
153 }
154
155 /**
156 * Get autogenerated template index.
157 *
158 * @param string|null $index Optional. Autogenerate template index.
159 */
160 public static function get_autogenerate_index( $index = null ) {
161 $config = self::get_config();
162
163 if ( ! empty( $config['autogenerate'] ) ) {
164 $index = $config['autogenerate'];
165 }
166
167 /**
168 * Filters autogenerate template index.
169 *
170 * @since 3.1
171 *
172 * @param string $index Image file format.
173 */
174 return apply_filters( 'get_autogenerate_index', $index );
175 }
176
177 /**
178 * Get quality of generated poster.
179 *
180 * @param int $quality Optional. Default image quality.
181 *
182 * @return int Image quality.
183 */
184 public static function get_quality( $quality = 90 ) {
185 $config = self::get_config();
186
187 if ( isset( $config['quality'] ) ) {
188 $quality = $config['quality'];
189 }
190
191 /**
192 * Filters poster image quality.
193 *
194 * @param string $quality Image quality.
195 */
196 return apply_filters( 'sharing_image_poster_quality', $quality );
197 }
198
199 /**
200 * Try to get default poster image data.
201 * Returns array with image url, width and height.
202 *
203 * @see https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/
204 *
205 * @return array|false Array of image data, or boolean false if no image is available.
206 */
207 public static function get_default_poster_src() {
208 $config = self::get_config();
209
210 if ( empty( $config['default'] ) ) {
211 return false;
212 }
213
214 $poster = wp_get_attachment_image_src( $config['default'], 'full' );
215
216 if ( is_array( $poster ) ) {
217 $poster = array_slice( $poster, 0, 3 );
218 }
219
220 /**
221 * Filters default poster data.
222 *
223 * @param array|false Array of image data, or boolean false if no image is available.
224 */
225 return apply_filters( 'sharing_image_default_poster_src', $poster );
226 }
227
228 /**
229 * Check if Post widget is hidden.
230 *
231 * @return bool Whether Post widget is hidden.
232 */
233 public static function is_hidden_post_widget() {
234 $config = self::get_config();
235
236 /**
237 * Easy way to hide Post Metabox/Gutenberg sidebar.
238 *
239 * @param bool Set true to hide metabox.
240 */
241 return apply_filters( 'sharing_image_hide_metabox', isset( $config['nowidget'] ) );
242 }
243
244 /**
245 * Check if plugin snippets is active.
246 *
247 * @return bool Whether plugin snippets are enabled.
248 */
249 public static function is_active_snippets() {
250 $active_snippets = false;
251
252 // Get config data.
253 $config = self::get_config();
254
255 if ( isset( $config['meta'] ) && 'snippets' === $config['meta'] ) {
256 $active_snippets = true;
257 }
258
259 /**
260 * Last chance to activate plugin snippets.
261 *
262 * @since 3.0
263 *
264 * @param bool $active_snippets Set true to activate plugin snippets.
265 */
266 return apply_filters( 'sharing_image_active_snippets', $active_snippets );
267 }
268
269 /**
270 * Check that header meta is forced enabled.
271 *
272 * @return bool Whether header meta is enabled.
273 */
274 public static function is_custom_header_meta() {
275 $show_header = true;
276
277 // Get config data.
278 $config = self::get_config();
279
280 if ( isset( $config['meta'] ) && 'hidden' === $config['meta'] ) {
281 $show_header = false;
282 }
283
284 /**
285 * Last chance to show customer header meta.
286 *
287 * @since 3.0
288 *
289 * @param bool $show_header Set true to show custom header meta.
290 */
291 return apply_filters( 'sharing_image_show_header', $show_header );
292 }
293
294 /**
295 * Sanitize config settings.
296 *
297 * @param array $config Config settings.
298 *
299 * @return array Sanitized config settings.
300 */
301 private static function sanitize_config( $config ) {
302 $sanitized = array();
303
304 if ( ! empty( $config['default'] ) ) {
305 $sanitized['default'] = absint( $config['default'] );
306 }
307
308 $sanitized['format'] = 'jpg';
309
310 if ( isset( $config['format'] ) ) {
311 $format = $config['format'];
312
313 if ( in_array( $format, array( 'jpg', 'png', 'gif' ), true ) ) {
314 $sanitized['format'] = $config['format'];
315 }
316 }
317
318 if ( isset( $config['quality'] ) ) {
319 $quality = (int) $config['quality'];
320
321 if ( $quality >= 1 && $quality <= 100 ) {
322 $sanitized['quality'] = $quality;
323 }
324 }
325
326 $sanitized['uploads'] = 'default';
327
328 if ( isset( $config['uploads'] ) ) {
329 $uploads = $config['uploads'];
330
331 if ( in_array( $uploads, array( 'custom', 'default' ), true ) ) {
332 $sanitized['uploads'] = $config['uploads'];
333 }
334 }
335
336 $sanitized['autogenerate'] = '';
337
338 if ( ! empty( $config['autogenerate'] ) ) {
339 $sanitized['autogenerate'] = sanitize_key( $config['autogenerate'] );
340 }
341
342 if ( isset( $config['storage'] ) ) {
343 $sanitized['storage'] = sanitize_text_field( $config['storage'] );
344 }
345
346 if ( isset( $config['suspend'] ) ) {
347 $sanitized['suspend'] = 'suspend';
348 }
349
350 if ( isset( $config['attachment'] ) ) {
351 $sanitized['attachment'] = 'attachment';
352 }
353
354 if ( isset( $config['nowidget'] ) ) {
355 $sanitized['nowidget'] = 'nowidget';
356 }
357
358 if ( isset( $config['meta'] ) ) {
359 $sanitized['meta'] = sanitize_text_field( $config['meta'] );
360 }
361
362 $sanitized['meta'] = 'snippets';
363
364 if ( isset( $config['meta'] ) ) {
365 $meta = $config['meta'];
366
367 if ( in_array( $meta, array( 'hidden', 'custom' ), true ) ) {
368 $sanitized['meta'] = $config['meta'];
369 }
370 }
371
372 if ( ! empty( $config['demo'] ) ) {
373 $sanitized['demo'] = 1;
374 }
375
376 /**
377 * Filters template editor sanitized fields.
378 *
379 * @param array $sanitized List of sanitized config fields.
380 * @param array $config List of config fields before sanitization.
381 */
382 return apply_filters( 'sharing_image_sanitize_config', $sanitized, $config );
383 }
384
385 /**
386 * Create upload directory and return its path and url
387 *
388 * @param string $storage Relative directory path.
389 *
390 * @return array Path and url to upload directory.
391 */
392 private static function create_upload_dir( $storage ) {
393 $storage = trim( $storage, '/' );
394
395 /**
396 * Change permissions when creating new folders.
397 *
398 * @param int $permissions New directory access permissions. By default 0755.
399 */
400 $permissions = apply_filters( 'sharing_image_directory_permissions', 0755 );
401
402 // We do not pay attention to the possible error.
403 wp_mkdir_p( ABSPATH . $storage, $permissions, true );
404
405 return array( ABSPATH . $storage, site_url( $storage ) );
406 }
407 }
408