/var/www/html_us/wp-content/plugins/woocommerce/src/Admin/RemoteInboxNotifications/SpecRunner.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
<?php
/**
 * Runs a single spec.
 */

namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;

defined'ABSPATH' ) || exit;

use 
Automattic\WooCommerce\Admin\Notes\Note;
use 
Automattic\WooCommerce\Admin\Notes\Notes;
use 
Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\EvaluateAndGetStatus;
use 
Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\RuleEvaluator;

/**
 * Runs a single spec.
 */
class SpecRunner {
    
/**
     * Run the spec.
     *
     * @param object $spec         The spec to run.
     * @param object $stored_state Stored state.
     */
    
public static function run_spec$spec$stored_state ) {
        
$data_store Notes::load_data_store();

        
// Create or update the note.
        
$existing_note_ids $data_store->get_notes_with_name$spec->slug );
        if ( ! 
is_countable$existing_note_ids ) || count$existing_note_ids ) === ) {
            
$note = new Note();
            
$note->set_statusNote::E_WC_ADMIN_NOTE_PENDING );
        } else {
            
$note Notes::get_note$existing_note_ids[0] );
            if ( 
$note === false ) {
                return;
            }
        }

        
// Evaluate the spec and get the new note status.
        
$previous_status $note->get_status();
        try {
            
$status EvaluateAndGetStatus::evaluate(
                
$spec,
                
$previous_status,
                
$stored_state,
                new 
RuleEvaluator()
            );
        } catch ( 
\Throwable $e ) {
            return 
$e;
        }

        
// If the status is changing, update the created date to now.
        
if ( $previous_status !== $status ) {
            
$note->set_date_createdtime() );
        }

        
// Get the matching locale or fall back to en-US.
        
$locale self::get_locale$spec->locales );

        if ( 
$locale === null ) {
            return;
        }

        
// Set up the note.
        
$note->set_title$locale->title );
        
$note->set_content$locale->content );
        
$note->set_content_data( isset( $spec->content_data ) ? $spec->content_data : (object) array() );
        
$note->set_status$status );
        
$note->set_type$spec->type );
        
$note->set_name$spec->slug );
        if ( isset( 
$spec->source ) ) {
            
$note->set_source$spec->source );
        }
        if ( isset( 
$spec->layout ) ) {
            
$note->set_layout$spec->layout );
        }

        
// Recreate actions.
        
$note->set_actionsself::get_actions$spec ) );

        
$note->save();
    }

    
/**
     * Get the URL for an action.
     *
     * @param object $action The action.
     *
     * @return string The URL for the action.
     */
    
private static function get_url$action ) {
        if ( ! isset( 
$action->url ) ) {
            return 
'';
        }

        if ( isset( 
$action->url_is_admin_query ) && $action->url_is_admin_query ) {
            if ( 
strpos$action->url'&path' ) === ) {
                return 
wc_admin_url$action->url );
            }
            return 
admin_url$action->url );
        }

        return 
$action->url;
    }

    
/**
     * Get the locale for the WordPress locale, or fall back to the en_US
     * locale.
     *
     * @param Array $locales The locales to search through.
     *
     * @returns object The locale that was found, or null if no matching locale was found.
     */
    
public static function get_locale$locales ) {
        
$wp_locale           get_user_locale();
        
$matching_wp_locales array_values(
            
array_filter(
                
$locales,
                function ( 
$l ) use ( $wp_locale ) {
                    return 
$wp_locale === $l->locale;
                }
            )
        );

        if ( 
count$matching_wp_locales ) !== ) {
            return 
$matching_wp_locales[0];
        }

        
// Fall back to en_US locale.
        
$en_us_locales array_values(
            
array_filter(
                
$locales,
                function ( 
$l ) {
                    return 
$l->locale === 'en_US';
                }
            )
        );

        if ( 
count$en_us_locales ) !== ) {
            return 
$en_us_locales[0];
        }

        return 
null;
    }

    
/**
     * Get the action locale that matches the note locale, or fall back to the
     * en_US locale.
     *
     * @param Array $action_locales The locales from the spec's action.
     *
     * @return object The matching locale, or the en_US fallback locale, or null if neither was found.
     */
    
public static function get_action_locale$action_locales ) {
        
$wp_locale           get_user_locale();
        
$matching_wp_locales array_values(
            
array_filter(
                
$action_locales,
                function ( 
$l ) use ( $wp_locale ) {
                    return 
$wp_locale === $l->locale;
                }
            )
        );

        if ( 
count$matching_wp_locales ) !== ) {
            return 
$matching_wp_locales[0];
        }

        
// Fall back to en_US locale.
        
$en_us_locales array_values(
            
array_filter(
                
$action_locales,
                function ( 
$l ) {
                    return 
$l->locale === 'en_US';
                }
            )
        );

        if ( 
count$en_us_locales ) !== ) {
            return 
$en_us_locales[0];
        }

        return 
null;
    }

    
/**
     * Get the actions for a note.
     *
     * @param object $spec The spec.
     *
     * @return array The actions.
     */
    
public static function get_actions$spec ) {
        
$note    = new Note();
        
$actions = isset( $spec->actions ) ? $spec->actions : array();
        foreach ( 
$actions as $action ) {
            
$action_locale self::get_action_locale$action->locales );

            
$url self::get_url$action );

            
$note->add_action(
                
$action->name,
                ( 
$action_locale === null || ! isset( $action_locale->label ) )
                    ? 
''
                    
$action_locale->label,
                
$url,
                
$action->status
            
);
        }
        return 
$note->get_actions();
    }
}