Skip to content

Instantly share code, notes, and snippets.

View bhubbard's full-sized avatar
:octocat:
Hello

Brandon Hubbard bhubbard

:octocat:
Hello
View GitHub Profile
@bhubbard
bhubbard / wp-cron-retry.php
Created December 10, 2024 22:07
This pattern automatically retries a failed WP Cron job with a configurable number of retry attempts and a delay between retries.
How It Works:
• When the cron job fails, it increments a retry count and schedules a retry attempt after a set delay.
• It will attempt to retry the job a maximum of MY_CRON_JOB_MAX_RETRIES times, with a delay of MY_CRON_JOB_RETRY_DELAY between each retry.
• After successful execution or after reaching the retry limit, the retry count is reset.
@bhubbard
bhubbard / time-window-cron.php
Created December 10, 2024 22:06
This pattern ensures that a WP Cron job only runs during a specific time window (e.g., between midnight and 3 AM).
How It Works:
• The cron job only runs if the current time is within the defined time window (midnight to 3 AM in this example).
• If it is outside the time window, the job is skipped.
@bhubbard
bhubbard / wp-cron-trottling.php
Created December 10, 2024 22:06
This pattern limits the execution frequency of WP Cron jobs, ensuring that they do not run more frequently than a set threshold, even if the scheduled time has passed.
How It Works:
• The cron job runs only if the time since the last run is greater than or equal to the defined throttle interval (in this case, 1 hour).
• It checks this condition before executing the task, ensuring that the job isn’t triggered too frequently.
@bhubbard
bhubbard / gist:d27811cc90b1d22271eccff2f0411b2c
Created December 10, 2024 21:57
The Deferred Hook Execution Pattern is useful when you want to delay the execution of a hook until a certain condition is met during the request lifecycle, such as when waiting for external data to load or deferring certain tasks until after the main action completes. This pattern prevents running resource-intensive operations too early and help…
<?php
/**
* Deferred Hook Execution Class
*
* Defers hook execution until a specified condition is met during the lifecycle of a WordPress request.
*/
class Deferred_Hook_Execution {
/**
* The name of the WordPress hook.
*
@bhubbard
bhubbard / role-scope-listener.php
Created December 10, 2024 21:55
Role-Scoped Listener Pattern • Description: Extends WordPress hooks by allowing specific actions or filters to execute only for users with defined roles or capabilities. Ideal for ensuring role-based access to functionality or modifying content dynamically based on user permissions.
<?php
/**
* Role-Scoped Listener Class
*
* Allows specific WordPress hooks to be executed only for users with designated roles.
*/
class Role_Scoped_Listener {
/**
* The name of the WordPress hook.
*
@bhubbard
bhubbard / adaptive-hook-pattern.php
Created December 10, 2024 21:42
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…
<?php
class AdaptiveHook {
private $hookName;
private $callbacks = [];
private $defaultCallback;
public function __construct(string $hookName, callable $defaultCallback) {
$this->hookName = $hookName;
$this->defaultCallback = $defaultCallback;
@bhubbard
bhubbard / scoped-hook-pattern.php
Created December 10, 2024 21:41
The Scoped Hook Pattern is designed for WordPress projects where specific actions or filters should be executed only within a particular scope or context. Unlike traditional hooks that execute globally, this pattern allows you to define scoped hooks that trigger only when certain conditions are met (e.g., a specific post type, user role, or admi…
<?php
class ScopedHook {
private $hookName;
private $callback;
private $condition;
public function __construct(string $hookName, callable $callback, callable $condition) {
$this->hookName = $hookName;
$this->callback = $callback;
@bhubbard
bhubbard / cascade_isolation.php
Last active December 10, 2024 21:39
Concept The “Impulse Programming Pattern” is designed for tasks that require a reactive, single-trigger response to changes in application state or environment variables. Unlike traditional observer patterns that continuously monitor state changes, the impulse pattern is event-specific, running a predefined set of actions the moment an impulse (…
<?php
/*
The Cascade Isolation Pattern is designed to manage a chain of dependent operations where the execution of each step in the chain is isolated from the others unless explicitly passed a “success state.” This pattern ensures that downstream operations in a chain won’t execute unless the preceding steps complete successfully.
This is particularly useful in applications requiring tightly controlled workflows, like multi-step transaction processing, data pipelines, or sequential validations.
*/
class CascadeStep {
private $callback;
<?php
/**
* Plugin Name: WP Cherwell API
* Plugin URI: https://example.com/wp-cherwell-api
* Description: A WordPress plugin that provides an API wrapper for Cherwell.
* Version: 1.0.0
* Author: Your Name
* Author URI: https://example.com
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
<?php
/**
* Plugin Name: WP Gemini API
* Plugin URI: https://example.com/wp-gemini-api
* Description: A WordPress plugin that provides an API wrapper for Gemini.
* Version: 1.0.0
* Author: Your Name
* Author URI: https://example.com
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html