Add refresh & delete notification tools (#48961)

* Add Remote inbox notifications tools

* Add sanitize_text_field

* Add changefile(s) from automation for the following project(s): woocommerce

* Update plugins/woocommerce/src/Admin/RemoteInboxNotifications/RemoteInboxNotificationsEngine.php

Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>
This commit is contained in:
Moon 2024-07-18 02:01:21 -07:00 committed by GitHub
parent 824bf8e218
commit 5736b7a056
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 114 additions and 4 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Add Refresh Remote Inbox Notifications and Delete Remote Notification Tools

View File

@ -7,11 +7,14 @@ namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\Notes\Notes;
use Automattic\WooCommerce\Admin\PluginsProvider\PluginsProvider;
use Automattic\WooCommerce\Internal\Admin\Onboarding\OnboardingProfile;
use Automattic\WooCommerce\Admin\Notes\Note;
use Automattic\WooCommerce\Admin\RemoteSpecs\RemoteSpecsEngine;
use Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\StoredStateSetupForProducts;
use Automattic\WooCommerce\Internal\Traits\AccessiblePrivateMethods;
/**
* Remote Inbox Notifications engine.
@ -19,11 +22,15 @@ use Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\StoredStateSetupForP
* specs that are able to be triggered.
*/
class RemoteInboxNotificationsEngine extends RemoteSpecsEngine {
use AccessiblePrivateMethods;
const STORED_STATE_OPTION_NAME = 'wc_remote_inbox_notifications_stored_state';
const WCA_UPDATED_OPTION_NAME = 'wc_remote_inbox_notifications_wca_updated';
/**
* Initialize the engine.
* phpcs:disable WooCommerce.Functions.InternalInjectionMethod.MissingFinal
* phpcs:disable WooCommerce.Functions.InternalInjectionMethod.MissingInternalTag
*/
public static function init() {
// Init things that need to happen before admin_init.
@ -42,10 +49,13 @@ class RemoteInboxNotificationsEngine extends RemoteSpecsEngine {
// Hook into WCA updated. This is hooked up here rather than in
// on_admin_init because that runs too late to hook into the action.
add_action( 'woocommerce_run_on_woocommerce_admin_updated', array( __CLASS__, 'run_on_woocommerce_admin_updated' ) );
add_action(
'woocommerce_run_on_woocommerce_admin_updated',
array( __CLASS__, 'run_on_woocommerce_admin_updated' )
);
add_action(
'woocommerce_updated',
function() {
function () {
$next_hook = WC()->queue()->get_next(
'woocommerce_run_on_woocommerce_admin_updated',
array(),
@ -63,6 +73,11 @@ class RemoteInboxNotificationsEngine extends RemoteSpecsEngine {
);
add_filter( 'woocommerce_get_note_from_db', array( __CLASS__, 'get_note_from_db' ), 10, 1 );
self::add_filter( 'woocommerce_debug_tools', array( __CLASS__, 'add_debug_tools' ) );
self::add_action(
'wp_ajax_woocommerce_json_inbox_notifications_search',
array( __CLASS__, 'ajax_action_inbox_notification_search' )
);
}
/**
@ -155,7 +170,7 @@ class RemoteInboxNotificationsEngine extends RemoteSpecsEngine {
public static function get_stored_state() {
$stored_state = get_option( self::STORED_STATE_OPTION_NAME );
if ( $stored_state === false ) {
if ( false === $stored_state ) {
$stored_state = new \stdClass();
$stored_state = StoredStateSetupForProducts::init_stored_state(
@ -199,6 +214,7 @@ class RemoteInboxNotificationsEngine extends RemoteSpecsEngine {
* Get the note. This is used to display localized note.
*
* @param Note $note_from_db The note object created from db.
*
* @return Note The note.
*/
public static function get_note_from_db( $note_from_db ) {
@ -211,7 +227,7 @@ class RemoteInboxNotificationsEngine extends RemoteSpecsEngine {
continue;
}
$locale = SpecRunner::get_locale( $spec->locales, true );
if ( $locale === null ) {
if ( null === $locale ) {
// No locale found, so don't update the note.
break;
}
@ -233,4 +249,94 @@ class RemoteInboxNotificationsEngine extends RemoteSpecsEngine {
return $note_from_db;
}
/**
* Add the debug tools to the WooCommerce debug tools (WooCommerce > Status > Tools).
*
* @param array $tools a list of tools.
*
* @return mixed
*/
private static function add_debug_tools( $tools ) {
// Check if the feature flag is disabled.
if ( ! Features::is_enabled( 'remote-inbox-notifications' ) ) {
return false;
}
// Check if the site has opted out of marketplace suggestions.
if ( get_option( 'woocommerce_show_marketplace_suggestions', 'yes' ) !== 'yes' ) {
return false;
}
$tools['refresh_remote_inbox_notifications'] = array(
'name' => __( 'Refresh Remote Inbox Notifications', 'woocommerce' ),
'button' => __( 'Refresh', 'woocommerce' ),
'desc' => __( 'This will refresh the remote inbox notifications', 'woocommerce' ),
'callback' => function () {
RemoteInboxNotificationsDataSourcePoller::get_instance()->read_specs_from_data_sources();
RemoteInboxNotificationsEngine::run();
return __( 'Remote inbox notifications have been refreshed', 'woocommerce' );
},
);
$tools['delete_inbox_notification'] = array(
'name' => __( 'Delete an Inbox Notification', 'woocommerce' ),
'button' => __( 'Delete', 'woocommerce' ),
'desc' => __( 'This will delete an inbox notification by slug', 'woocommerce' ),
'selector' => array(
'description' => __( 'Select an inbox notification to delete:', 'woocommerce' ),
'class' => 'wc-product-search',
'search_action' => 'woocommerce_json_inbox_notifications_search',
'name' => 'delete_inbox_notification_note_id',
'placeholder' => esc_attr__( 'Search for an inbox notification&hellip;', 'woocommerce' ),
),
'callback' => function () {
check_ajax_referer( 'debug_action', '_wpnonce' );
if ( ! isset( $_GET['delete_inbox_notification_note_id'] ) ) {
return __( 'No inbox notification selected', 'woocommerce' );
}
$note_id = wc_clean( sanitize_text_field( wp_unslash( $_GET['delete_inbox_notification_note_id'] ) ) );
$note = Notes::get_note( $note_id );
if ( ! $note ) {
return __( 'Inbox notification not found', 'woocommerce' );
}
$note->delete( true );
return __( 'Inbox notification has been deleted', 'woocommerce' );
},
);
return $tools;
}
/**
* Add ajax action for remote inbox notification search.
*
* @return void
*/
private static function ajax_action_inbox_notification_search() {
global $wpdb;
check_ajax_referer( 'search-products', 'security' );
if ( ! isset( $_GET['term'] ) ) {
wp_send_json( array() );
}
$search = wc_clean( sanitize_text_field( wp_unslash( $_GET['term'] ) ) );
$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT note_id, name FROM {$wpdb->prefix}wc_admin_notes WHERE name LIKE %s",
'%' . $wpdb->esc_like( $search ) . '%'
)
);
$rows = array();
foreach ( $results as $result ) {
$rows[ $result->note_id ] = $result->name;
}
wp_send_json( $rows );
}
}