|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Activate a widget on a site. |
|
4
|
|
|
* |
|
5
|
|
|
* https://public-api.wordpress.com/rest/v1.1/sites/$site/widgets/new |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
new WPCOM_JSON_API_Add_Widgets_Endpoint( array ( |
|
9
|
|
|
'description' => 'Activate a widget on a site.', |
|
10
|
|
|
'group' => 'sites', |
|
11
|
|
|
'stat' => 'widgets:new', |
|
12
|
|
|
'method' => 'POST', |
|
13
|
|
|
'min_version' => '1.1', |
|
14
|
|
|
'path' => '/sites/%s/widgets/new', |
|
15
|
|
|
'path_labels' => array( |
|
16
|
|
|
'$site' => '(string) Site ID or domain.' |
|
17
|
|
|
), |
|
18
|
|
|
'request_format' => array( |
|
19
|
|
|
'id_base' => '(string) The base ID of the widget.', |
|
20
|
|
|
'sidebar' => '(string) Optional. The ID of the sidebar where this widget will be active. If empty, the widget will be added in the first sidebar available.', |
|
21
|
|
|
'position' => '(int) Optional. The position of the widget in the sidebar.', |
|
22
|
|
|
'settings' => '(object) Optional. The settings for the new widget.', |
|
23
|
|
|
), |
|
24
|
|
|
'response_format' => array( |
|
25
|
|
|
'id' => '(string) The actual ID of the widget.', |
|
26
|
|
|
'sidebar' => '(string) The ID of the sidebar where this widget will be active.', |
|
27
|
|
|
'position' => '(int) The final position of the widget in the sidebar.', |
|
28
|
|
|
'settings' => '(array) The settings for the new widget.', |
|
29
|
|
|
), |
|
30
|
|
|
'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/12345678/widgets/new', |
|
31
|
|
|
'example_request_data' => array( |
|
32
|
|
|
'headers' => array( |
|
33
|
|
|
'authorization' => 'Bearer YOUR_API_TOKEN' |
|
34
|
|
|
), |
|
35
|
|
|
'body' => array( |
|
36
|
|
|
'id_base' => 'text', |
|
37
|
|
|
'sidebar' => 'sidebar-2', |
|
38
|
|
|
'position' => '0', |
|
39
|
|
|
'settings' => array( 'title' => 'hello world' ), |
|
40
|
|
|
) |
|
41
|
|
|
), |
|
42
|
|
|
'example_response' => ' |
|
43
|
|
|
{ |
|
44
|
|
|
"id": "text-3", |
|
45
|
|
|
"id_base": "text", |
|
46
|
|
|
"settings": { |
|
47
|
|
|
"title": "hello world" |
|
48
|
|
|
}, |
|
49
|
|
|
"sidebar": "sidebar-2", |
|
50
|
|
|
"position": 0 |
|
51
|
|
|
}' |
|
52
|
|
|
) ); |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
class WPCOM_JSON_API_Add_Widgets_Endpoint extends WPCOM_JSON_API_Endpoint { |
|
56
|
|
|
/** |
|
57
|
|
|
* API callback. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $path |
|
60
|
|
|
* @param int $blog_id |
|
61
|
|
|
* @uses jetpack_require_lib |
|
62
|
|
|
* @uses Jetpack_Widgets |
|
63
|
|
|
* |
|
64
|
|
|
* @return array|WP_Error |
|
65
|
|
|
*/ |
|
66
|
|
|
function callback( $path = '', $blog_id = 0 ) { |
|
67
|
|
|
// Switch to the given blog. |
|
68
|
|
|
$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); |
|
69
|
|
|
if ( is_wp_error( $blog_id ) ) { |
|
70
|
|
|
return $blog_id; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if ( ! current_user_can( 'edit_theme_options' ) ) { |
|
74
|
|
|
return new WP_Error( 'unauthorized', 'User is not authorized to access widgets', 403 ); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
jetpack_require_lib( 'widgets' ); |
|
78
|
|
|
$args = $this->input( false, false ); // Don't filter the input |
|
79
|
|
|
if ( empty( $args ) || ! is_array( $args ) ) { |
|
80
|
|
|
return new WP_Error( 'no_data', 'No data was provided.', 400 ); |
|
81
|
|
|
} |
|
82
|
|
|
if ( isset( $args['widgets'] ) || ! empty( $args['widgets'] ) ) { |
|
83
|
|
|
$widgets = Jetpack_Widgets::activate_widgets( $args['widgets'] ); |
|
84
|
|
|
if ( is_wp_error( $widgets ) ) { |
|
85
|
|
|
return $widgets; |
|
86
|
|
|
} |
|
87
|
|
|
return array( 'widgets' => $widgets ); |
|
88
|
|
|
} |
|
89
|
|
|
if ( ! isset( $args['id_base'] ) ) { |
|
90
|
|
|
return new WP_Error( 'missing_data', 'The data you provided was not accurate.', 400 ); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if ( empty( $args['sidebar'] ) ) { |
|
94
|
|
|
$active_sidebars = Jetpack_Widgets::get_active_sidebars(); |
|
95
|
|
|
reset( $active_sidebars ); |
|
96
|
|
|
$args['sidebar'] = key( $active_sidebars ); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return Jetpack_Widgets::activate_widget( $args['id_base'], $args['sidebar'], $args['position'], $args['settings'] ); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
|