PHP code example of mzeahmed / wp-toolkit
1. Go to this page and download the library: Download mzeahmed/wp-toolkit library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mzeahmed / wp-toolkit example snippets
use MzeAhmed\WpToolKit\Utils\Ajax;
Ajax::register('my_action', 'my_callback');
function my_callback()
{
$data = [
'message' => 'Action executed successfully!',
'additional_info' => 'Extra data here'
];
Ajax::sendJsonSuccess('Success!', $data);
}
Ajax::sendJsonSuccess('Success message', ['data' => 'value']);
Ajax::sendJsonError('Error message', ['error' => 'reason']);
Ajax::verifyNonce('nonce_field_name', 'action_name');
define('AJAX_SECURITY_NONCE_ACTION', 'my_action');
define('AJAX_SECURITY_NONCE', 'nonce_field_name');
wp_localize_script('your-script-handle', 'ajax_object', [
'ajax_url' => admin_url('admin-ajax.php'),
AJAX_SECURITY_NONCE => wp_create_nonce(AJAX_SECURITY_NONCE_ACTION),
]);
namespace YourNamespace\Repositories;
use Mzeahmed\WpToolKit\Database\AbstractRepository;
class YourCustomRepository extends AbstractRepository
{
public function __construct()
{
parent::__construct();
$this->tableName = $this->wpdbPrefix . 'your_table_name';
}
public function findActiveItems(): array
{
return $this->findByCriteria(['status' => 'active']);
}
}
use YourNamespace\Repositories\YourCustomRepository;
$repository = new YourCustomRepository();
$activeItems = $repository->findActiveItems();
// Bulk insert example
$repository->bulkInsert([
['column1' => 'value1', 'column2' => 'value2'],
['column1' => 'value3', 'column2' => 'value4'],
]);
// Bulk delete example
$repository->bulkDelete('id', [1, 2, 3]);
$repository->beginTransaction();
try {
$repository->insert(['column1' => 'value1']);
$repository->update(['column2' => 'value2'], ['id' => 1]);
$repository->commit();
} catch (\Exception $e) {
$repository->rollback();
error_log($e->getMessage());
}
use MzeAhmed\WpToolKit\UserActivityMonitor;
$monitor = new UserActivityMonitor();
$monitor->updateStatus($userId);
$isOnline = $monitor->isUserOnline($userId);
if ($isOnline) {
echo "User $userId is online.";
} else {
echo "User $userId is offline.";
}
$onlineUsers = $monitor->getOnlineUsers();
foreach ($onlineUsers as $user) {
echo "User $user->ID is online.";
}
$recentlyOfflineUsers = $monitor->getRecentlyOfflineUsers();
foreach ($recentlyOfflineUsers as $user) {
echo "User $user->ID recently went offline.";
}
use MzeAhmed\WpToolKit\Utils\Sanitizer;
$cleanText = Sanitizer::text(' John ');
// returns 'John'
$cleanEmailArray = Sanitizer::email([' [email protected] ', '[email protected] ']);
// returns ['[email protected] ', '[email protected] ']
$dirtyArray = [
'name' => ' John ',
'meta' => [
'city' => ' Paris ',
'desc' => " Hello\nWorld "
]
];
$clean = Sanitizer::recursiveText($raw);
// returns [
// 'name' => 'John',
// 'meta' => [
// 'city' => 'Paris',
// 'desc' => "Hello\nWorld"
// ]
// ]
$data = [
'name' => ' John Doe ',
'email' => ' [email protected] ',
'website' => ' https://example.com ',
'bio' => " Hello\nI'm John "
];
$rules = [
'name' => 'text',
'email' => 'email',
'website' => 'url',
'bio' => 'textarea'
];
$cleaned = Sanitizer::byRules($data, $rules);
// returns [
// 'name' => 'John Doe',
// 'email' => '[email protected] ',
// 'website' => 'https://example.com',
// 'bio' => "Hello\nI'm John"
// ]
use MzeAhmed\WpToolKit\Http\HttpClient;
$client = new HttpClient();
$response = $client->get('https://jsonplaceholder.typicode.com/posts');
if (is_wp_error($response)) {
error_log($response->get_error_message());
} else {
var_dump($response); // array of posts
}
$response = $client->post('https://example.com/api/data', [
'name' => 'John',
'email' => '[email protected] '
]);
$response = $client->get('http://example.com', [], true); // safe mode enabled