Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #11 from WebDevStudios/feature/update-revalidation
Browse files Browse the repository at this point in the history
update revalidation functionality
  • Loading branch information
gregrickaby authored Apr 26, 2022
2 parents a1d6222 + b3225a4 commit ac4b4ff
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 53 deletions.
51 changes: 0 additions & 51 deletions inc/links.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,57 +12,6 @@
use \WP_Post;
use \WP_REST_Response;


/**
* Flush the frontend cache when a post is updated.
*
* @see https://nextjs.org/docs/basic-features/data-fetching/incremental-static-regeneration#using-on-demand-revalidation
* @since 2.1.3
* @author WebDevStudios
*/
function on_demand_revalidation() {

// These constsants are required. If they're not here, bail...
if ( ! defined( 'HEADLESS_FRONTEND_URL' ) || ! defined( 'PREVIEW_SECRET_TOKEN' ) ) {
return;
}

// Attempt to get post object.
$post = get_post();

// No post object? Bail...
if ( ! $post ) {
return;
}

// Define the post slug.
$slug = str_replace( HEADLESS_FRONTEND_URL, '', get_the_permalink( $post->ID ) );

// No slug? Bail...
if ( ! $slug ) {
return;
}

// Send the POST request to the frontend.
wp_remote_post(
HEADLESS_FRONTEND_URL . 'api/wordpress/revalidate',
[
'blocking' => true,
'headers' => [
'Content-Type' => 'application/json',
'Expect' => '',
],
'body' => wp_json_encode(
[
'secret' => PREVIEW_SECRET_TOKEN,
'slug' => "/${slug}",
]
),
]
);
}
add_action( 'post_updated', __NAMESPACE__ . '\on_demand_revalidation', 10, 3 );

/**
* Customize the preview button in the WordPress admin to point to the headless client.
*
Expand Down
73 changes: 73 additions & 0 deletions inc/revalidation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Revalidation functionality.
*
* @author WebDevStudios
* @package WDS_Headless_Core
* @since 2.1.3
*/

namespace WDS_Headless_Core;

use \WP_Post;

/**
* Flush the frontend cache when a post is updated.
*
* This function will fire anytime a post is updated.
* Including: the post status, comments, meta, terms, etc.
*
* @see https://developer.wordpress.org/reference/hooks/edit_post/
* @see https://nextjs.org/docs/basic-features/data-fetching/incremental-static-regeneration#using-on-demand-revalidation
* @since 2.1.3
* @author WebDevStudios
* @param int $post_ID The post ID.
* @param WP_Post $post The post object.
*/
function on_demand_revalidation( $post_ID, WP_Post $post ) {

// These constsants are required. If they're not here, bail...
if ( ! defined( 'HEADLESS_FRONTEND_URL' ) || ! defined( 'PREVIEW_SECRET_TOKEN' ) ) {
return;
}

// No post ID? Bail...
if ( ! $post_ID ) {
return;
}

// Define the post slug and remove frontend URL.
$slug = str_replace( HEADLESS_FRONTEND_URL, '', get_the_permalink( $post_ID ) );

// No slug? Bail...
if ( ! $slug ) {
return;
}

// Send POST request to the frontend and revalidate the cache.
$response = wp_remote_post(
HEADLESS_FRONTEND_URL . 'api/wordpress/revalidate',
[
'blocking' => true,
'headers' => [
'Content-Type' => 'application/json',
'Expect' => '',
],
'body' => wp_json_encode(
[
'secret' => PREVIEW_SECRET_TOKEN,
'slug' => "/${slug}",
]
),
]
);

// Check response code.
$response_code = wp_remote_retrieve_response_code( $response );

// If there is an error, log it.
if ( 200 !== $response_code ) {
return;
}
}
add_action( 'edit_post', __NAMESPACE__ . '\on_demand_revalidation', 10, 3 );
5 changes: 3 additions & 2 deletions wds-headless-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Description: The core WordPress plugin for the Next.js WordPress Starter.
* Author: WebDevStudios <[email protected]>
* Author URI: https://webdevstudios.com
* Version: 2.1.3
* Version: 2.1.4
* Requires at least: 5.6
* Requires PHP: 7.4
* License: GPL-2
Expand All @@ -23,7 +23,7 @@
// Define constants.
define( 'WDS_HEADLESS_CORE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'WDS_HEADLESS_CORE_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'WDS_HEADLESS_CORE_VERSION', '2.1.3' );
define( 'WDS_HEADLESS_CORE_VERSION', '2.1.4' );
define( 'WDS_HEADLESS_CORE_OPTION_NAME', 'headless_config' );

// Register de/activation hooks.
Expand All @@ -36,3 +36,4 @@
require_once 'inc/menus.php';
require_once 'inc/settings.php';
require_once 'inc/wp-graphql.php';
require_once 'inc/revalidation.php';

0 comments on commit ac4b4ff

Please sign in to comment.