Download the PHP package mzeahmed/wp-toolkit without Composer
On this page you can find all versions of the php package mzeahmed/wp-toolkit. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mzeahmed/wp-toolkit
More information about mzeahmed/wp-toolkit
Files in mzeahmed/wp-toolkit
Package wp-toolkit
Short Description A PHP library with useful and reusable functions for WordPress.
License MIT
Informations about the package wp-toolkit
WP ToolKit
WP ToolKit is a PHP library that simplifies WordPress development by providing reusable classes to handle common operations :
- AJAX requests
- Database operations
- and more...
Installation
Then include Composer's autoloader in your project:
Features
- AJAX Requests
- Database Operations
- User Activity Monitoring
- Data Sanitization 5HTTP Client
Ajax Requests
The Ajax utility class simplifies working with AJAX in WordPress by providing methods for registering AJAX actions, validating requests, and sending JSON responses.
Features
- Register AJAX Actions: Supports both authenticated (wpajax) and public (wp_ajaxnopriv) actions.
- Send JSON Responses: Easily send success or error responses to the client.
- Nonce Validation: Verifies nonces to ensure request authenticity.
- Error Logging: Logs AJAX errors when WP_DEBUG is enabled.
Usage
1. Register an AJAX Action
2. Send JSON Responses
Use the sendJsonSuccess and sendJsonError methods to send standardized JSON responses:
3. Nonce Validation
Verify nonces to ensure that AJAX requests are secure:
Need to define theses constants in the application: AJAX_SECURITY_NONCE_ACTION and AJAX_SECURITY_NONCE.
And use wp_localize_script to pass the nonce to the client side.
In the client side, you can use the nonce like this:
Database Operations
- Abstract Repository: Base class for implementing the repository pattern with
$wpdb. - Pagination Support: Easily paginate database results.
- Bulk Operations: Perform bulk inserts, updates, and deletes.
- Transaction Management: Start, commit, and rollback database transactions.
- Error Logging: Automatically log and debug database errors.
- Dynamic Query Building: Flexible support for building dynamic
WHEREandJOINclauses.
Usage
1. Implement a Repository
Create a repository class that extends the AbstractRepository class. For example:
2. Use the Repository
In your WordPress plugin or theme:
3. Bulk Operations
Perform bulk inserts, updates, or deletes:
4. Transactions
Manage transactions to ensure data consistency:
User Activity Monitoring
The UserActivityMonitor class helps track user activity and determine online/offline status.
Features
- Track User Activity: Monitor user activity and update the last seen timestamp.
- Check Online Status: Determine if a user is online within a specified margin.
- Retrieve Online Users: Get a list of all users currently online.
- Identify Recently Offline Users: Retrieve users who recently went offline.
Usage
1. Update User Activity
2. Check Online Status
3. Retrieve Online Users
4. Identify Recently Offline Users
Data Sanitization
The Sanitizer class provides static methods to clean user input and ensure data safety in WordPress environments.
Features
- ✅ Sanitize strings, emails, URLs, and textarea contents
- 🔁 Sanitize arrays and nested arrays
- 🎯 Apply custom sanitization rules per field
Supported Rules
| Rule | Description |
|---|---|
text |
Uses sanitize_text_field() |
email |
Uses sanitize_email() |
url |
Uses esc_url_raw() |
textarea |
Uses sanitize_textarea_field() |
Usage
1. Sanitize a single value or array
2. Recursively sanitize text values in nested arrays
3. Apply custom sanitization rules
HTTP Client
The HttpClient class simplifies making HTTP requests in WordPress using wp_remote_* functions. It supports GET, POST, PUT, DELETE (and PATCH) and automatically decodes JSON responses.
Features
- Perform requests with
wp_remote_get,wp_remote_post, etc. - Supports safe URLs with
reject_unsafe_urls - Automatically decodes JSON response bodies
- Unified interface for REST APIs
Usage
1. Initialize the client
2. Make a GET request
3. Make a POST request
4. Use safe mode (rejects unsafe URLs)
📖 API Reference
MzeAhmed\WpToolKit\Http\HttpClient
| Method | Description | Return Type |
|---|---|---|
get() |
Perform an HTTP GET request | array|\WP_Error |
post() |
Perform an HTTP POST request | array|\WP_Error |
put() |
Perform an HTTP PUT request | array|\WP_Error |
delete() |
Perform an HTTP DELETE request | array|\WP_Error |
- All methods accept:
$url(string) – The endpoint to query$args(array) – Optional arguments (headers, timeout, etc.)$safe(bool) – Enable WordPress URL validation withreject_unsafe_urls
Automatically decodes the JSON response body with
json_decode(..., JSON_THROW_ON_ERROR).