Utilities & Helpers
The WpMVC Helpers package is a collection of high-performance, reusable utility functions engineered to standardize common tasks in WordPress development. By providing a unified API for file management, array manipulation, and environment sensing, the library ensures consistency and reduces boilerplate across complex plugin ecosystems.
Technical Integration
Installation
Integrate the helper library into your project via Composer:
composer require wpmvc/helpersMethod Reference
Environment & System
get_plugin_version
Retrieves the semantic version of an installed plugin. Returns null if the plugin is not found or inactive.
$version = Helpers::get_plugin_version( 'woocommerce' );
// Output: "8.5.0"get_user_ip_address
Standardized methodology for retrieving the client’s IP address. This implementation accounts for HTTP_X_FORWARDED_FOR and other proxy headers to ensure accuracy in load-balanced environments.
$ip = Helpers::get_user_ip_address();Media & File Management
upload_file
Automates the process of sideloading or uploading files to the WordPress Media Library. Encapsulates error handling and permission checks.
try {
$attachment_id = Helpers::upload_file( $_FILES['my_file'] );
} catch ( \Exception $e ) {
Log::error( 'Media upload failed', [ 'message' => $e->getMessage() ] );
}delete_attachments_by_ids
Performs bulk deletion of media components.
Helpers::delete_attachments_by_ids( [ 123, 456, 789 ] );Data Transformation & Validation
maybe_json_decode
Input is automatically validated before decoding. If the value is already structured or is an invalid JSON string, the original value is returned to prevent data loss.
A defensive decoding utility that attempts to parse a string as JSON.
$data = Helpers::maybe_json_decode( '{"key":"value"}' ); // Returns ['key' => 'value']
$text = Helpers::maybe_json_decode( 'Not JSON' ); // Returns 'Not JSON'array_merge_deep
Deeply merges two or more arrays. Unlike standard PHP merge functions, this retains nested structures by merging corresponding keys recursively.
$merged = Helpers::array_merge_deep(
[ 'a' => [ 'b' => 1 ] ],
[ 'a' => [ 'c' => 2 ] ]
);
// Result: [ 'a' => [ 'b' => 1, 'c' => 2 ] ]remove_null_values
Sanitizes data structures for API transport or database storage by stripping keys with null values.
$clean = Helpers::remove_null_values( [ 'id' => 1, 'meta' => null ] );
// Result: [ 'id' => 1 ]is_one_level_array
Validates the dimensionality of an array.
$is_flat = Helpers::is_one_level_array( [ 'a', 'b', 'c' ] ); // trueDate & Time Manipulation
The Date class extends PHP’s native DateTime to provide WordPress-aware defaults and a chainable API for complex calendar math and comparisons.
Initialization
By default, the constructor initializes to the current time using wp_timezone().
use WpMVC\Helpers\Date;
$now = new Date();
$tomorrow = new Date( 'tomorrow' );
$timezone = new Date( 'tomorrow', new \DateTimeZone( 'America/New_York' ) );
$specific = Date::now();Arithmetic
Perform accurate calendar math that correctly handles varying month lengths and leap years.
$date = new Date( '2024-02-29' );
$date->add_years( 1 ); // Returns 2025-03-01 (Correct overflow)
$date->sub_months( 2 )->add_days( 5 );
$date->add_timestamp( 3600 ); // Add 1 hour in secondsRelative Checks & Comparisons
Simple boolean checks for common date states.
$date->is_today(); // bool
$date->is_past(); // bool
$date->is_weekend(); // bool
$date->is_same_day( '2023-10-25' ); Formatting & Boundaries
echo $date->start_of_day(); // "Wed Oct 25 2023 00:00:00 GMT+0000"
echo $date->to_iso8601(); // "2023-10-25T00:00:00+00:00"
echo $date->to_date_string(); // "2023-10-25"
echo $date->getTimestamp(); // 1698192000Request Handling
request
Provides a bridge to the WordPress REST API infrastructure by generating a WP_REST_Request object from the current environment state (GET/POST/SERVER globals).
$request = Helpers::request();
$data = $request->get_params();