Skip to content

Commit

Permalink
feat: settings use value instead of placeholder (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasguillot authored Aug 21, 2020
1 parent 8138a11 commit 7276c5d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 47 deletions.
49 changes: 17 additions & 32 deletions includes/class-newspack-sponsors-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,34 +78,22 @@ public static function get_settings_list() {

return [
[
'label' => __( 'Default Sponsor Byline Prefix', 'newspack-sponsors' ),
'placeholder' => sprintf(
// Translators: placeholder for default byline value.
__( 'Default: “%s”', 'newspack-sponsors' ),
$defaults['byline']
),
'key' => 'newspack_sponsors_default_byline',
'type' => 'input',
'label' => __( 'Default Sponsor Byline Prefix', 'newspack-sponsors' ),
'value' => $defaults['byline'],
'key' => 'newspack_sponsors_default_byline',
'type' => 'input',
],
[
'label' => __( 'Default Sponsored Flag Label', 'newspack-sponsors' ),
'placeholder' => sprintf(
// Translators: placeholder for default flag value.
__( 'Default: “%s”', 'newspack-sponsors' ),
$defaults['flag']
),
'key' => 'newspack_sponsors_default_flag',
'type' => 'input',
'label' => __( 'Default Sponsored Flag Label', 'newspack-sponsors' ),
'value' => $defaults['flag'],
'key' => 'newspack_sponsors_default_flag',
'type' => 'input',
],
[
'label' => __( 'Default Sponsorship Disclaimer', 'newspack-sponsors' ),
'placeholder' => sprintf(
// Translators: placeholder for default disclaimer value.
__( 'Default: “%s”', 'newspack-sponsors' ),
$defaults['disclaimer']
),
'key' => 'newspack_sponsors_default_disclaimer',
'type' => 'textarea',
'label' => __( 'Default Sponsorship Disclaimer', 'newspack-sponsors' ),
'value' => $defaults['disclaimer'],
'key' => 'newspack_sponsors_default_disclaimer',
'type' => 'textarea',
],
];
}
Expand Down Expand Up @@ -174,25 +162,22 @@ public static function page_init() {
* @param array $setting Settings array.
*/
public static function newspack_sponsors_settings_callback( $setting ) {
$key = $setting['key'];
$type = $setting['type'];
$placeholder = $setting['placeholder'];
$value = get_option( $key, false );
$key = $setting['key'];
$type = $setting['type'];
$value = ( '' !== get_option( $key, false ) ) ? get_option( $key, false ) : $setting['value'];

if ( 'textarea' === $type ) {
printf(
'<textarea id="%s" name="%s" class="widefat" rows="4" placeholder="%s">%s</textarea>',
'<textarea id="%s" name="%s" class="widefat" rows="4">%s</textarea>',
esc_attr( $key ),
esc_attr( $key ),
esc_attr( $placeholder ),
esc_attr( $value )
);
} else {
printf(
'<input type="text" id="%s" name="%s" placeholder="%s" value="%s" class="widefat" />',
'<input type="text" id="%s" name="%s" value="%s" class="widefat" />',
esc_attr( $key ),
esc_attr( $key ),
esc_attr( $placeholder ),
esc_attr( $value )
);
}
Expand Down
21 changes: 6 additions & 15 deletions src/editor/sidebar/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import { __ } from '@wordpress/i18n';
import { SelectControl, TextareaControl, TextControl, ToggleControl } from '@wordpress/components';
import { compose } from '@wordpress/compose';
import { withDispatch, withSelect } from '@wordpress/data';
Expand Down Expand Up @@ -63,11 +63,7 @@ const SidebarComponent = props => {
<TextControl
className="newspack-sponsors__text-control"
label={ __( 'Sponsor Flag Override (Optional)', 'newspack-sponsors' ) }
placeholder={ sprintf(
// Translators: placeholder text for Sponsor Flag field.
__( 'Default: “%s”', 'newspack-sponsors' ),
settings.flag || defaults.flag
) }
placeholder={ settings.flag || defaults.flag }
help={ __(
'The label for the flag that appears in lieu of category flags. If not empty, this field will override the site-wide setting.',
'newspack-sponsors'
Expand All @@ -79,10 +75,9 @@ const SidebarComponent = props => {
<TextareaControl
className="newspack-sponsors__textarea-control"
label={ __( 'Sponsor Disclaimer Override (Optional)', 'newspack-sponsors' ) }
placeholder={ sprintf(
// Translators: placeholder text for Sponsor Disclaimer field.
__( 'Default: “%s”', 'newspack-sponsors' ),
( settings.disclaimer || defaults.disclaimer ).replace( '[sponsor name]', title )
placeholder={ ( settings.disclaimer || defaults.disclaimer ).replace(
'[sponsor name]',
title
) }
help={ __(
'Text shown to explain sponsorship by this sponsor. If not empty, this field will override the site-wide setting.',
Expand All @@ -98,11 +93,7 @@ const SidebarComponent = props => {
'The prefix for the sponsor attribution that appears in lieu of author byline. If not empty, this field will override the site-wide setting.',
'newspack-sponsors'
) }
placeholder={ sprintf(
// Translators: placeholder text for Sponsor Byline Prefix field.
__( 'Default: “%s”', 'newspack-sponsors' ),
settings.byline || defaults.byline
) }
placeholder={ settings.byline || defaults.byline }
type="url"
value={ newspack_sponsor_byline_prefix }
onChange={ value => updateMetaValue( 'newspack_sponsor_byline_prefix', value ) }
Expand Down

0 comments on commit 7276c5d

Please sign in to comment.