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