| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimization\Modules\Settings; |
| 4 |
|
| 5 |
use ImageOptimization\Classes\Module_Base; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly |
| 9 |
} |
| 10 |
|
| 11 |
class Module extends Module_Base { |
| 12 |
const SETTING_PREFIX = 'image_optimizer_'; |
| 13 |
const SETTING_GROUP = 'image_optimizer_settings'; |
| 14 |
const SETTING_BASE_SLUG = 'image-optimization-settings'; |
| 15 |
const SETTING_CAPABILITY = 'manage_options'; |
| 16 |
|
| 17 |
public function get_name(): string { |
| 18 |
return 'settings'; |
| 19 |
} |
| 20 |
|
| 21 |
public static function component_list() : array { |
| 22 |
return [ |
| 23 |
'Settings_Pointer', |
| 24 |
]; |
| 25 |
} |
| 26 |
|
| 27 |
public static function get_options() : array { |
| 28 |
return [ |
| 29 |
'compression_level' => [ 'default' => 'lossy' ], |
| 30 |
'optimize_on_upload' => [ |
| 31 |
'type' => 'boolean', |
| 32 |
'default' => true, |
| 33 |
], |
| 34 |
'resize_larger_images' => [ |
| 35 |
'type' => 'boolean', |
| 36 |
'default' => true, |
| 37 |
], |
| 38 |
'resize_larger_images_size' => [ |
| 39 |
'type' => 'integer', |
| 40 |
'default' => 1920, |
| 41 |
], |
| 42 |
'exif_metadata' => [ |
| 43 |
'type' => 'boolean', |
| 44 |
'default' => true, |
| 45 |
], |
| 46 |
'original_images' => [ |
| 47 |
'type' => 'boolean', |
| 48 |
'default' => true, |
| 49 |
], |
| 50 |
'convert_to_webp' => [ |
| 51 |
'type' => 'boolean', |
| 52 |
'default' => true, |
| 53 |
], |
| 54 |
'custom_sizes' => [ |
| 55 |
'type' => 'string', |
| 56 |
'default' => 'all', |
| 57 |
], |
| 58 |
]; |
| 59 |
} |
| 60 |
|
| 61 |
public function register_options() { |
| 62 |
$options = $this->get_options(); |
| 63 |
|
| 64 |
foreach ( $options as $key => &$args ) { |
| 65 |
$args['type'] = $args['type'] ?? 'string'; |
| 66 |
$args['show_in_rest'] = $args['show_in_rest'] ?? true; |
| 67 |
$args['default'] = $args['default'] ?? ''; |
| 68 |
|
| 69 |
register_setting( |
| 70 |
self::SETTING_GROUP, |
| 71 |
self::SETTING_PREFIX . $key, |
| 72 |
$args |
| 73 |
); |
| 74 |
|
| 75 |
// Set defaults |
| 76 |
add_option( self::SETTING_PREFIX . $key, $args['default'] ); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
public function render_app() { |
| 81 |
?> |
| 82 |
<!-- The hack required to wrap WP notifications --> |
| 83 |
<div class="wrap"> |
| 84 |
<h1 style="display: none;" role="presentation"></h1> |
| 85 |
</div> |
| 86 |
|
| 87 |
<div id="image-optimization-app"></div> |
| 88 |
<?php |
| 89 |
} |
| 90 |
|
| 91 |
public function register_page() { |
| 92 |
add_media_page( |
| 93 |
__( 'Image Optimizer', 'image-optimization' ), |
| 94 |
__( 'Image Optimizer', 'image-optimization' ), |
| 95 |
self::SETTING_CAPABILITY, |
| 96 |
self::SETTING_BASE_SLUG, |
| 97 |
[ $this, 'render_app' ], |
| 98 |
6 |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
public function __construct() { |
| 103 |
$this->register_components(); |
| 104 |
|
| 105 |
add_action( 'admin_init', [ $this, 'register_options' ] ); |
| 106 |
add_action( 'rest_api_init', [ $this, 'register_options' ] ); |
| 107 |
add_action( 'admin_menu', [ $this, 'register_page' ] ); |
| 108 |
} |
| 109 |
} |
| 110 |
|