-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrest_rvy.php
116 lines (89 loc) · 3 KB
/
rest_rvy.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class Revisionary_REST {
var $request = '';
var $method = '';
var $is_view_method = false;
var $endpoint_class = '';
var $post_type = '';
var $post_id = 0;
var $operation = '';
var $is_posts_request = false;
function __construct() {
add_filter( 'pp_rest_post_cap_requirement', array( $this, 'rest_post_cap_requirement' ), 10, 2 );
}
function rest_post_cap_requirement( $orig_cap, $item_id ) {
if ( 'edit' == $this->operation ) {
$post_type = get_post_field( 'post_type', $item_id );
if ( $type_obj = get_post_type_object( $post_type ) ) {
if ( $orig_cap == $type_obj->cap->read_post ) {
$orig_cap = 'edit_post';
}
}
}
return $orig_cap;
}
function pre_dispatch( $rest_response, $rest_server, $request ) {
$this->method = $request->get_method();
$path = $request->get_route();
foreach ( $rest_server->get_routes() as $route => $handlers ) {
if ( ! $match = @preg_match( '@^' . $route . '$@i', $path, $args ) )
continue;
foreach ( $handlers as $handler ) {
if ( ! empty( $handler['methods'][ $this->method ] ) && is_array($handler['callback']) && is_object($handler['callback'][0]) ) {
$this->endpoint_class = get_class( $handler['callback'][0] );
$compatible_endpoints = apply_filters(
'revisionary_rest_post_endpoints',
['WP_REST_Posts_Controller', 'LD_REST_Posts_Gutenberg_Controller']
);
if (!in_array($this->endpoint_class, $compatible_endpoints)) {
continue;
}
$this->request = $request;
$this->is_view_method = in_array( $this->method, array( WP_REST_Server::READABLE, 'GET' ) );
$post_id = self::get_id_element( $path );
if (is_numeric($post_id)) {
// back post type out of path because controller object does not expose it
$type_base = $this->get_path_element( $path );
$this->post_type = $this->get_type_from_rest_base( $type_base );
$this->post_id = $post_id;
$this->is_posts_request = true;
}
}
}
}
return $rest_response;
} // end function pre_dispatch
public static function get_id_element( $path, $position_from_right = 0 ) {
$arr_path = explode( '/', $path );
$count = -1;
for( $i=count($arr_path) - 1; $i>=0; $i-- ) {
$count++;
if ( $count == $position_from_right )
return $arr_path[$i];
}
return '';
}
function get_path_element( $path, $string_num_from_right = 1 ) {
$arr_path = explode( '/', $path );
$count = 0;
for( $i=count($arr_path) - 1; $i>=0; $i-- ) {
if ( is_numeric( $arr_path[$i] ) )
continue;
$count++;
if ( $count == $string_num_from_right )
return $arr_path[$i];
}
return '';
}
private function get_type_from_rest_base( $rest_base ) {
if ( $types = get_post_types( array( 'rest_base' => $rest_base ) ) ) {
$post_type = reset( $types );
return $post_type;
} elseif( post_type_exists( $rest_base ) ) {
return $rest_base;
} else {
return false;
}
}
}