From f487d2bb08d39d833b3b9dcd1cd1e565a8f65b2e Mon Sep 17 00:00:00 2001 From: goncaloasimoes <32324608+goncaloasimoes@users.noreply.github.com> Date: Wed, 1 Feb 2023 17:22:36 +0000 Subject: [PATCH] Fix query map issue and allow for correct edit links for unreleased/revision. --- lib/Hooks/Admin.php | 3 ++- lib/Hooks/Query.php | 2 +- lib/Hooks/Revision.php | 59 +++++++++++++++++++++++++++++++++++++++--- 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/lib/Hooks/Admin.php b/lib/Hooks/Admin.php index 0d92294..2e3e104 100644 --- a/lib/Hooks/Admin.php +++ b/lib/Hooks/Admin.php @@ -32,6 +32,8 @@ public function register() : void { // Edit the callback for the submit meta box. add_action( 'edit_form_after_editor', [ $this, 'edit_form_after_editor' ], PHP_INT_MAX ); + + // TODO: Quick edit still allows for publishing (and other statuses). } /** @@ -107,7 +109,6 @@ public function edit_form_after_editor() : void { return; } - // Check if the current screen is for a post_type that is versioned. $post_type = get_current_screen()->post_type; if ( ! Options::is_post_type_versioned( $post_type ) ) { diff --git a/lib/Hooks/Query.php b/lib/Hooks/Query.php index f0aed2d..1acbd8c 100644 --- a/lib/Hooks/Query.php +++ b/lib/Hooks/Query.php @@ -49,7 +49,7 @@ public function map_results_to_latest_version( array $posts, WP_Query $query ) : * @param array $posts * @param WP_Query $query */ - if ( ! apply_filters( 'post_version_show_unreleased', false, $posts, $query ) ) { + if ( apply_filters( 'post_version_show_unreleased', false, $posts, $query ) ) { return $posts; } diff --git a/lib/Hooks/Revision.php b/lib/Hooks/Revision.php index 55d7523..b01684d 100644 --- a/lib/Hooks/Revision.php +++ b/lib/Hooks/Revision.php @@ -24,23 +24,29 @@ class Revision { public function register() : void { // Stop revision's from being deleted when they are the last revision. - add_action( 'pre_delete_post', [ $this, 'stop_revision_delete' ], PHP_INT_MAX, 3 ); + add_filter( 'pre_delete_post', [ $this, 'stop_revision_delete' ], PHP_INT_MAX, 3 ); // Add filter to copy meta and terms to revision from original posts. add_action( 'wp_insert_post', [ $this, 'add_filter_copy_meta_terms' ], PHP_INT_MAX, 3 ); // Check for changes in the post's meta or terms when its saved. - add_action( 'wp_save_post_revision_check_for_changes', [ $this, 'wp_save_post_revision_check_for_changes' ], PHP_INT_MAX, 10 ); + add_filter( 'wp_save_post_revision_check_for_changes', [ $this, 'wp_save_post_revision_check_for_changes' ], PHP_INT_MAX, 10 ); // Allow for more post statues for revisions during queries. add_action( 'pre_get_posts', [ $this, 'allow_more_statuses_for_revisions' ], PHP_INT_MAX ); // Add version to formatted revision title. - add_action( 'wp_post_revision_title_expanded', [ $this, 'add_version_to_revision_formatted_title' ], PHP_INT_MAX, 3 ); + add_filter( 'wp_post_revision_title_expanded', [ $this, 'add_version_to_revision_formatted_title' ], PHP_INT_MAX, 3 ); + + // Fix revision post type object. Needed for missing admin bar buttons when loading old versions in the frontend. + add_action( 'init', [ $this, 'update_revision_post_type_object' ] ); + + // Return post parent edit link in most cases for a revision edit. + add_filter( 'get_edit_post_link', [ $this, 'get_edit_post_link' ], PHP_INT_MAX, 3 ); // Add terms diff to revision diff. // TODO: Add non ACF meta to diff. - add_action( 'wp_get_revision_ui_diff', [ $this, 'add_info_to_revision_diff' ], PHP_INT_MAX, 3 ); + add_filter( 'wp_get_revision_ui_diff', [ $this, 'add_info_to_revision_diff' ], PHP_INT_MAX, 3 ); } /** @@ -460,6 +466,51 @@ public function add_version_to_revision_formatted_title( string $revision_date_a return $revision_date_author; } + /** + * Update revision post type object. + * + * @since 0.0.2 + * + * @return void + */ + public function update_revision_post_type_object() : void { + global $wp_post_types; + + if ( ! isset( $wp_post_types['revision'] ) ) { + return; + } + + $wp_post_types['revision']->show_in_admin_bar = true; + } + + /** + * Return revision's post_parent's edit link in most context's. + * + * @since 0.0.2 + * + * @param string $link + * @param int $post_id + * @param string $context + * @return string + */ + public function get_edit_post_link( string $link, int $post_id, string $context ) : string { + $post = get_post( $post_id ); + if ( + ! $post instanceof WP_Post + || $post->post_type !== 'revision' + || ! Options::is_post_type_versioned( get_post( $post->post_parent )->post_type ) + ) { + return $link; + } + + // In post edit, we need to return the normal link to the revisions edit page. + if ( is_admin() && get_current_screen()->base === 'post' ) { + return $link; + } + + return get_edit_post_link( $post->post_parent ); + } + /** * Add information to revision diff. *