| 1 |
<?php |
| 2 |
namespace ImageOptimization\Modules\Optimization; |
| 3 |
|
| 4 |
use ImageOptimization\Classes\Module_Base; |
| 5 |
use ImageOptimization\Modules\Backups\Rest\Restore_Single; |
| 6 |
use ImageOptimization\Modules\Optimization\Rest\Optimize_Single_Image; |
| 7 |
use Throwable; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
class Module extends Module_Base { |
| 14 |
public function get_name(): string { |
| 15 |
return 'optimization'; |
| 16 |
} |
| 17 |
|
| 18 |
public static function routes_list() : array { |
| 19 |
return [ |
| 20 |
'Optimize_Single_Image', |
| 21 |
'Optimize_Bulk', |
| 22 |
'Get_Bulk_Optimization_Images', |
| 23 |
'Optimization_Status', |
| 24 |
'Cancel_Bulk_Optimization', |
| 25 |
]; |
| 26 |
} |
| 27 |
|
| 28 |
public static function component_list() : array { |
| 29 |
return [ |
| 30 |
'Avif_Compatibility', |
| 31 |
'Media_Control', |
| 32 |
'Single_Optimization', |
| 33 |
'Upload_Optimization', |
| 34 |
'Bulk_Optimization', |
| 35 |
'List_View_Pointer', |
| 36 |
'Admin_Bulk_Actions', |
| 37 |
'Admin_Filter', |
| 38 |
'Retry', |
| 39 |
'Actions_Cleanup', |
| 40 |
]; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Enqueue styles and scripts |
| 45 |
*/ |
| 46 |
public function enqueue_scripts() { |
| 47 |
$asset_file = include IMAGE_OPTIMIZATION_ASSETS_PATH . 'build/control.asset.php'; |
| 48 |
|
| 49 |
foreach ( $asset_file['dependencies'] as $style ) { |
| 50 |
wp_enqueue_style( $style ); |
| 51 |
} |
| 52 |
|
| 53 |
add_thickbox(); |
| 54 |
|
| 55 |
wp_enqueue_style( |
| 56 |
'image-optimization-control', |
| 57 |
$this->get_css_assets_url( 'control' ), |
| 58 |
[], |
| 59 |
IMAGE_OPTIMIZATION_VERSION, |
| 60 |
); |
| 61 |
|
| 62 |
wp_enqueue_script( |
| 63 |
'image-optimization-control', |
| 64 |
$this->get_js_assets_url( 'control' ), |
| 65 |
$asset_file['dependencies'], |
| 66 |
IMAGE_OPTIMIZATION_VERSION, |
| 67 |
true |
| 68 |
); |
| 69 |
|
| 70 |
wp_set_script_translations( 'image-optimization-control', 'image-optimization' ); |
| 71 |
|
| 72 |
wp_localize_script( |
| 73 |
'image-optimization-control', |
| 74 |
'imageOptimizerControlSettings', |
| 75 |
[ |
| 76 |
'optimizeSingleImageNonce' => wp_create_nonce( Optimize_Single_Image::NONCE_NAME ), |
| 77 |
'restoreSingleImageNonce' => wp_create_nonce( Restore_Single::NONCE_NAME ), |
| 78 |
] |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
public static function load_template( $path, $name, $args = [] ): bool { |
| 83 |
$templates_path = sprintf( |
| 84 |
'%s/templates/%s/%s.php', |
| 85 |
dirname( __FILE__ ), |
| 86 |
$path, |
| 87 |
$name |
| 88 |
); |
| 89 |
|
| 90 |
try { |
| 91 |
load_template( $templates_path, false, $args ); |
| 92 |
} catch ( Throwable $t ) { |
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
return true; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Module constructor. |
| 101 |
*/ |
| 102 |
public function __construct() { |
| 103 |
$this->register_components(); |
| 104 |
$this->register_routes(); |
| 105 |
|
| 106 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 107 |
add_action( 'elementor/editor/after_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 108 |
} |
| 109 |
} |
| 110 |
|