/var/www/html/wp-content/plugins/woocommerce/includes/abstracts/class-wc-background-process.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
<?php
/**
 * Abstract WP_Background_Process class.
 *
 * Uses https://github.com/A5hleyRich/wp-background-processing to handle DB
 * updates in the background.
 *
 * @package WooCommerce\Classes
 */

defined'ABSPATH' ) || exit;

if ( ! 
class_exists'WP_Async_Request'false ) ) {
    include_once 
dirnameWC_PLUGIN_FILE ) . '/includes/libraries/wp-async-request.php';
}

if ( ! 
class_exists'WP_Background_Process'false ) ) {
    include_once 
dirnameWC_PLUGIN_FILE ) . '/includes/libraries/wp-background-process.php';
}

/**
 * WC_Background_Process class.
 */
abstract class WC_Background_Process extends WP_Background_Process {

    
/**
     * Is queue empty.
     *
     * @return bool
     */
    
protected function is_queue_empty() {
        global 
$wpdb;

        
$table  $wpdb->options;
        
$column 'option_name';

        if ( 
is_multisite() ) {
            
$table  $wpdb->sitemeta;
            
$column 'meta_key';
        }

        
$key $wpdb->esc_like$this->identifier '_batch_' ) . '%';

        
$count $wpdb->get_var$wpdb->prepare"SELECT COUNT(*) FROM {$table} WHERE {$column} LIKE %s"$key ) ); // @codingStandardsIgnoreLine.

        
return ! ( $count );
    }

    
/**
     * Get batch.
     *
     * @return stdClass Return the first batch from the queue.
     */
    
protected function get_batch() {
        global 
$wpdb;

        
$table        $wpdb->options;
        
$column       'option_name';
        
$key_column   'option_id';
        
$value_column 'option_value';

        if ( 
is_multisite() ) {
            
$table        $wpdb->sitemeta;
            
$column       'meta_key';
            
$key_column   'meta_id';
            
$value_column 'meta_value';
        }

        
$key $wpdb->esc_like$this->identifier '_batch_' ) . '%';

        
$query $wpdb->get_row$wpdb->prepare"SELECT * FROM {$table} WHERE {$column} LIKE %s ORDER BY {$key_column} ASC LIMIT 1"$key ) ); // @codingStandardsIgnoreLine.

        
$batch       = new stdClass();
        
$batch->key  $query->$column;
        
$batch->data array_filter( (array) maybe_unserialize$query->$value_column ) );

        return 
$batch;
    }

    
/**
     * See if the batch limit has been exceeded.
     *
     * @return bool
     */
    
protected function batch_limit_exceeded() {
        return 
$this->time_exceeded() || $this->memory_exceeded();
    }

    
/**
     * Handle.
     *
     * Pass each queue item to the task handler, while remaining
     * within server memory and time limit constraints.
     */
    
protected function handle() {
        
$this->lock_process();

        do {
            
$batch $this->get_batch();

            foreach ( 
$batch->data as $key => $value ) {
                
$task $this->task$value );

                if ( 
false !== $task ) {
                    
$batch->data$key ] = $task;
                } else {
                    unset( 
$batch->data$key ] );
                }

                if ( 
$this->batch_limit_exceeded() ) {
                    
// Batch limits reached.
                    
break;
                }
            }

            
// Update or delete current batch.
            
if ( ! empty( $batch->data ) ) {
                
$this->update$batch->key$batch->data );
            } else {
                
$this->delete$batch->key );
            }
        } while ( ! 
$this->batch_limit_exceeded() && ! $this->is_queue_empty() );

        
$this->unlock_process();

        
// Start next batch or complete process.
        
if ( ! $this->is_queue_empty() ) {
            
$this->dispatch();
        } else {
            
$this->complete();
        }
    }

    
/**
     * Get memory limit.
     *
     * @return int
     */
    
protected function get_memory_limit() {
        if ( 
function_exists'ini_get' ) ) {
            
$memory_limit ini_get'memory_limit' );
        } else {
            
// Sensible default.
            
$memory_limit '128M';
        }

        if ( ! 
$memory_limit || -=== intval$memory_limit ) ) {
            
// Unlimited, set to 32GB.
            
$memory_limit '32G';
        }

        return 
wp_convert_hr_to_bytes$memory_limit );
    }

    
/**
     * Schedule cron healthcheck.
     *
     * @param array $schedules Schedules.
     * @return array
     */
    
public function schedule_cron_healthcheck$schedules ) {
        
$interval apply_filters$this->identifier '_cron_interval');

        if ( 
property_exists$this'cron_interval' ) ) {
            
$interval apply_filters$this->identifier '_cron_interval'$this->cron_interval );
        }

        
// Adds every 5 minutes to the existing schedules.
        
$schedules$this->identifier '_cron_interval' ] = array(
            
'interval' => MINUTE_IN_SECONDS $interval,
            
/* translators: %d: interval */
            
'display'  => sprintf__'Every %d minutes''woocommerce' ), $interval ),
        );

        return 
$schedules;
    }

    
/**
     * Delete all batches.
     *
     * @return WC_Background_Process
     */
    
public function delete_all_batches() {
        global 
$wpdb;

        
$table  $wpdb->options;
        
$column 'option_name';

        if ( 
is_multisite() ) {
            
$table  $wpdb->sitemeta;
            
$column 'meta_key';
        }

        
$key $wpdb->esc_like$this->identifier '_batch_' ) . '%';

        
$wpdb->query$wpdb->prepare"DELETE FROM {$table} WHERE {$column} LIKE %s"$key ) ); // @codingStandardsIgnoreLine.

        
return $this;
    }

    
/**
     * Kill process.
     *
     * Stop processing queue items, clear cronjob and delete all batches.
     */
    
public function kill_process() {
        if ( ! 
$this->is_queue_empty() ) {
            
$this->delete_all_batches();
            
wp_clear_scheduled_hook$this->cron_hook_identifier );
        }
    }
}