feat(admin): rebuild page display settings
This commit is contained in:
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
/**
|
||||
* Lightweight WordPress stubs for the page/display schema save contract.
|
||||
*/
|
||||
|
||||
namespace {
|
||||
define('ABSPATH', __DIR__);
|
||||
$GLOBALS['aksrt_test_options'] = [];
|
||||
$GLOBALS['aksrt_test_manage_options'] = false;
|
||||
|
||||
function sanitize_key($value): string {
|
||||
return preg_replace('/[^a-z0-9_\-]/', '', strtolower((string) $value));
|
||||
}
|
||||
|
||||
function sanitize_text_field($value): string {
|
||||
return trim(strip_tags((string) $value));
|
||||
}
|
||||
|
||||
function sanitize_textarea_field($value): string {
|
||||
return trim(strip_tags((string) $value));
|
||||
}
|
||||
|
||||
function wp_kses_post($value): string {
|
||||
$value = preg_replace('#<script\b[^>]*>.*?</script>#is', '', (string) $value);
|
||||
return strip_tags($value, '<a><b><strong><em><i><span><div><p><br><badge>');
|
||||
}
|
||||
|
||||
function esc_url_raw($value): string {
|
||||
$value = trim((string) $value);
|
||||
if ($value === '') {
|
||||
return '';
|
||||
}
|
||||
$scheme = strtolower((string) parse_url($value, PHP_URL_SCHEME));
|
||||
return in_array($scheme, ['http', 'https', 'mailto'], true) ? $value : '';
|
||||
}
|
||||
|
||||
function wp_unslash($value) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
function trailingslashit($value): string {
|
||||
return rtrim((string) $value, '/\\') . '/';
|
||||
}
|
||||
|
||||
function get_template_directory_uri(): string {
|
||||
return 'https://example.test/wp-content/themes/aksrt';
|
||||
}
|
||||
|
||||
function home_url($path = ''): string {
|
||||
return 'https://example.test' . ($path ? '/' . ltrim((string) $path, '/') : '');
|
||||
}
|
||||
|
||||
function get_bloginfo($show = ''): string {
|
||||
return $show === 'title' ? 'AKSRT' : 'AKSRT';
|
||||
}
|
||||
|
||||
function get_option($key, $default = false) {
|
||||
return array_key_exists($key, $GLOBALS['aksrt_test_options']) ? $GLOBALS['aksrt_test_options'][$key] : $default;
|
||||
}
|
||||
|
||||
function update_option($key, $value): bool {
|
||||
$GLOBALS['aksrt_test_options'][$key] = $value;
|
||||
return true;
|
||||
}
|
||||
|
||||
function current_user_can($capability): bool {
|
||||
return $capability === 'manage_options' && !empty($GLOBALS['aksrt_test_manage_options']);
|
||||
}
|
||||
}
|
||||
|
||||
namespace AKSRT\Settings {
|
||||
class Settings {
|
||||
public static function flush_settings_cache(): void {}
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
require_once dirname(__DIR__, 2) . '/src/Admin/Schema/SchemaPageDisplay.php';
|
||||
require_once dirname(__DIR__, 2) . '/src/Admin/SchemaProvider.php';
|
||||
require_once dirname(__DIR__, 2) . '/src/Admin/ControllerBase.php';
|
||||
|
||||
$controller = new class extends \AKSRT\Admin\ControllerBase {
|
||||
public function register_routes(): void {}
|
||||
};
|
||||
$provider = \AKSRT\Admin\SchemaProvider::get_instance();
|
||||
|
||||
$permission_denied = !$controller->check_permission();
|
||||
$GLOBALS['aksrt_test_manage_options'] = true;
|
||||
$permission_allowed = $controller->check_permission();
|
||||
$unknown_section = $provider->get_section_schema('page_display', 'missing') === null;
|
||||
$homepage_defaults = $provider->get_defaults('page_display', 'homepage');
|
||||
$footer_defaults = $provider->get_defaults('page_display', 'footer');
|
||||
|
||||
$invalid = $provider->sanitize_payload('page_display', ['page_header_style' => '9'], 'other');
|
||||
$invalid_validation = $provider->validate('page_display', $invalid, 'other');
|
||||
|
||||
$header_payload = [
|
||||
'header_slider' => [[
|
||||
'background' => 'javascript:alert(1)',
|
||||
'unknown' => 'drop-me',
|
||||
'link' => ['url' => 'https://example.test/slide', 'target' => '_blank', 'text' => '<b>Go</b>', 'unknown' => 'drop-me'],
|
||||
'text' => ['title' => '<strong>Safe</strong><script>bad()</script>', 'desc' => '<em>Copy</em>', 'unknown' => 'drop-me'],
|
||||
]],
|
||||
];
|
||||
$header_clean = $provider->sanitize_payload('page_display', $header_payload, 'header-components');
|
||||
|
||||
$footer_payload = [
|
||||
'fcode_t1_code' => '<strong>Allowed</strong><script>bad()</script>',
|
||||
'footer_contact_weibo' => 'javascript:alert(1)',
|
||||
'footer_mini_img' => [[
|
||||
'text' => '<b>QR</b>',
|
||||
'image' => 'https://example.test/qr.png',
|
||||
'link' => ['url' => 'https://example.test/go', 'target' => '_blank', 'text' => 'Open', 'unknown' => 'drop-me'],
|
||||
'unknown' => 'drop-me',
|
||||
]],
|
||||
'unknown_top_level' => 'drop-me',
|
||||
];
|
||||
$footer_clean = $provider->sanitize_payload('page_display', $footer_payload, 'footer');
|
||||
$footer_validation = $provider->validate('page_display', $footer_clean, 'footer');
|
||||
$provider->write_settings('page_display', $footer_clean, 'footer');
|
||||
$footer_readback = $provider->read_settings('page_display', 'footer');
|
||||
|
||||
$category_payload = [
|
||||
'cat_orderby_option' => [
|
||||
'lists' => ['modified', 'views', 'like', 'comment_count'],
|
||||
'dropdown' => false,
|
||||
],
|
||||
];
|
||||
$category_clean = $provider->sanitize_payload('page_display', $category_payload, 'category');
|
||||
$category_validation = $provider->validate('page_display', $category_clean, 'category');
|
||||
|
||||
echo json_encode([
|
||||
'permission_denied' => $permission_denied,
|
||||
'permission_allowed' => $permission_allowed,
|
||||
'unknown_section' => $unknown_section,
|
||||
'homepage_defaults' => $homepage_defaults,
|
||||
'footer_defaults' => $footer_defaults,
|
||||
'invalid_enum_rejected' => !$invalid_validation['valid'],
|
||||
'nested_multiselect_valid' => $category_validation['valid'],
|
||||
'nested' => $header_clean,
|
||||
'footer_valid' => $footer_validation['valid'],
|
||||
'footer_clean' => $footer_clean,
|
||||
'footer_readback' => $footer_readback,
|
||||
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
Reference in New Issue
Block a user