Created
December 10, 2024 21:42
-
-
Save bhubbard/d88b708e0c1b55b5122d9d5ee919946a to your computer and use it in GitHub Desktop.
The Adaptive Hook Pattern Concept The Adaptive Hook Pattern is designed for WordPress projects where hooks need to dynamically adjust their behavior based on the context or runtime conditions. Unlike static hooks that always execute the same callback, adaptive hooks allow for flexible responses by modifying the callback logic or even switching t…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class AdaptiveHook { | |
private $hookName; | |
private $callbacks = []; | |
private $defaultCallback; | |
public function __construct(string $hookName, callable $defaultCallback) { | |
$this->hookName = $hookName; | |
$this->defaultCallback = $defaultCallback; | |
// Attach the main execution method to the hook | |
add_action($hookName, [$this, 'execute'], 10, 2); | |
} | |
public function addCondition(callable $condition, callable $callback) { | |
$this->callbacks[] = ['condition' => $condition, 'callback' => $callback]; | |
} | |
public function execute(...$args) { | |
foreach ($this->callbacks as $entry) { | |
if (call_user_func($entry['condition'])) { | |
call_user_func_array($entry['callback'], $args); | |
return; // Exit after the first matching condition | |
} | |
} | |
// Fallback to the default callback if no condition matches | |
call_user_func_array($this->defaultCallback, $args); | |
} | |
} | |
// Example Use Case: Dynamic Admin Notices | |
$adminNoticeHook = new AdaptiveHook('admin_notices', function() { | |
echo '<div class="notice notice-warning"><p>This is the default notice.</p></div>'; | |
}); | |
// Add conditions for specific contexts | |
$adminNoticeHook->addCondition(function() { | |
return current_user_can('editor'); | |
}, function() { | |
echo '<div class="notice notice-info"><p>Hello, Editor! Here’s your custom notice.</p></div>'; | |
}); | |
$adminNoticeHook->addCondition(function() { | |
return current_user_can('administrator'); | |
}, function() { | |
echo '<div class="notice notice-success"><p>Welcome, Admin! Here’s your custom notice.</p></div>'; | |
}); | |
// Another Example: Adaptive Enqueue Scripts | |
$enqueueScriptsHook = new AdaptiveHook('wp_enqueue_scripts', function() { | |
wp_enqueue_style('default-style', get_template_directory_uri() . '/css/default.css'); | |
}); | |
// Add conditions for specific pages | |
$enqueueScriptsHook->addCondition(function() { | |
return is_front_page(); | |
}, function() { | |
wp_enqueue_style('front-page-style', get_template_directory_uri() . '/css/front-page.css'); | |
}); | |
$enqueueScriptsHook->addCondition(function() { | |
return is_singular('post'); | |
}, function() { | |
wp_enqueue_style('single-post-style', get_template_directory_uri() . '/css/single-post.css'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Use Case | |
1. Context-Sensitive Admin Notices: | |
• Display different notices to users based on roles, page types, or even custom plugin settings. | |
2. Dynamic Asset Loading: | |
• Enqueue specific scripts or styles depending on whether the user is viewing a front page, a single post, or another custom context. | |
3. Event-Based Responses: | |
• Trigger different behaviors in hooks like save_post or init based on runtime conditions, such as user permissions, plugin settings, or environment variables. | |
4. Fallback Logic: | |
• Ensure there’s always a fallback action in place if none of the dynamic conditions are met, reducing potential errors. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment