Automating Netlify Builds With a WordPress Plugin

Happy to relay that I have coded a WordPress plugin that automates rebuilding a static frontend on netlify when you modify the post status of your WordPress content. In fact, it’s how this post was deployed. Fancy!

I am going to try to do a blog post a day for a while (“hah!” says everyone who ever said that), so maybe I’ll write about it some more beyond this quick announcement post, but it’s actually a super simple thing.

First, set up an array of actions that trigger builds but aren’t an update (those have to be handled separately):

/**
  * Post Status Transitions
  */
$this->transitions = [
    'draft_to_publish',
    'publish_to_draft',
    'publish_to_trash',
    'publish_to_private',
    'private_to_public',
    'new_to_publish',
];

And then give people a way to modify it:

/**
  * Filter status transitions
  * @var array
  */
public function filterTransitions()
{
    has_filter('netlify_transitions') &&
        apply_filters('netlify_transitions', $this->transitions);
    return $this;
}

Check for environmental variables so Bedrock/Trellis users can easily configure it:

/**
  * Sets webhook URLs for POSTing
  * @return self $this
  */
public function setWebhooks()
{
    $this->hooks = (object) [
        'development' => self::getNetlifyHook('DEVELOPMENT')->value,
        'staging'     => self::getNetlifyHook('STAGING')->value,
        'production'  => self::getNetlifyHook('PRODUCTION')->value,
    ];
    return $this;
}

/**
  * Return hook for a given environment
  * @param string $environment
  * @return object
  */
public static function getNetlifyHook($environment)
{
    return (object) [
        'key'   => "NETLIFY_WEBHOOK_{$environment}",
        'value' => env("NETLIFY_WEBHOOK_{$environment}"),
    ];
}

And then give people a way to easily override that, too.

/**
  * Filters envvar overrides
  * @return self $this
  */
public function filterOverrides()
{
    $this->override = '';
    has_filter('netlify_env_override') &&
        apply_filters('netlify_env_override', $this->override);
    return $this;
}

Specify a sane set of default content types to trigger the webhook

/**
  * Default PostTypes
  * @var array
  */
public $postTypes = [
    'post',
    'page',
];

And then, you guessed it, let people mix it up.

/**
  * Filter PostTypes that are set to trigger webhook
  * @param array postTypes
  * @return self $this
  */
public function filterPostTypes()
{
    has_filter('netlify_posttypes') &&
        apply_filters('netlify_posttypes', $this->plugin->postTypes);
    $this->postTypes = $this->plugin->postTypes;
    return $this;
}

All that said and done, figure out what we’re even doing anymore:

/**
  * Set target hook, accounting for overrides
  * @return self $this
  */
public function setTargetHook()
{
    if ($this->override !== '') {
        $this->targetHook = $this->override;
    } elseif ($this->plugin->runtime->env) {
        $this->targetHook = self::getNetlifyHook(
            $this->plugin->runtime->env
        )->value;
    }
    return $this;
}

And then do it!

/**
  * Hooks into WordPress lifecycle
  * @return self $this
  */
public function addActions()
{
    foreach ($this->transitions as $transition) {
        add_action($transition, [
            $this, 'onStatusTransition'
        ], 10, 3);
    }

    foreach ($this->postTypes as $postType) {
        add_action("publish_{$postType}", [
            $this, 'postToWebhook',
        ], 10, 2);
    }
    return $this;
}

/**
  * On Status Transition
  * @param WP_Post $post
  */
public function onStatusTransition($post)
{
    if (in_array($post->post_type, $this->postTypes)) {
        $this->postToWebhook();
    }
}

/**
  * POSTs to appropriate webhook on publish actions
  */
public function postToWebhook()
{
    // make the run
    $this->targetHook &&
        $this->client->post($this->targetHook);
}

Works like a charm. You can check out the full source on Github. It’s also available to download with Composer.

Developing Gutenberg Blocks with Parcel.js and Astroturf
Up Next

Developing Gutenberg Blocks with Parcel.js and Astroturf

Developing Gutenberg Blocks with Parcel.js and Astroturf