RemoteInboxNotificationsDataSourcePoller.php
3 months ago
RemoteInboxNotificationsEngine.php
2 weeks ago
RuleProcessorInterface.php
1 year ago
SpecRunner.php
2 months ago
TransformerInterface.php
1 year ago
RemoteInboxNotificationsEngine.php
337 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handles running specs |
| 4 | */ |
| 5 | |
| 6 | namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications; |
| 7 | |
| 8 | defined( 'ABSPATH' ) || exit; |
| 9 | |
| 10 | use Automattic\WooCommerce\Admin\Notes\Notes; |
| 11 | use Automattic\WooCommerce\Admin\PluginsProvider\PluginsProvider; |
| 12 | use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile; |
| 13 | use Automattic\WooCommerce\Admin\Notes\Note; |
| 14 | use Automattic\WooCommerce\Admin\RemoteSpecs\RemoteSpecsEngine; |
| 15 | use Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\StoredStateSetupForProducts; |
| 16 | |
| 17 | /** |
| 18 | * Remote Inbox Notifications engine. |
| 19 | * This goes through the specs and runs (creates admin notes) for those |
| 20 | * specs that are able to be triggered. |
| 21 | */ |
| 22 | class RemoteInboxNotificationsEngine extends RemoteSpecsEngine { |
| 23 | const STORED_STATE_OPTION_NAME = 'wc_remote_inbox_notifications_stored_state'; |
| 24 | const WCA_UPDATED_OPTION_NAME = 'wc_remote_inbox_notifications_wca_updated'; |
| 25 | |
| 26 | /** |
| 27 | * Initialize the engine. |
| 28 | * phpcs:disable WooCommerce.Functions.InternalInjectionMethod.MissingFinal |
| 29 | * phpcs:disable WooCommerce.Functions.InternalInjectionMethod.MissingInternalTag |
| 30 | */ |
| 31 | public static function init() { |
| 32 | // Init things that need to happen before admin_init. |
| 33 | add_action( 'init', array( __CLASS__, 'on_init' ), 0, 0 ); |
| 34 | |
| 35 | // Continue init via admin_init. |
| 36 | add_action( 'admin_init', array( __CLASS__, 'on_admin_init' ) ); |
| 37 | |
| 38 | // Trigger when the profile data option is updated (during onboarding). |
| 39 | add_action( |
| 40 | 'update_option_' . OnboardingProfile::DATA_OPTION, |
| 41 | array( __CLASS__, 'update_profile_option' ), |
| 42 | 10, |
| 43 | 2 |
| 44 | ); |
| 45 | |
| 46 | // Hook into WCA updated. This is hooked up here rather than in |
| 47 | // on_admin_init because that runs too late to hook into the action. |
| 48 | add_action( |
| 49 | 'woocommerce_run_on_woocommerce_admin_updated', |
| 50 | array( __CLASS__, 'run_on_woocommerce_admin_updated' ) |
| 51 | ); |
| 52 | add_action( |
| 53 | 'woocommerce_updated', |
| 54 | function () { |
| 55 | $next_hook = WC()->queue()->get_next( |
| 56 | 'woocommerce_run_on_woocommerce_admin_updated', |
| 57 | array(), |
| 58 | 'woocommerce-remote-inbox-engine' |
| 59 | ); |
| 60 | if ( null === $next_hook ) { |
| 61 | WC()->queue()->schedule_single( |
| 62 | time(), |
| 63 | 'woocommerce_run_on_woocommerce_admin_updated', |
| 64 | array(), |
| 65 | 'woocommerce-remote-inbox-engine' |
| 66 | ); |
| 67 | } |
| 68 | } |
| 69 | ); |
| 70 | |
| 71 | add_filter( 'woocommerce_get_note_from_db', array( __CLASS__, 'get_note_from_db' ), 10, 1 ); |
| 72 | add_filter( 'woocommerce_debug_tools', array( __CLASS__, 'add_debug_tools' ) ); |
| 73 | add_action( |
| 74 | 'wp_ajax_woocommerce_json_inbox_notifications_search', |
| 75 | array( __CLASS__, 'ajax_action_inbox_notification_search' ) |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * This is triggered when the profile option is updated and if the |
| 81 | * profiler is being completed, triggers a run of the engine. |
| 82 | * |
| 83 | * @param mixed $old_value Old value. |
| 84 | * @param mixed $new_value New value. |
| 85 | */ |
| 86 | public static function update_profile_option( $old_value, $new_value ) { |
| 87 | // Return early if we're not completing the profiler. |
| 88 | if ( |
| 89 | ( isset( $old_value['completed'] ) && $old_value['completed'] ) || |
| 90 | ! isset( $new_value['completed'] ) || |
| 91 | ! $new_value['completed'] |
| 92 | ) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | self::run(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Init is continued via admin_init so that WC is loaded when the product |
| 101 | * query is used, otherwise the query generates a "0 = 1" in the WHERE |
| 102 | * condition and thus doesn't return any results. |
| 103 | */ |
| 104 | public static function on_admin_init() { |
| 105 | add_action( 'activated_plugin', array( __CLASS__, 'run' ) ); |
| 106 | add_action( 'deactivated_plugin', array( __CLASS__, 'run_on_deactivated_plugin' ), 10, 1 ); |
| 107 | StoredStateSetupForProducts::admin_init(); |
| 108 | |
| 109 | // Pre-fetch stored state so it has the correct initial values. |
| 110 | self::get_stored_state(); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * An init hook is used here so that StoredStateSetupForProducts can set |
| 115 | * up a hook that gets triggered by action-scheduler - this is needed |
| 116 | * because the admin_init hook doesn't get triggered by WP Cron. |
| 117 | */ |
| 118 | public static function on_init() { |
| 119 | StoredStateSetupForProducts::init(); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Go through the specs and run them. |
| 124 | */ |
| 125 | public static function run() { |
| 126 | $specs = RemoteInboxNotificationsDataSourcePoller::get_instance()->get_specs_from_data_sources(); |
| 127 | |
| 128 | if ( false === $specs || ! is_countable( $specs ) || count( $specs ) === 0 ) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | $stored_state = self::get_stored_state(); |
| 133 | $errors = array(); |
| 134 | |
| 135 | foreach ( $specs as $spec ) { |
| 136 | $error = SpecRunner::run_spec( $spec, $stored_state ); |
| 137 | if ( isset( $error ) ) { |
| 138 | $errors[] = $error; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | if ( count( $errors ) > 0 ) { |
| 143 | self::log_errors( $errors ); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Set an option indicating that WooCommerce Admin has just been updated, |
| 149 | * run the specs, then clear that option. This lets the |
| 150 | * WooCommerceAdminUpdatedRuleProcessor trigger on WCA update. |
| 151 | */ |
| 152 | public static function run_on_woocommerce_admin_updated() { |
| 153 | update_option( self::WCA_UPDATED_OPTION_NAME, true, false ); |
| 154 | |
| 155 | self::run(); |
| 156 | |
| 157 | update_option( self::WCA_UPDATED_OPTION_NAME, false, false ); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Gets the stored state option, and does the initial set up if it doesn't |
| 162 | * already exist. |
| 163 | * |
| 164 | * @return object The stored state option. |
| 165 | */ |
| 166 | public static function get_stored_state() { |
| 167 | $stored_state = get_option( self::STORED_STATE_OPTION_NAME ); |
| 168 | |
| 169 | if ( false === $stored_state || ! is_object( $stored_state ) ) { |
| 170 | $stored_state = new \stdClass(); |
| 171 | |
| 172 | $stored_state = StoredStateSetupForProducts::init_stored_state( |
| 173 | $stored_state |
| 174 | ); |
| 175 | |
| 176 | update_option( |
| 177 | self::STORED_STATE_OPTION_NAME, |
| 178 | $stored_state, |
| 179 | false |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | return $stored_state; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * The deactivated_plugin hook happens before the option is updated |
| 188 | * (https://github.com/WordPress/WordPress/blob/master/wp-admin/includes/plugin.php#L826) |
| 189 | * so this captures the deactivated plugin path and pushes it into the |
| 190 | * PluginsProvider. |
| 191 | * |
| 192 | * @param string $plugin Path to the plugin file relative to the plugins directory. |
| 193 | */ |
| 194 | public static function run_on_deactivated_plugin( $plugin ) { |
| 195 | PluginsProvider::set_deactivated_plugin( $plugin ); |
| 196 | self::run(); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Update the stored state option. |
| 201 | * |
| 202 | * @param object $stored_state The stored state. |
| 203 | */ |
| 204 | public static function update_stored_state( $stored_state ) { |
| 205 | update_option( self::STORED_STATE_OPTION_NAME, $stored_state, false ); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Get the note. This is used to display localized note. |
| 210 | * |
| 211 | * @param Note $note_from_db The note object created from db. |
| 212 | * |
| 213 | * @return Note The note. |
| 214 | */ |
| 215 | public static function get_note_from_db( $note_from_db ) { |
| 216 | if ( ! $note_from_db instanceof Note || get_user_locale() === $note_from_db->get_locale() ) { |
| 217 | return $note_from_db; |
| 218 | } |
| 219 | $specs = RemoteInboxNotificationsDataSourcePoller::get_instance()->get_specs_from_data_sources(); |
| 220 | foreach ( $specs as $spec ) { |
| 221 | if ( $spec->slug !== $note_from_db->get_name() ) { |
| 222 | continue; |
| 223 | } |
| 224 | $locale = SpecRunner::get_locale( $spec->locales, true ); |
| 225 | if ( null === $locale ) { |
| 226 | // No locale found, so don't update the note. |
| 227 | break; |
| 228 | } |
| 229 | |
| 230 | $localized_actions = SpecRunner::get_actions( $spec ); |
| 231 | |
| 232 | // Manually copy the action id from the db to the localized action, since they were not being provided. |
| 233 | foreach ( $localized_actions as $localized_action ) { |
| 234 | $action = $note_from_db->get_action( $localized_action->name ); |
| 235 | if ( $action ) { |
| 236 | $localized_action->id = $action->id; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | $note_from_db->set_title( $locale->title ); |
| 241 | $note_from_db->set_content( $locale->content ); |
| 242 | $note_from_db->set_actions( $localized_actions ); |
| 243 | } |
| 244 | |
| 245 | return $note_from_db; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Add the debug tools to the WooCommerce debug tools (WooCommerce > Status > Tools). |
| 250 | * |
| 251 | * @param array $tools a list of tools. |
| 252 | * |
| 253 | * @return mixed |
| 254 | * |
| 255 | * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed. |
| 256 | */ |
| 257 | public static function add_debug_tools( $tools ) { |
| 258 | // Check if the site has opted out of marketplace suggestions. |
| 259 | if ( get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) !== 'yes' ) { |
| 260 | return false; |
| 261 | } |
| 262 | |
| 263 | $tools['refresh_remote_inbox_notifications'] = array( |
| 264 | 'name' => __( 'Refresh Remote Inbox Notifications', 'woocommerce' ), |
| 265 | 'button' => __( 'Refresh', 'woocommerce' ), |
| 266 | 'desc' => __( 'This will refresh the remote inbox notifications', 'woocommerce' ), |
| 267 | 'callback' => function () { |
| 268 | RemoteInboxNotificationsDataSourcePoller::get_instance()->read_specs_from_data_sources(); |
| 269 | RemoteInboxNotificationsEngine::run(); |
| 270 | |
| 271 | return __( 'Remote inbox notifications have been refreshed', 'woocommerce' ); |
| 272 | }, |
| 273 | ); |
| 274 | |
| 275 | $tools['delete_inbox_notification'] = array( |
| 276 | 'name' => __( 'Delete an Inbox Notification', 'woocommerce' ), |
| 277 | 'button' => __( 'Delete', 'woocommerce' ), |
| 278 | 'desc' => __( 'This will delete an inbox notification by slug', 'woocommerce' ), |
| 279 | 'selector' => array( |
| 280 | 'description' => __( 'Select an inbox notification to delete:', 'woocommerce' ), |
| 281 | 'class' => 'wc-product-search', |
| 282 | 'search_action' => 'woocommerce_json_inbox_notifications_search', |
| 283 | 'name' => 'delete_inbox_notification_note_id', |
| 284 | 'placeholder' => esc_attr__( 'Search for an inbox notification…', 'woocommerce' ), |
| 285 | ), |
| 286 | 'callback' => function () { |
| 287 | check_ajax_referer( 'debug_action', '_wpnonce' ); |
| 288 | |
| 289 | if ( ! isset( $_GET['delete_inbox_notification_note_id'] ) ) { |
| 290 | return __( 'No inbox notification selected', 'woocommerce' ); |
| 291 | } |
| 292 | $note_id = wc_clean( sanitize_text_field( wp_unslash( $_GET['delete_inbox_notification_note_id'] ) ) ); |
| 293 | $note = Notes::get_note( $note_id ); |
| 294 | |
| 295 | if ( ! $note ) { |
| 296 | return __( 'Inbox notification not found', 'woocommerce' ); |
| 297 | } |
| 298 | |
| 299 | $note->delete( true ); |
| 300 | return __( 'Inbox notification has been deleted', 'woocommerce' ); |
| 301 | }, |
| 302 | ); |
| 303 | |
| 304 | return $tools; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Add ajax action for remote inbox notification search. |
| 309 | * |
| 310 | * @return void |
| 311 | * |
| 312 | * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed. |
| 313 | */ |
| 314 | public static function ajax_action_inbox_notification_search() { |
| 315 | global $wpdb; |
| 316 | |
| 317 | check_ajax_referer( 'search-products', 'security' ); |
| 318 | |
| 319 | if ( ! isset( $_GET['term'] ) ) { |
| 320 | wp_send_json( array() ); |
| 321 | } |
| 322 | |
| 323 | $search = wc_clean( sanitize_text_field( wp_unslash( $_GET['term'] ) ) ); |
| 324 | $results = $wpdb->get_results( |
| 325 | $wpdb->prepare( |
| 326 | "SELECT note_id, name FROM {$wpdb->prefix}wc_admin_notes WHERE name LIKE %s", |
| 327 | '%' . $wpdb->esc_like( $search ) . '%' |
| 328 | ) |
| 329 | ); |
| 330 | $rows = array(); |
| 331 | foreach ( $results as $result ) { |
| 332 | $rows[ $result->note_id ] = $result->name; |
| 333 | } |
| 334 | wp_send_json( $rows ); |
| 335 | } |
| 336 | } |
| 337 |