mirror of
https://ghproxy.net/https://github.com/abhijitb/helix.git
synced 2025-08-28 02:52:20 +08:00
updated react app and API backend
1. Added API backend for settings 2. Added React components for the settings page 3. Added JS lint using @wordpress/scripts 4. Lint fixes for PHP
This commit is contained in:
parent
38067e490a
commit
e80316be89
36 changed files with 45313 additions and 1736 deletions
|
@ -127,8 +127,16 @@ add_action(
|
|||
return;
|
||||
}
|
||||
|
||||
// Check if we're already on the helix page or WordPress admin page.
|
||||
if ( in_array( $current_screen->id, array( 'toplevel_page_helix', 'toplevel_page_wordpress-admin' ), true ) ) {
|
||||
// Check if we're already on any Helix page or WordPress admin page.
|
||||
$helix_pages = array(
|
||||
'toplevel_page_helix',
|
||||
'toplevel_page_helix-posts',
|
||||
'toplevel_page_helix-users',
|
||||
'toplevel_page_helix-settings',
|
||||
'toplevel_page_wordpress-admin',
|
||||
);
|
||||
|
||||
if ( in_array( $current_screen->id, $helix_pages, true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -9,7 +9,19 @@
|
|||
add_action(
|
||||
'current_screen',
|
||||
function ( $current_screen ) {
|
||||
if ( ! $current_screen || 'toplevel_page_helix' !== $current_screen->id ) {
|
||||
if ( ! $current_screen ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if we're on any Helix admin page.
|
||||
$helix_pages = array(
|
||||
'toplevel_page_helix',
|
||||
'toplevel_page_helix-posts',
|
||||
'toplevel_page_helix-users',
|
||||
'toplevel_page_helix-settings',
|
||||
);
|
||||
|
||||
if ( ! in_array( $current_screen->id, $helix_pages, true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -27,27 +39,6 @@ add_action(
|
|||
|
||||
// Remove all admin menu separators.
|
||||
remove_all_admin_menu_separators();
|
||||
|
||||
// 2) Add custom Helix menu items.
|
||||
add_menu_page(
|
||||
'Helix Posts',
|
||||
'Posts',
|
||||
'edit_posts',
|
||||
'helix-posts',
|
||||
'helix_posts_callback',
|
||||
'dashicons-admin-post',
|
||||
3
|
||||
);
|
||||
|
||||
add_menu_page(
|
||||
'Helix Users',
|
||||
'Users',
|
||||
'list_users',
|
||||
'helix-users',
|
||||
'helix_users_callback',
|
||||
'dashicons-admin-users',
|
||||
4
|
||||
);
|
||||
},
|
||||
10
|
||||
);
|
||||
|
@ -78,7 +69,7 @@ add_action(
|
|||
|
||||
// Check if we're going to the Helix page by looking at the sanitized request.
|
||||
$page_requested = filter_input( INPUT_GET, 'page', FILTER_SANITIZE_URL );
|
||||
$is_helix_request = ( 'admin.php' === $pagenow && 'helix' === $page_requested );
|
||||
$is_helix_request = ( 'admin.php' === $pagenow && ( 'helix' === $page_requested || strpos( $page_requested, 'helix-' ) === 0 ) );
|
||||
$use_default_admin = get_option( 'helix_use_default_admin', false );
|
||||
|
||||
// Always show the WordPress Admin link when navigating to Helix, or when not in default admin mode.
|
||||
|
@ -88,6 +79,37 @@ add_action(
|
|||
update_option( 'helix_use_default_admin', false );
|
||||
}
|
||||
|
||||
// Add Helix menu items.
|
||||
add_menu_page(
|
||||
'Helix Posts',
|
||||
'Posts',
|
||||
'edit_posts',
|
||||
'helix-posts',
|
||||
'helix_posts_callback',
|
||||
'dashicons-admin-post',
|
||||
3
|
||||
);
|
||||
|
||||
add_menu_page(
|
||||
'Helix Users',
|
||||
'Users',
|
||||
'list_users',
|
||||
'helix-users',
|
||||
'helix_users_callback',
|
||||
'dashicons-admin-users',
|
||||
4
|
||||
);
|
||||
|
||||
add_menu_page(
|
||||
'Helix Settings',
|
||||
'Settings',
|
||||
'manage_options',
|
||||
'helix-settings',
|
||||
'helix_settings_callback',
|
||||
'dashicons-admin-settings',
|
||||
5
|
||||
);
|
||||
|
||||
// Add a menu item to go back to default WordPress admin.
|
||||
add_menu_page(
|
||||
'WordPress Admin', // Page title.
|
||||
|
@ -96,7 +118,7 @@ add_action(
|
|||
'wordpress-admin', // Menu slug.
|
||||
'wordpress_admin_callback', // Callback function.
|
||||
'dashicons-wordpress', // Icon.
|
||||
2 // Position (after main Helix menu).
|
||||
6 // Position (after Settings menu).
|
||||
);
|
||||
}
|
||||
},
|
||||
|
@ -142,3 +164,10 @@ function helix_posts_callback() {
|
|||
function helix_users_callback() {
|
||||
echo '<div id="helix-users-root"></div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function for Settings menu item.
|
||||
*/
|
||||
function helix_settings_callback() {
|
||||
echo '<div id="helix-settings-root"></div>';
|
||||
}
|
||||
|
|
|
@ -5,22 +5,259 @@
|
|||
* @package Helix
|
||||
*/
|
||||
|
||||
add_action(
|
||||
'rest_api_init',
|
||||
function () {
|
||||
register_rest_route(
|
||||
'helix/v1',
|
||||
'/settings',
|
||||
// Prevent direct access.
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
// Include settings API utilities.
|
||||
require_once plugin_dir_path( __FILE__ ) . 'settings-api.php';
|
||||
|
||||
add_action( 'rest_api_init', 'helix_register_rest_routes' );
|
||||
|
||||
/**
|
||||
* Register REST API routes for Helix.
|
||||
*
|
||||
* @since 1.0.0
|
||||
*/
|
||||
function helix_register_rest_routes() {
|
||||
// Settings endpoints.
|
||||
register_rest_route(
|
||||
'helix/v1',
|
||||
'/settings',
|
||||
array(
|
||||
array(
|
||||
'methods' => 'GET',
|
||||
'permission_callback' => '__return_true',
|
||||
'callback' => function () {
|
||||
return array(
|
||||
'siteTitle' => get_option( 'blogname' ),
|
||||
'language' => get_option( 'WPLANG' ),
|
||||
);
|
||||
},
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => 'helix_get_settings',
|
||||
'permission_callback' => 'helix_settings_permissions_check',
|
||||
'args' => helix_get_settings_schema(),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => 'helix_update_settings',
|
||||
'permission_callback' => 'helix_settings_permissions_check',
|
||||
'args' => helix_update_settings_schema(),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
// Individual setting endpoints.
|
||||
register_rest_route(
|
||||
'helix/v1',
|
||||
'/settings/(?P<setting>[a-zA-Z0-9_-]+)',
|
||||
array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => 'helix_get_single_setting',
|
||||
'permission_callback' => 'helix_settings_permissions_check',
|
||||
'args' => array(
|
||||
'setting' => array(
|
||||
'description' => __( 'Setting name to retrieve.', 'helix' ),
|
||||
'type' => 'string',
|
||||
'required' => true,
|
||||
),
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => 'helix_update_single_setting',
|
||||
'permission_callback' => 'helix_settings_permissions_check',
|
||||
'args' => array(
|
||||
'setting' => array(
|
||||
'description' => __( 'Setting name to update.', 'helix' ),
|
||||
'type' => 'string',
|
||||
'required' => true,
|
||||
),
|
||||
'value' => array(
|
||||
'description' => __( 'Setting value.', 'helix' ),
|
||||
'type' => array( 'string', 'boolean', 'integer', 'number' ),
|
||||
'required' => true,
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Permission callback for settings endpoints.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @return bool True if the request has read access for the item, false otherwise.
|
||||
*/
|
||||
function helix_settings_permissions_check() {
|
||||
return current_user_can( 'manage_options' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all WordPress settings.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
|
||||
*/
|
||||
function helix_get_settings() {
|
||||
$settings = helix_get_wordpress_settings();
|
||||
|
||||
if ( is_wp_error( $settings ) ) {
|
||||
return $settings;
|
||||
}
|
||||
|
||||
return rest_ensure_response( $settings );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update WordPress settings.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param WP_REST_Request $request Current request object.
|
||||
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
|
||||
*/
|
||||
function helix_update_settings( $request ) {
|
||||
$params = $request->get_json_params();
|
||||
|
||||
if ( empty( $params ) ) {
|
||||
return new WP_Error(
|
||||
'helix_no_settings_data',
|
||||
__( 'No settings data provided.', 'helix' ),
|
||||
array( 'status' => 400 )
|
||||
);
|
||||
}
|
||||
|
||||
$updated_settings = array();
|
||||
$errors = array();
|
||||
$allowed_settings = helix_get_allowed_settings();
|
||||
|
||||
foreach ( $params as $setting => $value ) {
|
||||
if ( ! in_array( $setting, $allowed_settings, true ) ) {
|
||||
$errors[ $setting ] = sprintf(
|
||||
/* translators: %s: Setting name */
|
||||
__( 'Setting "%s" is not allowed to be updated.', 'helix' ),
|
||||
$setting
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
$sanitized_value = helix_sanitize_setting_value( $setting, $value );
|
||||
|
||||
if ( is_wp_error( $sanitized_value ) ) {
|
||||
$errors[ $setting ] = $sanitized_value->get_error_message();
|
||||
continue;
|
||||
}
|
||||
|
||||
$option_name = helix_get_wp_option_name( $setting );
|
||||
$result = update_option( $option_name, $sanitized_value );
|
||||
|
||||
if ( $result ) {
|
||||
$updated_settings[ $setting ] = $sanitized_value;
|
||||
} else {
|
||||
$errors[ $setting ] = __( 'Failed to update setting.', 'helix' );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $errors ) && empty( $updated_settings ) ) {
|
||||
return new WP_Error(
|
||||
'helix_settings_update_failed',
|
||||
__( 'Failed to update any settings.', 'helix' ),
|
||||
array(
|
||||
'status' => 400,
|
||||
'errors' => $errors,
|
||||
)
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
$response_data = array(
|
||||
'updated' => $updated_settings,
|
||||
);
|
||||
|
||||
if ( ! empty( $errors ) ) {
|
||||
$response_data['errors'] = $errors;
|
||||
}
|
||||
|
||||
return rest_ensure_response( $response_data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single setting value.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param WP_REST_Request $request Current request object.
|
||||
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
|
||||
*/
|
||||
function helix_get_single_setting( $request ) {
|
||||
$setting = $request->get_param( 'setting' );
|
||||
|
||||
if ( ! helix_is_setting_allowed( $setting ) ) {
|
||||
return new WP_Error(
|
||||
'helix_setting_not_allowed',
|
||||
sprintf(
|
||||
/* translators: %s: Setting name */
|
||||
__( 'Setting "%s" is not allowed.', 'helix' ),
|
||||
$setting
|
||||
),
|
||||
array( 'status' => 403 )
|
||||
);
|
||||
}
|
||||
|
||||
$option_name = helix_get_wp_option_name( $setting );
|
||||
$value = get_option( $option_name );
|
||||
|
||||
return rest_ensure_response(
|
||||
array(
|
||||
'setting' => $setting,
|
||||
'value' => $value,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a single setting value.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param WP_REST_Request $request Current request object.
|
||||
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
|
||||
*/
|
||||
function helix_update_single_setting( $request ) {
|
||||
$setting = $request->get_param( 'setting' );
|
||||
$value = $request->get_param( 'value' );
|
||||
|
||||
if ( ! helix_is_setting_allowed( $setting ) ) {
|
||||
return new WP_Error(
|
||||
'helix_setting_not_allowed',
|
||||
sprintf(
|
||||
/* translators: %s: Setting name */
|
||||
__( 'Setting "%s" is not allowed to be updated.', 'helix' ),
|
||||
$setting
|
||||
),
|
||||
array( 'status' => 403 )
|
||||
);
|
||||
}
|
||||
|
||||
$sanitized_value = helix_sanitize_setting_value( $setting, $value );
|
||||
|
||||
if ( is_wp_error( $sanitized_value ) ) {
|
||||
return $sanitized_value;
|
||||
}
|
||||
|
||||
$option_name = helix_get_wp_option_name( $setting );
|
||||
$result = update_option( $option_name, $sanitized_value );
|
||||
|
||||
if ( ! $result && get_option( $option_name ) !== $sanitized_value ) {
|
||||
return new WP_Error(
|
||||
'helix_setting_update_failed',
|
||||
sprintf(
|
||||
/* translators: %s: Setting name */
|
||||
__( 'Failed to update setting "%s".', 'helix' ),
|
||||
$setting
|
||||
),
|
||||
array( 'status' => 500 )
|
||||
);
|
||||
}
|
||||
|
||||
return rest_ensure_response(
|
||||
array(
|
||||
'setting' => $setting,
|
||||
'value' => $sanitized_value,
|
||||
'updated' => true,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
493
admin/settings-api.php
Normal file
493
admin/settings-api.php
Normal file
|
@ -0,0 +1,493 @@
|
|||
<?php
|
||||
/**
|
||||
* Settings API utilities and helper functions.
|
||||
*
|
||||
* @package Helix
|
||||
*/
|
||||
|
||||
// Prevent direct access.
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the schema for settings GET requests.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @return array Schema array.
|
||||
*/
|
||||
function helix_get_settings_schema() {
|
||||
return array(
|
||||
'context' => array(
|
||||
'description' => __( 'Scope under which the request is made; determines fields present in response.', 'helix' ),
|
||||
'type' => 'string',
|
||||
'enum' => array( 'view', 'edit' ),
|
||||
'default' => 'view',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the schema for settings UPDATE requests.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @return array Schema array.
|
||||
*/
|
||||
function helix_update_settings_schema() {
|
||||
$settings_config = helix_get_settings_config();
|
||||
$schema = array();
|
||||
|
||||
foreach ( $settings_config as $category => $settings ) {
|
||||
foreach ( $settings as $setting_key => $setting_config ) {
|
||||
$schema[ $setting_key ] = array(
|
||||
'description' => $setting_config['description'],
|
||||
'type' => $setting_config['type'],
|
||||
);
|
||||
|
||||
if ( isset( $setting_config['enum'] ) ) {
|
||||
$schema[ $setting_key ]['enum'] = $setting_config['enum'];
|
||||
}
|
||||
|
||||
if ( isset( $setting_config['default'] ) ) {
|
||||
$schema[ $setting_key ]['default'] = $setting_config['default'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all WordPress settings in organized format.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @return array|WP_Error Array of settings or WP_Error on failure.
|
||||
*/
|
||||
function helix_get_wordpress_settings() {
|
||||
$settings_config = helix_get_settings_config();
|
||||
$settings = array();
|
||||
|
||||
foreach ( $settings_config as $category => $category_settings ) {
|
||||
$settings[ $category ] = array();
|
||||
|
||||
foreach ( $category_settings as $setting_key => $setting_config ) {
|
||||
$option_name = helix_get_wp_option_name( $setting_key );
|
||||
$value = get_option( $option_name, $setting_config['default'] ?? null );
|
||||
|
||||
// Apply formatting if specified.
|
||||
if ( isset( $setting_config['format_callback'] ) && is_callable( $setting_config['format_callback'] ) ) {
|
||||
$value = call_user_func( $setting_config['format_callback'], $value );
|
||||
}
|
||||
|
||||
$settings[ $category ][ $setting_key ] = array(
|
||||
'value' => $value,
|
||||
'label' => $setting_config['label'],
|
||||
'description' => $setting_config['description'],
|
||||
'type' => $setting_config['type'],
|
||||
);
|
||||
|
||||
// Add additional properties if they exist.
|
||||
if ( isset( $setting_config['enum'] ) ) {
|
||||
$settings[ $category ][ $setting_key ]['options'] = $setting_config['enum'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of allowed settings that can be updated.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @return array Array of allowed setting keys.
|
||||
*/
|
||||
function helix_get_allowed_settings() {
|
||||
$settings_config = helix_get_settings_config();
|
||||
$allowed_settings = array();
|
||||
|
||||
foreach ( $settings_config as $category => $settings ) {
|
||||
$allowed_settings = array_merge( $allowed_settings, array_keys( $settings ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the list of allowed settings.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param array $allowed_settings Array of allowed setting keys.
|
||||
*/
|
||||
return apply_filters( 'helix_allowed_settings', $allowed_settings );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a setting is allowed to be accessed/updated.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param string $setting Setting key to check.
|
||||
* @return bool True if allowed, false otherwise.
|
||||
*/
|
||||
function helix_is_setting_allowed( $setting ) {
|
||||
$allowed_settings = helix_get_allowed_settings();
|
||||
return in_array( $setting, $allowed_settings, true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get WordPress option name from setting key.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param string $setting Setting key.
|
||||
* @return string WordPress option name.
|
||||
*/
|
||||
function helix_get_wp_option_name( $setting ) {
|
||||
$option_mapping = helix_get_option_mapping();
|
||||
return $option_mapping[ $setting ] ?? $setting;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get mapping between setting keys and WordPress option names.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @return array Mapping array.
|
||||
*/
|
||||
function helix_get_option_mapping() {
|
||||
return array(
|
||||
'siteTitle' => 'blogname',
|
||||
'tagline' => 'blogdescription',
|
||||
'siteUrl' => 'siteurl',
|
||||
'homeUrl' => 'home',
|
||||
'adminEmail' => 'admin_email',
|
||||
'language' => 'WPLANG',
|
||||
'timezone' => 'timezone_string',
|
||||
'dateFormat' => 'date_format',
|
||||
'timeFormat' => 'time_format',
|
||||
'startOfWeek' => 'start_of_week',
|
||||
'postsPerPage' => 'posts_per_page',
|
||||
'showOnFront' => 'show_on_front',
|
||||
'pageOnFront' => 'page_on_front',
|
||||
'pageForPosts' => 'page_for_posts',
|
||||
'defaultCategory' => 'default_category',
|
||||
'defaultPostFormat' => 'default_post_format',
|
||||
'useSmilies' => 'use_smilies',
|
||||
'defaultCommentStatus' => 'default_comment_status',
|
||||
'defaultPingStatus' => 'default_ping_status',
|
||||
'siteLogo' => 'site_logo',
|
||||
'siteIcon' => 'site_icon',
|
||||
'thumbnailSizeW' => 'thumbnail_size_w',
|
||||
'thumbnailSizeH' => 'thumbnail_size_h',
|
||||
'mediumSizeW' => 'medium_size_w',
|
||||
'mediumSizeH' => 'medium_size_h',
|
||||
'largeSizeW' => 'large_size_w',
|
||||
'largeSizeH' => 'large_size_h',
|
||||
'uploadsUseYearmonthFolders' => 'uploads_use_yearmonth_folders',
|
||||
'usersCanRegister' => 'users_can_register',
|
||||
'defaultRole' => 'default_role',
|
||||
'blogPublic' => 'blog_public',
|
||||
'helixUseDefaultAdmin' => 'helix_use_default_admin',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize setting value based on setting type.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param string $setting Setting key.
|
||||
* @param mixed $value Value to sanitize.
|
||||
* @return mixed|WP_Error Sanitized value or WP_Error on failure.
|
||||
*/
|
||||
function helix_sanitize_setting_value( $setting, $value ) {
|
||||
$settings_config = helix_get_settings_config();
|
||||
$setting_config = null;
|
||||
|
||||
// Find the setting configuration.
|
||||
foreach ( $settings_config as $category => $settings ) {
|
||||
if ( isset( $settings[ $setting ] ) ) {
|
||||
$setting_config = $settings[ $setting ];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! $setting_config ) {
|
||||
return new WP_Error(
|
||||
'helix_invalid_setting',
|
||||
sprintf(
|
||||
/* translators: %s: Setting name */
|
||||
__( 'Invalid setting: %s', 'helix' ),
|
||||
$setting
|
||||
),
|
||||
array( 'status' => 400 )
|
||||
);
|
||||
}
|
||||
|
||||
// Apply custom sanitization if available.
|
||||
if ( isset( $setting_config['sanitize_callback'] ) && is_callable( $setting_config['sanitize_callback'] ) ) {
|
||||
return call_user_func( $setting_config['sanitize_callback'], $value );
|
||||
}
|
||||
|
||||
// Default sanitization based on type.
|
||||
switch ( $setting_config['type'] ) {
|
||||
case 'string':
|
||||
return sanitize_text_field( $value );
|
||||
|
||||
case 'email':
|
||||
$sanitized = sanitize_email( $value );
|
||||
if ( ! is_email( $sanitized ) ) {
|
||||
return new WP_Error(
|
||||
'helix_invalid_email',
|
||||
__( 'Invalid email address.', 'helix' ),
|
||||
array( 'status' => 400 )
|
||||
);
|
||||
}
|
||||
return $sanitized;
|
||||
|
||||
case 'url':
|
||||
return esc_url_raw( $value );
|
||||
|
||||
case 'integer':
|
||||
return absint( $value );
|
||||
|
||||
case 'number':
|
||||
return floatval( $value );
|
||||
|
||||
case 'boolean':
|
||||
return rest_sanitize_boolean( $value );
|
||||
|
||||
default:
|
||||
// For enum types, validate against allowed values.
|
||||
if ( isset( $setting_config['enum'] ) ) {
|
||||
if ( ! in_array( $value, $setting_config['enum'], true ) ) {
|
||||
return new WP_Error(
|
||||
'helix_invalid_enum_value',
|
||||
sprintf(
|
||||
/* translators: 1: Setting name, 2: Allowed values */
|
||||
__( 'Invalid value for %1$s. Allowed values: %2$s', 'helix' ),
|
||||
$setting,
|
||||
implode( ', ', $setting_config['enum'] )
|
||||
),
|
||||
array( 'status' => 400 )
|
||||
);
|
||||
}
|
||||
}
|
||||
return sanitize_text_field( $value );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get comprehensive settings configuration.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @return array Settings configuration array.
|
||||
*/
|
||||
function helix_get_settings_config() {
|
||||
return array(
|
||||
'site_information' => array(
|
||||
'siteTitle' => array(
|
||||
'label' => __( 'Site Title', 'helix' ),
|
||||
'description' => __( 'In a few words, explain what this site is about.', 'helix' ),
|
||||
'type' => 'string',
|
||||
'default' => get_bloginfo( 'name' ),
|
||||
),
|
||||
'tagline' => array(
|
||||
'label' => __( 'Tagline', 'helix' ),
|
||||
'description' => __( 'In a few words, explain what this site is about.', 'helix' ),
|
||||
'type' => 'string',
|
||||
'default' => get_bloginfo( 'description' ),
|
||||
),
|
||||
'siteUrl' => array(
|
||||
'label' => __( 'WordPress Address (URL)', 'helix' ),
|
||||
'description' => __( 'The address of your WordPress core files.', 'helix' ),
|
||||
'type' => 'url',
|
||||
'default' => site_url(),
|
||||
),
|
||||
'homeUrl' => array(
|
||||
'label' => __( 'Site Address (URL)', 'helix' ),
|
||||
'description' => __( 'The address you want people to type in their browser to reach your website.', 'helix' ),
|
||||
'type' => 'url',
|
||||
'default' => home_url(),
|
||||
),
|
||||
'adminEmail' => array(
|
||||
'label' => __( 'Administration Email Address', 'helix' ),
|
||||
'description' => __( 'This address is used for admin purposes.', 'helix' ),
|
||||
'type' => 'email',
|
||||
'default' => get_option( 'admin_email' ),
|
||||
),
|
||||
'language' => array(
|
||||
'label' => __( 'Site Language', 'helix' ),
|
||||
'description' => __( 'The language for your site.', 'helix' ),
|
||||
'type' => 'string',
|
||||
'default' => get_locale(),
|
||||
),
|
||||
'timezone' => array(
|
||||
'label' => __( 'Timezone', 'helix' ),
|
||||
'description' => __( 'Choose either a city in the same timezone as you or a UTC timezone offset.', 'helix' ),
|
||||
'type' => 'string',
|
||||
'default' => get_option( 'timezone_string', 'UTC' ),
|
||||
),
|
||||
),
|
||||
'content_reading' => array(
|
||||
'showOnFront' => array(
|
||||
'label' => __( 'Your homepage displays', 'helix' ),
|
||||
'description' => __( 'What to show on the front page.', 'helix' ),
|
||||
'type' => 'string',
|
||||
'enum' => array( 'posts', 'page' ),
|
||||
'default' => 'posts',
|
||||
),
|
||||
'pageOnFront' => array(
|
||||
'label' => __( 'Homepage', 'helix' ),
|
||||
'description' => __( 'The page to show on the front page.', 'helix' ),
|
||||
'type' => 'integer',
|
||||
'default' => 0,
|
||||
),
|
||||
'pageForPosts' => array(
|
||||
'label' => __( 'Posts page', 'helix' ),
|
||||
'description' => __( 'The page to show posts.', 'helix' ),
|
||||
'type' => 'integer',
|
||||
'default' => 0,
|
||||
),
|
||||
'postsPerPage' => array(
|
||||
'label' => __( 'Blog pages show at most', 'helix' ),
|
||||
'description' => __( 'Number of posts to show per page.', 'helix' ),
|
||||
'type' => 'integer',
|
||||
'default' => 10,
|
||||
),
|
||||
'blogPublic' => array(
|
||||
'label' => __( 'Search engine visibility', 'helix' ),
|
||||
'description' => __( 'Discourage search engines from indexing this site.', 'helix' ),
|
||||
'type' => 'boolean',
|
||||
'default' => true,
|
||||
),
|
||||
'dateFormat' => array(
|
||||
'label' => __( 'Date Format', 'helix' ),
|
||||
'description' => __( 'Format for displaying dates.', 'helix' ),
|
||||
'type' => 'string',
|
||||
'default' => get_option( 'date_format' ),
|
||||
),
|
||||
'timeFormat' => array(
|
||||
'label' => __( 'Time Format', 'helix' ),
|
||||
'description' => __( 'Format for displaying times.', 'helix' ),
|
||||
'type' => 'string',
|
||||
'default' => get_option( 'time_format' ),
|
||||
),
|
||||
'startOfWeek' => array(
|
||||
'label' => __( 'Week Starts On', 'helix' ),
|
||||
'description' => __( 'The day of the week the calendar should start on.', 'helix' ),
|
||||
'type' => 'integer',
|
||||
'enum' => array( 0, 1, 2, 3, 4, 5, 6 ),
|
||||
'default' => 1,
|
||||
),
|
||||
),
|
||||
'writing_publishing' => array(
|
||||
'defaultCategory' => array(
|
||||
'label' => __( 'Default Post Category', 'helix' ),
|
||||
'description' => __( 'The default category for new posts.', 'helix' ),
|
||||
'type' => 'integer',
|
||||
'default' => 1,
|
||||
),
|
||||
'defaultPostFormat' => array(
|
||||
'label' => __( 'Default Post Format', 'helix' ),
|
||||
'description' => __( 'The default format for new posts.', 'helix' ),
|
||||
'type' => 'string',
|
||||
'enum' => array( 'standard', 'aside', 'gallery', 'image', 'link', 'quote', 'status', 'video', 'audio', 'chat' ),
|
||||
'default' => 'standard',
|
||||
),
|
||||
'useSmilies' => array(
|
||||
'label' => __( 'Convert emoticons', 'helix' ),
|
||||
'description' => __( 'Convert emoticons like :-) and :-P to graphics on display.', 'helix' ),
|
||||
'type' => 'boolean',
|
||||
'default' => true,
|
||||
),
|
||||
'defaultCommentStatus' => array(
|
||||
'label' => __( 'Default comment status', 'helix' ),
|
||||
'description' => __( 'Allow people to submit comments on new posts.', 'helix' ),
|
||||
'type' => 'string',
|
||||
'enum' => array( 'open', 'closed' ),
|
||||
'default' => 'open',
|
||||
),
|
||||
'defaultPingStatus' => array(
|
||||
'label' => __( 'Default ping status', 'helix' ),
|
||||
'description' => __( 'Allow link notifications from other blogs (pingbacks and trackbacks) on new posts.', 'helix' ),
|
||||
'type' => 'string',
|
||||
'enum' => array( 'open', 'closed' ),
|
||||
'default' => 'open',
|
||||
),
|
||||
),
|
||||
'media_assets' => array(
|
||||
'siteLogo' => array(
|
||||
'label' => __( 'Site Logo', 'helix' ),
|
||||
'description' => __( 'The site logo.', 'helix' ),
|
||||
'type' => 'integer',
|
||||
'default' => 0,
|
||||
),
|
||||
'siteIcon' => array(
|
||||
'label' => __( 'Site Icon', 'helix' ),
|
||||
'description' => __( 'The site icon (favicon).', 'helix' ),
|
||||
'type' => 'integer',
|
||||
'default' => 0,
|
||||
),
|
||||
'thumbnailSizeW' => array(
|
||||
'label' => __( 'Thumbnail Width', 'helix' ),
|
||||
'description' => __( 'Maximum width of thumbnail images.', 'helix' ),
|
||||
'type' => 'integer',
|
||||
'default' => 150,
|
||||
),
|
||||
'thumbnailSizeH' => array(
|
||||
'label' => __( 'Thumbnail Height', 'helix' ),
|
||||
'description' => __( 'Maximum height of thumbnail images.', 'helix' ),
|
||||
'type' => 'integer',
|
||||
'default' => 150,
|
||||
),
|
||||
'mediumSizeW' => array(
|
||||
'label' => __( 'Medium Width', 'helix' ),
|
||||
'description' => __( 'Maximum width of medium-sized images.', 'helix' ),
|
||||
'type' => 'integer',
|
||||
'default' => 300,
|
||||
),
|
||||
'mediumSizeH' => array(
|
||||
'label' => __( 'Medium Height', 'helix' ),
|
||||
'description' => __( 'Maximum height of medium-sized images.', 'helix' ),
|
||||
'type' => 'integer',
|
||||
'default' => 300,
|
||||
),
|
||||
'largeSizeW' => array(
|
||||
'label' => __( 'Large Width', 'helix' ),
|
||||
'description' => __( 'Maximum width of large-sized images.', 'helix' ),
|
||||
'type' => 'integer',
|
||||
'default' => 1024,
|
||||
),
|
||||
'largeSizeH' => array(
|
||||
'label' => __( 'Large Height', 'helix' ),
|
||||
'description' => __( 'Maximum height of large-sized images.', 'helix' ),
|
||||
'type' => 'integer',
|
||||
'default' => 1024,
|
||||
),
|
||||
'uploadsUseYearmonthFolders' => array(
|
||||
'label' => __( 'Organize uploads into date-based folders', 'helix' ),
|
||||
'description' => __( 'Organize my uploads into month- and year-based folders.', 'helix' ),
|
||||
'type' => 'boolean',
|
||||
'default' => true,
|
||||
),
|
||||
),
|
||||
'users_membership' => array(
|
||||
'usersCanRegister' => array(
|
||||
'label' => __( 'Anyone can register', 'helix' ),
|
||||
'description' => __( 'Allow anyone to register as a user.', 'helix' ),
|
||||
'type' => 'boolean',
|
||||
'default' => false,
|
||||
),
|
||||
'defaultRole' => array(
|
||||
'label' => __( 'New User Default Role', 'helix' ),
|
||||
'description' => __( 'The default role for new users.', 'helix' ),
|
||||
'type' => 'string',
|
||||
'enum' => array( 'subscriber', 'contributor', 'author', 'editor', 'administrator' ),
|
||||
'default' => 'subscriber',
|
||||
),
|
||||
),
|
||||
'helix_specific' => array(
|
||||
'helixUseDefaultAdmin' => array(
|
||||
'label' => __( 'Use Default WordPress Admin', 'helix' ),
|
||||
'description' => __( 'Use the default WordPress admin interface instead of Helix.', 'helix' ),
|
||||
'type' => 'boolean',
|
||||
'default' => false,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue