C:\xampp\htdocs\landing\wp-content\plugins\imagify\classes\Stats\OptimizedMediaWithoutWebp.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
namespace Imagify\Stats;

defined'ABSPATH' ) || die( 'Cheatin’ uh?' );

/**
 * Class to get and cache the number of optimized media without webp versions.
 *
 * @since  1.9
 * @author Grégory Viguier
 */
class OptimizedMediaWithoutWebp implements StatInterface {
    use \
Imagify\Traits\InstanceGetterTrait;

    
/**
     * Name of the transient storing the cached result.
     *
     * @var    string
     * @since  1.9
     * @access protected
     * @author Grégory Viguier
     */
    
const NAME 'imagify_stat_without_webp';

    
/**
     * Launch hooks.
     *
     * @since  1.9
     * @access public
     * @author Grégory Viguier
     */
    
public function init() {
        
add_action'imagify_after_optimize',      [ $this'maybe_clear_cache_after_optimization' ], 10);
        
add_action'imagify_after_restore_media', [ $this'maybe_clear_cache_after_restoration' ], 10);
        
add_action'imagify_delete_media',        [ $this'maybe_clear_cache_on_deletion' ] );
    }


    
/** ----------------------------------------------------------------------------------------- */
    /** GET/CACHE THE STAT ====================================================================== */
    /** ----------------------------------------------------------------------------------------- */

    /**
     * Get the number of optimized media without webp versions.
     *
     * @since  1.9
     * @access public
     * @author Grégory Viguier
     *
     * @return int
     */
    
public function get_stat() {
        
$ajax_post = \Imagify_Admin_Ajax_Post::get_instance();
        
$stat      0;

        
// Sum the counts of each context.
        
foreach ( imagify_get_context_names() as $context ) {
            
$stat += $ajax_post->get_bulk_instance$context )->has_optimized_media_without_webp();
        }

        return 
$stat;
    }

    
/**
     * Get and cache the number of optimized media without webp versions.
     *
     * @since  1.9
     * @access public
     * @author Grégory Viguier
     *
     * @return int
     */
    
public function get_cached_stat() {
        
$contexts implode'|'imagify_get_context_names() );
        
$stat     get_transient( static::NAME );

        if ( isset( 
$stat['stat'], $stat['contexts'] ) && $stat['contexts'] === $contexts ) {
            
// The number is stored and the contexts are the same.
            
return (int) $stat['stat'];
        }

        
$stat = [
            
'contexts' => $contexts,
            
'stat'     => $this->get_stat(),
        ];

        
set_transient( static::NAME$statDAY_IN_SECONDS );

        return 
$stat['stat'];
    }

    
/**
     * Clear the stat cache.
     *
     * @since  1.9
     * @access public
     * @author Grégory Viguier
     */
    
public function clear_cache() {
        
delete_transient( static::NAME );
    }


    
/** ----------------------------------------------------------------------------------------- */
    /** HOOKS =================================================================================== */
    /** ----------------------------------------------------------------------------------------- */

    /**
     * Clear cache after optimizing a media.
     *
     * @since  1.9
     * @access public
     * @author Grégory Viguier
     *
     * @param ProcessInterface $process The optimization process.
     * @param array            $item    The item being processed.
     */
    
public function maybe_clear_cache_after_optimization$process$item ) {
        if ( ! 
$process->get_media()->is_image() || false === get_transient( static::NAME ) ) {
            return;
        }

        
$sizes     $process->get_data()->get_optimization_data();
        
$sizes     = isset( $sizes['sizes'] ) ? (array) $sizes['sizes'] : [];
        
$new_sizes array_flip$item['sizes_done'] );
        
$new_sizes array_intersect_key$sizes$new_sizes );
        
$size_name 'full' $process::WEBP_SUFFIX;

        if ( ! isset( 
$new_sizes['full'] ) && ! empty( $new_sizes$size_name ]['success'] ) ) {
            
/**
             * We just successfully generated the webp version of the full size.
             * The full size was not optimized at the same time, that means it was optimized previously.
             * Meaning: we just added a webp version to a media that was previously optimized, so there is one less optimized media without webp.
             */
            
$this->clear_cache();
            return;
        }

        if ( ! empty( 
$new_sizes['full']['success'] ) && empty( $new_sizes$size_name ]['success'] ) ) {
            
/**
             * We now have a new optimized media without webp.
             */
            
$this->clear_cache();
        }
    }

    
/**
     * Clear cache after restoring a media.
     *
     * @since  1.9
     * @access public
     * @author Grégory Viguier
     *
     * @param ProcessInterface $process The optimization process.
     * @param bool|WP_Error    $response The result of the operation: true on success, a WP_Error object on failure.
     * @param array            $files    The list of files, before restoring them.
     * @param array            $data     The optimization data, before deleting it.
     */
    
public function maybe_clear_cache_after_restoration$process$response$files$data ) {
        if ( ! 
$process->get_media()->is_image() || false === get_transient( static::NAME ) ) {
            return;
        }

        
$sizes     = isset( $data['sizes'] ) ? (array) $data['sizes'] : [];
        
$size_name 'full' $process::WEBP_SUFFIX;

        if ( ! empty( 
$sizes['full']['success'] ) && empty( $sizes$size_name ]['success'] ) ) {
            
/**
             * This media had no webp versions.
             */
            
$this->clear_cache();
        }
    }

    
/**
     * Clear cache on media deletion.
     *
     * @since  1.9
     * @access public
     * @author Grégory Viguier
     *
     * @param ProcessInterface $process An optimization process.
     */
    
public function maybe_clear_cache_on_deletion$process ) {
        if ( 
false === get_transient( static::NAME ) ) {
            return;
        }

        
$data      $process->get_data()->get_optimization_data();
        
$sizes     = isset( $data['sizes'] ) ? (array) $data['sizes'] : [];
        
$size_name 'full' $process::WEBP_SUFFIX;

        if ( ! empty( 
$sizes['full']['success'] ) && empty( $sizes$size_name ]['success'] ) ) {
            
/**
             * This media had no webp versions.
             */
            
$this->clear_cache();
        }
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586