MemberPress AI Assistant - Hooks Tester
A simple plugin to test and verify the hooks in MemberPress AI Assistant
by MemberPress · github.com/sethshoultes/memberpress-ai-assistant-hooks-tester
Install
No release zip yet. The repository archive installs, but the folder name will carry the branch suffix and updates will not flow:
wp plugin install https://github.com/sethshoultes/memberpress-ai-assistant-hooks-tester/archive/refs/heads/main.zipThis plugin demonstrates how to use the hooks and filters in MemberPress AI Assistant. It provides a testing framework for verifying that hooks are firing correctly and allows developers to see how to implement their own extensions.
Features
- Tests all hooks and filters in MemberPress AI Assistant
- Logs hook executions to a log file
- Provides a UI for viewing the log and manually triggering hooks
- Demonstrates how to implement content, UI, API, and error handling hooks
Content and UI Hooks Implementation
The plugin includes a dedicated class (MPAI_Content_Hooks_Tester) that demonstrates how to use the content and UI hooks introduced in Phase 3 of the MemberPress AI Assistant hooks implementation plan.
Content Generation Hooks
MPAI_HOOK_FILTER_generated_content: Adds a disclaimer to blog postsMPAI_HOOK_FILTER_content_formatting: Adds a custom formatting rule for calloutsMPAI_HOOK_FILTER_blog_post_content: Adds a category and prefix to blog post titlesMPAI_HOOK_FILTER_content_type: Detects custom content types based on content patternsMPAI_HOOK_FILTER_content_marker: Adds a new marker for subtitle
Admin Interface Hooks
MPAI_HOOK_FILTER_admin_menu_items: Adds a new submenu item for the hooks testerMPAI_HOOK_FILTER_settings_fields: Adds a custom settingMPAI_HOOK_FILTER_settings_tabs: Adds a custom settings tabMPAI_HOOK_ACTION_before_display_settings: Adds a notice at the top of the settings pageMPAI_HOOK_ACTION_after_display_settings: Adds a footer to the settings pageMPAI_HOOK_FILTER_chat_interface_render: Customizes the chat interface with a custom theme
API Integration and Error Handling Hooks
The plugin includes a dedicated class (MPAI_API_Hooks_Tester) that demonstrates how to use the API integration and error handling hooks introduced in Phase 4 of the MemberPress AI Assistant hooks implementation plan.
API Integration Hooks
MPAI_HOOK_ACTION_before_api_request: Logs API requests before they are sentMPAI_HOOK_ACTION_after_api_request: Logs API responses and tracks usage metricsMPAI_HOOK_FILTER_api_request_params: Adjusts temperature based on API providerMPAI_HOOK_FILTER_api_response: Adds a watermark to responsesMPAI_HOOK_FILTER_api_provider: Selects specific providers for different content typesMPAI_HOOK_FILTER_cache_ttl: Customizes cache TTL based on request type
Error Handling Hooks
MPAI_HOOK_FILTER_api_error_handling: Customizes error handling based on API providerMPAI_HOOK_ACTION_before_error_recovery: Logs error recovery attemptsMPAI_HOOK_ACTION_after_error_recovery: Logs error recovery resultsMPAI_HOOK_FILTER_error_message: Makes error messages more user-friendlyMPAI_HOOK_FILTER_error_should_retry: Determines if certain errors should be retried
Logging Hooks
MPAI_HOOK_FILTER_log_entry: Adds additional context to log entriesMPAI_HOOK_FILTER_should_log: Filters out certain types of log messagesMPAI_HOOK_FILTER_log_level: Upgrades certain warnings to errorsMPAI_HOOK_FILTER_log_retention: Sets log retention to 60 daysMPAI_HOOK_FILTER_sanitize_log_data: Redacts sensitive data in logs
Usage
- Install and activate the plugin
- Go to Tools > MPAI Hooks Tester to view the log of hook executions
- Use the buttons on the page to manually trigger specific hooks
- Interact with MemberPress AI Assistant to trigger hooks automatically
- Check the log to see which hooks were fired and what data was passed
Implementation Details
Content Hooks Tester
The MPAI_Content_Hooks_Tester class demonstrates how to:
- Register hooks and filters for content generation and admin interface
- Modify generated content before use
- Add custom formatting rules
- Customize blog post content
- Detect custom content types
- Add custom settings and tabs
- Customize the chat interface
API Hooks Tester
The MPAI_API_Hooks_Tester class demonstrates how to:
- Register hooks and filters for API integration and error handling
- Log API requests and responses
- Track API usage metrics
- Customize API provider selection
- Enhance error handling and recovery
- Sanitize sensitive data in logs
Example: Adding a Custom Formatting Rule
public function filter_content_formatting($rules) {
// Add a custom formatting rule for callouts
$rules['callout'] = [
'tag' => 'div',
'wrapper' => '<!-- wp:custom-block {"className":"mpai-callout"} --><div class="mpai-callout">%s</div><!-- /wp:custom-block -->'
];
return $rules;
}
Example: Customizing the Chat Interface
public function filter_chat_interface_render($content, $position, $welcome_message) {
// Add a custom class to the chat container
$content = str_replace('class="mpai-chat-container', 'class="mpai-chat-container mpai-hooks-tester-theme', $content);
// Add custom CSS for the theme
$custom_css = '<style>
.mpai-hooks-tester-theme .mpai-chat-header {
background: linear-gradient(135deg, #6e48aa, #9d50bb);
}
.mpai-hooks-tester-theme .mpai-chat-submit {
background-color: #9d50bb;
}
</style>';
return $custom_css . $content;
}
Example: Tracking API Usage Metrics
public function after_api_request($api_name, $request_params, $response, $duration) {
// Log API response
$is_error = is_wp_error($response);
$log_method = $is_error ? 'mpai_log_warning' : 'mpai_log_debug';
$log_method("MPAI Hooks Tester: After API request to {$api_name} (took {$duration}s)", 'api-hooks-tester', [
'success' => !$is_error,
'error' => $is_error ? $response->get_error_message() : null
]);
// Track API usage metrics
$this->track_api_usage($api_name, $duration, !$is_error);
}
Example: Customizing Error Messages
public function filter_error_message($message, $error, $context) {
// Make error messages more user-friendly
if (strpos($message, 'API key') !== false) {
return 'The AI service is currently unavailable. Please try again later or contact support.';
} elseif (strpos($message, 'rate limit') !== false) {
return 'The AI service is experiencing high demand. Please try again in a few minutes.';
}
return $message;
}
Development
This plugin is intended as a reference implementation for developers who want to extend MemberPress AI Assistant. Feel free to use it as a starting point for your own extensions.
License
GPL v2 or later