Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/DataView/DataViewConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ class DataViewConfig {
public readonly string $capability;
public readonly array $storage_options;

/**
* Whether DataView renders its own success/error notices.
*
* Set to false when the host environment already renders save notices for
* this page — e.g. a settings page under the WordPress "Settings" menu,
* where core's options-head.php shows a native "Settings saved." notice for
* the `updated=1` redirect. Leaving this true there would double the notice.
*
* @var bool
*/
public readonly bool $notices;

/**
* Full field configurations including repeater sub-fields.
*
Expand Down Expand Up @@ -70,6 +82,7 @@ public function __construct( array $config ) {
$this->mode = $config['mode'] ?? 'plural';
$this->capability = $config['capability'] ?? 'manage_options';
$this->storage_options = $config['storage_options'] ?? [];
$this->notices = $config['notices'] ?? true;

// Parse field configurations (supports both simple and complex definitions).
$this->field_configs = $this->parse_field_configs( $config['fields'] );
Expand Down
4 changes: 4 additions & 0 deletions src/DataView/RequestRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,10 @@ protected function render_back_link(): void {
* Render success/error notices from query params.
*/
protected function render_notices(): void {
if ( ! $this->config->notices ) {
return;
}

// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['created'] ) ) {
echo '<div class="notice notice-success is-dismissible"><p>' . esc_html( $this->get_label( 'item_created' ) ) . '</p></div>';
Expand Down
69 changes: 69 additions & 0 deletions tests/phpunit/data-view.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,75 @@ private function get_router( DataView $view ): \Tangible\DataView\RequestRouter
return $property->getValue( $view );
}

/**
* Render the protected notices output for a DataView's router.
*/
private function render_notices_output( DataView $view ): string {
$router = $this->get_router( $view );
$method = new \ReflectionMethod( \Tangible\DataView\RequestRouter::class, 'render_notices' );
$method->setAccessible( true );

ob_start();
$method->invoke( $router );
return ob_get_clean();
}

public function test_notices_config_defaults_to_true(): void {
$config = new DataViewConfig( [
'slug' => 'dv_test_notices_default',
'label' => 'Settings',
'fields' => [ 'enabled' => 'boolean' ],
] );

$this->assertTrue( $config->notices );
}

public function test_notices_config_can_be_disabled(): void {
$config = new DataViewConfig( [
'slug' => 'dv_test_notices_off',
'label' => 'Settings',
'fields' => [ 'enabled' => 'boolean' ],
'notices' => false,
] );

$this->assertFalse( $config->notices );
}

public function test_render_notices_outputs_when_enabled(): void {
$_GET['updated'] = '1';

$view = new DataView( [
'slug' => 'dv_test_notices_on_render',
'label' => 'Settings',
'mode' => 'singular',
'storage' => 'option',
'fields' => [ 'enabled' => 'boolean' ],
] );

$output = $this->render_notices_output( $view );
unset( $_GET['updated'] );

$this->assertStringContainsString( 'notice-success', $output );
}

public function test_render_notices_suppressed_when_disabled(): void {
$_GET['updated'] = '1';

$view = new DataView( [
'slug' => 'dv_test_notices_off_render',
'label' => 'Settings',
'mode' => 'singular',
'storage' => 'option',
'fields' => [ 'enabled' => 'boolean' ],
'notices' => false,
] );

$output = $this->render_notices_output( $view );
unset( $_GET['updated'] );

$this->assertSame( '', $output, 'render_notices() must output nothing when notices are disabled' );
}

public function test_field_type_registry_has_repeater_type(): void {
$registry = new FieldTypeRegistry();

Expand Down