Skip to content

Commit

Permalink
docs: added to all classes and functions
Browse files Browse the repository at this point in the history
  • Loading branch information
thorbrink committed Nov 29, 2023
1 parent d5aa242 commit c5c84f7
Show file tree
Hide file tree
Showing 13 changed files with 261 additions and 9 deletions.
28 changes: 28 additions & 0 deletions source/php/AcfFields/FieldOptionsModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,31 @@

use WebhooksManager\Options\OptionsInterface;

/**
* Class FieldOptionsModifier
*
* This class is responsible for modifying the options of ACF fields based on the Webhooks Manager settings.
*/
class FieldOptionsModifier
{
private OptionsInterface $options;

/**
* FieldOptionsModifier constructor.
*
* @param OptionsInterface $options The options object used to retrieve the available actions and HTTP methods.
*/
public function __construct(OptionsInterface $options)
{
$this->options = $options;
}

/**
* Modifies the options of the action field based on the Webhooks Manager settings.
*
* @param array $field The ACF field options.
* @return array The modified ACF field options.
*/
public function getActionFieldOptions($field)
{
if (!$this->isOnWebhooksManagerPage()) {
Expand All @@ -26,6 +42,12 @@ public function getActionFieldOptions($field)
return $field;
}

/**
* Modifies the options of the HTTP method field based on the Webhooks Manager settings.
*
* @param array $field The ACF field options.
* @return array The modified ACF field options.
*/
public function getHttpMethodFieldOptions($field)
{
if (!$this->isOnWebhooksManagerPage()) {
Expand All @@ -39,8 +61,14 @@ public function getHttpMethodFieldOptions($field)
return $field;
}

/**
* Checks if the current page is the Webhooks Manager page.
*
* @return bool True if the current page is the Webhooks Manager page, false otherwise.
*/
private function isOnWebhooksManagerPage(): bool
{
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
return isset($_GET['page']) && $_GET['page'] === 'webhooks-manager';
}
}
21 changes: 15 additions & 6 deletions source/php/Options/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@

namespace WebhooksManager\Options;

/**
* Class Options
*
* Represents the options for the WebhooksManager plugin.
*/
class Options implements OptionsInterface
{
public const DEFAULT_ACTIONS = [
'post_updated',
'post_created',
'post_deleted',
];

/**
* An array of HTTP methods supported by the plugin.
*
* @return array The supported HTTP methods.
*/
public function getHttpMethods(): array
{
return ['GET', 'POST'];
}

/**
* Retrieves the available webhook actions.
*
* @return array The available webhook actions.
*/
public function getActions(): array
{
return apply_filters('WebhooksManager\Options\getActions', self::DEFAULT_ACTIONS);
Expand Down
16 changes: 16 additions & 0 deletions source/php/Options/OptionsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,24 @@

namespace WebhooksManager\Options;

/**
* Interface OptionsInterface
*
* This interface defines the methods for retrieving available options.
*/
interface OptionsInterface
{
/**
* Get the available HTTP methods.
*
* @return array An array of available HTTP methods.
*/
public function getHttpMethods(): array;

/**
* Get the available actions.
*
* @return array An array of available actions.
*/
public function getActions(): array;
}
15 changes: 15 additions & 0 deletions source/php/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,35 @@
use WebhooksManager\Options\Options;
use WebhooksManager\WebhooksRegistry\WebhooksRegistry;

/**
* Class Plugin
*
* This class represents the main plugin class for the Webhooks Manager plugin.
* It is responsible for initializing the plugin and setting up the necessary hooks and actions.
*/
class Plugin
{
/**
* Initializes the plugin by setting up the necessary hooks and actions.
*/
public function initialize()
{
$options = new Options();
$settingsPage = new SettingsPage();
$acfFieldOptionsModifier = new FieldOptionsModifier($options);
$webhooksRegistry = new WebhooksRegistry();

// Add settings page on plugin initialization
add_action('init', [$settingsPage, 'addPage']);

// Register webhooks on plugins loaded
add_action('plugins_loaded', [$webhooksRegistry, 'registerWebhooks'], 10);

// Modify ACF field options for 'action' and 'http_method' fields
add_filter('acf/load_field/name=action', [$acfFieldOptionsModifier, 'getActionFieldOptions']);
add_filter('acf/load_field/name=http_method', [$acfFieldOptionsModifier, 'getHttpMethodFieldOptions']);

// Bind webhooks to actions on plugins loaded
add_action('plugins_loaded', function () use ($webhooksRegistry) {
foreach ($webhooksRegistry->getWebhooks() as $webhook) {
$dispatcher = new WebhookDispatcher();
Expand Down
8 changes: 8 additions & 0 deletions source/php/SettingsPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

namespace WebhooksManager;

/**
* Class SettingsPage
*
* Represents the settings page for the Webhooks Manager plugin.
*/
class SettingsPage
{
public const SLUG = 'webhooks-manager';
public const PARENT_SLUG = 'tools.php';
public const CAPABILITY = 'manage_options';

/**
* Adds the settings page to the WordPress admin menu.
*/
public function addPage()
{
if (function_exists('acf_add_options_page')) {
Expand Down
44 changes: 44 additions & 0 deletions source/php/Webhook/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@

namespace WebhooksManager\Webhook;

/**
* Class Webhook
* Represents a webhook with its properties and methods.
*/
class Webhook implements WebhookInterface
{
/**
* Webhook constructor.
*
* @param string $payloadUrl The URL where the payload will be sent.
* @param string $httpMethod The HTTP method to be used for the webhook.
* @param string $action The action to be performed by the webhook.
* @param int $actionPriority The priority of the action.
* @param bool $shouldSendPayload Determines if the payload should be sent.
* @param bool $isActive Determines if the webhook is active.
*/
public function __construct(
private string $payloadUrl,
private string $httpMethod,
Expand All @@ -14,31 +28,61 @@ public function __construct(
) {
}

/**
* Get the payload URL of the webhook.
*
* @return string The payload URL.
*/
public function getPayloadUrl(): string
{
return $this->payloadUrl;
}

/**
* Get the HTTP method of the webhook.
*
* @return string The HTTP method.
*/
public function getHttpMethod(): string
{
return $this->httpMethod;
}

/**
* Get the action of the webhook.
*
* @return string The action.
*/
public function getAction(): string
{
return $this->action;
}

/**
* Get the action priority of the webhook.
*
* @return int The action priority.
*/
public function getActionPriority(): int
{
return $this->actionPriority;
}

/**
* Check if the payload should be sent.
*
* @return bool True if the payload should be sent, false otherwise.
*/
public function shouldSendPayload(): bool
{
return $this->shouldSendPayload;
}

/**
* Check if the webhook is active.
*
* @return bool True if the webhook is active, false otherwise.
*/
public function isActive(): bool
{
return $this->isActive;
Expand Down
38 changes: 38 additions & 0 deletions source/php/Webhook/WebhookInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,50 @@

namespace WebhooksManager\Webhook;

/**
* Interface for defining a webhook.
*/
interface WebhookInterface
{
/**
* Get the payload URL for the webhook.
*
* @return string The payload URL.
*/
public function getPayloadUrl(): string;

/**
* Get the HTTP method for the webhook.
*
* @return string The HTTP method.
*/
public function getHttpMethod(): string;

/**
* Get the action for the webhook.
*
* @return string The action.
*/
public function getAction(): string;

/**
* Get the action priority for the webhook.
*
* @return int The action priority.
*/
public function getActionPriority(): int;

/**
* Check if the webhook should send the payload.
*
* @return bool True if the payload should be sent, false otherwise.
*/
public function shouldSendPayload(): bool;

/**
* Check if the webhook is active.
*
* @return bool True if the webhook is active, false otherwise.
*/
public function isActive(): bool;
}
18 changes: 18 additions & 0 deletions source/php/WebhookActionBinder/WebhookActionBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,28 @@
use WebhooksManager\Webhook\WebhookInterface;
use WebhooksManager\WebhookDispatcher\WebhookDispatcherInterface;

/**
* Class WebhookActionBinder
*
* Binds a webhook to an action in WordPress.
*/
class WebhookActionBinder implements WebhookActionBinderInterface
{
/**
* WebhookActionBinder constructor.
*
* @param WebhookInterface $webhook
* @param WebhookDispatcherInterface $webhookDispatcher
*/
public function __construct(
private WebhookInterface $webhook,
private WebhookDispatcherInterface $webhookDispatcher
) {
}

/**
* Binds the webhook to the specified action in WordPress.
*/
public function bindWebhookToAction(): void
{
$action = $this->webhook->getAction();
Expand All @@ -22,6 +36,10 @@ public function bindWebhookToAction(): void
add_action($action, $callback, $priority, $acceptedArgs);
}

/**
* Callback function for the action.
* Dispatches the webhook if it is active.
*/
public function actionCallback(): void
{
if (!$this->webhook->isActive()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

namespace WebhooksManager\WebhookActionBinder;

/**
* Interface for binding webhooks to actions.
*/
interface WebhookActionBinderInterface
{
/**
* Binds a webhook to an action.
*
* @return void
*/
public function bindWebhookToAction(): void;
}
Loading

0 comments on commit c5c84f7

Please sign in to comment.