/var/www/html_us/wp-content/plugins/woocommerce/src/Internal/Admin/Notes/OrderMilestones.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
/**
 * WooCommerce Admin (Dashboard) Order Milestones Note Provider.
 *
 * Adds a note to the merchant's inbox when certain order milestones are reached.
 */

namespace Automattic\WooCommerce\Internal\Admin\Notes;

defined'ABSPATH' ) || exit;

use 
Automattic\WooCommerce\Admin\Notes\Note;
use 
Automattic\WooCommerce\Admin\Notes\Notes;
/**
 * Order_Milestones
 */
class OrderMilestones {
    
/**
     * Name of the "other milestones" note.
     */
    
const NOTE_NAME 'wc-admin-orders-milestone';

    
/**
     * Option key name to store last order milestone.
     */
    
const LAST_ORDER_MILESTONE_OPTION_KEY 'woocommerce_admin_last_orders_milestone';

    
/**
     * Hook to process order milestones.
     */
    
const PROCESS_ORDERS_MILESTONE_HOOK 'wc_admin_process_orders_milestone';

    
/**
     * Allowed order statuses for calculating milestones.
     *
     * @var array
     */
    
protected $allowed_statuses = array(
        
'pending',
        
'processing',
        
'completed',
    );

    
/**
     * Orders count cache.
     *
     * @var int
     */
    
protected $orders_count null;

    
/**
     * Further order milestone thresholds.
     *
     * @var array
     */
    
protected $milestones = array(
        
1,
        
10,
        
100,
        
250,
        
500,
        
1000,
        
5000,
        
10000,
        
500000,
        
1000000,
    );

    
/**
     * Delay hook attachment until after the WC post types have been registered.
     *
     * This is required for retrieving the order count.
     */
    
public function __construct() {
        
/**
         * Filter Order statuses that will count towards milestones.
         *
         * @since 3.5.0
         *
         * @param array $allowed_statuses Order statuses that will count towards milestones.
         */
        
$this->allowed_statuses apply_filters'woocommerce_admin_order_milestone_statuses'$this->allowed_statuses );

        
add_action'woocommerce_after_register_post_type', array( $this'init' ) );
        
register_deactivation_hookWC_PLUGIN_FILE, array( $this'clear_scheduled_event' ) );
    }

    
/**
     * Hook everything up.
     */
    
public function init() {
        if ( ! 
wp_next_scheduledself::PROCESS_ORDERS_MILESTONE_HOOK ) ) {
            
wp_schedule_eventtime(), 'hourly'self::PROCESS_ORDERS_MILESTONE_HOOK );
        }

        
add_action'wc_admin_installed', array( $this'backfill_last_milestone' ) );

        
add_actionself::PROCESS_ORDERS_MILESTONE_HOOK, array( $this'possibly_add_note' ) );
    }

    
/**
     * Clear out our hourly milestone hook upon plugin deactivation.
     */
    
public function clear_scheduled_event() {
        
wp_clear_scheduled_hookself::PROCESS_ORDERS_MILESTONE_HOOK );
    }

    
/**
     * Get the total count of orders (in the allowed statuses).
     *
     * @param bool $no_cache Optional. Skip cache.
     * @return int Total orders count.
     */
    
public function get_orders_count$no_cache false ) {
        if ( 
$no_cache || is_null$this->orders_count ) ) {
            
$status_counts      array_map'wc_orders_count'$this->allowed_statuses );
            
$this->orders_count array_sum$status_counts );
        }

        return 
$this->orders_count;
    }

    
/**
     * Backfill the store's current milestone.
     *
     * Used to avoid celebrating milestones that were reached before plugin activation.
     */
    
public function backfill_last_milestone() {
        
// If the milestone notes have been disabled via filter, bail.
        
if ( ! $this->are_milestones_enabled() ) {
            return;
        }

        
$this->set_last_milestone$this->get_current_milestone() );
    }

    
/**
     * Get the store's last milestone.
     *
     * @return int Last milestone reached.
     */
    
public function get_last_milestone() {
        return 
get_optionself::LAST_ORDER_MILESTONE_OPTION_KEY);
    }

    
/**
     * Update the last reached milestone.
     *
     * @param int $milestone Last milestone reached.
     */
    
public function set_last_milestone$milestone ) {
        
update_optionself::LAST_ORDER_MILESTONE_OPTION_KEY$milestone );
    }

    
/**
     * Calculate the current orders milestone.
     *
     * Based on the threshold values in $this->milestones.
     *
     * @return int Current orders milestone.
     */
    
public function get_current_milestone() {
        
$milestone_reached 0;
        
$orders_count      $this->get_orders_count();

        foreach ( 
$this->milestones as $milestone ) {
            if ( 
$milestone <= $orders_count ) {
                
$milestone_reached $milestone;
            }
        }

        return 
$milestone_reached;
    }

    
/**
     * Get the appropriate note title for a given milestone.
     *
     * @param int $milestone Order milestone.
     * @return string Note title for the milestone.
     */
    
public static function get_note_title_for_milestone$milestone ) {
        switch ( 
$milestone ) {
            case 
1:
                return 
__'First order received''woocommerce' );
            case 
10:
            case 
100:
            case 
250:
            case 
500:
            case 
1000:
            case 
5000:
            case 
10000:
            case 
500000:
            case 
1000000:
                return 
sprintf(
                    
/* translators: Number of orders processed. */
                    
__'Congratulations on processing %s orders!''woocommerce' ),
                    
wc_format_decimal$milestone )
                );
            default:
                return 
'';
        }
    }

    
/**
     * Get the appropriate note content for a given milestone.
     *
     * @param int $milestone Order milestone.
     * @return string Note content for the milestone.
     */
    
public static function get_note_content_for_milestone$milestone ) {
        switch ( 
$milestone ) {
            case 
1:
                return 
__'Congratulations on getting your first order! Now is a great time to learn how to manage your orders.''woocommerce' );
            case 
10:
                return 
__"You've hit the 10 orders milestone! Look at you go. Browse some WooCommerce success stories for inspiration."'woocommerce' );
            case 
100:
            case 
250:
            case 
500:
            case 
1000:
            case 
5000:
            case 
10000:
            case 
500000:
            case 
1000000:
                return 
__'Another order milestone! Take a look at your Orders Report to review your orders to date.''woocommerce' );
            default:
                return 
'';
        }
    }

    
/**
     * Get the appropriate note action for a given milestone.
     *
     * @param int $milestone Order milestone.
     * @return array Note actoion (name, label, query) for the milestone.
     */
    
public static function get_note_action_for_milestone$milestone ) {
        switch ( 
$milestone ) {
            case 
1:
                return array(
                    
'name'  => 'learn-more',
                    
'label' => __'Learn more''woocommerce' ),
                    
'query' => 'https://woocommerce.com/document/managing-orders/?utm_source=inbox&utm_medium=product',
                );
            case 
10:
                return array(
                    
'name'  => 'browse',
                    
'label' => __'Browse''woocommerce' ),
                    
'query' => 'https://woocommerce.com/success-stories/?utm_source=inbox&utm_medium=product',
                );
            case 
100:
            case 
250:
            case 
500:
            case 
1000:
            case 
5000:
            case 
10000:
            case 
500000:
            case 
1000000:
                return array(
                    
'name'  => 'review-orders',
                    
'label' => __'Review your orders''woocommerce' ),
                    
'query' => '?page=wc-admin&path=/analytics/orders',
                );
            default:
                return array(
                    
'name'  => '',
                    
'label' => '',
                    
'query' => '',
                );
        }
    }

    
/**
     * Convenience method to see if the milestone notes are enabled.
     *
     * @return boolean True if milestone notifications are enabled.
     */
    
public function are_milestones_enabled() {
        
/**
         * Filter to allow for disabling order milestones.
         *
         * @since 3.7.0
         *
         * @param boolean default true
         */
        
$milestone_notes_enabled apply_filters'woocommerce_admin_order_milestones_enabled'true );

        return 
$milestone_notes_enabled;
    }

    
/**
     * Get the note. This is used for localizing the note.
     *
     * @return Note
     */
    
public static function get_note() {
        
$note Notes::get_note_by_nameself::NOTE_NAME );
        if ( ! 
$note ) {
            return 
false;
        }
        
$content_data $note->get_content_data();
        if ( ! isset( 
$content_data->current_milestone ) ) {
            return 
false;
        }
        return 
self::get_note_by_milestone(
            
$content_data->current_milestone
        
);
    }

    
/**
     * Get the note by milestones.
     *
     * @param int $current_milestone Current milestone.
     *
     * @return Note
     */
    
public static function get_note_by_milestone$current_milestone ) {
        
$content_data = (object) array(
            
'current_milestone' => $current_milestone,
        );

        
$note = new Note();
        
$note->set_titleself::get_note_title_for_milestone$current_milestone ) );
        
$note->set_contentself::get_note_content_for_milestone$current_milestone ) );
        
$note->set_content_data$content_data );
        
$note->set_typeNote::E_WC_ADMIN_NOTE_INFORMATIONAL );
        
$note->set_nameself::NOTE_NAME );
        
$note->set_source'woocommerce-admin' );
        
$note_action self::get_note_action_for_milestone$current_milestone );
        
$note->add_action$note_action['name'], $note_action['label'], $note_action['query'] );
        return 
$note;
    }

    
/**
     * Checks if a note can and should be added.
     *
     * @return bool
     */
    
public function can_be_added() {
        
// If the milestone notes have been disabled via filter, bail.
        
if ( ! $this->are_milestones_enabled() ) {
            return 
false;
        }

        
$last_milestone    $this->get_last_milestone();
        
$current_milestone $this->get_current_milestone();

        if ( 
$current_milestone <= $last_milestone ) {
            return 
false;
        }

        return 
true;
    }

    
/**
     * Add milestone notes for other significant thresholds.
     */
    
public function possibly_add_note() {
        if ( ! 
self::can_be_added() ) {
            return;
        }
        
$current_milestone $this->get_current_milestone();
        
$this->set_last_milestone$current_milestone );

        
// We only want one milestone note at any time.
        
Notes::delete_notes_with_nameself::NOTE_NAME );
        
$note $this->get_note_by_milestone$current_milestone );
        
$note->save();
    }
}