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 |