← All changes
|
json-endpoints/class.wpcom-json-api-add-widget-endpoint.php
+153
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,153 @@ | ||
| 1 | +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName | |
| 2 | +/** | |
| 3 | + * Activate a widget on a site. | |
| 4 | + * | |
| 5 | + * Endpoint: https://public-api.wordpress.com/rest/v1.1/sites/$site/widgets/new | |
| 6 | + */ | |
| 7 | + | |
| 8 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 9 | + exit( 0 ); | |
| 10 | +} | |
| 11 | + | |
| 12 | +new WPCOM_JSON_API_Add_Widgets_Endpoint( | |
| 13 | + array( | |
| 14 | + 'description' => 'Activate a group of widgets on a site. The bulk version of using the /new endpoint', | |
| 15 | + 'group' => '__do_not_document', | |
| 16 | + 'stat' => 'widgets:new:bulk', | |
| 17 | + 'force' => 'wpcom', | |
| 18 | + 'method' => 'POST', | |
| 19 | + 'min_version' => '1.1', | |
| 20 | + 'path' => '/sites/%s/widgets', | |
| 21 | + 'path_labels' => array( | |
| 22 | + '$site' => '(string) Site ID or domain.', | |
| 23 | + ), | |
| 24 | + 'request_format' => array( | |
| 25 | + 'widgets' => '(array:widget) An array of widget objects to add.', | |
| 26 | + ), | |
| 27 | + 'response_format' => array( | |
| 28 | + 'widgets' => '(array:widget) An array of widget objects added.', | |
| 29 | + ), | |
| 30 | + 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/12345678/widgets', | |
| 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 | +new WPCOM_JSON_API_Add_Widgets_Endpoint( | |
| 56 | + array( | |
| 57 | + 'description' => 'Activate a widget on a site.', | |
| 58 | + 'group' => 'sites', | |
| 59 | + 'stat' => 'widgets:new', | |
| 60 | + 'method' => 'POST', | |
| 61 | + 'min_version' => '1.1', | |
| 62 | + 'path' => '/sites/%s/widgets/new', | |
| 63 | + 'path_labels' => array( | |
| 64 | + '$site' => '(string) Site ID or domain.', | |
| 65 | + ), | |
| 66 | + 'request_format' => array( | |
| 67 | + 'id_base' => '(string) The base ID of the widget.', | |
| 68 | + '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.', | |
| 69 | + 'position' => '(int) Optional. The position of the widget in the sidebar.', | |
| 70 | + 'settings' => '(object) Optional. The settings for the new widget.', | |
| 71 | + ), | |
| 72 | + 'response_format' => array( | |
| 73 | + 'id' => '(string) The actual ID of the widget.', | |
| 74 | + 'sidebar' => '(string) The ID of the sidebar where this widget will be active.', | |
| 75 | + 'position' => '(int) The final position of the widget in the sidebar.', | |
| 76 | + 'settings' => '(array) The settings for the new widget.', | |
| 77 | + ), | |
| 78 | + 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/12345678/widgets/new', | |
| 79 | + 'example_request_data' => array( | |
| 80 | + 'headers' => array( | |
| 81 | + 'authorization' => 'Bearer YOUR_API_TOKEN', | |
| 82 | + ), | |
| 83 | + 'body' => array( | |
| 84 | + 'id_base' => 'text', | |
| 85 | + 'sidebar' => 'sidebar-2', | |
| 86 | + 'position' => '0', | |
| 87 | + 'settings' => array( 'title' => 'hello world' ), | |
| 88 | + ), | |
| 89 | + ), | |
| 90 | + 'example_response' => ' | |
| 91 | + { | |
| 92 | + "id": "text-3", | |
| 93 | + "id_base": "text", | |
| 94 | + "settings": { | |
| 95 | + "title": "hello world" | |
| 96 | + }, | |
| 97 | + "sidebar": "sidebar-2", | |
| 98 | + "position": 0 | |
| 99 | + }', | |
| 100 | + ) | |
| 101 | +); | |
| 102 | + | |
| 103 | +/** | |
| 104 | + * The Add Widgets endpoint class. | |
| 105 | + * | |
| 106 | + * @phan-constructor-used-for-side-effects | |
| 107 | + */ | |
| 108 | +class WPCOM_JSON_API_Add_Widgets_Endpoint extends WPCOM_JSON_API_Endpoint { | |
| 109 | + /** | |
| 110 | + * API callback. | |
| 111 | + * | |
| 112 | + * @param string $path - the path. | |
| 113 | + * @param int $blog_id - the blog ID. | |
| 114 | + * @uses Jetpack_Widgets | |
| 115 | + * | |
| 116 | + * @return array|WP_Error | |
| 117 | + */ | |
| 118 | + public function callback( $path = '', $blog_id = 0 ) { | |
| 119 | + // Switch to the given blog. | |
| 120 | + $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); | |
| 121 | + if ( is_wp_error( $blog_id ) ) { | |
| 122 | + return $blog_id; | |
| 123 | + } | |
| 124 | + | |
| 125 | + if ( ! current_user_can( 'edit_theme_options' ) ) { | |
| 126 | + return new WP_Error( 'unauthorized', 'User is not authorized to access widgets', 403 ); | |
| 127 | + } | |
| 128 | + | |
| 129 | + require_once JETPACK__PLUGIN_DIR . '_inc/lib/widgets.php'; | |
| 130 | + $args = $this->input( false, false ); // Don't filter the input. | |
| 131 | + if ( empty( $args ) || ! is_array( $args ) ) { | |
| 132 | + return new WP_Error( 'no_data', 'No data was provided.', 400 ); | |
| 133 | + } | |
| 134 | + if ( isset( $args['widgets'] ) || ! empty( $args['widgets'] ) ) { | |
| 135 | + $widgets = Jetpack_Widgets::activate_widgets( $args['widgets'] ); | |
| 136 | + if ( is_wp_error( $widgets ) ) { | |
| 137 | + return $widgets; | |
| 138 | + } | |
| 139 | + return array( 'widgets' => $widgets ); | |
| 140 | + } | |
| 141 | + if ( ! isset( $args['id_base'] ) ) { | |
| 142 | + return new WP_Error( 'missing_data', 'The data you provided was not accurate.', 400 ); | |
| 143 | + } | |
| 144 | + | |
| 145 | + if ( empty( $args['sidebar'] ) ) { | |
| 146 | + $active_sidebars = Jetpack_Widgets::get_active_sidebars(); | |
| 147 | + reset( $active_sidebars ); | |
| 148 | + $args['sidebar'] = key( $active_sidebars ); | |
| 149 | + } | |
| 150 | + | |
| 151 | + return Jetpack_Widgets::activate_widget( $args['id_base'], $args['sidebar'], $args['position'], $args['settings'] ); | |
| 152 | + } | |
| 153 | +} | |