69 lines
2.1 KiB
PHP
69 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace AKSRT\WidgetSystem;
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
/**
|
|
* Single bootstrap for the schema-driven widget runtime.
|
|
*/
|
|
final class Runtime {
|
|
private static bool $registered = false;
|
|
|
|
public static function init(): void {
|
|
if (self::$registered) {
|
|
return;
|
|
}
|
|
self::$registered = true;
|
|
|
|
add_action('widgets_init', [self::class, 'unregister_core_widgets'], 1);
|
|
add_action('widgets_init', [WidgetAreas::class, 'register'], 5);
|
|
add_action('widgets_init', [self::class, 'register_widgets'], 10);
|
|
add_action('save_post_page', [WidgetAreas::class, 'flush_page_cache']);
|
|
add_action('rest_api_init', static function (): void {
|
|
(new RestController())->register_routes();
|
|
});
|
|
add_action('wp_enqueue_scripts', [AssetManager::class, 'enqueue_active'], 20);
|
|
add_action('in_widget_form', [self::class, 'render_placement_hint'], 20, 1);
|
|
|
|
AdminFormController::register_hooks();
|
|
TransferController::register_hooks();
|
|
}
|
|
|
|
public static function unregister_core_widgets(): void {
|
|
foreach ([
|
|
'WP_Widget_RSS',
|
|
'WP_Widget_Recent_Posts',
|
|
'WP_Widget_Recent_Comments',
|
|
'WP_Widget_Pages',
|
|
'WP_Widget_Search',
|
|
'WP_Widget_Calendar',
|
|
] as $class) {
|
|
unregister_widget($class);
|
|
}
|
|
}
|
|
|
|
public static function register_widgets(): void {
|
|
foreach (WidgetRegistry::all() as $definition) {
|
|
register_widget(new SchemaWidget($definition));
|
|
}
|
|
}
|
|
|
|
public static function render_placement_hint(\WP_Widget $widget): void {
|
|
if (!$widget instanceof SchemaWidget) {
|
|
return;
|
|
}
|
|
$definition = $widget->definition();
|
|
$messages = [
|
|
'wide' => __('宽版模块:建议放入全宽或主内容区域;放入侧栏时会显示尺寸警告。', 'aksrt'),
|
|
'sidebar' => __('侧栏模块:建议放入侧栏区域;放入全宽区域时会显示尺寸警告。', 'aksrt'),
|
|
'any' => __('自适应模块:可放入主内容、全宽或侧栏区域。', 'aksrt'),
|
|
];
|
|
printf(
|
|
'<p class="aksrt-widget-placement-hint" data-widget-size="%1$s"><span class="dashicons dashicons-info-outline" aria-hidden="true"></span>%2$s</p>',
|
|
esc_attr($definition->size()),
|
|
esc_html($messages[$definition->size()] ?? $messages['any'])
|
|
);
|
|
}
|
|
}
|