| 1 |
<?php |
| 2 |
/** |
| 3 |
* Callback for WordPress actions. |
| 4 |
* |
| 5 |
* @package FaustWP |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPE\FaustWP\Events; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
add_action( 'update_option_rewrite_rules', __NAMESPACE__ . '\\rewrite_rules_change', 10, 3 ); |
| 15 |
/** |
| 16 |
* Callback for WordPress 'update_option_rewrite_rules' action. |
| 17 |
* |
| 18 |
* Detect when the rewrite rules have changed. |
| 19 |
* |
| 20 |
* Store a hash of the current rewrite rules to prevent multiple api requests. |
| 21 |
* |
| 22 |
* @param string $old_value The old option value. |
| 23 |
* @param string $new_value The new option value. |
| 24 |
* @param string $option_name The option name. |
| 25 |
* |
| 26 |
* @return void |
| 27 |
*/ |
| 28 |
function rewrite_rules_change( $old_value, $new_value, $option_name ) { |
| 29 |
if ( '' !== $new_value ) { |
| 30 |
$existing_hash = get_option( 'rewrite_rules_hash' ); |
| 31 |
$new_hash = md5( wp_json_encode( $new_value ) ); |
| 32 |
|
| 33 |
if ( $existing_hash !== $new_hash ) { |
| 34 |
update_option( 'rewrite_rules_hash', $new_hash ); |
| 35 |
|
| 36 |
error_log( 'Rewrite rules changed...' ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
add_action( 'post_updated', __NAMESPACE__ . '\\check_post_slug', 10, 3 ); |
| 42 |
/** |
| 43 |
* Callback for WordPress 'post_updated' action. |
| 44 |
* |
| 45 |
* Detect a post or page slug change on published posts. |
| 46 |
* |
| 47 |
* @param int $post_ID Post ID. |
| 48 |
* @param WP_Post $post_after Post object following the update. |
| 49 |
* @param WP_Post $post_before Post object before the update. |
| 50 |
* |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
function check_post_slug( $post_ID, $post_after, $post_before ) { |
| 54 |
if ( ! in_array( $post_after->post_type, array( 'post', 'page' ), true ) ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
// Ensure post status is 'publish'. |
| 59 |
if ( 'publish' !== $post_before->post_status || 'publish' !== $post_after->post_status ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
// Ensure post_name (slug) is not empty. |
| 64 |
if ( ! $post_after->post_name ) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
// Ensure post_name has changed. |
| 69 |
if ( $post_after->post_name === $post_before->post_name ) { |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
error_log( "Post {$post_ID} slug changed..." ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 74 |
} |
| 75 |
|
| 76 |
add_action( 'draft_to_publish', __NAMESPACE__ . '\\post_status_changed' ); |
| 77 |
add_action( 'publish_to_draft', __NAMESPACE__ . '\\post_status_changed' ); |
| 78 |
add_action( 'publish_to_trash', __NAMESPACE__ . '\\post_status_changed' ); |
| 79 |
/** |
| 80 |
* Callback for WordPress "{$old_status}_to_{$new_status}" action. |
| 81 |
* |
| 82 |
* Detect post status changes for post and page post types. |
| 83 |
* |
| 84 |
* @param WP_Post $post Post object. |
| 85 |
* |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
function post_status_changed( $post ) { |
| 89 |
if ( ! in_array( $post->post_type, array( 'post', 'page' ), true ) ) { |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
error_log( "Post {$post->ID} set to {$post->post_status}" ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 94 |
} |
| 95 |
|