Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a generic text-field view to replace last-name and first-name views. #3

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions config/settings-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@
'assb1_text_field_first_name' => [
'title' => __( 'First Name', 'as-settings-better-v1' ),
'default' => 'Elliot',
'view' => AS_BETTER_SETTINGS_1_DIR . 'views/first-name-field.php',
'view' => AS_BETTER_SETTINGS_1_DIR . 'views/text-field.php',
],
'assb1_text_field_last_name' => [
'title' => __( 'Last Name', 'as-settings-better-v1' ),
'default' => 'Alderson',
'view' => AS_BETTER_SETTINGS_1_DIR . 'views/last-name-field.php',
'view' => AS_BETTER_SETTINGS_1_DIR . 'views/text-field.php',
],
],
],
Expand Down
11 changes: 6 additions & 5 deletions src/SettingsPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,12 @@ protected function add_pages_entry( $data, $name ) {
$data['menu_slug'] = $name;

// Prepare rendering callback.
$data['function'] = function () use ( $data ) {
$data['function'] = function () use ( $data, $name ) {
if ( ! array_key_exists( 'view', $data ) ) {
return;
}

$this->render_view( $data['view'] );
$this->render_view( $data['view'], array(), $name );
};

$page_hook = $this->invoke_function( $function, $data );
Expand Down Expand Up @@ -259,7 +259,7 @@ protected function add_field( $data, $name, $args ) {
: '';
}

$this->render_view( $data['view'], [ 'options' => $options ] );
$this->render_view( $data['view'], [ 'options' => $options ], $name );
};

add_settings_field(
Expand All @@ -280,11 +280,12 @@ protected function add_field( $data, $name, $args ) {
* or an instance of a View object.
* @param array|null $context Optional. Context array for which to render
* the view.
* @param string|null $name Optional. Name of the field that will be rendered.
*/
protected function render_view( $view, array $context = [] ) {
protected function render_view( $view, array $context = [], $name = '' ) {
$view_object = is_string( $view ) ? new View( $view ) : $view;
echo wp_kses(
$view_object->render( $context ),
$view_object->render( $context, $name ),
$this->allowed_tags
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct( $filename ) {
* @param array $context Optional. Associative array with context variables.
* @return string HTML rendering of the view.
*/
public function render( array $context = [] ) {
public function render( array $context = [], $name ) {
if ( ! is_readable( $this->filename ) ) {
return '';
}
Expand Down
23 changes: 0 additions & 23 deletions views/last-name-field.php

This file was deleted.

9 changes: 6 additions & 3 deletions views/first-name-field.php → views/text-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@

namespace AlainSchlesser\BetterSettings1;

/** @var array $options Options passed through from SettingsPage class. */
/**
* @var string $name Name of the field passed through from SettingsPage class.
* @var array $options Options passed through from SettingsPage class.
*/

?>
<input type='text' name='assb1_settings[assb1_text_field_first_name]'
value='<?php echo $options['assb1_text_field_first_name']; ?>'>
<input type='text' name='assb1_settings[<?php echo $name; ?>]'
value='<?php echo $options["{$name}"]; ?>'>