-
Notifications
You must be signed in to change notification settings - Fork 21
/
RevisionSupport.php
85 lines (73 loc) · 2.01 KB
/
RevisionSupport.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
* RevisionSupport
*
* @package convert-to-blocks
*/
namespace ConvertToBlocks;
/**
* RevisionSupport ensures that User navigation across the WordPress Revision
* Screens.
*/
class RevisionSupport {
/**
* Parent plugin object
*
* @var Plugin
*/
public $container;
/**
* Registers the revision support with WordPress.
*
* @return void
*/
public function register() {
add_action( 'wp_redirect', [ $this, 'update_redirect' ], 10, 2 );
add_filter( 'get_edit_post_link', [ $this, 'update_edit_post_link' ] );
add_filter( 'wp_prepare_revision_for_js', [ $this, 'update_revision_js' ], 10, 3 );
}
/**
* Only register in admin context.
*/
public function can_register() {
return is_admin();
}
/**
* If referer was from classic editor or on CE forward back to classic editor.
*
* @param string $location The redirect location
* @param string $status The redirect status code
* @return string
*/
public function update_redirect( $location, $status ) {
if ( $this->container->is_classic_referer() || $this->container->has_classic_param() ) {
$location = add_query_arg( [ 'classic' => 1 ], $location );
}
return $location;
}
/**
* Updates post link if originating from a classic editor page
*
* @param string $url The post url
* @return string
*/
public function update_edit_post_link( $url ) {
if ( $this->container->is_classic_post_referer() ) {
return add_query_arg( [ 'classic' => 1 ], $url );
}
return $url;
}
/**
* Ensures that restoring revision from Classic Editor takes user back to CE.
*
* @param array $revision_data The revision data array
* @param array $revision The revision meta
* @param WP_Post $post The revision post object
*/
public function update_revision_js( $revision_data, $revision, $post ) {
if ( $this->container->is_classic_referer() || $this->container->has_classic_param() ) {
$revision_data['restoreUrl'] = add_query_arg( [ 'classic' => 1 ], $revision_data['restoreUrl'] );
}
return $revision_data;
}
}