add file transport implementation #2
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
/**
|
||||
* Admin notice value object.
|
||||
*
|
||||
* @package WPContentSync
|
||||
*/
|
||||
|
||||
namespace WPContentSync\Admin;
|
||||
|
||||
final class AdminNotice {
|
||||
private const TYPES = array( 'success', 'warning', 'error', 'info' );
|
||||
|
||||
private string $type;
|
||||
private string $message;
|
||||
|
||||
public function __construct( string $type, string $message ) {
|
||||
$this->type = in_array( $type, self::TYPES, true ) ? $type : 'info';
|
||||
$this->message = sanitize_text_field( $message );
|
||||
}
|
||||
|
||||
public function type(): string {
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
public function message(): string {
|
||||
return $this->message;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
/**
|
||||
* Builds admin notices from redirect query args.
|
||||
*
|
||||
* @package WPContentSync
|
||||
*/
|
||||
|
||||
namespace WPContentSync\Admin;
|
||||
|
||||
final class AdminNoticeRepository {
|
||||
/**
|
||||
* @return array<int, AdminNotice>
|
||||
*/
|
||||
public function current(): array {
|
||||
$notices = array();
|
||||
|
||||
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reads redirect-only admin notice query args.
|
||||
if ( isset( $_GET['wpcs_imported'] ) ) {
|
||||
$notices[] = new AdminNotice( 'success', __( 'The package JSON file was imported successfully.', 'wp-content-sync' ) );
|
||||
}
|
||||
|
||||
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reads redirect-only admin notice query args.
|
||||
if ( isset( $_GET['wpcs_import_error'] ) ) {
|
||||
$notices[] = new AdminNotice( 'error', $this->queryValue( 'wpcs_import_error' ) );
|
||||
}
|
||||
|
||||
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reads redirect-only admin notice query args.
|
||||
if ( isset( $_GET['wpcs_settings_saved'] ) ) {
|
||||
$notices[] = new AdminNotice( 'success', __( 'Settings saved.', 'wp-content-sync' ) );
|
||||
}
|
||||
|
||||
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reads redirect-only admin notice query args.
|
||||
if ( isset( $_GET['wpcs_connection_ok'] ) ) {
|
||||
$notices[] = new AdminNotice( 'success', __( 'REST connection succeeded.', 'wp-content-sync' ) );
|
||||
}
|
||||
|
||||
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reads redirect-only admin notice query args.
|
||||
if ( isset( $_GET['wpcs_logs_cleared'] ) ) {
|
||||
$notices[] = new AdminNotice( 'success', __( 'Logs cleared.', 'wp-content-sync' ) );
|
||||
}
|
||||
|
||||
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reads redirect-only admin notice query args.
|
||||
if ( isset( $_GET['wpcs_connection_error'] ) ) {
|
||||
$notices[] = new AdminNotice( 'error', $this->queryValue( 'wpcs_connection_error' ) );
|
||||
}
|
||||
|
||||
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reads redirect-only admin notice query args.
|
||||
if ( isset( $_GET['wpcs_export_error'] ) ) {
|
||||
$notices[] = new AdminNotice( 'error', $this->queryValue( 'wpcs_export_error' ) );
|
||||
}
|
||||
|
||||
return $notices;
|
||||
}
|
||||
|
||||
private function queryValue( string $key ): string {
|
||||
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reads redirect-only admin notice query args.
|
||||
return sanitize_text_field( wp_unslash( $_GET[ $key ] ?? '' ) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for redirect-driven admin notices.
|
||||
*
|
||||
* @package WPContentSync
|
||||
*/
|
||||
|
||||
namespace WPContentSync\Tests\Unit\Admin;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use WPContentSync\Admin\AdminNoticeRepository;
|
||||
|
||||
class AdminNoticeRepositoryTest extends TestCase {
|
||||
protected function tearDown(): void {
|
||||
$_GET = array();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_it_builds_import_success_notices(): void {
|
||||
$_GET['wpcs_imported'] = '1';
|
||||
$notices = ( new AdminNoticeRepository() )->current();
|
||||
|
||||
self::assertSame( 'success', $notices[0]->type() );
|
||||
self::assertSame( 'The package JSON file was imported successfully.', $notices[0]->message() );
|
||||
}
|
||||
|
||||
public function test_it_sanitizes_error_notices(): void {
|
||||
$_GET['wpcs_import_error'] = '<script>Bad package</script>';
|
||||
$notices = ( new AdminNoticeRepository() )->current();
|
||||
|
||||
self::assertSame( 'error', $notices[0]->type() );
|
||||
self::assertSame( 'Bad package', $notices[0]->message() );
|
||||
}
|
||||
|
||||
public function test_it_builds_settings_connection_logs_and_export_notices(): void {
|
||||
$_GET['wpcs_settings_saved'] = '1';
|
||||
$_GET['wpcs_connection_ok'] = '1';
|
||||
$_GET['wpcs_logs_cleared'] = '1';
|
||||
$_GET['wpcs_connection_error'] = 'REST authentication failed.';
|
||||
$_GET['wpcs_export_error'] = 'Export failed.';
|
||||
|
||||
$notices = ( new AdminNoticeRepository() )->current();
|
||||
|
||||
self::assertSame( 'Settings saved.', $notices[0]->message() );
|
||||
self::assertSame( 'REST connection succeeded.', $notices[1]->message() );
|
||||
self::assertSame( 'Logs cleared.', $notices[2]->message() );
|
||||
self::assertSame( 'REST authentication failed.', $notices[3]->message() );
|
||||
self::assertSame( 'Export failed.', $notices[4]->message() );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user