2021-04-26 22:57:04 +01:00
< ? php // phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.EscapeOutput.OutputNotEscaped
2021-04-15 17:03:46 +01:00
/**
2021-05-01 21:34:46 +01:00
* Admin . Layouts : Engine class
2021-04-15 17:03:46 +01:00
*
2021-05-01 21:34:46 +01:00
* The Admin . Layouts subpackage is composed of the { @ see Engine }
* abstract class , which is extended by the { @ see Settings }
2021-05-01 19:22:41 +01:00
* sub - class . The subpackage is initialised at runtime by the { @ see
2021-05-01 21:34:46 +01:00
* Init } class .
2021-04-27 10:04:13 +01:00
*
2021-05-01 21:34:46 +01:00
* @ package footnotes
2021-04-30 18:03:15 +01:00
* @ since 1.5 . 0
* @ since 2.8 . 0 Rename file from `layout.php` to `class-footnotes-layout-engine.php` ,
2021-05-01 19:22:41 +01:00
* rename `dashboard/` sub - directory to `layout/` .
2021-04-15 17:03:46 +01:00
*/
2021-04-26 22:57:04 +01:00
2021-05-02 19:19:46 +01:00
declare ( strict_types = 1 );
2021-05-01 21:34:46 +01:00
namespace footnotes\admin\layout ;
2021-05-01 21:38:11 +01:00
2021-05-01 21:34:46 +01:00
use footnotes\includes as Includes ;
2021-05-02 19:47:58 +01:00
require_once plugin_dir_path ( __DIR__ ) . 'layout/class-init.php' ;
2021-04-15 17:03:46 +01:00
/**
2021-04-30 18:03:15 +01:00
* Class to be extended by page layout sub - classes .
2021-04-15 17:03:46 +01:00
*
2021-04-30 18:03:15 +01:00
* @ abstract
2021-05-01 21:34:46 +01:00
*
* @ package footnotes
2021-04-15 17:03:46 +01:00
* @ since 1.5 . 0
*/
2021-05-01 21:34:46 +01:00
abstract class Engine {
2021-04-15 17:03:46 +01:00
2021-04-27 09:30:53 +01:00
/**
* The ID of this plugin .
*
2021-04-30 18:03:15 +01:00
* @ access protected
* @ var string $plugin_name The ID of this plugin .
*
* @ since 2.8 . 0
2021-04-27 09:30:53 +01:00
*/
2021-05-02 19:19:46 +01:00
protected string $plugin_name ;
2021-04-27 09:30:53 +01:00
2021-04-15 17:03:46 +01:00
/**
2021-04-30 18:03:15 +01:00
* Stores the Hook connection string for the child sub - page .
*
* @ access protected
* @ var null | string
2021-04-15 17:03:46 +01:00
*
* @ since 1.5 . 0
*/
2021-05-02 20:46:44 +01:00
protected ? string $sub_page_hook = null ;
2021-04-15 17:03:46 +01:00
/**
2021-04-30 18:03:15 +01:00
* Stores all Sections for the child sub - page .
2021-04-15 17:03:46 +01:00
*
2021-04-30 18:03:15 +01:00
* @ access protected
* @ var array
*
* @ since 1.5 . 0
2021-04-15 17:03:46 +01:00
*/
2021-05-02 20:46:44 +01:00
protected array $sections = array ();
2021-04-15 17:03:46 +01:00
/**
2021-04-30 18:03:15 +01:00
* Returns a Priority index . Lower numbers have a higher priority .
*
* @ abstract
2021-04-15 17:03:46 +01:00
*
2021-04-30 18:03:15 +01:00
* @ since 1.5 . 0
2021-04-15 17:03:46 +01:00
*/
2021-05-02 19:19:46 +01:00
abstract public function get_priority () : int ;
2021-04-15 17:03:46 +01:00
/**
2021-04-30 18:03:15 +01:00
* Registers a sub - page .
2021-04-15 17:03:46 +01:00
*
* @ since 1.5 . 0
*/
2021-05-02 19:19:46 +01:00
public function register_sub_page () : void {
2021-04-15 17:03:46 +01:00
global $submenu ;
2021-05-02 20:46:44 +01:00
if ( array_key_exists ( plugin_basename ( Init :: MAIN_MENU_SLUG ), $submenu ) ) {
foreach ( $submenu [ plugin_basename ( Init :: MAIN_MENU_SLUG ) ] as $sub_menu ) {
if ( plugin_basename ( Init :: MAIN_MENU_SLUG . $this -> get_sub_page_slug () ) === $sub_menu [ 2 ] ) {
remove_submenu_page ( Init :: MAIN_MENU_SLUG , Init :: MAIN_MENU_SLUG . $this -> get_sub_page_slug () );
2021-04-15 17:03:46 +01:00
}
}
}
2021-05-02 20:46:44 +01:00
$this -> sub_page_hook = add_submenu_page (
Init :: MAIN_MENU_SLUG ,
2021-04-15 17:03:46 +01:00
$this -> get_sub_page_title (),
$this -> get_sub_page_title (),
'manage_options' ,
2021-05-02 20:46:44 +01:00
Init :: MAIN_MENU_SLUG . $this -> get_sub_page_slug (),
2021-05-02 19:55:38 +01:00
fn () => $this -> display_content ()
2021-04-15 17:03:46 +01:00
);
}
/**
2021-04-30 18:03:15 +01:00
* Registers all sections for a sub - page .
2021-04-15 17:03:46 +01:00
*
2021-04-30 18:03:15 +01:00
* @ since 1.5 . 0
2021-04-15 17:03:46 +01:00
*/
2021-05-02 19:19:46 +01:00
public function register_sections () : void {
2021-05-02 20:46:44 +01:00
foreach ( $this -> get_sections () as $section ) {
2021-04-15 17:03:46 +01:00
// Append tab to the tab-array.
2021-05-02 20:46:44 +01:00
$this -> sections [ $section [ 'id' ] ] = $section ;
2021-04-15 17:03:46 +01:00
add_settings_section (
2021-05-02 20:46:44 +01:00
$section [ 'id' ],
2021-04-15 17:03:46 +01:00
'' ,
2021-05-02 19:55:38 +01:00
fn () => $this -> description (),
2021-05-02 20:46:44 +01:00
$section [ 'id' ]
2021-04-15 17:03:46 +01:00
);
2021-05-02 20:46:44 +01:00
$this -> register_meta_boxes ( $section [ 'id' ] );
2021-04-15 17:03:46 +01:00
}
}
// phpcs:disable WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
/**
2021-04-30 18:03:15 +01:00
* Displays the content of specific sub - page .
2021-04-15 17:03:46 +01:00
*
* @ since 1.5 . 0
2021-04-30 18:03:15 +01:00
* @ todo Review nonce verification .
2021-04-15 17:03:46 +01:00
*/
2021-05-02 19:19:46 +01:00
public function display_content () : void {
2021-04-15 17:03:46 +01:00
$this -> append_scripts ();
2021-05-02 20:46:44 +01:00
$active_section_id = isset ( $_GET [ 't' ] ) ? wp_unslash ( $_GET [ 't' ] ) : array_key_first ( $this -> sections );
$active_section = $this -> sections [ $active_section_id ];
2021-04-15 17:03:46 +01:00
// Store settings.
2021-05-02 20:46:44 +01:00
$settings_updated = false ;
2021-05-02 10:45:52 +01:00
if ( array_key_exists ( 'save-settings' , $_POST ) && 'save' === $_POST [ 'save-settings' ] ) {
unset ( $_POST [ 'save-settings' ] );
unset ( $_POST [ 'submit' ] );
2021-05-02 20:46:44 +01:00
$settings_updated = $this -> save_settings ();
2021-04-15 17:03:46 +01:00
}
// Display all sections and highlight the active section.
echo '<div class="wrap">' ;
echo '<h2 class="nav-tab-wrapper">' ;
// Iterate through all register sections.
2021-05-02 20:46:44 +01:00
foreach ( $this -> sections as $id => $description ) {
2021-04-15 17:03:46 +01:00
echo sprintf (
'<a class="nav-tab%s" href="?page=%s&t=%s">%s</a>' ,
2021-05-02 20:46:44 +01:00
( $id === $active_section [ 'id' ] ) ? ' nav-tab-active' : '' ,
Init :: MAIN_MENU_SLUG ,
$id ,
$description [ 'title' ]
2021-04-15 17:03:46 +01:00
);
}
echo '</h2><br/>' ;
2021-05-02 20:46:44 +01:00
if ( $settings_updated ) {
2021-04-15 17:03:46 +01:00
echo sprintf ( '<div id="message" class="updated">%s</div>' , __ ( 'Settings saved' , 'footnotes' ) );
}
// Form to submit the active section.
echo '<!--suppress HtmlUnknownTarget --><form method="post" action="">' ;
echo '<input type="hidden" name="save-settings" value="save" />' ;
// Outputs the settings field of the active section.
2021-05-02 20:46:44 +01:00
do_settings_sections ( $active_section [ 'id' ] );
do_meta_boxes ( $active_section [ 'id' ], 'main' , null );
2021-04-15 17:03:46 +01:00
// Add submit button to active section if defined.
2021-05-02 20:46:44 +01:00
if ( $active_section [ 'submit' ] ) {
2021-04-15 17:03:46 +01:00
submit_button ();
}
echo '</form>' ;
echo '</div>' ;
// Echo JavaScript for the expand/collapse function of the meta boxes.
echo '<script type="text/javascript">' ;
echo 'jQuery(document).ready(function ($) {' ;
2021-04-16 21:50:02 +01:00
echo 'jQuery(".footnotes-color-picker").wpColorPicker();' ;
2021-04-15 17:03:46 +01:00
echo " jQuery('.if-js-closed').removeClass('if-js-closed').addClass('closed'); " ;
2021-05-02 20:46:44 +01:00
echo " postboxes.add_postbox_toggles(' " . $this -> sub_page_hook . " '); " ;
2021-04-15 17:03:46 +01:00
echo '});' ;
echo '</script>' ;
}
2021-04-30 18:03:15 +01:00
// phpcs:enable WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
2021-05-02 21:05:37 +01:00
/**
* Output the description of a section . May be overwritten in any section .
*
* @ since 1.5 . 0
* @ todo Required ? Should be `abstract` ?
*/
public function description () : void {
// Default no description will be displayed.
}
2021-04-15 17:03:46 +01:00
/**
2021-05-02 21:05:37 +01:00
* Returns the unique slug of the child sub - page .
2021-04-15 17:03:46 +01:00
*
2021-05-02 21:05:37 +01:00
* @ abstract
* @ access protected
2021-04-30 18:03:15 +01:00
*
* @ since 1.5 . 0
2021-04-15 17:03:46 +01:00
*/
2021-05-02 21:05:37 +01:00
abstract protected function get_sub_page_slug () : string ;
2021-04-15 17:03:46 +01:00
2021-05-02 21:05:37 +01:00
/**
* Returns the title of the child sub - page .
*
* @ abstract
* @ access protected
*
* @ since 1.5 . 0
*/
abstract protected function get_sub_page_title () : string ;
/**
* Returns an array of all registered sections for a sub - page .
*
* @ abstract
* @ access protected
*
* @ since 1.5 . 0
*/
abstract protected function get_sections () : array ;
/**
* Returns an array of all registered meta boxes .
*
* @ abstract
* @ access protected
*
* @ since 1.5 . 0
*/
abstract protected function get_meta_boxes () : array ;
/**
* Returns an array describing a sub - page section .
*
* @ access protected
* @ param string $id Unique ID suffix .
* @ param string $title Title of the section .
* @ param int $settings_container_index Settings Container index .
* @ param bool $has_submit_button Whether a ‘ Submit’ button should
* be displayed for this section . Default `true` .
* @ return array {
* A dashboard section .
*
* @ type string $id Section ID .
* @ type string $title Section title .
* @ type bool $submit Whether the section has a submit button or not .
* @ type int $container Settings Container index .
* }
*
* @ since 1.5 . 0
* @ todo Refactor sections into their own class ?
*/
protected function add_section ( string $id , string $title , int $settings_container_index , bool $has_submit_button = true ) : array {
return array (
'id' => $this -> plugin_name . '-' . $id ,
'title' => $title ,
'submit' => $has_submit_button ,
'container' => $settings_container_index ,
);
2021-04-15 17:03:46 +01:00
}
/**
2021-05-02 21:05:37 +01:00
* Returns an array describing a meta box .
*
* @ access protected
* @ param string $section_id Parent section ID .
* @ param string $id Unique ID suffix .
* @ param string $title Title for the meta box .
* @ param string $callback_function_name Class method name for callback .
* @ return array {
* A dashboard meta box .
*
* @ type string $parent Parent section ID .
* @ type string $id Meta box ID .
* @ type string $title Meta box title .
* @ type string $callback Meta box callback function .
* }
2021-04-15 17:03:46 +01:00
*
2021-04-30 18:03:15 +01:00
* @ since 1.5 . 0
2021-05-02 21:05:37 +01:00
* @ todo Refactor meta boxes into their own class ?
* @ todo Pass actual functions rather than strings ?
2021-04-15 17:03:46 +01:00
*/
2021-05-02 21:05:37 +01:00
protected function add_meta_box ( string $section_id , string $id , string $title , string $callback_function_name ) : array {
return array (
'parent' => $this -> plugin_name . '-' . $section_id ,
'id' => $id ,
'title' => $title ,
'callback' => $callback_function_name ,
);
2021-04-15 17:03:46 +01:00
}
/**
2021-04-30 18:03:15 +01:00
* Loads a specified setting .
*
* @ access protected
2021-05-02 20:46:44 +01:00
* @ param string $setting_key_name Setting key .
2021-04-30 18:03:15 +01:00
* @ return array {
* A configurable setting .
*
* @ type string $id Setting key .
* @ type string $name Setting name .
* @ type string $value Setting value .
* }
*
* @ since 1.5 . 0
* @ since 2.5 . 11 Broken due to accidental removal of `esc_attr()` call .
* @ since 2.6 . 1 Restore `esc_attr()` call .
2021-04-15 17:03:46 +01:00
*/
2021-05-02 20:46:44 +01:00
protected function load_setting ( string $setting_key_name ) : array {
2021-04-15 17:03:46 +01:00
// Get current section.
2021-05-02 20:46:44 +01:00
reset ( $this -> sections );
$return = array ();
$return [ 'id' ] = $setting_key_name ;
$return [ 'name' ] = $setting_key_name ;
$return [ 'value' ] = esc_attr ( Includes\Settings :: instance () -> get ( $setting_key_name ) );
return $return ;
2021-04-15 17:03:46 +01:00
}
2021-05-01 19:22:41 +01:00
2021-04-15 17:03:46 +01:00
/**
2021-05-02 19:19:46 +01:00
* Returns a simple text inside a 'span' element .
2021-04-15 17:03:46 +01:00
*
2021-04-30 18:03:15 +01:00
* @ access protected
2021-05-02 20:46:44 +01:00
* @ param string $text Message to be surrounded with `<span>` tags .
2021-04-15 17:03:46 +01:00
*
* @ since 1.5 . 0
2021-04-30 18:03:15 +01:00
* @ todo Refactor HTML generation .
2021-04-15 17:03:46 +01:00
*/
2021-05-02 20:46:44 +01:00
protected function add_text ( string $text ) : string {
return sprintf ( '<span>%s</span>' , $text );
2021-04-15 17:03:46 +01:00
}
/**
2021-05-02 19:19:46 +01:00
* Returns the HTML tag for a 'label' element .
2021-04-30 18:03:15 +01:00
*
* @ access protected
2021-05-02 20:46:44 +01:00
* @ param string $setting_name Settings key .
* @ param string $caption Label caption .
2021-04-15 17:03:46 +01:00
*
* @ since 1.5 . 0
2021-04-30 18:03:15 +01:00
* @ todo Refactor HTML generation .
2021-04-15 17:03:46 +01:00
*/
2021-05-02 20:46:44 +01:00
protected function add_label ( string $setting_name , string $caption ) : string {
if ( empty ( $caption ) ) {
2021-04-15 17:03:46 +01:00
return '' ;
}
/*
* Remove the colon causing localization issues with French , and with
* languages not using punctuation at all , and with languages using other
* punctuation marks instead of colon , e . g . Greek using a raised dot .
* In French , colon is preceded by a space , forcibly non - breaking , and
* narrow per new school .
* Add colon to label strings for inclusion in localization . Colon after
* label is widely preferred best practice , mandatory per
2021-04-30 18:03:15 +01:00
* { @ link https :// softwareengineering . stackexchange . com / questions / 234546 / colons - in - internationalized - ui
* style guides } .
2021-04-15 17:03:46 +01:00
*/
2021-05-02 20:46:44 +01:00
return sprintf ( '<label for="%s">%s</label>' , $setting_name , $caption );
2021-04-15 17:03:46 +01:00
}
/**
2021-05-02 19:19:46 +01:00
* Constructs the HTML for a text 'input' element .
2021-04-30 18:03:15 +01:00
*
* @ access protected
2021-05-02 20:46:44 +01:00
* @ param string $setting_name Setting key .
* @ param int $max_length Maximum length of the input . Default length 999 chars .
* @ param bool $readonly Set the input to be read only . Default `false` .
* @ param bool $hidden Set the input to be hidden . Default `false` .
2021-04-15 17:03:46 +01:00
*
* @ since 1.5 . 0
2021-04-30 18:03:15 +01:00
* @ todo Refactor HTML generation .
2021-04-15 17:03:46 +01:00
*/
2021-05-02 20:46:44 +01:00
protected function add_text_box ( string $setting_name , int $max_length = 999 , bool $readonly = false , bool $hidden = false ) : string {
$style = '' ;
2021-04-15 17:03:46 +01:00
// Collect data for given settings field.
2021-05-02 20:46:44 +01:00
$data = $this -> load_setting ( $setting_name );
if ( $hidden ) {
$style .= 'display:none;' ;
2021-04-15 17:03:46 +01:00
}
return sprintf (
'<input type="text" name="%s" id="%s" maxlength="%d" style="%s" value="%s" %s/>' ,
2021-05-02 20:46:44 +01:00
$data [ 'name' ],
$data [ 'id' ],
$max_length ,
$style ,
$data [ 'value' ],
$readonly ? 'readonly="readonly"' : ''
2021-04-15 17:03:46 +01:00
);
}
/**
2021-05-02 19:19:46 +01:00
* Constructs the HTML for a checkbox 'input' element .
2021-04-30 18:03:15 +01:00
*
* @ access protected
2021-05-02 20:46:44 +01:00
* @ param string $setting_name Setting key .
2021-04-15 17:03:46 +01:00
*
* @ since 1.5 . 0
2021-04-30 18:03:15 +01:00
* @ todo Refactor HTML generation .
2021-04-15 17:03:46 +01:00
*/
2021-05-02 20:46:44 +01:00
protected function add_checkbox ( string $setting_name ) : string {
2021-04-15 17:03:46 +01:00
// Collect data for given settings field.
2021-05-02 20:46:44 +01:00
$data = $this -> load_setting ( $setting_name );
2021-04-15 17:03:46 +01:00
return sprintf (
'<input type="checkbox" name="%s" id="%s" %s/>' ,
2021-05-02 20:46:44 +01:00
$data [ 'name' ],
$data [ 'id' ],
Includes\Convert :: to_bool ( $data [ 'value' ] ) ? 'checked="checked"' : ''
2021-04-15 17:03:46 +01:00
);
}
/**
2021-05-02 19:19:46 +01:00
* Constructs the HTML for a 'select' element .
2021-04-15 17:03:46 +01:00
*
2021-04-30 18:03:15 +01:00
* @ access protected
2021-05-02 20:46:44 +01:00
* @ param string $setting_name Setting key .
* @ param array $options Possible options .
2021-04-15 17:03:46 +01:00
*
2021-04-30 18:03:15 +01:00
* @ since 1.5 . 0
* @ todo Refactor HTML generation .
2021-04-15 17:03:46 +01:00
*/
2021-05-02 20:46:44 +01:00
protected function add_select_box ( string $setting_name , array $options ) : string {
2021-04-15 17:03:46 +01:00
// Collect data for given settings field.
2021-05-02 21:25:27 +01:00
$data = $this -> load_setting ( $setting_name );
2021-05-02 21:21:32 +01:00
$select_options = '' ;
2021-04-15 17:03:46 +01:00
// Loop through all array keys.
2021-05-02 20:46:44 +01:00
foreach ( $options as $value => $caption ) {
2021-05-02 21:21:32 +01:00
$select_options .= sprintf (
2021-04-15 17:03:46 +01:00
'<option value="%s" %s>%s</option>' ,
2021-05-02 20:46:44 +01:00
$value ,
2021-04-15 17:03:46 +01:00
// Only check for equality, not identity, WRT backlink symbol arrows.
2021-04-26 17:15:48 +01:00
// phpcs:disable WordPress.PHP.StrictComparisons.LooseComparison
2021-05-02 20:46:44 +01:00
$value == $data [ 'value' ] ? 'selected' : '' ,
2021-04-26 17:15:48 +01:00
// phpcs:enable WordPress.PHP.StrictComparisons.LooseComparison
2021-05-02 20:46:44 +01:00
$caption
2021-04-15 17:03:46 +01:00
);
}
return sprintf (
'<select name="%s" id="%s">%s</select>' ,
2021-05-02 20:46:44 +01:00
$data [ 'name' ],
$data [ 'id' ],
2021-05-02 21:21:32 +01:00
$select_options
2021-04-15 17:03:46 +01:00
);
}
/**
2021-05-02 19:19:46 +01:00
* Constructs the HTML for a 'textarea' element .
2021-04-30 18:03:15 +01:00
*
* @ access protected
2021-05-02 20:46:44 +01:00
* @ param string $setting_name Setting key .
2021-04-15 17:03:46 +01:00
*
2021-04-30 18:03:15 +01:00
* @ since 1.5 . 0
* @ todo Refactor HTML generation .
2021-04-15 17:03:46 +01:00
*/
2021-05-02 20:46:44 +01:00
protected function add_textarea ( $setting_name ) : string {
2021-04-15 17:03:46 +01:00
// Collect data for given settings field.
2021-05-02 20:46:44 +01:00
$data = $this -> load_setting ( $setting_name );
2021-04-15 17:03:46 +01:00
return sprintf (
'<textarea name="%s" id="%s">%s</textarea>' ,
2021-05-02 20:46:44 +01:00
$data [ 'name' ],
$data [ 'id' ],
$data [ 'value' ]
2021-04-15 17:03:46 +01:00
);
}
/**
2021-05-02 19:19:46 +01:00
* Constructs the HTML for a text 'input' element with the colour selection
2021-04-30 18:03:15 +01:00
* class .
*
* @ access protected
2021-05-02 20:46:44 +01:00
* @ param string $setting_name Setting key .
2021-04-15 17:03:46 +01:00
*
* @ since 1.5 . 6
2021-04-30 18:03:15 +01:00
* @ todo Refactor HTML generation .
* @ todo Use proper colorpicker element .
2021-04-15 17:03:46 +01:00
*/
2021-05-02 20:46:44 +01:00
protected function add_color_selection ( string $setting_name ) : string {
2021-04-15 17:03:46 +01:00
// Collect data for given settings field.
2021-05-02 20:46:44 +01:00
$data = $this -> load_setting ( $setting_name );
2021-04-15 17:03:46 +01:00
return sprintf (
2021-04-16 21:50:02 +01:00
'<input type="text" name="%s" id="%s" class="footnotes-color-picker" value="%s"/>' ,
2021-05-02 20:46:44 +01:00
$data [ 'name' ],
$data [ 'id' ],
$data [ 'value' ]
2021-04-15 17:03:46 +01:00
);
}
/**
2021-05-02 19:19:46 +01:00
* Constructs the HTML for numeric 'input' element .
2021-04-30 18:03:15 +01:00
*
* @ access protected
2021-05-02 20:46:44 +01:00
* @ param string $setting_name Setting key .
2021-05-01 19:22:41 +01:00
* @ param int $p_in_min Minimum value .
2021-05-02 20:46:44 +01:00
* @ param int $max Maximum value .
* @ param bool $deci `true` if float , `false` if integer . Default `false` .
2021-04-15 17:03:46 +01:00
*
* @ since 1.5 . 0
2021-04-30 18:03:15 +01:00
* @ todo Refactor HTML generation .
2021-04-15 17:03:46 +01:00
*/
2021-05-02 20:46:44 +01:00
protected function add_num_box ( string $setting_name , int $p_in_min , int $max , bool $deci = false ) : string {
2021-04-15 17:03:46 +01:00
// Collect data for given settings field.
2021-05-02 20:46:44 +01:00
$data = $this -> load_setting ( $setting_name );
2021-04-15 17:03:46 +01:00
2021-05-02 20:46:44 +01:00
if ( $deci ) {
$value = number_format ( floatval ( $data [ 'value' ] ), 1 );
2021-04-15 17:03:46 +01:00
return sprintf (
'<input type="number" name="%s" id="%s" value="%s" step="0.1" min="%d" max="%d"/>' ,
2021-05-02 20:46:44 +01:00
$data [ 'name' ],
$data [ 'id' ],
$value ,
2021-04-15 17:03:46 +01:00
$p_in_min ,
2021-05-02 20:46:44 +01:00
$max
2021-04-15 17:03:46 +01:00
);
2021-05-02 20:00:55 +01:00
}
return sprintf (
2021-05-02 20:12:47 +01:00
'<input type="number" name="%s" id="%s" value="%d" min="%d" max="%d"/>' ,
2021-05-02 20:46:44 +01:00
$data [ 'name' ],
$data [ 'id' ],
$data [ 'value' ],
2021-05-02 20:12:47 +01:00
$p_in_min ,
2021-05-02 20:46:44 +01:00
$max
2021-05-02 20:12:47 +01:00
);
2021-04-15 17:03:46 +01:00
}
2021-05-02 21:05:37 +01:00
/**
* Registers all Meta boxes for a sub - page .
*
* @ access private
* @ param string $parent_id Parent section unique ID .
*
* @ since 1.5 . 0
*/
private function register_meta_boxes ( string $parent_id ) : void {
// Iterate through each meta box.
foreach ( $this -> get_meta_boxes () as $meta_box ) {
if ( $parent_id !== $meta_box [ 'parent' ] ) {
continue ;
}
add_meta_box (
$parent_id . '-' . $meta_box [ 'id' ],
$meta_box [ 'title' ],
array ( $this , $meta_box [ 'callback' ] ),
$parent_id ,
'main'
);
}
}
/**
* Append JavaScript and CSS files for specific sub - page .
*
* @ access private
*
* @ since 1.5 . 0
* @ todo Move to { @ see Includes\Admin } .
*/
private function append_scripts () : void {
wp_enqueue_script ( 'postbox' );
wp_enqueue_style ( 'wp-color-picker' );
wp_enqueue_script ( 'wp-color-picker' );
}
// phpcs:enable WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
// phpcs:disable WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
/**
* Save all plugin settings .
*
* @ access private
* @ return bool `true` on save success , else `false` .
*
* @ since 1.5 . 0
* @ todo Review nonce verification .
*/
private function save_settings () : bool {
$new_settings = array ();
$active_section_id = isset ( $_GET [ 't' ] ) ? wp_unslash ( $_GET [ 't' ] ) : array_key_first ( $this -> sections );
$active_section = $this -> sections [ $active_section_id ];
foreach ( array_keys ( Includes\Settings :: instance () -> get_defaults ( $active_section [ 'container' ] ) ) as $key ) {
$new_settings [ $key ] = array_key_exists ( $key , $_POST ) ? wp_unslash ( $_POST [ $key ] ) : '' ;
}
// Update settings.
return Includes\Settings :: instance () -> save_options ( $active_section [ 'container' ], $new_settings );
}
2021-04-15 17:03:46 +01:00
}