Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 54 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ImageBulkOptimizer | |
0.00% |
0 / 54 |
|
0.00% |
0 / 4 |
30 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| is_enqueue_allowed | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
2 | |||
| enqueue_bulk_optimizer_script | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
6 | |||
| get_inline_script | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Performance\Images; |
| 4 | |
| 5 | use NewfoldLabs\WP\Module\Performance\Services\I18nService; |
| 6 | |
| 7 | /** |
| 8 | * Manages bulk optimization functionality for the Media Library. |
| 9 | */ |
| 10 | class ImageBulkOptimizer { |
| 11 | |
| 12 | /** |
| 13 | * Constructor to initialize the bulk optimizer feature. |
| 14 | */ |
| 15 | public function __construct() { |
| 16 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_bulk_optimizer_script' ) ); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Establish whether the current page allows scripts to be enqueued. |
| 21 | * |
| 22 | * @return boolean |
| 23 | */ |
| 24 | private function is_enqueue_allowed() { |
| 25 | global $pagenow; |
| 26 | |
| 27 | $excluded_pages = array( |
| 28 | 'admin.php', |
| 29 | 'themes.php', |
| 30 | 'plugins.php', |
| 31 | 'users.php', |
| 32 | 'tools.php', |
| 33 | 'import.php', |
| 34 | 'export.php', |
| 35 | 'site-health.php', |
| 36 | 'export-personal-data.php', |
| 37 | 'erase-personal-data.php', |
| 38 | 'theme-editor.php', |
| 39 | 'plugin-editor.php', |
| 40 | ); |
| 41 | |
| 42 | $is_excluded = in_array( $pagenow, $excluded_pages, true ); |
| 43 | |
| 44 | return apply_filters( 'newfold_performance_image_bulk_optimizer_enqueue_allowed', ! $is_excluded ); |
| 45 | } |
| 46 | /** |
| 47 | * Enqueues the bulk optimizer script. |
| 48 | */ |
| 49 | public function enqueue_bulk_optimizer_script() { |
| 50 | wp_register_script( |
| 51 | 'nfd-performance-bulk-optimizer', |
| 52 | NFD_PERFORMANCE_BUILD_URL . '/assets/image-bulk-optimizer/image-bulk-optimizer.min.js', |
| 53 | array( 'wp-api-fetch', 'wp-element', 'wp-i18n' ), |
| 54 | filemtime( NFD_PERFORMANCE_BUILD_DIR . '/assets/image-bulk-optimizer/image-bulk-optimizer.min.js' ), |
| 55 | true |
| 56 | ); |
| 57 | |
| 58 | I18nService::load_js_translations( |
| 59 | 'wp-module-performance', |
| 60 | 'nfd-performance-bulk-optimizer', |
| 61 | NFD_PERFORMANCE_PLUGIN_LANGUAGES_DIR |
| 62 | ); |
| 63 | |
| 64 | wp_register_style( |
| 65 | 'nfd-performance-bulk-optimizer-style', |
| 66 | NFD_PERFORMANCE_BUILD_URL . '/assets/image-bulk-optimizer/image-bulk-optimizer.min.css', |
| 67 | array(), |
| 68 | filemtime( NFD_PERFORMANCE_BUILD_DIR . '/assets/image-bulk-optimizer/image-bulk-optimizer.min.css' ) |
| 69 | ); |
| 70 | |
| 71 | if ( $this->is_enqueue_allowed() ) { |
| 72 | wp_enqueue_style( 'nfd-performance-bulk-optimizer-style' ); |
| 73 | wp_enqueue_script( 'nfd-performance-bulk-optimizer' ); |
| 74 | wp_add_inline_script( |
| 75 | 'nfd-performance-bulk-optimizer', |
| 76 | $this->get_inline_script(), |
| 77 | 'before' |
| 78 | ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Generates inline settings for the bulk optimizer script. |
| 84 | * |
| 85 | * @return string JavaScript code to inline. |
| 86 | */ |
| 87 | private function get_inline_script() { |
| 88 | $api_url = add_query_arg( |
| 89 | 'rest_route', |
| 90 | '/newfold-performance/v1/images/optimize', |
| 91 | get_rest_url() |
| 92 | ); |
| 93 | |
| 94 | return sprintf( |
| 95 | 'window.nfdPerformance = window.nfdPerformance || {}; |
| 96 | window.nfdPerformance.imageOptimization = window.nfdPerformance.imageOptimization || {}; |
| 97 | window.nfdPerformance.imageOptimization.bulkOptimizer = { |
| 98 | apiUrl: "%s" |
| 99 | };', |
| 100 | esc_url( $api_url ) |
| 101 | ); |
| 102 | } |
| 103 | } |