|
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\Data\Video_Storage\Video_Storage_Factory; |
|
10
|
|
|
use Wordlift\Videoobject\Data\Video\Video; |
|
11
|
|
|
use WP_REST_Server; |
|
12
|
|
|
|
|
13
|
|
|
class Rest_Controller { |
|
14
|
|
|
|
|
15
|
|
|
public function register_all_routes() { |
|
16
|
|
|
$that = $this; |
|
17
|
|
|
add_action( 'rest_api_init', function () use ( $that ) { |
|
18
|
|
|
$that->register_get_all_videos_route(); |
|
19
|
|
|
$that->register_save_all_videos_route(); |
|
20
|
|
|
} ); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function get_all_videos( $request ) { |
|
24
|
|
|
$data = $request->get_params(); |
|
25
|
|
|
$post_id = (int) $data['post_id']; |
|
26
|
|
|
$storage = Video_Storage_Factory::get_storage(); |
|
27
|
|
|
|
|
28
|
|
|
return $storage->get_all_videos( $post_id ); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function save_all_videos( $request ) { |
|
32
|
|
|
$data = $request->get_params(); |
|
33
|
|
|
$post_id = (int) $data['post_id']; |
|
34
|
|
|
$videos = (array) $data['videos']; |
|
35
|
|
|
if ( ! $videos ) { |
|
|
|
|
|
|
36
|
|
|
return; |
|
37
|
|
|
} |
|
38
|
|
|
$storage = Video_Storage_Factory::get_storage(); |
|
39
|
|
|
$storage->remove_all_videos( $post_id ); |
|
40
|
|
|
|
|
41
|
|
|
foreach ( $videos as $video ) { |
|
42
|
|
|
$video_obj = new Video(); |
|
43
|
|
|
$video_obj->from( (array) $video ); |
|
44
|
|
|
$storage->add_video( $post_id, $video_obj ); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
View Code Duplication |
private function register_get_all_videos_route() { |
|
|
|
|
|
|
51
|
|
|
register_rest_route( |
|
52
|
|
|
WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
53
|
|
|
'/videos', |
|
54
|
|
|
array( |
|
55
|
|
|
'methods' => WP_REST_Server::CREATABLE, |
|
56
|
|
|
'callback' => array( $this, 'get_all_videos' ), |
|
57
|
|
|
'permission_callback' => function () { |
|
58
|
|
|
return current_user_can( 'manage_options' ); |
|
59
|
|
|
}, |
|
60
|
|
|
'args' => array( |
|
61
|
|
|
'post_id' => array( |
|
62
|
|
|
'validate_callback' => function ( $param, $request, $key ) { |
|
|
|
|
|
|
63
|
|
|
return is_numeric( $param ) && $param; |
|
64
|
|
|
}, |
|
65
|
|
|
'required' => true, |
|
66
|
|
|
), |
|
67
|
|
|
), |
|
68
|
|
|
) |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
View Code Duplication |
private function register_save_all_videos_route() { |
|
|
|
|
|
|
73
|
|
|
register_rest_route( |
|
74
|
|
|
WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
75
|
|
|
'/videos/save', |
|
76
|
|
|
array( |
|
77
|
|
|
'methods' => WP_REST_Server::CREATABLE, |
|
78
|
|
|
'callback' => array( $this, 'save_all_videos' ), |
|
79
|
|
|
'permission_callback' => function () { |
|
80
|
|
|
return current_user_can( 'manage_options' ); |
|
81
|
|
|
}, |
|
82
|
|
|
'args' => array( |
|
83
|
|
|
'post_id' => array( |
|
84
|
|
|
'validate_callback' => function ( $param, $request, $key ) { |
|
|
|
|
|
|
85
|
|
|
return is_numeric( $param ) && $param; |
|
86
|
|
|
}, |
|
87
|
|
|
'required' => true, |
|
88
|
|
|
), |
|
89
|
|
|
'videos' => array( |
|
90
|
|
|
'validate_callback' => function ( $param, $request, $key ) { |
|
|
|
|
|
|
91
|
|
|
return is_array( $param ) && $param; |
|
|
|
|
|
|
92
|
|
|
}, |
|
93
|
|
|
'required' => true, |
|
94
|
|
|
), |
|
95
|
|
|
), |
|
96
|
|
|
) |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
} |
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.