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-settings.php

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

530 lines 13.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Settings page class
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 * Settings class.
17 *
18 * @class Settings
19 */
20 class Settings {
21 /**
22 * Admin screen id.
23 *
24 * @var string
25 */
26 const SCREEN_ID = 'settings_page_sharing-image';
27
28 /**
29 * Plugin admin menu slug.
30 *
31 * @var string
32 */
33 const SETTINGS_SLUG = 'sharing-image';
34
35 /**
36 * Common nonce for settings tabs.
37 */
38 const SETTINGS_NONCE = 'sharing-image-nonce';
39
40 /**
41 * List of settings tabs.
42 *
43 * @var array
44 */
45 private static $tabs = array();
46
47 /**
48 * Init class actions and filters.
49 */
50 public static function init() {
51 self::init_tabs();
52
53 add_action( 'admin_menu', array( __CLASS__, 'add_menu' ) );
54 add_action( 'admin_title', array( __CLASS__, 'update_settings_title' ) );
55 add_action( 'admin_init', array( __CLASS__, 'allow_custom_fonts' ) );
56
57 add_filter( 'plugin_action_links', array( __CLASS__, 'add_settings_link' ), 10, 2 );
58 }
59
60 /**
61 * Add plugin settings page in WordPress menu.
62 */
63 public static function add_menu() {
64 /**
65 * Easy way to hide settings page.
66 *
67 * @param bool $hide_settings Set true to hide settings page.
68 */
69 $hide_settings = apply_filters( 'sharing_image_hide_settings', false );
70
71 if ( $hide_settings ) {
72 return;
73 }
74
75 add_options_page(
76 esc_html__( 'Sharing Image settings', 'sharing-image' ),
77 esc_html__( 'Sharing Image', 'sharing-image' ),
78 'manage_options',
79 self::SETTINGS_SLUG,
80 array( __CLASS__, 'display_settings' )
81 );
82
83 // Add required assets and objects.
84 add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_styles' ) );
85 add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) );
86 }
87
88 /**
89 * Allow uploading custom fonts for templates editor.
90 * This function may affect the security of the site.
91 * Disable font uploading if you are not going to use it.
92 */
93 public static function allow_custom_fonts() {
94 /**
95 * Easy way to disable custom font uploading.
96 *
97 * @param bool $disable_fonts Set true to disable fonts uploading.
98 */
99 $disable_fonts = apply_filters( 'sharing_image_disable_custom_fonts', false );
100
101 if ( $disable_fonts ) {
102 return;
103 }
104
105 // Allow fonts uploading.
106 add_filter( 'wp_check_filetype_and_ext', array( __CLASS__, 'fix_fonts_mime_type' ), 10, 3 );
107
108 // Add new .ttf and .otf font mime types.
109 add_filter( 'upload_mimes', array( __CLASS__, 'add_fonts_mime_type' ) );
110 }
111
112 /**
113 * Add settings link to plugins list.
114 *
115 * @param array $actions An array of plugin action links.
116 * @param string $plugin_file Path to the plugin file relative to the plugins directory.
117
118 * @return array Array of settings actions.
119 */
120 public static function add_settings_link( $actions, $plugin_file ) {
121 $actions = (array) $actions;
122
123 if ( plugin_basename( SHARING_IMAGE_FILE ) === $plugin_file ) {
124 $actions[] = sprintf(
125 '<a href="%s">%s</a>',
126 admin_url( 'options-general.php?page=' . self::SETTINGS_SLUG ),
127 __( 'Settings', 'sharing-image' )
128 );
129 }
130
131 return $actions;
132 }
133
134 /**
135 * Show plugin settings.
136 */
137 public static function display_settings() {
138 if ( ! self::is_settings_screen() ) {
139 return;
140 }
141
142 include_once SHARING_IMAGE_DIR . 'templates/settings.php';
143
144 /**
145 * Fires on settings template including.
146 */
147 do_action( 'sharing_image_settings' );
148 }
149
150 /**
151 * Fix .ttf and .otf files mime.
152 *
153 * @param array $types Values for the extension, mime type, and corrected filename.
154 * @param string $file Full path to the file.
155 * @param string $filename The name of the file (may differ from $file due to.
156 *
157 * @return array List of file types.
158 */
159 public static function fix_fonts_mime_type( $types, $file, $filename ) {
160 $extension = pathinfo( $filename, PATHINFO_EXTENSION );
161
162 if ( 'ttf' === $extension ) {
163 $types['ext'] = false;
164
165 if ( current_user_can( 'manage_options' ) ) {
166 $types['ext'] = 'ttf';
167 $types['type'] = 'font/ttf';
168 }
169 }
170
171 if ( 'otf' === $extension ) {
172 $types['ext'] = false;
173
174 if ( current_user_can( 'manage_options' ) ) {
175 $types['ext'] = 'otf';
176 $types['type'] = 'font/otf';
177 }
178 }
179
180 return $types;
181 }
182
183 /**
184 * Add new .ttf and .otf font mime types.
185 *
186 * @param array $types Allowed file types to upload.
187 *
188 * @return array Allowed file types to upload.
189 */
190 public static function add_fonts_mime_type( $types ) {
191 $types['ttf'] = 'font/ttf';
192 $types['otf'] = 'font/otf';
193
194 return $types;
195 }
196
197 /**
198 * Enqueue settings styles.
199 */
200 public static function enqueue_styles() {
201 if ( ! self::is_settings_screen() ) {
202 return;
203 }
204
205 $asset = require SHARING_IMAGE_DIR . 'assets/settings/index.asset.php';
206
207 wp_enqueue_media();
208
209 wp_enqueue_style(
210 'sharing-image-settings',
211 plugins_url( 'assets/settings/index.css', SHARING_IMAGE_FILE ),
212 array(),
213 SHARING_IMAGE_VERSION,
214 'all'
215 );
216 }
217
218 /**
219 * Enqueue settings scripts.
220 */
221 public static function enqueue_scripts() {
222 if ( ! self::is_settings_screen() ) {
223 return;
224 }
225
226 $asset = require SHARING_IMAGE_DIR . 'assets/widget/index.asset.php';
227
228 wp_enqueue_script(
229 'sharing-image-settings',
230 plugins_url( 'assets/settings/index.js', SHARING_IMAGE_FILE ),
231 $asset['dependencies'],
232 $asset['version'],
233 true
234 );
235
236 wp_enqueue_media();
237
238 // Translations availible only for WP 5.0+.
239 wp_set_script_translations( 'sharing-image-settings', 'sharing-image' );
240
241 $object = self::create_script_object();
242
243 // Add settings script object.
244 wp_localize_script( 'sharing-image-settings', 'sharingImageSettings', $object );
245 }
246
247 /**
248 * Update settings page title.
249 *
250 * @param string $title Plugin settings page title.
251 *
252 * @return string Plugin settings title
253 */
254 public static function update_settings_title( $title ) {
255 if ( ! self::is_settings_screen() ) {
256 return $title;
257 }
258
259 $tab = self::get_current_tab();
260
261 if ( null === $tab ) {
262 return $title;
263 }
264
265 if ( empty( self::$tabs[ $tab ]['label'] ) ) {
266 return $title;
267 }
268
269 $label = esc_html( self::$tabs[ $tab ]['label'] );
270
271 return sprintf( '%s &ndash; %s', $label, $title );
272 }
273
274 /**
275 * Create script object to inject with settings.
276 *
277 * @return array Filtered script settings object.
278 */
279 private static function create_script_object() {
280 $uploads = wp_get_upload_dir();
281
282 // Get uploads directory path from WordPress root.
283 $basedir = str_replace( ABSPATH, '', $uploads['basedir'] );
284
285 $object = array(
286 'nonce' => wp_create_nonce( self::SETTINGS_NONCE ),
287 'links' => array(
288 'uploads' => esc_url( admin_url( 'upload.php' ) ),
289 'action' => esc_url( admin_url( 'admin-post.php' ) ),
290 'premium' => esc_url_raw( self::get_tab_link( 'premium' ) ),
291 'storage' => path_join( $basedir, 'sharing-image' ),
292 ),
293 'fonts' => self::get_fonts(),
294 'templates' => Templates::get_templates(),
295 'index' => Templates::create_unique_index(),
296 'snippets' => Snippets::get_snippets(),
297 'config' => Config::get_config(),
298 'license' => Premium::get_license(),
299 );
300
301 /**
302 * Filters settings script object.
303 *
304 * @param array $object Array of settings script object.
305 */
306 return apply_filters( 'sharing_image_settings_object', $object );
307 }
308
309 /**
310 * Show settings tab template.
311 */
312 private static function show_settings_section() {
313 $tab = self::get_current_tab();
314
315 if ( null === $tab ) {
316 return;
317 }
318
319 include_once SHARING_IMAGE_DIR . "templates/{$tab}.php";
320 }
321
322 /**
323 * Show settings messages and errors after post actions.
324 */
325 private static function show_settings_message() {
326 // phpcs:ignore WordPress.Security.NonceVerification
327 $message = isset( $_GET['message'] ) ? absint( $_GET['message'] ) : 0;
328
329 switch ( $message ) {
330 case 1:
331 add_settings_error( 'sharing-image', 'sharing-image', __( 'Settings updated successfully.', 'sharing-image' ), 'updated' );
332 break;
333
334 case 2:
335 add_settings_error( 'sharing-image', 'sharing-image', __( 'Unable to save template settings.', 'sharing-image' ) );
336 break;
337
338 case 3:
339 add_settings_error( 'sharing-image', 'sharing-image', __( 'Template deleted successfully.', 'sharing-image' ), 'updated' );
340 break;
341
342 case 4:
343 add_settings_error( 'sharing-image', 'sharing-image', __( 'Unable to delete template.', 'sharing-image' ) );
344 break;
345
346 case 5:
347 add_settings_error( 'sharing-image', 'sharing-image', __( 'Unable to save configuration settings.', 'sharing-image' ) );
348 break;
349
350 case 6:
351 add_settings_error( 'sharing-image', 'sharing-image', __( 'Error uploading file. Please try again.', 'sharing-image' ) );
352 break;
353
354 case 7:
355 add_settings_error( 'sharing-image', 'sharing-image', __( 'The imported file is empty.', 'sharing-image' ) );
356 break;
357
358 case 8:
359 add_settings_error( 'sharing-image', 'sharing-image', __( 'The maximum allowed number of templates has been reached. Please upgrade to Premium.', 'sharing-image' ) );
360 break;
361
362 case 9:
363 add_settings_error( 'sharing-image', 'sharing-image', __( 'Templates imported successfully.', 'sharing-image' ), 'updated' );
364 break;
365
366 case 10:
367 add_settings_error( 'sharing-image', 'sharing-image', __( 'Unable to clone template.', 'sharing-image' ) );
368 break;
369
370 case 11:
371 add_settings_error( 'sharing-image', 'sharing-image', __( 'Template cloned successfully.', 'sharing-image' ), 'updated' );
372 break;
373
374 case 12:
375 add_settings_error( 'sharing-image', 'sharing-image', __( 'Posters removed successfully.', 'sharing-image' ), 'updated' );
376 break;
377 }
378
379 settings_errors( 'sharing-image' );
380 }
381
382 /**
383 * Set list of settings page tabs.
384 */
385 private static function init_tabs() {
386 $tabs = array(
387 'templates' => array(
388 'label' => __( 'Templates', 'sharing-image' ),
389 'link' => admin_url( 'options-general.php?page=' . self::SETTINGS_SLUG ),
390 'default' => true,
391 ),
392 'config' => array(
393 'label' => __( 'Configuration', 'sharing-image' ),
394 'link' => admin_url( 'options-general.php?page=' . self::SETTINGS_SLUG . '&tab=config' ),
395 ),
396 'tools' => array(
397 'label' => __( 'Tools', 'sharing-image' ),
398 'link' => admin_url( 'options-general.php?page=' . self::SETTINGS_SLUG . '&tab=tools' ),
399 ),
400 'premium' => array(
401 'label' => __( 'Premium', 'sharing-image' ),
402 'link' => admin_url( 'options-general.php?page=' . self::SETTINGS_SLUG . '&tab=premium' ),
403 ),
404 );
405
406 /**
407 * Filters tabs in settings page.
408 *
409 * @param array $tabs List of settings tabs.
410 */
411 self::$tabs = apply_filters( 'sharing_image_settings_tabs', $tabs );
412 }
413
414 /**
415 * Print menu on settings page.
416 */
417 private static function show_settings_menu() {
418 $current = self::get_current_tab();
419
420 foreach ( self::$tabs as $tab => $args ) {
421 $classes = array(
422 'sharing-image-tab',
423 );
424
425 if ( $current === $tab ) {
426 $classes[] = 'active';
427 }
428
429 if ( null === $current && ! empty( $args['default'] ) ) {
430 $classes[] = 'active';
431 }
432
433 printf(
434 '<a href="%2$s" class="%1$s">%3$s</a>',
435 esc_attr( implode( ' ', $classes ) ),
436 esc_url( $args['link'] ),
437 esc_html( $args['label'] )
438 );
439 }
440 }
441
442 /**
443 * Get availible fonts.
444 *
445 * @return array List of availible poster fonts.
446 */
447 private static function get_fonts() {
448 $fonts = array(
449 'open-sans' => 'Open Sans',
450 'open-sans-light' => 'Open Sans Light',
451 'open-sans-bold' => 'Open Sans Bold',
452 'merriweather' => 'Merriweather',
453 'roboto-slab' => 'Roboto Slab',
454 'ubuntu' => 'Ubuntu',
455 'rubik-bold' => 'Rubik Bold',
456 'alice' => 'Alice',
457 'lobster' => 'Lobster',
458 );
459
460 /**
461 * Filters poster fonts.
462 *
463 * @param array List of availible poster fonts.
464 */
465 return apply_filters( 'sharing_image_poster_fonts', $fonts );
466 }
467
468 /**
469 * Get tab link by slug.
470 *
471 * @param string $tab Tab name.
472 *
473 * @return string|null Tab link.
474 */
475 public static function get_tab_link( $tab ) {
476 if ( empty( self::$tabs[ $tab ]['link'] ) ) {
477 return admin_url( 'options-general.php?page=' . self::SETTINGS_SLUG );
478 }
479
480 return self::$tabs[ $tab ]['link'];
481 }
482
483 /**
484 * Get current tab.
485 *
486 * @return string|null Current tab name.
487 */
488 public static function get_current_tab() {
489 // phpcs:disable WordPress.Security.NonceVerification
490 if ( ! empty( $_GET['tab'] ) ) {
491 $tab = sanitize_file_name( wp_unslash( $_GET['tab'] ) );
492
493 if ( array_key_exists( $tab, self::$tabs ) ) {
494 return $tab;
495 }
496 }
497 // phpcs:enable WordPress.Security.NonceVerification
498
499 return null;
500 }
501
502 /**
503 * Add message id to the back link and redirect
504 *
505 * @param string $redirect Redirect link.
506 * @param int $message Settings error message id.
507 */
508 public static function redirect_with_message( $redirect, $message ) {
509 $redirect = add_query_arg( array( 'message' => $message ), $redirect );
510
511 wp_safe_redirect( $redirect );
512 exit;
513 }
514
515 /**
516 * Is current admin screen the plugin options screen.
517 *
518 * @return bool Whether the settings screen of this plugin is displayed or not.
519 */
520 private static function is_settings_screen() {
521 $current_screen = get_current_screen();
522
523 if ( $current_screen && self::SCREEN_ID === $current_screen->id ) {
524 return true;
525 }
526
527 return false;
528 }
529 }
530