Phiên bản: 1.0 Ngày tạo: 2025 Tác giả: Development Team
Rake WordPress Adapter là bridge giữa Rake Core Framework và WordPress, cung cấp:
┌─────────────────────────────────────────────────────────────┐
│ RAKE WORDPRESS ADAPTER │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────┐ │
│ │ DATABASE │ │ HOOKS │ │ ADMIN │ │
│ │ ADAPTER │ │ INTEGRATION │ │ INTEGRATION │ │
│ │ │ │ │ │ │ │
│ │ • WP Database │ │ • add_action │ │ • Menu │ │
│ │ • Query Builder │ │ • add_filter │ │ • Pages │ │
│ │ • Prefix Handle │ │ • do_action │ │ • Scripts │ │
│ │ • wpdb Wrapper │ │ • apply_filters │ │ • Styles │ │
│ └─────────────────┘ └─────────────────┘ └─────────────┘ │
│ │
│ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────┐ │
│ │ SECURITY │ │ CACHE │ │ CONFIG │ │
│ │ LAYER │ │ INTEGRATION │ │ INTEGRATION │ │
│ │ │ │ │ │ │ │
│ │ • Nonce Check │ │ • WP Cache │ │ • WP Config │ │
│ │ • Capability │ │ • Transients │ │ • Options │ │
│ │ • Sanitization │ │ • Object Cache │ │ • Settings │ │
│ │ • Validation │ │ • Query Cache │ │ • Constants │ │
│ └─────────────────┘ └─────────────────┘ └─────────────┘ │
└─────────────────────────────────────────────────────────────┘
┌─────────────────┐ depends on ┌─────────────────┐
│ CRAWFLOW │ ────────────────▶ │ RAKE WORDPRESS │
│ PLUGIN │ │ ADAPTER │
└─────────────────┘ └─────────────────┘
│
│ depends on
▼
┌─────────────────┐
│ RAKE CORE │
│ FRAMEWORK │
└─────────────────┘
WordPress Adapter implements các interfaces từ Rake Core:
// Database Adapter Interface từ Rake Core
interface DatabaseAdapterInterface
{
public function query(string $sql): bool;
public function getResults(string $sql): array;
public function getRow(string $sql): ?array;
public function getVar(string $sql): mixed;
public function insert(string $table, array $data): int;
public function update(string $table, array $data, array $where): int;
public function delete(string $table, array $where): int;
public function getPrefix(): string;
public function escape(string $value): string;
}
// WordPress Adapter Implementation
class WordPressDatabaseAdapter implements DatabaseAdapterInterface
{
// Implementation cho WordPress
}
// Trong Rake Container
$container->bind(DatabaseAdapterInterface::class, WordPressDatabaseAdapter::class);
$container->bind(WordPressHooksInterface::class, WordPressHooksAdapter::class);
$container->bind(WordPressAdminInterface::class, WordPressAdminAdapter::class);
rake-wordpress-adapter/
├── src/
│ ├── Database/ # WordPress Database Integration
│ │ ├── WordPressDatabaseAdapter.php
│ │ ├── WordPressQueryBuilder.php
│ │ └── WordPressPrefixHandler.php
│ ├── Hooks/ # WordPress Hooks Integration
│ │ ├── WordPressHooksAdapter.php
│ │ └── WordPressHooksInterface.php
│ ├── Admin/ # WordPress Admin Integration
│ │ ├── WordPressAdminAdapter.php
│ │ ├── WordPressMenuBuilder.php
│ │ └── WordPressScriptManager.php
│ ├── Security/ # WordPress Security Layer
│ │ ├── WordPressSecurityAdapter.php
│ │ ├── WordPressNonceHandler.php
│ │ └── WordPressCapabilityChecker.php
│ ├── Cache/ # WordPress Cache Integration
│ │ ├── WordPressCacheAdapter.php
│ │ ├── WordPressTransientHandler.php
│ │ └── WordPressObjectCache.php
│ └── Config/ # WordPress Config Integration
│ ├── WordPressConfigAdapter.php
│ └── WordPressOptionsHandler.php
├── composer.json
└── README.md
{
"name": "puleeno/rake-wordpress-adapter",
"require": {
"php": ">=8.1",
"ramphor/rake": "^2.0"
},
"autoload": {
"psr-4": {
"Rake\\WordPress\\": "src/"
}
}
}
use Rake\WordPress\Database\WordPressDatabaseAdapter;
$adapter = new WordPressDatabaseAdapter();
// Basic operations
$adapter->insert('posts', [
'post_title' => 'Test Post',
'post_content' => 'Test content',
'post_status' => 'publish'
]);
$posts = $adapter->getResults("SELECT * FROM {$adapter->getPrefix()}posts WHERE post_type = 'post'");
$adapter->update('posts',
['post_status' => 'draft'],
['ID' => 1]
);
$adapter->delete('posts', ['ID' => 1]);
use Rake\WordPress\Database\WordPressQueryBuilder;
$query = new WordPressQueryBuilder($adapter);
$posts = $query->select(['ID', 'post_title', 'post_content'])
->from('posts')
->where('post_type', '=', 'post')
->where('post_status', '=', 'publish')
->orderBy('post_date', 'DESC')
->limit(10)
->get();
// Tự động xử lý WordPress table prefix
$adapter = new WordPressDatabaseAdapter();
echo $adapter->getPrefix(); // wp_
// Tự động thêm prefix khi cần
$table = $adapter->addPrefix('posts'); // wp_posts
use Rake\WordPress\Hooks\WordPressHooksAdapter;
$hooks = new WordPressHooksAdapter();
// Add actions
$hooks->addAction('init', [$this, 'initialize']);
$hooks->addAction('wp_loaded', [$this, 'onWpLoaded']);
// Add filters
$hooks->addFilter('the_content', [$this, 'modifyContent']);
// Do actions
$hooks->doAction('custom_action', $data);
// Apply filters
$modified = $hooks->applyFilters('custom_filter', $value);
use Rake\WordPress\Admin\WordPressAdminAdapter;
$admin = new WordPressAdminAdapter();
// Add menu pages
$admin->addMenuPage(
'My Plugin',
'My Plugin',
'manage_options',
'my-plugin',
[$this, 'renderPage']
);
// Enqueue scripts
$admin->enqueueScript('my-script', '/path/to/script.js');
// Enqueue styles
$admin->enqueueStyle('my-style', '/path/to/style.css');
use Rake\WordPress\Security\WordPressSecurityAdapter;
$security = new WordPressSecurityAdapter();
// Nonce verification
if ($security->verifyNonce($_POST['nonce'], 'my_action')) {
// Process form
}
// Capability checking
if ($security->currentUserCan('manage_options')) {
// Admin action
}
// Data sanitization
$clean = $security->sanitizeTextField($_POST['data']);
composer require puleeno/rake-wordpress-adapter
git clone https://github.com/puleeno/rake-wordpress-adapter.git
cd rake-wordpress-adapter
composer install
use Rake\Rake;
use Rake\WordPress\Database\WordPressDatabaseAdapter;
use Rake\WordPress\Hooks\WordPressHooksAdapter;
use Rake\WordPress\Admin\WordPressAdminAdapter;
// Tạo Rake container
$app = new Rake();
// Register WordPress adapters
$app->singleton(DatabaseAdapterInterface::class, WordPressDatabaseAdapter::class);
$app->singleton(WordPressHooksInterface::class, WordPressHooksAdapter::class);
$app->singleton(WordPressAdminInterface::class, WordPressAdminAdapter::class);
// Bootstrap
$app->make(WordPressHooksInterface::class);
// Basic CRUD operations
$adapter = new WordPressDatabaseAdapter();
// Insert
$postId = $adapter->insert('posts', [
'post_title' => 'New Post',
'post_content' => 'Post content',
'post_status' => 'publish',
'post_type' => 'post'
]);
// Select
$posts = $adapter->getResults("
SELECT * FROM {$adapter->getPrefix()}posts
WHERE post_type = 'post'
ORDER BY post_date DESC
LIMIT 10
");
// Update
$affected = $adapter->update('posts',
['post_status' => 'draft'],
['ID' => $postId]
);
// Delete
$deleted = $adapter->delete('posts', ['ID' => $postId]);
$hooks = new WordPressHooksAdapter();
// Register plugin hooks
$hooks->addAction('plugins_loaded', function() {
// Plugin initialization
});
$hooks->addAction('admin_menu', function() {
// Add admin menu
});
$hooks->addFilter('the_title', function($title) {
return 'Modified: ' . $title;
});
$admin = new WordPressAdminAdapter();
// Add admin menu
$admin->addMenuPage(
'My Plugin',
'My Plugin',
'manage_options',
'my-plugin',
function() {
echo '<div class="wrap"><h1>My Plugin</h1></div>';
}
);
// Enqueue admin assets
$admin->enqueueScript('my-admin-script', '/js/admin.js');
$admin->enqueueStyle('my-admin-style', '/css/admin.css');
$security = new WordPressSecurityAdapter();
// Form processing
if ($_POST && $security->verifyNonce($_POST['nonce'], 'save_data')) {
if ($security->currentUserCan('manage_options')) {
$cleanData = $security->sanitizeTextField($_POST['data']);
// Process data
}
}
$cache = new WordPressCacheAdapter();
// Set cache
$cache->set('my_key', $data, 3600); // 1 hour
// Get cache
$data = $cache->get('my_key');
// Delete cache
$cache->delete('my_key');
📖 docs/technical-documentation.md
Nội dung:
// Transaction handling
$adapter->beginTransaction();
try {
$adapter->insert('posts', $postData);
$adapter->insert('postmeta', $metaData);
$adapter->commit();
} catch (Exception $e) {
$adapter->rollback();
throw $e;
}
// Custom hooks
$hooks->addAction('my_custom_hook', function($data) {
// Process data
});
$hooks->doAction('my_custom_hook', $data);
// Submenu pages
$admin->addSubmenuPage(
'my-plugin',
'Settings',
'Settings',
'manage_options',
'my-plugin-settings',
[$this, 'renderSettings']
);
// Always use WordPress functions with backslash prefix
$result = \wp_verify_nonce($nonce, $action);
// Use WordPress security functions
$sanitized = \sanitize_text_field($input);
// Check capabilities before actions
if (\current_user_can('manage_options')) {
// Perform admin action
}
// Use WordPress hooks properly
\add_action('init', [$this, 'initialize']);
<?php
declare(strict_types=1);
namespace Rake\WordPress\Database;
use Rake\Database\DatabaseAdapterInterface;
class WordPressDatabaseAdapter implements DatabaseAdapterInterface
{
private \wpdb $wpdb;
private string $prefix;
public function __construct()
{
global $wpdb;
$this->wpdb = $wpdb;
$this->prefix = $wpdb->prefix;
}
public function query(string $sql): bool
{
return $this->wpdb->query($sql) !== false;
}
}
class WordPressDatabaseAdapterTest extends TestCase
{
private WordPressDatabaseAdapter $adapter;
protected function setUp(): void
{
$this->adapter = new WordPressDatabaseAdapter();
}
public function testInsert(): void
{
// Arrange
$data = ['post_title' => 'Test Post'];
// Act
$id = $this->adapter->insert('posts', $data);
// Assert
$this->assertGreaterThan(0, $id);
}
}
class WordPressIntegrationTest extends TestCase
{
public function testDatabaseAdapter(): void
{
// Arrange
$adapter = new WordPressDatabaseAdapter();
// Act
$result = $adapter->query('SELECT 1');
// Assert
$this->assertTrue($result);
}
}
class WordPressException extends Exception
{
public function __construct(string $message, array $context = [], int $code = 0, ?Throwable $previous = null)
{
parent::__construct("WordPress error: {$message}", $code, $previous);
}
}
// Usage
try {
$adapter = new WordPressDatabaseAdapter();
$result = $adapter->insert('table', $data);
} catch (WordPressException $e) {
Logger::error('WordPress operation failed: ' . $e->getMessage());
}
Adapter tự động sử dụng WordPress database settings:
// Tự động detect từ WordPress
$adapter = new WordPressDatabaseAdapter();
echo $adapter->getPrefix(); // wp_
echo $adapter->getCharset(); // utf8mb4
echo $adapter->getCollation(); // utf8mb4_unicode_ci
// Nếu cần custom settings
$adapter = new WordPressDatabaseAdapter();
// Custom database operations
$adapter->query("SET SESSION sql_mode = 'NO_AUTO_VALUE_ON_ZERO'");
Class 'Rake\WordPress\Database\WordPressDatabaseAdapter' not foundSolution:
composer dump-autoload
WordPress not loadedSolution:
// Ensure WordPress is loaded
require_once 'wp-load.php';
Database connection failedSolution:
// Enable debug mode
$adapter = new WordPressDatabaseAdapter();
// Check last error
echo $adapter->getLastError();
// Check affected rows
echo $adapter->affectedRows();
// Use transactions for multiple operations
$adapter->beginTransaction();
try {
foreach ($posts as $post) {
$adapter->insert('posts', $post);
}
$adapter->commit();
} catch (Exception $e) {
$adapter->rollback();
throw $e;
}
// Use batch operations
$adapter->getResults("SELECT * FROM posts LIMIT 1000");
// Use specific columns
$adapter->getResults("SELECT ID, post_title FROM posts WHERE post_type = 'post'");
Rake WordPress Adapter cung cấp bridge hoàn chỉnh giữa Rake Core Framework và WordPress với:
// Initialize adapter
$adapter = new WordPressDatabaseAdapter();
$hooks = new WordPressHooksAdapter();
$admin = new WordPressAdminAdapter();
// Use database
$results = $adapter->getResults('SELECT * FROM wp_posts');
// Use hooks
$hooks->addAction('init', [$this, 'initialize']);
// Use admin
$admin->addMenuPage('My Plugin', 'My Plugin', 'manage_options', 'my-plugin', [$this, 'renderPage']);
Tài liệu này sẽ được cập nhật thường xuyên khi có thay đổi trong adapter.