|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @since 3.31.0 |
|
4
|
|
|
* @author Naveen Muthusamy <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Wordlift\Videoobject\Api; |
|
8
|
|
|
|
|
9
|
|
|
use Wordlift\Videoobject\Background_Process\Videoobject_Background_Process; |
|
10
|
|
|
use Wordlift\Videoobject\Data\Video_Storage\Video_Storage_Factory; |
|
11
|
|
|
use Wordlift\Videoobject\Data\Video\Video; |
|
12
|
|
|
use WP_REST_Server; |
|
13
|
|
|
|
|
14
|
|
|
class Rest_Controller { |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Videoobject_Background_Process |
|
17
|
|
|
*/ |
|
18
|
|
|
private $background_process; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Rest_Controller constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param $background_process Videoobject_Background_Process |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct( $background_process ) { |
|
26
|
|
|
$this->background_process = $background_process; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function register_all_routes() { |
|
30
|
|
|
$that = $this; |
|
31
|
|
|
add_action( 'rest_api_init', function () use ( $that ) { |
|
32
|
|
|
$that->register_get_all_videos_route(); |
|
33
|
|
|
$that->register_save_all_videos_route(); |
|
34
|
|
|
$that->register_get_sync_state_endpoint(); |
|
35
|
|
|
$that->register_background_process_start_endpoint(); |
|
36
|
|
|
$that->register_background_process_stop_endpoint(); |
|
37
|
|
|
} ); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function get_all_videos( $request ) { |
|
41
|
|
|
$data = $request->get_params(); |
|
42
|
|
|
$post_id = (int) $data['post_id']; |
|
43
|
|
|
$storage = Video_Storage_Factory::get_storage(); |
|
44
|
|
|
|
|
45
|
|
|
return $storage->get_all_videos( $post_id ); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function save_all_videos( $request ) { |
|
49
|
|
|
$data = $request->get_params(); |
|
50
|
|
|
$post_id = (int) $data['post_id']; |
|
51
|
|
|
$videos = (array) $data['videos']; |
|
52
|
|
|
if ( ! $videos ) { |
|
|
|
|
|
|
53
|
|
|
return; |
|
54
|
|
|
} |
|
55
|
|
|
$storage = Video_Storage_Factory::get_storage(); |
|
56
|
|
|
$storage->remove_all_videos( $post_id ); |
|
57
|
|
|
|
|
58
|
|
|
foreach ( $videos as $video ) { |
|
59
|
|
|
$video_obj = new Video(); |
|
60
|
|
|
$video_obj->from( (array) $video ); |
|
61
|
|
|
$storage->add_video( $post_id, $video_obj ); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
View Code Duplication |
private function register_get_all_videos_route() { |
|
|
|
|
|
|
68
|
|
|
register_rest_route( |
|
69
|
|
|
WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
70
|
|
|
'/videos', |
|
71
|
|
|
array( |
|
72
|
|
|
'methods' => WP_REST_Server::CREATABLE, |
|
73
|
|
|
'callback' => array( $this, 'get_all_videos' ), |
|
74
|
|
|
'permission_callback' => function () { |
|
75
|
|
|
return current_user_can( 'manage_options' ); |
|
76
|
|
|
}, |
|
77
|
|
|
'args' => array( |
|
78
|
|
|
'post_id' => array( |
|
79
|
|
|
'validate_callback' => function ( $param, $request, $key ) { |
|
|
|
|
|
|
80
|
|
|
return is_numeric( $param ) && $param; |
|
81
|
|
|
}, |
|
82
|
|
|
'required' => true, |
|
83
|
|
|
), |
|
84
|
|
|
), |
|
85
|
|
|
) |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
View Code Duplication |
private function register_save_all_videos_route() { |
|
|
|
|
|
|
90
|
|
|
register_rest_route( |
|
91
|
|
|
WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
92
|
|
|
'/videos/save', |
|
93
|
|
|
array( |
|
94
|
|
|
'methods' => WP_REST_Server::CREATABLE, |
|
95
|
|
|
'callback' => array( $this, 'save_all_videos' ), |
|
96
|
|
|
'permission_callback' => function () { |
|
97
|
|
|
return current_user_can( 'manage_options' ); |
|
98
|
|
|
}, |
|
99
|
|
|
'args' => array( |
|
100
|
|
|
'post_id' => array( |
|
101
|
|
|
'validate_callback' => function ( $param, $request, $key ) { |
|
|
|
|
|
|
102
|
|
|
return is_numeric( $param ) && $param; |
|
103
|
|
|
}, |
|
104
|
|
|
'required' => true, |
|
105
|
|
|
), |
|
106
|
|
|
'videos' => array( |
|
107
|
|
|
'validate_callback' => function ( $param, $request, $key ) { |
|
|
|
|
|
|
108
|
|
|
return is_array( $param ) && $param; |
|
|
|
|
|
|
109
|
|
|
}, |
|
110
|
|
|
'required' => true, |
|
111
|
|
|
), |
|
112
|
|
|
), |
|
113
|
|
|
) |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
View Code Duplication |
public function register_get_sync_state_endpoint() { |
|
|
|
|
|
|
119
|
|
|
$that = $this; |
|
120
|
|
|
register_rest_route( |
|
121
|
|
|
WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
122
|
|
|
'/videos/background/get_state', |
|
123
|
|
|
array( |
|
124
|
|
|
'methods' => WP_REST_Server::CREATABLE, |
|
125
|
|
|
'callback' => function () use ( $that ) { |
|
126
|
|
|
return $that->background_process->get_state()->get_array(); |
|
127
|
|
|
}, |
|
128
|
|
|
'permission_callback' => function () { |
|
129
|
|
|
return current_user_can( 'manage_options' ); |
|
130
|
|
|
}, |
|
131
|
|
|
) |
|
132
|
|
|
); |
|
133
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
View Code Duplication |
public function register_background_process_start_endpoint() { |
|
|
|
|
|
|
137
|
|
|
$that = $this; |
|
138
|
|
|
register_rest_route( |
|
139
|
|
|
WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
140
|
|
|
'/videos/background/start', |
|
141
|
|
|
array( |
|
142
|
|
|
'methods' => WP_REST_Server::CREATABLE, |
|
143
|
|
|
'callback' => function () use ( $that ) { |
|
144
|
|
|
$that->background_process->start(); |
|
145
|
|
|
}, |
|
146
|
|
|
'permission_callback' => function () { |
|
147
|
|
|
return current_user_can( 'manage_options' ); |
|
148
|
|
|
}, |
|
149
|
|
|
) |
|
150
|
|
|
); |
|
151
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
View Code Duplication |
public function register_background_process_stop_endpoint() { |
|
|
|
|
|
|
156
|
|
|
$that = $this; |
|
157
|
|
|
register_rest_route( |
|
158
|
|
|
WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
159
|
|
|
'/videos/background/stop', |
|
160
|
|
|
array( |
|
161
|
|
|
'methods' => WP_REST_Server::CREATABLE, |
|
162
|
|
|
'callback' => function () use ( $that ) { |
|
163
|
|
|
$that->background_process->cancel(); |
|
164
|
|
|
}, |
|
165
|
|
|
'permission_callback' => function () { |
|
166
|
|
|
return current_user_can( 'manage_options' ); |
|
167
|
|
|
}, |
|
168
|
|
|
) |
|
169
|
|
|
); |
|
170
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.