pfeifferNet

Settings
in package

Manage the pfeifferNet site(s) settings.

  • Create a settings admin page.
  • Settings are kept in an array in single table entry.
  • Maintain settings that are unique to each site.
  • Provide hooks to allow other plugins to modify or extend settings sections and fields.
  • Provide hook to allow other plugins to validate extended settings fields.
  • Provide settings import/export.
Tags
since
1.0.0
since
5.5.0

Inherit standard singleton class.

since
5.11.0

Deprecated global singleton.

since
5.11.0

Simplification. Use associative arrays for fields and sections.

since
5.11.0

Simplification. Use partials for input render elements.

since
6.0.0

Provide settings import/export.

since
6.0.0

Fix text domain loading too early error.

since
6.2.0

Simplification. Reduce size and complexity of set_fields().

used-by
Settings_Import_Export
used-by
Settings_Import_Export::__construct()
used-by
Settings_Import::__construct()

Table of Contents

Constants

CAPABILITY  = 'manage_options'
The user capability required to access the settings page.
DEFAULT_INPUT_CONTAINER  = 'input-default.php'
Default setting field input partial.
DESCRIPTION_FIELD_CONTAINER  = 'setting-description.php'
The settings field or section description paragraph element template.
FILE_NAME  = 'pfeifferNet-settings.json'
The settings export file name.
PAGE_SLUG  = 'pnet-site-settings'
The settings menu slug.
SETTINGS_CONTAINER  = 'form-settings.php'
Settings page partial.

Properties

$current_settings  : array<string|int, mixed>
The array of settings values currently stored.
$default_input  : string
The fully qualified path to the default setting field input partial.
$description_field  : string
The fully qualified path to the setting section or field description partial.
$fields  : array<string|int, mixed>
The array of settings input fields and their definition.
$instances  : array<string|int, object>
The array of instances of this class and sub-classes.
$media_selector  : Media_Selector
The instance of the Media_Selector class.
$page_hook  : string
The page hook.
$page_icon_url  : string
The page icon url.
$page_title  : string
The page title.
$sections  : array<string|int, mixed>
The array of settings sections and their definition.
$settings_container  : string
The fully qualified path to the settings page partial.
$settings_key  : string
The plugin settings options key.

Methods

add_options_menu()  : void
Add entry to options menu.
get_export()  : array<string|int, mixed>
Get the settings export file contents.
get_instance()  : object
Creates and/or returns the instance of this class.
import()  : bool
Import settings.
initialise_settings()  : void
Register the setting and add sections and fields.
render()  : void
Render settings page.
render_field()  : void
Settings field callback to render the field.
render_section()  : void
Settings section callback,
sanitise_settings()  : array<string|int, mixed>
Settings sanitisation callback.
set_action_links()  : array<string|int, string>
Return the site settings plugin action links.
__construct()  : void
Initialise the class.
add_settings_fields()  : void
Add the fields to the form.
add_settings_sections()  : void
Add the sections to the form.
render_input_element()  : void
Render the input field based on field type.
set_fields()  : void
Initialise the fields array.
set_fields_contact_requests()  : void
Set the email address for contact requests.
set_fields_external_api()  : void
Set the external API requirements.
set_fields_general()  : void
General site settings fields.
set_fields_google_recaptcha()  : void
Set the threshold and keys for the Google reCaptcha API.
set_fields_images()  : void
Set home and fallback images.
set_fields_sharing()  : void
Set the supported social sharing services.
set_fields_smtp()  : void
Set SMTP username, password and sender settings.
set_fields_social_media()  : void
Set the social media accounts.
set_fields_users()  : void
Set the user account settings.
set_sections()  : void
Initialise the sections array.
set_token_expiration_description()  : string
Set the description for the token expiry field.
validate_github_token()  : array<string|int, mixed>
Validate the GitHub token by checking the expiry date.

Constants

CAPABILITY

The user capability required to access the settings page.

protected string CAPABILITY = 'manage_options'
Tags
since
5.8.0

DEFAULT_INPUT_CONTAINER

Default setting field input partial.

protected string DEFAULT_INPUT_CONTAINER = 'input-default.php'
Tags
since
5.11.0

DESCRIPTION_FIELD_CONTAINER

The settings field or section description paragraph element template.

protected string DESCRIPTION_FIELD_CONTAINER = 'setting-description.php'
Tags
since
5.8.0

FILE_NAME

The settings export file name.

protected string FILE_NAME = 'pfeifferNet-settings.json'
Tags
since
6.0.0

PAGE_SLUG

The settings menu slug.

protected string PAGE_SLUG = 'pnet-site-settings'
Tags
since
1.0.0

SETTINGS_CONTAINER

Settings page partial.

protected string SETTINGS_CONTAINER = 'form-settings.php'
Tags
since
1.0.0

Properties

$current_settings

The array of settings values currently stored.

protected array<string|int, mixed> $current_settings = array()
Tags
since
5.8.0

$default_input

The fully qualified path to the default setting field input partial.

protected string $default_input = ''
Tags
since
5.11.0

$description_field

The fully qualified path to the setting section or field description partial.

protected string $description_field = ''
Tags
since
5.11.0

$fields

The array of settings input fields and their definition.

protected array<string|int, mixed> $fields = array()
  • Note that $uid is the key to the value in the settings array.

{ Array of settings input fields, keyed by field uid. @type array $uid { The field settings. Always use underscores in key name. @type string $label Field label on settings page. @type string $section Section field belongs in. @type string $type Input field type. @type string $class CSS class to apply to field. @type string $placeholder Field placeholder tag text. @type string $title Field title tag text. @type string $description Field description text. } }

Tags
since
5.5.0
since
5.11.0

Changed to an associative array.

see
Settings::set_fields()

$instances

The array of instances of this class and sub-classes.

protected static array<string|int, object> $instances = array()
Tags
since
5.11.0

$page_hook

The page hook.

protected string $page_hook = ''
Tags
since
1.0.0

$page_icon_url

The page icon url.

protected string $page_icon_url = ''
Tags
since
5.11.0

$page_title

The page title.

protected string $page_title = ''
Tags
since
6.0.0

$sections

The array of settings sections and their definition.

protected array<string|int, mixed> $sections = array()

{ Array of sections, keyed by section id. @type array $id { The section settings. @type callable $callback The section display callback. @type array $description Optional. The section description to display below title.. @type string $title The section title. } }

Tags
since
5.5.0
since
5.11.0

Changed to an associative array.

see
Settings::set_sections()

$settings_container

The fully qualified path to the settings page partial.

protected string $settings_container = ''
Tags
since
1.0.0

$settings_key

The plugin settings options key.

protected string $settings_key = ''
Tags
since
5.9.0

Methods

get_export()

Get the settings export file contents.

public get_export() : array<string|int, mixed>
Tags
since
6.0.0
uses
get_plugin_metadata()
Return values
array<string|int, mixed>

$export { The export file contents. @type string $filename The settings export filename. @type string $sitename The site that created the settings file. @type string $plugin The name of this plugin. @type string $version This plugin version. @type string $created The human-readable date/time stamp when the export file was created. This will be in the server timezone. @type string $timestamp The linux date/time stamp when the export file was created. @type array $settings An array of settings key/value pairs }

get_instance()

Creates and/or returns the instance of this class.

public static get_instance() : object
Tags
since
5.11.0
Return values
object

The instance of this class.

import()

Import settings.

public import(array<string|int, mixed> $import) : bool
Parameters
$import : array<string|int, mixed>

The import file contents.

Tags
since
6.0.0
uses
get_plugin_metadata()
see
Settings::get_export()
Return values
bool

True if settings imported, false if not.

render()

Render settings page.

public render() : void
  • Require the user to have appropriate permissions.
Tags
since
1.0.0
since
5.8.0

Added capability check.

render_field()

Settings field callback to render the field.

public render_field(array<string|int, mixed> $field) : void
Parameters
$field : array<string|int, mixed>

Array of field definition data.

Tags
since
1.0.0
see
Settings::$fields

render_section()

Settings section callback,

public render_section(array<string|int, mixed> $section) : void
  • Echo any content at the top of the section (between heading and fields).
Parameters
$section : array<string|int, mixed>

{ Array of data added to the callback. @type string $id The settings section id. @type string $title The settings section title. @type callable $callback The settings section callback. }

Tags
SuppressWarnings

(PHPMD.UnusedLocalVariable)

since
1.0.0
since
5.11.0

Uses sections associative array.

see
Settings::$sections

sanitise_settings()

Settings sanitisation callback.

public sanitise_settings(array<string|int, mixed> $input) : array<string|int, mixed>
  • Provide hook to allow other plugins to validate extended settings fields.
Parameters
$input : array<string|int, mixed>

Array of settings data from form or input file.

Tags
since
1.0.0
see
Settings::$set_fields()
Return values
array<string|int, mixed>

The sanitised settings data.

Return the site settings plugin action links.

public set_action_links(array<string|int, string> $actions, string $plugin_file, array<string|int, mixed> $plugin_data, string $context) : array<string|int, string>
  • This will only be called if the plugin is active.
Parameters
$actions : array<string|int, string>

Associative array of action names to anchor tags.

$plugin_file : string

Plugin file name, ie my-plugin/my-plugin.php.

$plugin_data : array<string|int, mixed>

Associative array of plugin data from the plugin file headers.

$context : string

Plugin status context, ie 'all', 'active', 'inactive', 'recently_active'.

Tags
since
1.0.0
link
https://developer.wordpress.org/reference/hooks/plugin_action_links_plugin_file/
Return values
array<string|int, string>

The modified array of plugin action links.

__construct()

Initialise the class.

protected __construct() : void
Tags
since
1.0.0
since
6.3.0

Removed reference to Date_Time class.

uses
Media_Selector
uses
get_plugin_metadata()

render_input_element()

Render the input field based on field type.

protected render_input_element(array<string|int, mixed> $field, string $field_name, string $field_id[, string $field_value = '' ]) : void
Parameters
$field : array<string|int, mixed>

Array of field definition data.

$field_name : string

The name of the field.

$field_id : string

The value for the field id attribute.

$field_value : string = ''

Optional. The current value of the field.

Tags
SuppressWarnings

(PHPMD.UnusedLocalVariable)

since
1.0.0
since
5.11.0

Refactor to simplify.

uses
Media_Selector::render()
see
Settings::add_settings_fields()

set_fields()

Initialise the fields array.

protected set_fields() : void
  • Populate the array of settings fields and their definition.
Tags
since
5.5.0
since
5.11.0

Changed to an associative array.

since
6.2.0

Simplification. Reduce size and complexity of set_fields(). Now uses smaller methods.

set_fields_contact_requests()

Set the email address for contact requests.

protected set_fields_contact_requests() : void
  • Populate the array of settings fields and their definition.
  • The array key is the field uid. This the name of the setting within the array of settings.
  • The sequence determines the order within the section.
Tags
since
6.2.0
see
Settings::$fields

set_fields_external_api()

Set the external API requirements.

protected set_fields_external_api() : void
  • Populate the array of settings fields and their definition.
  • The array key is the field uid. This the name of the setting within the array of settings.
  • The sequence determines the order within the section.
Tags
since
6.2.0
see
Settings::$fields

set_fields_general()

General site settings fields.

protected set_fields_general() : void
  • Populate the array of settings fields and their definition.
  • The array key is the field uid. This the name of the setting within the array of settings.
  • The sequence determines the order within the section.
Tags
since
6.7.0
see
Settings::$fields

set_fields_google_recaptcha()

Set the threshold and keys for the Google reCaptcha API.

protected set_fields_google_recaptcha() : void
  • Populate the array of settings fields and their definition.
  • The array key is the field uid. This the name of the setting within the array of settings.
  • The sequence determines the order within the section.
Tags
since
6.2.0
see
Settings::$fields

set_fields_images()

Set home and fallback images.

protected set_fields_images() : void
  • Populate the array of settings fields and their definition.
  • The array key is the field uid. This the name of the setting within the array of settings.
  • The sequence determines the order within the section.
Tags
since
6.2.0
see
Settings::$fields

set_fields_sharing()

Set the supported social sharing services.

protected set_fields_sharing() : void
  • Populate the array of settings fields and their definition.
  • The array key is the field uid. This the name of the setting within the array of settings.
  • The sequence determines the order within the section.
Tags
since
6.2.0
see
Settings::$fields

set_fields_smtp()

Set SMTP username, password and sender settings.

protected set_fields_smtp() : void
  • Populate the array of settings fields and their definition.
  • The array key is the field uid. This the name of the setting within the array of settings.
  • The sequence determines the order within the section.
Tags
since
6.2.0
see
Settings::$fields

set_fields_social_media()

Set the social media accounts.

protected set_fields_social_media() : void
  • Populate the array of settings fields and their definition.
  • The array key is the field uid. This the name of the setting within the array of settings.
  • The sequence determines the order within the section.
Tags
since
6.2.0
see
Settings::$fields

set_fields_users()

Set the user account settings.

protected set_fields_users() : void
  • Populate the array of settings fields and their definition.
  • The array key is the field uid. This the name of the setting within the array of settings.
  • The sequence determines the order within the section.
Tags
since
6.2.0
see
Settings::$fields

set_sections()

Initialise the sections array.

protected set_sections() : void
  • Populate the array of settings sections and their definition.
  • The array key is the section id.
  • The sequence determines the order down the settings page.
Tags
since
5.5.0
since
5.11.0

Changed to an associative array.

see
Settings::$sections

set_token_expiration_description()

Set the description for the token expiry field.

protected set_token_expiration_description(string $expiry_date) : string
Parameters
$expiry_date : string

The token expiry date and time.

Tags
since
6.3.0
Return values
string

The token expiry description text.

validate_github_token()

Validate the GitHub token by checking the expiry date.

protected validate_github_token(string $token) : array<string|int, mixed>
  • If token is unchanged, exit early.
  • If the token is valid, set the expiry date field.
  • If the token is invalid, clear both fields.
Parameters
$token : string

The GitHub token.

Tags
since
6.3.0
Return values
array<string|int, mixed>

The (maybe) modified GitHub token data. @type string $github_token The GitHub token. @type string $github_token_expiry The GitHub token expiry date.


        
On this page

Search results