feat: save admin sync settings
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* Handles admin settings saves.
|
||||
*
|
||||
* @package WPContentSync
|
||||
*/
|
||||
|
||||
namespace WPContentSync\Admin;
|
||||
|
||||
use WPContentSync\Settings\Settings;
|
||||
use WPContentSync\Settings\SettingsRepository;
|
||||
|
||||
final class SettingsController {
|
||||
private SettingsRepository $settings_repository;
|
||||
|
||||
public function __construct( SettingsRepository $settings_repository ) {
|
||||
$this->settings_repository = $settings_repository;
|
||||
}
|
||||
|
||||
public function register(): void {
|
||||
add_action( 'admin_post_wpcs_save_settings', array( $this, 'handleSave' ) );
|
||||
}
|
||||
|
||||
public function handleSave(): void {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
throw new \RuntimeException( 'You do not have permission to save WP Content Sync settings.' );
|
||||
}
|
||||
|
||||
if ( ! check_admin_referer( 'wpcs_save_settings', 'wpcs_settings_nonce' ) ) {
|
||||
throw new \RuntimeException( 'The settings save request could not be verified.' );
|
||||
}
|
||||
|
||||
// phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Nonce verified above; Settings::fromArray sanitizes the full option payload.
|
||||
$data = isset( $_POST['wpcs_settings'] ) ? wp_unslash( $_POST['wpcs_settings'] ) : array();
|
||||
$data = is_array( $data ) ? $data : array();
|
||||
|
||||
$this->settings_repository->save( Settings::fromArray( $data ) );
|
||||
|
||||
wp_safe_redirect(
|
||||
add_query_arg(
|
||||
array( 'wpcs_settings_saved' => '1' ),
|
||||
admin_url( 'admin.php?page=wp-content-sync' )
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ namespace WPContentSync;
|
||||
|
||||
use WPContentSync\Admin\AdminPage;
|
||||
use WPContentSync\Admin\FileImportController;
|
||||
use WPContentSync\Admin\SettingsController;
|
||||
use WPContentSync\Content\ContentHandlerRegistry;
|
||||
use WPContentSync\Content\ContentRecordNormalizer;
|
||||
use WPContentSync\Content\MediaContentHandler;
|
||||
@@ -169,6 +170,15 @@ final class Plugin {
|
||||
}
|
||||
);
|
||||
|
||||
$container->factory(
|
||||
SettingsController::class,
|
||||
static function () use ( $container ): SettingsController {
|
||||
return new SettingsController(
|
||||
$container->get( SettingsRepository::class )
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
$container->factory(
|
||||
RestTransportClient::class,
|
||||
static function (): RestTransportClient {
|
||||
@@ -209,8 +219,12 @@ final class Plugin {
|
||||
/** @var RestPackageController $rest_package_controller */
|
||||
$rest_package_controller = $this->container->get( RestPackageController::class );
|
||||
|
||||
/** @var SettingsController $settings_controller */
|
||||
$settings_controller = $this->container->get( SettingsController::class );
|
||||
|
||||
$admin_page->register();
|
||||
$file_import_controller->register();
|
||||
$rest_package_controller->register();
|
||||
$settings_controller->register();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user