Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/allow custom taxonomies #7

Merged
merged 2 commits into from
Jun 6, 2023
Merged
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
2 changes: 1 addition & 1 deletion on-demand-revalidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Plugin URI: https://wordpress.org/plugins/on-demand-revalidation
* GitHub Plugin URI: https://github.com/gdidentity/on-demand-revalidation
* Description: Next.js On-Demand Revalidation on the post update, revalidate specific paths on the post update.
* Version: 1.0.16
* Version: 1.1.0
* Author: GD IDENTITY
* Author URI: https://gdidentity.sk
* Text Domain: on-demand-revalidation
Expand Down
2 changes: 2 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Feel free to create PR to [plugin Github repo](https://github.com/gdidentity/on-


== Changelog ==
= 1.1.0 =
- Allow custom taxonomies revalidation from @humet

= 1.0.16 =
- fix from @slimzc
Expand Down
4 changes: 2 additions & 2 deletions src/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public function register_settings() {
[
'name' => 'revalidate_paths',
'label' => __( 'Additional paths to revalidate on Post update', 'on-demand-revalidation' ),
'desc' => 'One path per row.<br/><br/><i>Available current Post placeholders:</i><br/><code>%slug%</code> <code>%author_nicename%</code> <code>%categories%</code> <code>%tags%</code>',
'placeholder' => '/category/%categories%',
'desc' => 'One path per row.<br/><br/><i>Available current Post placeholders:</i><br/><code>%slug%</code> <code>%author_nicename%</code> <code>%author_username%</code> <code>%category%</code> <code>%post_tag%</code> <code>%custom_taxonomy%</code><br/><br/><i>Note:</i> Replace <code>%custom_taxonomy%</code> with your custom taxonomy name.',
'placeholder' => '/category/%category%',
'type' => 'textarea',
],
[
Expand Down
56 changes: 40 additions & 16 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,48 @@ public static function rewritePaths( $paths, $post ) {

foreach ( $paths as $path ) {
$path = trim( $path );

if ( strpos( $path, '%slug%' ) !== false ) {
$final_paths[] = str_replace( '%slug%', $post->post_name, $path );
} elseif ( strpos( $path, '%author_nicename%' ) !== false ) {
$final_paths[] = str_replace( '%author_nicename%', get_the_author_meta( 'user_nicename', $post->post_author ), $path );
} elseif ( strpos( $path, '%categories%' ) !== false ) {
$categories = wp_get_post_categories( $post->ID, [ 'fields' => 'slugs' ] ) ?? [];
foreach ( $categories as $category ) {
$final_paths[] = str_replace( '%categories%', $category, $path );
}
} elseif ( strpos( $path, '%tags%' ) !== false ) {
$tags = wp_get_post_tags( $post->ID, [ 'fields' => 'slugs' ] ) ?? [];
foreach ( $tags as $tag ) {
$final_paths[] = str_replace( '%tags%', $tag, $path );

// Match all placeholders in the path
preg_match_all( '/%(.+?)%/', $path, $matches );
$placeholders = $matches[1];

$current_paths = [ $path ];

foreach ( $placeholders as $placeholder ) {
$new_paths = [];

foreach ( $current_paths as $current_path ) {
if ( 'slug' === $placeholder ) {
$new_paths[] = str_replace( '%slug%', $post->post_name, $current_path );
} elseif ( 'author_nicename' === $placeholder ) {
$new_paths[] = str_replace( '%author_nicename%', get_the_author_meta( 'user_nicename', $post->post_author ), $current_path );
} elseif ( 'author_username' === $placeholder ) {
$new_paths[] = str_replace( '%author_username%', get_the_author_meta( 'user_login', $post->post_author ), $current_path );
} elseif ( 'categories' === $placeholder ) {
$terms = wp_get_post_terms( $post->ID, 'category', [ 'fields' => 'slugs' ] ) ?? [];
foreach ( $terms as $term ) {
$new_paths[] = str_replace( '%categories%', $term, $current_path );
}
} elseif ( 'tags' === $placeholder ) {
$terms = wp_get_post_terms( $post->ID, 'post_tag', [ 'fields' => 'slugs' ] ) ?? [];
foreach ( $terms as $term ) {
$new_paths[] = str_replace( '%tags%', $term, $current_path );
}
} elseif ( in_array( $placeholder, get_post_taxonomies( $post ), true ) ) {
$terms = wp_get_post_terms( $post->ID, $placeholder, [ 'fields' => 'slugs' ] ) ?? [];
foreach ( $terms as $term ) {
$new_paths[] = str_replace( '%' . $placeholder . '%', $term, $current_path );
}
} else {
$new_paths[] = $current_path;
}
}
} else {
$final_paths[] = $path;
$current_paths = $new_paths;
}

// Add the paths to the final array
$final_paths = array_merge( $final_paths, $current_paths );
}

return $final_paths;
Expand Down
Loading