Skip to content

Commit

Permalink
Use partial template handler
Browse files Browse the repository at this point in the history
  • Loading branch information
mundschenk-at committed Jul 10, 2023
1 parent fddff8e commit 9b547d2
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 53 deletions.
35 changes: 22 additions & 13 deletions admin/partials/settings/settings-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* This file is part of wp-Typography.
*
* Copyright 2014-2018 Peter Putzer.
* Copyright 2014-2023 Peter Putzer.
* Copyright 2009-2011 KINGdesk, LLC.
*
* This program is free software; you can redistribute it and/or
Expand All @@ -25,34 +25,43 @@
* @license http://www.gnu.org/licenses/gpl-2.0.html
*/

use WP_Typography\Data_Storage\Options;
namespace WP_Typography\Partials\Admin;

/**
* Required template variables:
*
* @var string $plugin_name The plugin name.
* @var string $active_tab The active tab.
* @var string $option_group The active option group name.
* @var array $admin_form_tabs The admin form tabs.
* @var string $restore_defaults_button The `name` attribute of the Restore Defaults submit button.
* @var string $clear_cache_button The `name` attribute of the Clear Cache submit button.
*
* @phpstan-var array<string, array{heading:string, description:string}> $admin_form_tabs
*/

?><div class='wrap'>
<h1><?php echo \esc_html( \sprintf( /* translators: settings page headline, %s is the plugin name */ \__( '%s Settings', 'wp-typography' ), $this->plugin_name ) ); ?></h1>
<?php
// Check active tab.
$active_tab = $this->get_active_settings_tab();
?>
<h1><?php echo \esc_html( \sprintf( /* translators: settings page headline, %s is the plugin name */ \__( '%s Settings', 'wp-typography' ), $plugin_name ) ); ?></h1>
<h2 class="nav-tab-wrapper">
<?php foreach ( $this->admin_form_tabs as $tab_id => $settings_tab ) : ?>
<?php foreach ( $admin_form_tabs as $tab_id => $settings_tab ) : ?>
<?php
$query_string = '?page=' . \strtolower( $this->plugin_name ) . '&tab=' . $tab_id;
$query_string = '?page=' . \strtolower( $plugin_name ) . '&tab=' . $tab_id;
$classes = 'nav-tab' . ( $tab_id === $active_tab ? ' nav-tab-active' : '' );
?>
<a href="<?php echo \esc_url( $query_string ); ?>" class="<?php echo \esc_attr( $classes ); ?>"><?php echo \esc_html( $settings_tab['heading'] ); ?></a>
<?php endforeach; ?>
</h2>

<form method="post" action="options.php">
<?php \settings_fields( self::OPTION_GROUP . $active_tab ); ?>
<?php \do_settings_sections( self::OPTION_GROUP . $active_tab ); ?>
<?php \settings_fields( $option_group ); ?>
<?php \do_settings_sections( $option_group ); ?>

<p class="submit">
<?php \submit_button( \__( 'Save Changes', 'wp-typography' ), 'primary', 'save_changes', false, [ 'tabindex' => 1 ] ); ?>
<span class="aux-buttons">
<?php \submit_button( \__( 'Restore Defaults', 'wp-typography' ), 'delete', $this->options->get_name( Options::RESTORE_DEFAULTS ), false, [ 'tabindex' => 2 ] ); ?>
<?php \submit_button( \__( 'Restore Defaults', 'wp-typography' ), 'delete', $restore_defaults_button, false, [ 'tabindex' => 2 ] ); ?>
<?php // The whitespace is necessary. ?>
<?php \submit_button( \__( 'Clear Cache', 'wp-typography' ), 'secondary', $this->options->get_name( Options::CLEAR_CACHE ), false, [ 'tabindex' => 3 ] ); ?>
<?php \submit_button( \__( 'Clear Cache', 'wp-typography' ), 'secondary', $clear_cache_button, false, [ 'tabindex' => 3 ] ); ?>
</span>
</p><!-- .submit -->
</form>
Expand Down
6 changes: 6 additions & 0 deletions includes/wp-typography/class-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
use WP_Typography\Data_Storage\Options;
use WP_Typography\Data_Storage\Transients;

use WP_Typography\UI\Template;

use WP_Typography\Integration\Container as Integrations;
use WP_Typography\Settings\Basic_Locale_Settings;
use WP_Typography\Settings\Locale_Settings;
Expand Down Expand Up @@ -113,6 +115,10 @@ protected function get_rules(): array {
Cache::class => self::SHARED,
Transients::class => self::SHARED,
Options::class => self::SHARED,
Template::class => [
'shared' => true,
'constructParams' => [ \WP_TYPOGRAPHY_PLUGIN_PATH ],
],

// API implementation.
'substitutions' => [
Expand Down
42 changes: 32 additions & 10 deletions includes/wp-typography/components/class-admin-interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ class Admin_Interface implements Plugin_Component {
*/
private $options;

/**
* The template helper.
*
* @var UI\Template
*/
private UI\Template $template;

/**
* The user-visible name of the plugin.
*
Expand Down Expand Up @@ -161,13 +168,16 @@ class Admin_Interface implements Plugin_Component {
*
* @since 5.6.0 Parameters $basename and $plugin_path removed.
* @since 5.7.0 Parameter $api added.
* @since 5.9.2 Parameter $template added.
*
* @param Implementation $api The core API.
* @param Options $options The Options API handler.
* @param Implementation $api The core API.
* @param Options $options The Options API handler.
* @param UI\Template $template The template helper.
*/
public function __construct( Implementation $api, Options $options ) {
$this->api = $api;
$this->options = $options;
public function __construct( Implementation $api, Options $options, UI\Template $template ) {
$this->api = $api;
$this->options = $options;
$this->template = $template;
}

/**
Expand Down Expand Up @@ -445,15 +455,16 @@ public function add_context_help(): void {
*/
public function print_settings_section( array $section ): void {
$section_id = ! empty( $section['id'] ) ? $section['id'] : '';
$args = [];

if ( ! empty( $this->admin_form_tabs[ $section_id ]['description'] ) ) {
$description = $this->admin_form_tabs[ $section_id ]['description'];
$args['description'] = $this->admin_form_tabs[ $section_id ]['description'];
} elseif ( ! empty( $this->admin_form_sections[ $section_id ]['description'] ) ) {
$description = $this->admin_form_sections[ $section_id ]['description'];
$args['description'] = $this->admin_form_sections[ $section_id ]['description'];
}

// Load the settings page HTML.
require \WP_TYPOGRAPHY_PLUGIN_PATH . '/admin/partials/settings/section.php';
$this->template->print_partial( '/admin/partials/settings/section.php', $args );
}

/**
Expand Down Expand Up @@ -487,21 +498,32 @@ public function get_admin_page_content(): void {
$this->load_language_options( $this->admin_form_controls[ Plugin_Configuration::HYPHENATE_LANGUAGES ], $this->api->get_hyphenation_languages() );
$this->load_language_options( $this->admin_form_controls[ Plugin_Configuration::DIACRITIC_LANGUAGES ], $this->api->get_diacritic_languages() );

$active_tab = $this->get_active_settings_tab();
$args = [
'plugin_name' => $this->plugin_name,
'active_tab' => $active_tab,
'option_group' => self::OPTION_GROUP . $active_tab,
'admin_form_tabs' => $this->admin_form_tabs,
'restore_defaults_button' => $this->options->get_name( Options::RESTORE_DEFAULTS ),
'clear_cache_button' => $this->options->get_name( Options::CLEAR_CACHE ),
];

// Load the settings page HTML.
require \WP_TYPOGRAPHY_PLUGIN_PATH . '/admin/partials/settings/settings-page.php';
$this->template->print_partial( '/admin/partials/settings/settings-page.php', $args );
}

/**
* Loads the available language option values into the control.
*
* @since 5.9.0
* @since 5.9.2 Visibility changed to `protected`.
*
* @param Control $control The UI control (needs to be a Select element).
* @param array<string,string> $languages The list of available language choices (in the form `$code => $translated_name`).
*
* @return bool Returns `true` on success.
*/
private function load_language_options( Control $control, array $languages ): bool {
protected function load_language_options( Control $control, array $languages ): bool {
if ( $control instanceof Select ) {
$control->set_option_values( $languages );

Expand Down
86 changes: 56 additions & 30 deletions tests/components/class-admin-interface-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use WP_Typography\Data_Storage\Options;
use WP_Typography\Implementation;
use WP_Typography\Settings\Plugin_Configuration as Config;
use WP_Typography\UI\Template;

use Mundschenk\UI;

Expand All @@ -37,8 +38,6 @@
use Brain\Monkey\Filters;
use Brain\Monkey\Functions;

use org\bovigo\vfs\vfsStream;

use Mockery as m;

/**
Expand Down Expand Up @@ -88,6 +87,13 @@ class Admin_Interface_Test extends TestCase {
*/
protected $api;

/**
* Test fixture.
*
* @var Template&m\MockInterface
*/
protected $template;

/**
* Test fixture.
*
Expand All @@ -102,35 +108,18 @@ class Admin_Interface_Test extends TestCase {
protected function set_up(): void {
parent::set_up();

// Set up virtual filesystem.
vfsStream::setup(
'root',
null,
[
'plugin' => [
'admin' => [
'partials' => [
'settings' => [
'settings-page.php' => 'SETTINGS_PHP',
'section.php' => 'SECTION',
],
],
],
],
]
);
set_include_path( 'vfs://root/' ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.runtime_configuration_set_include_path

// Mock WP_Typography\Data_Storage\Options instance.
$this->options = m::mock( Options::class )
// Test fixtures.
$this->options = m::mock( Options::class )
->shouldReceive( 'get' )->andReturn( false )->byDefault()
->shouldReceive( 'set' )->andReturn( false )->byDefault()
->getMock();

$this->api = m::mock( Implementation::class )->shouldReceive( 'get_version' )->andReturn( '6.6.6' )->byDefault()->getMock();
$this->api = m::mock( Implementation::class )
->shouldReceive( 'get_version' )->andReturn( '6.6.6' )->byDefault()
->getMock();
$this->template = m::mock( Template::class );

// Mock WP_Typography\Components\Admin_Interface instance.
$this->admin = m::mock( Admin_Interface::class, [ $this->api, $this->options ] )
$this->admin = m::mock( Admin_Interface::class, [ $this->api, $this->options, $this->template ] )
->shouldAllowMockingProtectedMethods()->makePartial();

/**
Expand Down Expand Up @@ -173,10 +162,11 @@ function( $str, $domain ) {
* @covers ::__construct
*/
public function test_constructor(): void {
$admin = m::mock( Admin_Interface::class, [ $this->api, $this->options ] );
$admin = m::mock( Admin_Interface::class, [ $this->api, $this->options, $this->template ] );

$this->assert_attribute_same( $this->api, 'api', $admin );
$this->assert_attribute_same( $this->options, 'options', $admin );
$this->assert_attribute_same( $this->template, 'template', $admin );
}


Expand Down Expand Up @@ -221,12 +211,48 @@ public function test_get_admin_page_content(): void {
$this->api->shouldReceive( 'get_hyphenation_languages' )->andReturn( [ 'CODE' => 'Language' ] );
$this->api->shouldReceive( 'get_diacritic_languages' )->andReturn( [ 'CODE' => 'Language' ] );

$this->expectOutputString( 'SETTINGS_PHP' );
$this->options->shouldReceive( 'get_name' )->twice()->andReturn( 'button1', 'button2' );

$this->template->shouldReceive( 'print_partial' )->once()->with( '/admin/partials/settings/settings-page.php', m::type( 'array' ) );

// Do it.
$this->admin->get_admin_page_content();
}

/**
* Tests load_language_options.
*
* @covers ::load_language_options
*/
public function test_load_language_options(): void {
/**
* Valid UI control mock.
*
* @var UI\Control&m\MockInterface
*/
$valid_control = m::mock( UI\Controls\Select::class );

/**
* Invalid UI control mock.
*
* @var UI\Control&m\MockInterface
*/
$invalid_control = m::mock( UI\Controls\Input::class );

$languages = [
'CODE1' => 'Language 1',
'CODE2' => 'Language 2',
];

// Set up expectations.
$valid_control->shouldReceive( 'set_option_values' )->once()->with( $languages );
$invalid_control->shouldReceive( 'set_option_values' )->never();

// Run test.
$this->assertTrue( $this->invoke_method( $this->admin, 'load_language_options', [ $valid_control, $languages ] ) );
$this->assertFalse( $this->invoke_method( $this->admin, 'load_language_options', [ $invalid_control, $languages ] ) );
}

/**
* Test print_admin_styles.
*
Expand Down Expand Up @@ -734,7 +760,7 @@ public function test_print_settings_section(): void {
$this->setValue( $this->admin, 'admin_form_sections', $sections, Admin_Interface::class );

// Set up expectations.
$this->expectOutputString( 'SECTION' );
$this->template->shouldReceive( 'print_partial' )->once()->with( '/admin/partials/settings/section.php', m::type( 'array' ) );

// Do it.
$this->admin->print_settings_section( [ 'id' => 'section1' ] );
Expand Down Expand Up @@ -778,7 +804,7 @@ public function test_print_settings_section_with_tab(): void {
$this->setValue( $this->admin, 'admin_form_sections', $sections, Admin_Interface::class );

// Set up expectations.
$this->expectOutputString( 'SECTION' );
$this->template->shouldReceive( 'print_partial' )->once()->with( '/admin/partials/settings/section.php', m::type( 'array' ) );

// Do it.
$this->admin->print_settings_section( [ 'id' => '3rd_tab' ] );
Expand Down

0 comments on commit 9b547d2

Please sign in to comment.