Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ImageOptimizedMarker
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 4
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 is_enqueue_allowed
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
2
 enqueue_marker_assets
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
6
 add_media_library_data_attribute
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace NewfoldLabs\WP\Module\Performance\Images;
4
5use NewfoldLabs\WP\Module\Performance\Services\I18nService;
6
7/**
8 * Marks optimized images in the WordPress Media Library.
9 */
10class ImageOptimizedMarker {
11
12    /**
13     * Initializes the class by registering hooks.
14     */
15    public function __construct() {
16        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_marker_assets' ) );
17        add_filter( 'wp_prepare_attachment_for_js', array( $this, 'add_media_library_data_attribute' ), 10, 2 );
18    }
19
20    /**
21     * Establish whether the current page allows scripts to be enqueued.
22     *
23     * @return boolean
24     */
25    private function is_enqueue_allowed() {
26        global $pagenow;
27
28        $excluded_pages = array(
29            'admin.php',
30            'themes.php',
31            'plugins.php',
32            'users.php',
33            'tools.php',
34            'import.php',
35            'export.php',
36            'site-health.php',
37            'export-personal-data.php',
38            'erase-personal-data.php',
39            'theme-editor.php',
40            'plugin-editor.php',
41        );
42
43        $is_excluded = in_array( $pagenow, $excluded_pages, true );
44
45        return apply_filters( 'newfold_performance_optimized_image_marker_enqueue_allowed', ! $is_excluded );
46    }
47
48    /**
49     * Enqueues JS and CSS files for marking optimized images.
50     */
51    public function enqueue_marker_assets() {
52        wp_register_script(
53            'nfd-performance-optimizer-marker',
54            NFD_PERFORMANCE_BUILD_URL . '/assets/image-optimized-marker/image-optimized-marker.min.js',
55            array( 'wp-api-fetch', 'wp-element', 'wp-i18n' ),
56            filemtime( NFD_PERFORMANCE_BUILD_DIR . '/assets/image-optimized-marker/image-optimized-marker.min.js' ),
57            true
58        );
59
60        I18nService::load_js_translations(
61            'wp-module-performance',
62            'nfd-performance-optimizer-marker',
63            NFD_PERFORMANCE_PLUGIN_LANGUAGES_DIR
64        );
65
66        wp_register_style(
67            'nfd-performance-optimizer-marker-style',
68            NFD_PERFORMANCE_BUILD_URL . '/assets/image-optimized-marker/image-optimized-marker.min.css',
69            array(),
70            filemtime( NFD_PERFORMANCE_BUILD_DIR . '/assets/image-optimized-marker/image-optimized-marker.min.css' )
71        );
72
73        if ( $this->is_enqueue_allowed() ) {
74            wp_enqueue_style( 'nfd-performance-optimizer-marker-style' );
75            wp_enqueue_script( 'nfd-performance-optimizer-marker' );
76        }
77    }
78
79    /**
80     * Adds a custom data attribute to media library items if optimized.
81     *
82     * @param array   $response  The prepared attachment response.
83     * @param WP_Post $attachment The current attachment object.
84     *
85     * @return array The modified response.
86     */
87    public function add_media_library_data_attribute( $response, $attachment ) {
88        if ( get_post_meta( $attachment->ID, '_nfd_performance_image_optimized', true ) ) {
89            $response['nfdPerformanceImageOptimized'] = true;
90        }
91
92        return $response;
93    }
94}