This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from WebDevStudios/feature/update-revalidation
update revalidation functionality
- Loading branch information
Showing
3 changed files
with
76 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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. | ||
|
@@ -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'; |