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.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Admin / RemoteInboxNotifications / SpecRunner.php
woocommerce / src / Admin / RemoteInboxNotifications Last commit date
RemoteInboxNotificationsDataSourcePoller.php 2 months ago RemoteInboxNotificationsEngine.php 1 year ago RuleProcessorInterface.php 1 year ago SpecRunner.php 4 weeks ago TransformerInterface.php 1 year ago
SpecRunner.php
212 lines
1 <?php
2 /**
3 * Runs a single spec.
4 */
5
6 namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
7
8 defined( 'ABSPATH' ) || exit;
9
10 use Automattic\WooCommerce\Admin\Notes\Note;
11 use Automattic\WooCommerce\Admin\Notes\Notes;
12 use Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\EvaluateAndGetStatus;
13 use Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\RuleEvaluator;
14
15 /**
16 * Runs a single spec.
17 */
18 class SpecRunner {
19 /**
20 * Run the spec.
21 *
22 * @param object $spec The spec to run.
23 * @param object $stored_state Stored state.
24 */
25 public static function run_spec( $spec, $stored_state ) {
26 $data_store = Notes::load_data_store();
27
28 // Create or update the note.
29 $existing_note_ids = $data_store->get_notes_with_name( $spec->slug );
30 if ( ! is_countable( $existing_note_ids ) || count( $existing_note_ids ) === 0 ) {
31 $note = new Note();
32 $note->set_status( Note::E_WC_ADMIN_NOTE_PENDING );
33 } else {
34 $note = Notes::get_note( $existing_note_ids[0] );
35 if ( $note === false ) {
36 return;
37 }
38 }
39
40 // Evaluate the spec and get the new note status.
41 $previous_status = $note->get_status();
42 try {
43 $status = EvaluateAndGetStatus::evaluate(
44 $spec,
45 $previous_status,
46 $stored_state,
47 new RuleEvaluator()
48 );
49 } catch ( \Throwable $e ) {
50 return $e;
51 }
52
53 // If the status is changing, update the created date to now.
54 if ( $previous_status !== $status ) {
55 $note->set_date_created( time() );
56 }
57
58 // Get the matching locale or fall back to en-US.
59 $locale = self::get_locale( $spec->locales );
60
61 if ( $locale === null ) {
62 return;
63 }
64
65 // Set up the note.
66 $note->set_title( $locale->title );
67 $note->set_content( $locale->content );
68 $note->set_content_data( isset( $spec->content_data ) ? $spec->content_data : (object) array() );
69 $note->set_status( $status );
70 $note->set_type( $spec->type );
71 $note->set_name( $spec->slug );
72 if ( isset( $spec->source ) ) {
73 $note->set_source( $spec->source );
74 }
75 // `$spec->layout` is intentionally ignored: inbox notes no longer support layout variants and the property is deprecated.
76
77 // Recreate actions.
78 $note->set_actions( self::get_actions( $spec ) );
79
80 $note->save();
81 }
82
83 /**
84 * Get the URL for an action.
85 *
86 * @param object $action The action.
87 *
88 * @return string The URL for the action.
89 */
90 private static function get_url( $action ) {
91 if ( ! isset( $action->url ) ) {
92 return '';
93 }
94
95 if ( isset( $action->url_is_admin_query ) && $action->url_is_admin_query ) {
96 if ( strpos( $action->url, '&path' ) === 0 ) {
97 return wc_admin_url( $action->url );
98 }
99 return admin_url( $action->url );
100 }
101
102 return $action->url;
103 }
104
105 /**
106 * Get the locale for the WordPress locale, or fall back to the en_US
107 * locale.
108 *
109 * @param Array $locales The locales to search through.
110 *
111 * @returns object The locale that was found, or null if no matching locale was found.
112 */
113 public static function get_locale( $locales ) {
114 $wp_locale = get_user_locale();
115 $matching_wp_locales = array_values(
116 array_filter(
117 $locales,
118 function ( $l ) use ( $wp_locale ) {
119 return $wp_locale === $l->locale;
120 }
121 )
122 );
123
124 if ( count( $matching_wp_locales ) !== 0 ) {
125 return $matching_wp_locales[0];
126 }
127
128 // Fall back to en_US locale.
129 $en_us_locales = array_values(
130 array_filter(
131 $locales,
132 function ( $l ) {
133 return $l->locale === 'en_US';
134 }
135 )
136 );
137
138 if ( count( $en_us_locales ) !== 0 ) {
139 return $en_us_locales[0];
140 }
141
142 return null;
143 }
144
145 /**
146 * Get the action locale that matches the note locale, or fall back to the
147 * en_US locale.
148 *
149 * @param Array $action_locales The locales from the spec's action.
150 *
151 * @return object The matching locale, or the en_US fallback locale, or null if neither was found.
152 */
153 public static function get_action_locale( $action_locales ) {
154 $wp_locale = get_user_locale();
155 $matching_wp_locales = array_values(
156 array_filter(
157 $action_locales,
158 function ( $l ) use ( $wp_locale ) {
159 return $wp_locale === $l->locale;
160 }
161 )
162 );
163
164 if ( count( $matching_wp_locales ) !== 0 ) {
165 return $matching_wp_locales[0];
166 }
167
168 // Fall back to en_US locale.
169 $en_us_locales = array_values(
170 array_filter(
171 $action_locales,
172 function ( $l ) {
173 return $l->locale === 'en_US';
174 }
175 )
176 );
177
178 if ( count( $en_us_locales ) !== 0 ) {
179 return $en_us_locales[0];
180 }
181
182 return null;
183 }
184
185 /**
186 * Get the actions for a note.
187 *
188 * @param object $spec The spec.
189 *
190 * @return array The actions.
191 */
192 public static function get_actions( $spec ) {
193 $note = new Note();
194 $actions = isset( $spec->actions ) ? $spec->actions : array();
195 foreach ( $actions as $action ) {
196 $action_locale = self::get_action_locale( $action->locales );
197
198 $url = self::get_url( $action );
199
200 $note->add_action(
201 $action->name,
202 ( $action_locale === null || ! isset( $action_locale->label ) )
203 ? ''
204 : $action_locale->label,
205 $url,
206 $action->status
207 );
208 }
209 return $note->get_actions();
210 }
211 }
212