woocommerce/plugins/woocommerce-beta-tester/api/remote-inbox-notifications/class-wca-test-helper-remot...

196 lines
5.0 KiB
PHP
Raw Normal View History

WCA Test helper - add remote inbox notification staging importer (#48735) * Add Remote inbox notifications management * Remove admin notes section -- replaced by remote inbox notifications * Add import from staging and production * Add changefile(s) from automation for the following project(s): woocommerce-beta-tester * WIP - change test to run * Display failed rules on error * Change run to test * Add changefile(s) from automation for the following project(s): woocommerce-beta-tester, woocommerce * Retire Remote Spec Ruel Validation -- use test action from Remote Inbox Notifications * Run spec when all rules have passed * Fix typo * Change btn text to Run * Update copy text * Place delete all button on the left side * Update plugins/woocommerce-beta-tester/api/remote-inbox-notifications/class-wca-test-helper-remote-inbox-notifications.php Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com> * Update plugins/woocommerce/src/Admin/RemoteSpecs/RuleProcessors/RuleEvaluator.php Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com> * Update plugins/woocommerce-beta-tester/api/remote-inbox-notifications/class-wca-test-helper-remote-inbox-notifications.php Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com> * Add changefile(s) from automation for the following project(s): woocommerce-beta-tester * Separate remote inbox notification heler functions into a class and fix DELETE method errors * Fix typo * Display all errors * Fix errors * Fix error with test * Add import from URL * Change func name to importFromUrl * Rename 48735-feature-remote-notiifcation-importer to 48735-feature-remote-notification-importer Fix filename typo * Add changefile(s) from automation for the following project(s): woocommerce-beta-tester * Fix changelog filename typo Fix changelog filename typo * Add changefile(s) from automation for the following project(s): woocommerce-beta-tester * Revert adding new changelog It seems the new CI is automatically creating a changelog based on branch name https://github.com/woocommerce/woocommerce/pull/48735/commits/43d6abe3e70e50af626b24436b02af414279c1aa --------- Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: Ilyas Foo <foo.ilyas@gmail.com>
2024-06-25 06:32:36 +00:00
<?php
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\SpecRunner;
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\RemoteInboxNotificationsEngine;
use Automattic\WooCommerce\Admin\RemoteInboxNotifications\RemoteInboxNotificationsDataSourcePoller;
use Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\GetRuleProcessor;
use Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\RuleEvaluator;
require_once dirname( __FILE__ ) . '/class-wc-beta-tester-remote-inbox-notifications-helper.php';
register_woocommerce_admin_test_helper_rest_route(
'/remote-inbox-notifications',
array( WCA_Test_Helper_Remote_Inbox_Notifications::class, 'get_items' ),
array(
'methods' => 'GET',
)
);
register_woocommerce_admin_test_helper_rest_route(
'/remote-inbox-notifications/(?P<id>\d+)/delete',
array( WCA_Test_Helper_Remote_Inbox_Notifications::class, 'delete' ),
array(
'methods' => 'POST',
'args' => array(
'id' => array(
'description' => 'Rest API endpoint.',
'type' => 'integer',
'required' => true,
),
),
)
);
register_woocommerce_admin_test_helper_rest_route(
'/remote-inbox-notifications/(?P<name>(.*)+)/test',
array( WCA_Test_Helper_Remote_Inbox_Notifications::class, 'test' ),
array(
'methods' => 'GET',
'args' => array(
'name' => array(
'description' => 'Note name.',
'type' => 'string',
'required' => true,
),
),
)
);
register_woocommerce_admin_test_helper_rest_route(
'/remote-inbox-notifications/delete-all',
array( WCA_Test_Helper_Remote_Inbox_Notifications::class, 'delete_all_items' ),
array(
'methods' => 'POST',
)
);
register_woocommerce_admin_test_helper_rest_route(
'/remote-inbox-notifications/import',
array( WCA_Test_Helper_Remote_Inbox_Notifications::class, 'import' ),
array(
'methods' => 'POST',
)
);
/**
* Class WCA_Test_Helper_Remote_Inbox_Notifications.
*/
class WCA_Test_Helper_Remote_Inbox_Notifications {
/**
* Delete a notification.
*
* @param WP_REST_Request $request Request object.
* @return WP_REST_Response
*/
public static function delete( $request ) {
WC_Beta_Tester_Remote_Inbox_Notifications_Helper::delete_by_id( $request->get_param( 'id' ) );
return new WP_REST_Response(
array(
'success' => true,
'message' => 'Remote inbox notification deleted.',
),
200
);
}
/**
* Delete all notifications.
*
* @return WP_REST_Response
*/
public static function delete_all_items() {
$deleted = WC_Beta_Tester_Remote_Inbox_Notifications_Helper::delete_all();
return new WP_REST_Response( $deleted, 200 );
}
/**
* Return a list of class-based notes.
* These should be excluded from the list of notes to be displayed in the inbox as we don't have control over them.
*
* @return array
*/
private static function get_notes_to_exclude() {
$vars = array( 'other_note_classes', 'note_classes_to_added_or_updated' );
$note_names = array();
$reflection = new ReflectionClass( '\Automattic\WooCommerce\Internal\Admin\Events' );
foreach ( $vars as $var ) {
$property = $reflection->getProperty( $var );
$property->setAccessible( true );
$notes = $property->getValue();
$note_names = array_merge(
$note_names,
array_map(
function ( $note ) {
return $note::NOTE_NAME;
},
$notes
)
);
}
return $note_names;
}
/**
* Return all notifications.
*
* @return WP_REST_Response
*/
public static function get_items() {
global $wpdb;
$items = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}wc_admin_notes ORDER BY note_id desc", ARRAY_A );
$notes_added_via_classes = self::get_notes_to_exclude();
$items = array_filter(
$items,
function ( $item ) use ( $notes_added_via_classes ) {
return ! in_array( $item['name'], $notes_added_via_classes, true );
}
);
return new WP_REST_Response( array_values( $items ), 200 );
}
/**
* Test and run a remote inbox notification.
*
* @param WP_REST_Request $request The full request data.
*
* @return WP_REST_Response
*/
public static function test( $request ) {
$name = $request->get_param( 'name' );
$result = WC_Beta_Tester_Remote_Inbox_Notifications_Helper::test( $name, null, true );
if ( $result instanceof WP_Error ) {
$message = $result->get_error_data();
} else {
$message = $name . ': All rules passed successfully';
}
return new WP_REST_Response(
array(
'success' => true === $result,
'message' => $message,
),
200
);
}
/**
* Import remote inbox notifications.
*
* @param WP_REST_Request $request The full request data.
*
* @return WP_REST_Response
*/
public static function import( $request ) {
// Get the JSON data from the request body.
$specs = json_decode( wp_json_encode( $request->get_json_params() ) );
WC_Beta_Tester_Remote_Inbox_Notifications_Helper::import( $specs );
return new WP_REST_Response(
array(
'success' => true,
'message' => 'Remote inbox notifications imported.',
),
200
);
}
}