dream-encode/de-wordpress-plugin-utils
最新稳定版本:v1.9.0
Composer 安装命令:
composer require dream-encode/de-wordpress-plugin-utils
包简介
Reusable WordPress plugin utilities including upgrader and logger abstracts.
README 文档
README
Reusable WordPress plugin utilities including abstracts for plugin bootstrap, lifecycle, settings, REST API, logging, background processing, and data migrations.
Installation
composer require dream-encode/de-wordpress-plugin-utils
Requirements
- PHP >= 8.2
- WordPress
- WooCommerce/Action Scheduler (via
woocommerce/action-scheduler)
Provided Abstracts
Abstract_Plugin
Dream_Encode\WordPress_Plugin_Utils\Abstracts\Abstract_Plugin
Main plugin bootstrap template. Constructs the loader, loads dependencies, sets locale, and wires admin/public hooks.
Must implement:
get_plugin_slug(): string— returns the plugin slug used as$plugin_name.get_version_constant(): string— returns the PHP constant name that holds the plugin version.
Optional overrides:
get_default_version(): string— fallback version string when the constant is not yet defined (default'1.0.0').create_loader(): Plugin_Loader— returns the loader instance; defaults tonew Plugin_Loader().load_dependencies(): void— require additional files/classes.set_locale(): void— instantiate and register the i18n loader.define_admin_hooks(): void— register wp-admin actions and filters.define_public_hooks(): void— register front-end actions and filters.
Public API:
run(): void— fires all registered hooks via the loader.get_plugin_name(): stringget_version(): stringget_loader(): Plugin_Loader
Plugin_Loader
Dream_Encode\WordPress_Plugin_Utils\Loader\Plugin_Loader
Concrete class. Stores and bulk-registers WordPress actions and filters. Can be used directly or subclassed for custom hook-registration behaviour.
Public API:
add_action( $hook, $component, $callback, $priority, $accepted_args ): voidadd_filter( $hook, $component, $callback, $priority, $accepted_args ): voidrun(): void— callsadd_action/add_filterfor every registered hook.
Abstract_Plugin_Activator
Dream_Encode\WordPress_Plugin_Utils\Abstracts\Abstract_Plugin_Activator
Consistent entry point for register_activation_hook.
Static entry point: activate(): void
Optional overrides (all no-ops by default):
before_activate(): voidrun_install(): voidafter_activate(): void
Abstract_Plugin_Deactivator
Dream_Encode\WordPress_Plugin_Utils\Abstracts\Abstract_Plugin_Deactivator
Consistent entry point for register_deactivation_hook.
Static entry point: deactivate(): void
Optional overrides (all no-ops by default):
before_deactivate(): voiddelete_options(): voidafter_deactivate(): void
Abstract_Plugin_I18n
Dream_Encode\WordPress_Plugin_Utils\Abstracts\Abstract_Plugin_I18n
Loads plugin translations via load_plugin_textdomain.
Must implement:
get_text_domain(): stringget_languages_path(): string
Public API:
load_plugin_textdomain(): bool
Abstract_Plugin_Upgrader
Dream_Encode\WordPress_Plugin_Utils\Abstracts\Abstract_Plugin_Upgrader
Handles plugin upgrade/install logic with cache-clearing for Redis/object cache environments. Integrates with Action Scheduler for deferred tasks.
Asset_Manager
Dream_Encode\WordPress_Plugin_Utils\Assets\Asset_Manager
Concrete class. Screen-based stylesheet and script enqueuer for both admin and front-end contexts. All configuration is passed via constructor — no subclassing required for common usage.
Constructor (all params after $plugin_version are optional):
new Asset_Manager( handle_prefix: 'my-plugin-', plugin_path: MY_PLUGIN_PATH, plugin_url: MY_PLUGIN_URL, plugin_version: MY_PLUGIN_VERSION, screens_to_assets: [ 'edit-post' => [ ['name' => 'editor'] ] ], localization_global: 'MY_PLUGIN', screens_localization_data: [ 'edit-post' => [ 'REST_URL' => '...' ] ], asset_subdir: 'admin/', // default '' asset_file_prefix: 'admin-', // default '' );
Public API:
add_screens( array $screens ): void— merge additional screen entries at runtime.add_screens_localization_data( array $data ): void— merge additional localization entries.enqueue_styles(): voidenqueue_scripts(): voidcurrent_screen_assets(): array— asset definitions for the current screen.current_screen_has_assets(): intscreen_assets( WP_Screen $screen ): arrayscreen_has_assets( WP_Screen $screen ): intscreen_get_localized_data( WP_Screen $screen ): array
Abstract_REST_API
Dream_Encode\WordPress_Plugin_Utils\Abstracts\Abstract_REST_API
Base class for registering a REST API namespace and routes.
Abstract_REST_Controller
Dream_Encode\WordPress_Plugin_Utils\Abstracts\Abstract_REST_Controller
Extends WP_REST_Controller with a reusable pattern for REST endpoint controllers.
Must implement:
check_user_permission(): bool
REST_Authentication
Dream_Encode\WordPress_Plugin_Utils\RestApi\REST_Authentication
Handles WP REST API authentication, including cookie auth error checking, app password authentication, and permission helpers. Auto-registers the rest_authentication_errors filter when the file is loaded.
Static permission helpers:
check_logged_in_permission(): boolcheck_admin_permission(): boolcheck_editor_permission(): boolcheck_user_permission(): boolcheck_woocommerce_shop_manager_permission(): boolcheck_post_permissions( $post_type, $permission, $user_id ): bool
Static accessors:
get_wp_rest_nonce(): stringget_wp_user_id(): int
REST_Response
Dream_Encode\WordPress_Plugin_Utils\RestApi\REST_Response
Simple value object for REST API responses.
Properties:
$status—string, defaults to'error'.$message—string, defaults to''.$data—mixed, initialised to a freshstdClass.$success—bool|null, defaults tonull.
Object_Data
Dream_Encode\WordPress_Plugin_Utils\Data\Object_Data
Concrete class. WooCommerce-style data object. Tracks a $data array, a $changes diff, and optional $extra_data. Can be used directly or subclassed to declare typed $data/$extra_data defaults and domain-specific getters/setters.
Public API:
get_prop( $prop ): mixed— reads from$changesfirst, then$data.set_prop( $prop, $value ): voidset_props( $props ): voidapply_changes(): void— merges$changesinto$data.get_data(): arrayget_changes(): arrayget_extra_data(): arrayget_no_cache(): boolset_no_cache( $no_cache ): void
Abstract_WC_Logger
Dream_Encode\WordPress_Plugin_Utils\Abstracts\Abstract_WC_Logger
Logs data through WooCommerce's WC_Logger, falling back to error_log when WooCommerce is unavailable.
Abstract_Data_Migrator
Dream_Encode\WordPress_Plugin_Utils\Abstracts\Abstract_Data_Migrator
Template-method base for multi-run data migrations driven by Action Scheduler. Each migration is broken into individually-scheduled runs; progress, status, and results are persisted via plugin-supplied storage hooks.
Must implement (persistence — decoupled from any specific storage):
create_new_migration( array $migration ): false|arraycreate_new_migration_run( array $migration ): false|intget_migration_option( $migration_id ): false|mixedupdate_migration_option( $migration_id, $data ): voidupdate_migration( array $progress ): voidupdate_migration_run( array $migration_run ): voidget_migration_run_option( $migration_run_id ): false|mixedupdate_migration_run_option( $migration_run_id, $data ): voidsave_completed_migration_run( array $migration_run ): voidsave_completed_migration( array $progress ): voidcreate_migration_message( array $args ): void
Must implement (metadata):
get_migrator_label( $migrator ): stringget_plugin_setting( $key ): mixedget_migrator_setting( $migrator, $key, $default ): mixedget_all_migration_param_keys(): string[]get_migration_progress_keys(): string[]get_action_scheduler_hook(): stringget_action_scheduler_group(): string
Public API:
queue_migrator( $init_params ): void— queues the first migration run via Action Scheduler.run( $migration_run_id ): void— invoked by Action Scheduler to execute one run.process_migration(): void— override to perform the actual row-level work.get_progress(): array— current progress snapshot.update_percent_complete(): voidget_migration_id(): false|intget_migration_run_id(): false|intget_status(): stringget_percent_complete(): floatget_total_rows(): intget_total_rows_migrated(): intget_total_rows_failed(): intget_total_rows_skipped(): intget_batch_size(): intget_is_dry_run(): bool
Abstract_Background_Processor
Dream_Encode\WordPress_Plugin_Utils\Abstracts\Abstract_Background_Processor
Template-method base for long-running asynchronous background processors driven by Action Scheduler. Supports prerequisite sub-processors, configurable batch sizes, and per-run progress tracking.
Must implement (persistence):
queue_new_background_process( $args ): false|arraycreate_new_background_process( $args ): false|arraycreate_new_background_process_run( $background_process ): false|intget_background_process_option( $background_processes_id, $force_refresh ): false|mixedupdate_background_process_option( $background_processes_id, $data ): voidupdate_background_process( $data ): voidget_background_process_run_option( $background_processes_run_id ): false|mixedupdate_background_process_run_option( $background_processes_run_id, $data ): voidupdate_background_process_run( $data ): voidsave_completed_background_process( $data ): voidsave_completed_background_process_run( $data ): voidget_background_process_by_id( $background_processes_id ): object|nullupdate_prerequisite_sub_process_parent_background_process( $background_processes_id ): void
Must implement (metadata):
get_all_background_process_param_keys(): string[]get_background_process_progress_keys(): string[]get_processor_sub_params(): arrayget_background_processor_label( $processor ): stringcreate_background_processes_message( $args ): voidget_plugin_setting( $key ): mixedget_processor_setting( $processor, $key, $default ): mixedget_action_scheduler_hook(): stringget_action_scheduler_group(): string
Settings
Plugin_Settings_Repository
Dream_Encode\WordPress_Plugin_Utils\Settings\Plugin_Settings_Repository
Thin read/write wrapper around a single WordPress option key. Results are cached in-memory for the request lifetime.
Constructor: __construct( string $option_name, array $defaults = [], bool $autoload = true )
Public API:
all(): array— returns all settings, merged with defaults.get( $key, $default = null ): mixedset( $key, $value ): boolupdate( array $settings ): bool— overwrites the full option.delete(): boolrefresh(): void— clears the in-memory cache.get_option_name(): string
Functions
Dream_Encode\WordPress_Plugin_Utils\Common\Functions
All methods are static.
maybe_define_constant( string $name, mixed $value ): void— defines a PHP constant only if it is not already defined.get_mysql_datetime( false|float|int $time, string $timezone_string ): string|false— converts a Unix timestamp to a MySQLY-m-d H:i:sstring (defaults to current time, UTC).mysql_datetime_to_datetime_long( string $datetime ): string|false— formats a MySQL datetime string to"Monday January 5, 2026 at 3:42:00 pm".format_timestamp_to_datetime_long( null|float|int $timestamp, string $timezone_string ): string|false— formats a Unix timestamp to the same long format.convert_seconds_to_minutes_seconds( int $seconds ): string— formats an integer number of seconds asMM:SS(e.g.313→"05:13").get_user_display_name( int $user_id ): string— returns the user'suser_nicename, or'N/A'if the user does not exist.
Development
# Lint code composer lint # Auto-format code composer format # Static analysis composer analyze # Run all checks + tests composer test
License
GPL-3.0-or-later
统计信息
- 总下载量: 54
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2026-04-21