Skip to content

Utils Helper Library (Xe_Plugin\Utils)

The Xe_Plugin\Utils class provides a suite of static developer utility functions designed to simplify common WordPress development tasks.


1. Asset Management & Cache Busting

Enqueues a stylesheet or script with automatic cache-busting by hashing the file's last modified timestamp (filemtime) if $ver is omitted.

use Xe_Plugin\Utils;

// Enqueue stylesheet with auto-versioning
Utils::enqueue( 'style', 'my-plugin-style', 'assets/css/main.css' );

// Enqueue script in footer with jQuery dependency
Utils::enqueue( 'script', 'my-plugin-script', 'assets/js/main.js', ['jquery'] );

Utils::render_asset( $rel, $handle, $src, $media, $crossorigin )

Directly outputs <link> or <script> tags with filemtime version query strings for standalone web app templates (e.g. login/dashboard layouts).

// Renders <link rel="stylesheet" ...>
Utils::render_asset( 'style', 'theme-css', 'assets/css/theme.css' );

// Renders <script src="...">
Utils::render_asset( 'script', 'theme-js', 'assets/js/theme.js' );

// Renders favicon <link rel="icon" ...>
Utils::render_asset( 'icon', 'favicon', get_site_icon_url() );

2. Localization & Script Data

Utils::localize_script( $object_name, $data )

Renders inline JavaScript data definitions for templates without relying on registered WP script handles:

Utils::localize_script( 'xePlugin', [
  'ajaxUrl' => admin_url( 'admin-ajax.php' ),
  'nonce'   => wp_create_nonce( '_xe_plugin_ajax_nonce' ),
] );

3. Post Meta & Field Updating

Utils::update_field( $post_id, $name, $is_array, $validation, $meta_key, $delete = false )

Automatically checks $_POST[$name], sanitizes the input according to the chosen validation type, and updates the post meta.

  • $validation options: 'text', 'intval', 'floatval', 'textarea', 'email', 'url'.
// Sanitize string text field
Utils::update_field( $post_id, 'customer_name', false, 'text', '_customer_name' );

// Sanitize integer array field
Utils::update_field( $post_id, 'selected_ids', true, 'intval', '_selected_ids' );

// Sanitize email address
Utils::update_field( $post_id, 'billing_email', false, 'email', '_billing_email' );

4. Security & Nonce Verification

Utils::verify_save( $action, $nonce, $post_type, $post_id )

One-line verification check for metabox and CPT save routines. Verifies: 1. Nonce presence and validity (wp_verify_nonce). 2. Not during an autosave routine (DOING_AUTOSAVE). 3. User capability permissions (edit_post or edit_page).

public function save_metabox( int $post_id ) {
  if ( ! Utils::verify_save( 'my_nonce_action', 'my_nonce_name', 'my-cpt', $post_id ) ) {
    return $post_id;
  }

  // Safe to save fields...
}

5. CSS & Styling Utilities

Utils::minify_css( $css )

Removes comments, newlines, and whitespace from raw CSS strings:

$clean_css = Utils::minify_css( "body { color: #333; margin: 0px; }" );

Utils::add_inline_styles( $css )

Minifies and prints a <style> block:

Utils::add_inline_styles( "
  .banner { background-color: #f5f5f5; }
" );

Utils::hex2rgb( $color )

Converts hexadecimal color codes into comma-separated RGB values:

$rgb = Utils::hex2rgb( '#ffffff' ); // "255, 255, 255"

Utils::darken( $color, $dif = 20 )

Lightens or darkens a hexadecimal color code:

$darker_color = Utils::darken( '#ffffff', 20 ); // "#ebebeb"

6. Environment & User Helpers

Utils::localhost(): bool

Returns true if the server is running in a local environment (127.0.0.1 or ::1).

if ( Utils::localhost() ) {
  // Local environment specific logic...
}

Utils::debug(): bool

Returns true if WP_DEBUG and WP_DEBUG_LOG are enabled.

Utils::get_name( $id, $is_object = true ): string

Retrieves a user's full name (First Last), falling back to user_login if no name is set.

$user_display_name = Utils::get_name( get_current_user_id() );