Skip to content
This repository was archived by the owner on Feb 26, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ You can access the plugin's settings in WordPress by accessing the 'Settings' pa

From this screen you can configure the following:

- **Webhook URL** - The webhook URL that you have created to trigger a deployment. For more information on webhooks with Netlify [visit the Netlify documentation](https://www.netlify.com/docs/webhooks/).
- **Webhook URL** - The webhook URL that you have created to trigger a deployment. For more information on webhooks with Netlify [visit the Netlify documentation](https://www.netlify.com/docs/webhooks/) or a workflow dispatch in Github Actions [visit Github API documentation](https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event).
- **Access Token** - Your Build Hook access token. Needed for Github API authentication. Create a personal access token for your Github account [here](https://github.com/settings/tokens)
- **Workflow Run Reference** - The reference of a Github workflow run. The reference can be a branch, tag, or a commit SHA.
- **Webhook Method** - This is the required method for the webhook request. The available options are `GET` or `POST`. By default the plugin will automatically select `POST`.
- **Badge Image URL** - An optional field to specify the `src` of a badge, for services that support badges.
- **Badge Link** - An optional field to specify the `href` of a badge, for services that support badges.
Expand Down
42 changes: 38 additions & 4 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,52 @@

class Field
{
/**
* Render an input[type=text] field
*
* @param array $args
* @return void
*/
public static function text($args = [])
{
self::render_field('text', $args);
}

/**
* Render an input[type=password] field
*
* @param array $args
* @return void
*/
public static function password($args = [])
{
self::render_field('password', $args);
}

/**
* Render an input[type=url] field
*
* @param array $args
* @return void
*/
public static function url($args = [])
{
self::render_field('url', $args);
}

/**
* Render an input[type=url] field
*
* @param string $type
* @param array $args
* @return void
*/
private static function render_field($type, $args = [])
{
?><div>
<input type="url" class="regular-text" name="<?= esc_attr($args['name']); ?>" value="<?= esc_url($args['value']); ?>">
<?= !empty($args['description']) ? "<p class=\"description\">{$args['description']}</p>" : ''; ?>
</div><?php
<input type="<?= $type ?>" class="regular-text" name="<?= esc_attr($args['name']); ?>" value="<?= $type == 'url' ? esc_url($args['value']) : esc_attr($args['value']); ?>">
<?= !empty($args['description']) ? "<p class=\"description\">{$args['description']}</p>" : ''; ?>
</div><?php
}

/**
Expand Down Expand Up @@ -43,7 +77,7 @@ public static function select($args = [])
* @return void
*/
public static function checkboxes($args = [])
{
{
$args['value'] = is_array($args['value']) ? $args['value'] : [$args['value']];

?><fieldset>
Expand Down
20 changes: 18 additions & 2 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,20 @@ public static function register()
add_settings_field('webhook_url', __( 'Build Hook URL', 'wp-jamstack-deployments' ), ['Crgeary\JAMstackDeployments\Field', 'url'], $key, 'general', [
'name' => "{$key}[webhook_url]",
'value' => jamstack_deployments_get_webhook_url(),
'description' => sprintf( __( 'Your Build Hook URL. This is the URL that is pinged to start building/deploying the JAMstack site. See <a href="%1s" target="_blank" rel="noopener noreferrer">Netlify docs</a> or see <a href="%2s" target="_blank" rel="noopener noreferrer">Zeit docs</a>.', 'wp-jamstack-deployments' ), 'https://docs.netlify.com/configure-builds/build-hooks/', 'https://zeit.co/docs/v2/advanced/deploy-hooks/' )
]);
'description' => sprintf( __( 'Your Build Hook URL. This is the URL that is pinged to start building/deploying the JAMstack site. See <a href="%1s" target="_blank" rel="noopener noreferrer">Netlify docs</a> or see <a href="%2s" target="_blank" rel="noopener noreferrer">Zeit docs</a> or see <a href="%3s" target="_blank" rel="noopener noreferrer">Github API docs</a>.', 'wp-jamstack-deployments' ), 'https://docs.netlify.com/configure-builds/build-hooks/', 'https://zeit.co/docs/v2/advanced/deploy-hooks/', 'https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event' )
]);

add_settings_field('access_token', __( 'Access Token', 'wp-jamstack-deployments' ), ['Crgeary\JAMstackDeployments\Field', 'password'], $key, 'general', [
'name' => "{$key}[access_token]",
'value' => jamstack_deployments_get_access_token(),
'description' => sprintf( __( 'Your Build Hook access token. Needed for Github API authentication. Create a personal access token for your Github account <a href="%1s">here</a>', 'wp-jamstack-deployments' ), 'https://github.com/settings/tokens' )
]);

add_settings_field('workflow_run_reference', __( 'Workflow Run Reference', 'wp-jamstack-deployments' ), ['Crgeary\JAMstackDeployments\Field', 'text'], $key, 'general', [
'name' => "{$key}[workflow_run_reference]",
'value' => jamstack_deployments_get_workflow_run_reference(),
'description' => __( 'The reference of a Github workflow run. The reference can be a branch, tag, or a commit SHA.', 'wp-jamstack-deployments' )
]);

add_settings_field('webhook_method', __( 'Hook Method', 'wp-jamstack-deployments' ), ['Crgeary\JAMstackDeployments\Field', 'select'], $key, 'general', [
'name' => "{$key}[webhook_method]",
Expand Down Expand Up @@ -164,6 +176,10 @@ public static function sanitize($input)
$input['webhook_url'] = trim($input['webhook_url']);
}

if (!empty($input['access_token'])) {
$input['access_token'] = trim($input['access_token']);
}

if (isset($input['webhook_method']) && !in_array($input['webhook_method'], ['get', 'post'])) {
$input['webhook_method'] = 'post';
}
Expand Down
37 changes: 29 additions & 8 deletions src/WebhookTrigger.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static function init()

add_action('admin_footer', [__CLASS__, 'adminBarCssAndJs']);
add_action('wp_footer', [__CLASS__, 'adminBarCssAndJs']);

add_action('wp_enqueue_scripts', [__CLASS__, 'enqueueScripts']);
add_action('admin_enqueue_scripts', [__CLASS__, 'enqueueScripts']);

Expand Down Expand Up @@ -104,7 +104,7 @@ public static function triggerEditTerm($id, $tax_id, $tax_slug)
if (!self::canFireForTaxonomy($id, $tax_id, $tax_slug)) {
return;
}

self::fireWebhook();
}

Expand All @@ -126,7 +126,7 @@ protected static function canFireForTaxonomy($id, $tax_id, $tax_slug)

/**
* Show the admin bar css & js
*
*
* @todo move this somewhere else
* @return void
*/
Expand Down Expand Up @@ -163,7 +163,7 @@ public static function adminBarCssAndJs()

/**
* Enqueue js to the admin & frontend
*
*
* @return void
*/
public static function enqueueScripts()
Expand Down Expand Up @@ -234,7 +234,7 @@ public static function trigger()
if (!isset($_GET['action']) || 'jamstack-deployment-trigger' !== $_GET['action']) {
return;
}

check_admin_referer('crgeary_jamstack_deployment_trigger', 'crgeary_jamstack_deployment_trigger');

self::fireWebhook();
Expand All @@ -256,7 +256,7 @@ public static function triggerPostTransition($new, $old, $post)
{
$id = $post->ID;
$option = jamstack_deployments_get_options();

$saved_post_types = isset($option['webhook_post_types']) ? $option['webhook_post_types'] : [];
$post_types = apply_filters('jamstack_deployments_post_types', $saved_post_types, $id, $post);

Expand Down Expand Up @@ -306,9 +306,30 @@ public static function fireWebhook()
return;
}

$args = apply_filters('jamstack_deployments_webhook_request_args', [
$args = [
'blocking' => false
]);
];

$access_token = jamstack_deployments_get_access_token();

if ($access_token) {
$args['headers'] = [
'Content-Type' => 'application/json',
'Authorization' => "Bearer {$access_token}"
];
}

$workflow_run_reference = jamstack_deployments_get_workflow_run_reference();

if ($workflow_run_reference) {
$args['body'] = wp_json_encode([
'ref' => $workflow_run_reference
]);
$args['method'] = 'POST';
$args['data_format'] = 'body';
}

$args = apply_filters('jamstack_deployments_webhook_request_args', $args);

$method = jamstack_deployments_get_webhook_method();

Expand Down
24 changes: 24 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,30 @@ function jamstack_deployments_get_webhook_url() {
}
}

if (!function_exists('jamstack_deployments_get_access_token')) {
/**
* Return the webhook url
*
* @return string|null
*/
function jamstack_deployments_get_access_token() {
$options = jamstack_deployments_get_options();
return isset($options['access_token']) ? $options['access_token'] : null;
}
}

if (!function_exists('jamstack_deployments_get_workflow_run_reference')) {
/**
* Return the webhook url
*
* @return string|null
*/
function jamstack_deployments_get_workflow_run_reference() {
$options = jamstack_deployments_get_options();
return isset($options['workflow_run_reference']) ? $options['workflow_run_reference'] : null;
}
}

if (!function_exists('jamstack_deployments_get_webhook_method')) {
/**
* Return the webhook method (get/post)
Expand Down