/var/www/html_de/wp-content/plugins/woocommerce/src/Caches/OrderCountCache.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php

declare( strict_types=);

namespace 
Automattic\WooCommerce\Caches;

use 
Automattic\WooCommerce\Caching\ObjectCache;
use 
Automattic\WooCommerce\Enums\OrderStatus;
use 
Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore;
use 
Automattic\WooCommerce\Utilities\OrderUtil;

/**
 * A class to cache counts for various order statuses.
 */
class OrderCountCache {

    
/**
     * Cache prefix.
     *
     * @var string
     */
    
private $cache_prefix 'order-count';

    
/**
     * Default value for the duration of the objects in the cache, in seconds
     * (may not be used depending on the cache engine used WordPress cache implementation).
     *
     * @var int
     */
    
protected $expiration DAY_IN_SECONDS;

    
/**
     * Retrieves the list of known statuses by order type. A cached array of statuses is saved per order type for
     * improved backward compatibility with some of the extensions that don't register all statuses they use with
     * WooCommerce.
     *
     * @param string $order_type The type of order.
     *
     * @return string[]
     */
    
private function get_saved_statuses_for_typestring $order_type ) {
        
$statuses wp_cache_get$this->get_saved_statuses_cache_key$order_type ) );
        if ( ! 
is_array$statuses ) ) {
            
$statuses = array();
        }

        return 
$statuses;
    }

    
/**
     * Adds the given statuses to the cached statuses array for the order type if they are not already stored.
     *
     * @param string   $order_type     The order type to save with.
     * @param string[] $order_statuses One or more normalised statuses to add.
     *
     * @return void
     */
    
private function ensure_statuses_for_typestring $order_type, array $order_statuses ) {
        if ( empty( 
$order_statuses ) ) {
            return;
        }

        
$existing     $this->get_saved_statuses_for_type$order_type );
        
$new_statuses array_diff$order_statuses$existing );
        if ( empty( 
$new_statuses ) ) {
            return;
        }
        
$merged array_uniquearray_merge$existing$new_statuses ) );

        
wp_cache_set$this->get_saved_statuses_cache_key$order_type ), $merged''$this->expiration );
    }

    
/**
     * Get the default statuses.
     *
     * @return string[]
     *
     * @deprecated 10.1.0 This method will be removed in the future.
     */
    
public function get_default_statuses() {
        return 
array_merge(
            
array_keyswc_get_order_statuses() ),
            array( 
OrderStatus::TRASH )
        );
    }

    
/**
     * Get the cache key for a given order type and status.
     *
     * @param string $order_type The type of order.
     * @param string $order_status The status of the order.
     * @return string The cache key.
     */
    
private function get_cache_key$order_type$order_status ) {
        return 
$this->cache_prefix '_' $order_type '_' $order_status;
    }

    
/**
     * Get the cache key saved statuses of the given order type.
     *
     * @param string $order_type The type of order.
     *
     * @return string The cache key.
     */
    
private function get_saved_statuses_cache_keystring $order_type ) {
        return 
$this->cache_prefix '_' $order_type '_statuses';
    }

    
/**
     * Check if the cache has a value for a given order type and status.
     *
     * @param string $order_type The type of order.
     * @param string $order_status The status of the order.
     * @return bool True if the cache has a value, false otherwise.
     */
    
public function is_cached$order_type$order_status ) {
        
$cache_key $this->get_cache_key$order_type$order_status );
        return 
wp_cache_get$cache_key ) !== false;
    }

    
/**
     * Set the cache value for a given order type and status.
     *
     * @param string $order_type The type of order.
     * @param string $order_status The status slug of the order.
     * @param int $value The value to set.
     * @return bool True if the value was set, false otherwise.
     */
    
public function set$order_type$order_statusint $value ): bool {
        
$this->ensure_statuses_for_type( (string) $order_type, array( (string) $order_status ) );
        
$cache_key $this->get_cache_key$order_type$order_status );
        return 
wp_cache_set$cache_key$value''$this->expiration );
    }


    
/**
     * Set the cache count value for multiple statuses at once.
     *
     * @param string $order_type The order type being set.
     * @param array  $counts     Normalized counts keyed by status slug
     *                           (e.g. [ 'wc-processing' => 10, 'wc-pending' => 5 ]).
     *
     * @return array|bool[]      Success map from wp_cache_set_multiple().
     */
    
public function set_multiplestring $order_type, array $counts ) {
        if ( empty( 
$counts ) ) {
            return array();
        }

        
$this->ensure_statuses_for_type$order_typearray_keys$counts ) );

        
$mapped_counts = array();
        foreach ( 
$counts as $status => $count ) {
            
$mapped_counts$this->get_cache_key$order_type$status ) ] = (int) $count;
        }

        return 
wp_cache_set_multiple$mapped_counts''$this->expiration );
    }


    
/**
     * Get the cache value for a given order type and set of statuses.
     *
     * @param string $order_type The type of order.
     * @param string[] $order_statuses The statuses of the order.
     * @return int[] The cache value.
     */
    
public function get$order_type$order_statuses = array() ) {
        
$order_type = (string) $order_type;
        if ( empty( 
$order_statuses ) ) {
            
$order_statuses $this->get_saved_statuses_for_type$order_type );
            if ( empty( 
$order_statuses ) ) {
                return 
null;
            }
        }

        
$cache_keys array_map( function( $order_statuses ) use ( $order_type ) {
            return 
$this->get_cache_key$order_type$order_statuses );
        }, 
$order_statuses );

        
$cache_values  wp_cache_get_multiple$cache_keys );
        
$status_values = array();

        foreach ( 
$cache_values as $key => $value ) {
            
// Return null for the entire cache if any of the requested statuses are not found because they fell out of cache.
            
if ( $value === false ) {
                return 
null;
            }

            
$order_status                   str_replace$this->get_cache_key$order_type'' ), ''$key );
            
$status_values$order_status ] = $value;
        }

        return 
$status_values;
    }

    
/**
     * Increment the cache value for a given order status.
     *
     * @param string $order_type The type of order.
     * @param string $order_status The status of the order.
     * @param int $offset The amount to increment by.
     * @return int The new value of the cache.
     */
    
public function increment$order_type$order_status$offset ) {
        
$cache_key $this->get_cache_key$order_type$order_status );
        return 
wp_cache_incr$cache_key$offset );
    }

    
/**
     * Decrement the cache value for a given order status.
     *
     * @param string $order_type The type of order.
     * @param string $order_status The status of the order.
     * @param int $offset The amount to decrement by.
     * @return int The new value of the cache.
     */
    
public function decrement$order_type$order_status$offset ) {
        
$cache_key $this->get_cache_key$order_type$order_status );
        return 
wp_cache_decr$cache_key$offset );
    }

    
/**
     * Flush the cache for a given order type and statuses.
     *
     * @param string $order_type The type of order.
     * @param string[] $order_statuses The statuses of the order.
     * @return void
     */
    
public function flush$order_type 'shop_order'$order_statuses = array() ) {
        
$order_type           = (string) $order_type;
        
$flush_saved_statuses false;
        if ( empty( 
$order_statuses ) ) {
            
$order_statuses       $this->get_saved_statuses_for_type$order_type );
            
$flush_saved_statuses true;
        }

        
$cache_keys array_map( function( $order_statuses ) use ( $order_type ) {
            return 
$this->get_cache_key$order_type$order_statuses );
        }, 
$order_statuses );

        if ( 
$flush_saved_statuses ) {
            
// If all statuses are being flushed, go ahead and flush the status list so any permanently removed statuses are cleared out.
            
$cache_keys[] = $this->get_saved_statuses_cache_key$order_type );
        }

        
wp_cache_delete_multiple$cache_keys );
    }
}