wp-settings-framework/examples/settings-without-tabs.php
Owais ahmed 6507e672e7
Improved Settings Class Handling (#3)
feat(core)!: overhaul settings engine, fix data loss, and improve API

This major release resolves a critical data loss bug and refactors the
entire framework to be more robust, configurable, and developer-friendly.

BREAKING CHANGE: The `Settings` class constructor signature has changed.
It now requires `$optionName` and `$pageSlug` as the first two arguments.
Page titles and menu titles must now be passed via the `$options` array
to allow for better configuration.

Example before:
new Settings('My Page', 'my-menu', 'my-option', 'my-slug');

Example now:
new Settings('my-option', 'my-slug', [
    'pageTitle' => 'My Page',
    'menuTitle' => 'My Menu',
]);

---

### Key Fixes and Improvements:

*   **Critical Data Loss Resolved:** A bug that erased settings from
    inactive tabs during save has been fixed. The `Sanitizer` is now
    state-aware, merging new input with existing options to preserve all data.

*   **Reliable Data Retrieval:** The public `get()` method, which was
    previously non-functional, has been completely repaired. It now
    correctly fetches saved values from the database with proper fallbacks
    to field defaults.

*   **Enhanced Configuration & API:**
    - New fluent methods `setCapability()` and `setParentSlug()` have been added.
    - All text strings are now configurable via a `labels` array in the
      constructor, improving support for internationalization (i18n).
    - External asset libraries (like Select2) can now be configured.

*   **Internal Architecture:** These improvements were made possible by a
    complete internal refactor, which introduced a new `Config` class
    for state management and enforced proper dependency injection.
2025-08-13 01:18:34 +05:00

182 lines
6.6 KiB
PHP

<?php
declare(strict_types=1);
use WPTechnix\WPSettings\Settings;
// Please make sure you require the composer autoload.
// require_once plugin_dir_path(__FILE__) . '/vendor/autoload.php';
add_action('plugins_loaded', 'wptechnix_settings_demo_without_tabs');
/**
* Initializes the settings page demo without tabs.
*/
function wptechnix_settings_demo_without_tabs(): void
{
// 1. Create a new Settings instance.
$settings = new Settings(
'wptechnix_options_no_tabs', // Unique option name for the database
'wptechnix-demo-no-tabs', // Unique page slug
[
'pageTitle' => 'Settings Demo (No Tabs)', // Page Title
'menuTitle' => 'Settings Demo (No Tabs)', // Menu Title
'parentSlug' => 'tools.php' // Place this page under the "Tools" menu.
]
);
// 2. Add sections directly to the page (no tabs are needed).
$settings->addSection('text_inputs', 'Text-Based Inputs', 'Fields for text, numbers, and passwords.')
->addSection(
'choice_inputs',
'Choice-Based Inputs',
'Fields for selecting one or more options.'
)
->addSection('ui_inputs', 'Enhanced UI Inputs', 'Fields with special user interfaces.')
->addSection(
'advanced_inputs',
'Advanced & Special Inputs',
'Media, code, and other powerful fields.'
)
->addSection(
'conditional_section',
'Conditional Logic Demo',
'Show and hide fields based on other fields\' values.'
);
// 3. Add fields and assign them to the correct sections.
// --- FIELDS FOR "Text-Based Inputs" SECTION ---
$settings
->addField('demo_text', 'text_inputs', 'text', 'Text Field')
->addField('demo_email', 'text_inputs', 'email', 'Email Field')
->addField('demo_password', 'text_inputs', 'password', 'Password Field')
->addField('demo_number', 'text_inputs', 'number', 'Number Field', ['default' => 42])
->addField('demo_url', 'text_inputs', 'url', 'URL Field')
->addField('demo_textarea', 'text_inputs', 'textarea', 'Textarea Field');
// --- FIELDS FOR "Choice-Based Inputs" SECTION ---
$settings
->addField('demo_checkbox', 'choice_inputs', 'checkbox', 'Checkbox Field')
->addField('demo_toggle', 'choice_inputs', 'toggle', 'Toggle Switch', ['default' => true])
->addField(
'demo_select',
'choice_inputs',
'select',
'Select Dropdown',
['options' => ['a' => 'Option A', 'b' => 'Option B'], 'placeholder' => 'Select Option']
)
->addField(
'demo_multiselect',
'choice_inputs',
'multiselect',
'Multi-Select',
['options' => ['a' => 'Choice A', 'b' => 'Choice B', 'c' => 'Choice C'], 'placeholder' => 'Select Options']
)
->addField(
'demo_radio',
'choice_inputs',
'radio',
'Radio Buttons',
['options' => ['yes' => 'Yes', 'no' => 'No']]
)
->addField(
'demo_buttongroup',
'choice_inputs',
'buttongroup',
'Button Group',
['options' => ['left' => 'Left', 'center' => 'Center', 'right' => 'Right']]
);
// --- FIELDS FOR "Enhanced UI Inputs" SECTION ---
$settings
->addField('demo_color', 'ui_inputs', 'color', 'Color Picker')
->addField('demo_date', 'ui_inputs', 'date', 'Date Picker')
->addField('demo_datetime', 'ui_inputs', 'datetime', 'Date & Time Picker')
->addField('demo_time', 'ui_inputs', 'time', 'Time Picker')
->addField('demo_range', 'ui_inputs', 'range', 'Range Slider', ['default' => 75]);
// --- FIELDS FOR "Advanced & Special Inputs" SECTION ---
$settings
->addField('demo_media', 'advanced_inputs', 'media', 'Media Uploader')
->addField(
'demo_code_html',
'advanced_inputs',
'code',
'Code Editor (HTML)',
[
'description' => 'A code editor with HTML syntax highlighting.',
'language' => 'html',
]
)
->addField(
'demo_code_css',
'advanced_inputs',
'code',
'Code Editor (CSS)',
[
'description' => 'A code editor with CSS syntax highlighting.',
'language' => 'css',
]
)
->addField(
'demo_code_js',
'advanced_inputs',
'code',
'Code Editor (JS)',
[
'description' => 'A code editor with JavaScript syntax highlighting.',
'language' => 'javascript',
]
)
->addField(
'demo_description',
'advanced_inputs',
'description',
'Description Field',
['description' => 'This is a read-only field used to display important information. It supports <strong>HTML</strong>.']
);
// --- FIELDS FOR "Conditional Logic Demo" SECTION ---
$settings
->addField(
'demo_contact_method', // The CONTROLLING field
'conditional_section',
'radio',
'Preferred Contact Method',
[
'description' => 'Select a method to see different conditional fields appear.',
'options' => ['email' => 'Email', 'phone' => 'Phone Call', 'none' => 'No Contact'],
'default' => 'none',
]
)
->addField(
'demo_conditional_email', // A CONDITIONAL field
'conditional_section',
'email',
'Contact Email Address',
[
'description' => 'This only appears if "Email" is selected.',
'conditional' => [
'field' => 'demo_contact_method',
'value' => 'email',
],
]
)
->addField(
'demo_conditional_phone', // Another CONDITIONAL field
'conditional_section',
'text',
'Contact Phone Number',
[
'description' => 'This only appears if "Phone Call" is selected.',
'conditional' => [
'field' => 'demo_contact_method',
'value' => 'phone',
],
]
);
// 4. Initialize the settings page.
$settings->init();
}