There are cases where you might want to access application or WordPress resources without having to go through your web server and WordPress directly:
- Unit Tests
- Database Migration Scripts
- Workers and Message Queues
- Cronjobs for emails, notifications, or larger tasks
Many of these options require resources like database connections, configuration or caches in order to function. The Vulnero_WordPress API is written to implement certain WordPress API functions even if they're not available (e.g.: WordPress hasn't been loaded). It can create a database connection, work with options through get/setCustomOption() and even bootstrap most resources like caching.
Sample Command Line Script:
#!/usr/bin/env php
<?php
if (!defined('PLUGIN_BASE_PATH')) {
require_once dirname(__FILE__) . '/../wordpress-plugin.php';
}
ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);
$wordPress = $application->getBootstrap()
->bootstrap('wordPress')
->getResource('wordPress');
$db = $application->getBootstrap()
->bootstrap('db')
->getResource('db');
$cache = $application->getBootstrap()
->bootstrap('cache')
->getResource('cache');
if (!$value = $cache->load('sample')) {
$stmt = $db->query('SELECT 1');
$row = $stmt->fetch(PDO::FETCH_NUM);
$value = $row[0];
$cache->save($value);
}
$wordPress->setCustomOption('sample', $value);
exit(0);
The above script is suitable for command line execution straight from bash and works great as a message queue, cronjob, and so forth.
