diff --git a/src/Admin/AdminNotice.php b/src/Admin/AdminNotice.php new file mode 100644 index 0000000..b6cd502 --- /dev/null +++ b/src/Admin/AdminNotice.php @@ -0,0 +1,28 @@ +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; + } +} diff --git a/src/Admin/AdminNoticeRepository.php b/src/Admin/AdminNoticeRepository.php new file mode 100644 index 0000000..8e38708 --- /dev/null +++ b/src/Admin/AdminNoticeRepository.php @@ -0,0 +1,59 @@ + + */ + 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 ] ?? '' ) ); + } +} diff --git a/tests/Unit/Admin/AdminNoticeRepositoryTest.php b/tests/Unit/Admin/AdminNoticeRepositoryTest.php new file mode 100644 index 0000000..1b428b8 --- /dev/null +++ b/tests/Unit/Admin/AdminNoticeRepositoryTest.php @@ -0,0 +1,51 @@ +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'] = ''; + $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() ); + } +}