暂无描述

class-wp-rest-plugins-controller.php 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. <?php
  2. /**
  3. * REST API: WP_REST_Plugins_Controller class
  4. *
  5. * @package WordPress
  6. * @subpackage REST_API
  7. * @since 5.5.0
  8. */
  9. /**
  10. * Core class to access plugins via the REST API.
  11. *
  12. * @since 5.5.0
  13. *
  14. * @see WP_REST_Controller
  15. */
  16. class WP_REST_Plugins_Controller extends WP_REST_Controller {
  17. const PATTERN = '[^.\/]+(?:\/[^.\/]+)?';
  18. /**
  19. * Plugins controller constructor.
  20. *
  21. * @since 5.5.0
  22. */
  23. public function __construct() {
  24. $this->namespace = 'wp/v2';
  25. $this->rest_base = 'plugins';
  26. }
  27. /**
  28. * Registers the routes for the plugins controller.
  29. *
  30. * @since 5.5.0
  31. */
  32. public function register_routes() {
  33. register_rest_route(
  34. $this->namespace,
  35. '/' . $this->rest_base,
  36. array(
  37. array(
  38. 'methods' => WP_REST_Server::READABLE,
  39. 'callback' => array( $this, 'get_items' ),
  40. 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  41. 'args' => $this->get_collection_params(),
  42. ),
  43. array(
  44. 'methods' => WP_REST_Server::CREATABLE,
  45. 'callback' => array( $this, 'create_item' ),
  46. 'permission_callback' => array( $this, 'create_item_permissions_check' ),
  47. 'args' => array(
  48. 'slug' => array(
  49. 'type' => 'string',
  50. 'required' => true,
  51. 'description' => __( 'WordPress.org plugin directory slug.' ),
  52. 'pattern' => '[\w\-]+',
  53. ),
  54. 'status' => array(
  55. 'description' => __( 'The plugin activation status.' ),
  56. 'type' => 'string',
  57. 'enum' => is_multisite() ? array( 'inactive', 'active', 'network-active' ) : array( 'inactive', 'active' ),
  58. 'default' => 'inactive',
  59. ),
  60. ),
  61. ),
  62. 'schema' => array( $this, 'get_public_item_schema' ),
  63. )
  64. );
  65. register_rest_route(
  66. $this->namespace,
  67. '/' . $this->rest_base . '/(?P<plugin>' . self::PATTERN . ')',
  68. array(
  69. array(
  70. 'methods' => WP_REST_Server::READABLE,
  71. 'callback' => array( $this, 'get_item' ),
  72. 'permission_callback' => array( $this, 'get_item_permissions_check' ),
  73. ),
  74. array(
  75. 'methods' => WP_REST_Server::EDITABLE,
  76. 'callback' => array( $this, 'update_item' ),
  77. 'permission_callback' => array( $this, 'update_item_permissions_check' ),
  78. 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
  79. ),
  80. array(
  81. 'methods' => WP_REST_Server::DELETABLE,
  82. 'callback' => array( $this, 'delete_item' ),
  83. 'permission_callback' => array( $this, 'delete_item_permissions_check' ),
  84. ),
  85. 'args' => array(
  86. 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
  87. 'plugin' => array(
  88. 'type' => 'string',
  89. 'pattern' => self::PATTERN,
  90. 'validate_callback' => array( $this, 'validate_plugin_param' ),
  91. 'sanitize_callback' => array( $this, 'sanitize_plugin_param' ),
  92. ),
  93. ),
  94. 'schema' => array( $this, 'get_public_item_schema' ),
  95. )
  96. );
  97. }
  98. /**
  99. * Checks if a given request has access to get plugins.
  100. *
  101. * @since 5.5.0
  102. *
  103. * @param WP_REST_Request $request Full details about the request.
  104. * @return true|WP_Error True if the request has read access, WP_Error object otherwise.
  105. */
  106. public function get_items_permissions_check( $request ) {
  107. if ( ! current_user_can( 'activate_plugins' ) ) {
  108. return new WP_Error(
  109. 'rest_cannot_view_plugins',
  110. __( 'Sorry, you are not allowed to manage plugins for this site.' ),
  111. array( 'status' => rest_authorization_required_code() )
  112. );
  113. }
  114. return true;
  115. }
  116. /**
  117. * Retrieves a collection of plugins.
  118. *
  119. * @since 5.5.0
  120. *
  121. * @param WP_REST_Request $request Full details about the request.
  122. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  123. */
  124. public function get_items( $request ) {
  125. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  126. $plugins = array();
  127. foreach ( get_plugins() as $file => $data ) {
  128. if ( is_wp_error( $this->check_read_permission( $file ) ) ) {
  129. continue;
  130. }
  131. $data['_file'] = $file;
  132. if ( ! $this->does_plugin_match_request( $request, $data ) ) {
  133. continue;
  134. }
  135. $plugins[] = $this->prepare_response_for_collection( $this->prepare_item_for_response( $data, $request ) );
  136. }
  137. return new WP_REST_Response( $plugins );
  138. }
  139. /**
  140. * Checks if a given request has access to get a specific plugin.
  141. *
  142. * @since 5.5.0
  143. *
  144. * @param WP_REST_Request $request Full details about the request.
  145. * @return true|WP_Error True if the request has read access for the item, WP_Error object otherwise.
  146. */
  147. public function get_item_permissions_check( $request ) {
  148. if ( ! current_user_can( 'activate_plugins' ) ) {
  149. return new WP_Error(
  150. 'rest_cannot_view_plugin',
  151. __( 'Sorry, you are not allowed to manage plugins for this site.' ),
  152. array( 'status' => rest_authorization_required_code() )
  153. );
  154. }
  155. $can_read = $this->check_read_permission( $request['plugin'] );
  156. if ( is_wp_error( $can_read ) ) {
  157. return $can_read;
  158. }
  159. return true;
  160. }
  161. /**
  162. * Retrieves one plugin from the site.
  163. *
  164. * @since 5.5.0
  165. *
  166. * @param WP_REST_Request $request Full details about the request.
  167. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  168. */
  169. public function get_item( $request ) {
  170. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  171. $data = $this->get_plugin_data( $request['plugin'] );
  172. if ( is_wp_error( $data ) ) {
  173. return $data;
  174. }
  175. return $this->prepare_item_for_response( $data, $request );
  176. }
  177. /**
  178. * Checks if the given plugin can be viewed by the current user.
  179. *
  180. * On multisite, this hides non-active network only plugins if the user does not have permission
  181. * to manage network plugins.
  182. *
  183. * @since 5.5.0
  184. *
  185. * @param string $plugin The plugin file to check.
  186. * @return true|WP_Error True if can read, a WP_Error instance otherwise.
  187. */
  188. protected function check_read_permission( $plugin ) {
  189. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  190. if ( ! $this->is_plugin_installed( $plugin ) ) {
  191. return new WP_Error( 'rest_plugin_not_found', __( 'Plugin not found.' ), array( 'status' => 404 ) );
  192. }
  193. if ( ! is_multisite() ) {
  194. return true;
  195. }
  196. if ( ! is_network_only_plugin( $plugin ) || is_plugin_active( $plugin ) || current_user_can( 'manage_network_plugins' ) ) {
  197. return true;
  198. }
  199. return new WP_Error(
  200. 'rest_cannot_view_plugin',
  201. __( 'Sorry, you are not allowed to manage this plugin.' ),
  202. array( 'status' => rest_authorization_required_code() )
  203. );
  204. }
  205. /**
  206. * Checks if a given request has access to upload plugins.
  207. *
  208. * @since 5.5.0
  209. *
  210. * @param WP_REST_Request $request Full details about the request.
  211. * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise.
  212. */
  213. public function create_item_permissions_check( $request ) {
  214. if ( ! current_user_can( 'install_plugins' ) ) {
  215. return new WP_Error(
  216. 'rest_cannot_install_plugin',
  217. __( 'Sorry, you are not allowed to install plugins on this site.' ),
  218. array( 'status' => rest_authorization_required_code() )
  219. );
  220. }
  221. if ( 'inactive' !== $request['status'] && ! current_user_can( 'activate_plugins' ) ) {
  222. return new WP_Error(
  223. 'rest_cannot_activate_plugin',
  224. __( 'Sorry, you are not allowed to activate plugins.' ),
  225. array(
  226. 'status' => rest_authorization_required_code(),
  227. )
  228. );
  229. }
  230. return true;
  231. }
  232. /**
  233. * Uploads a plugin and optionally activates it.
  234. *
  235. * @since 5.5.0
  236. *
  237. * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
  238. *
  239. * @param WP_REST_Request $request Full details about the request.
  240. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  241. */
  242. public function create_item( $request ) {
  243. global $wp_filesystem;
  244. require_once ABSPATH . 'wp-admin/includes/file.php';
  245. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  246. require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  247. require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
  248. $slug = $request['slug'];
  249. // Verify filesystem is accessible first.
  250. $filesystem_available = $this->is_filesystem_available();
  251. if ( is_wp_error( $filesystem_available ) ) {
  252. return $filesystem_available;
  253. }
  254. $api = plugins_api(
  255. 'plugin_information',
  256. array(
  257. 'slug' => $slug,
  258. 'fields' => array(
  259. 'sections' => false,
  260. 'language_packs' => true,
  261. ),
  262. )
  263. );
  264. if ( is_wp_error( $api ) ) {
  265. if ( false !== strpos( $api->get_error_message(), 'Plugin not found.' ) ) {
  266. $api->add_data( array( 'status' => 404 ) );
  267. } else {
  268. $api->add_data( array( 'status' => 500 ) );
  269. }
  270. return $api;
  271. }
  272. $skin = new WP_Ajax_Upgrader_Skin();
  273. $upgrader = new Plugin_Upgrader( $skin );
  274. $result = $upgrader->install( $api->download_link );
  275. if ( is_wp_error( $result ) ) {
  276. $result->add_data( array( 'status' => 500 ) );
  277. return $result;
  278. }
  279. // This should be the same as $result above.
  280. if ( is_wp_error( $skin->result ) ) {
  281. $skin->result->add_data( array( 'status' => 500 ) );
  282. return $skin->result;
  283. }
  284. if ( $skin->get_errors()->has_errors() ) {
  285. $error = $skin->get_errors();
  286. $error->add_data( array( 'status' => 500 ) );
  287. return $error;
  288. }
  289. if ( is_null( $result ) ) {
  290. // Pass through the error from WP_Filesystem if one was raised.
  291. if ( $wp_filesystem instanceof WP_Filesystem_Base
  292. && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors()
  293. ) {
  294. return new WP_Error(
  295. 'unable_to_connect_to_filesystem',
  296. $wp_filesystem->errors->get_error_message(),
  297. array( 'status' => 500 )
  298. );
  299. }
  300. return new WP_Error(
  301. 'unable_to_connect_to_filesystem',
  302. __( 'Unable to connect to the filesystem. Please confirm your credentials.' ),
  303. array( 'status' => 500 )
  304. );
  305. }
  306. $file = $upgrader->plugin_info();
  307. if ( ! $file ) {
  308. return new WP_Error(
  309. 'unable_to_determine_installed_plugin',
  310. __( 'Unable to determine what plugin was installed.' ),
  311. array( 'status' => 500 )
  312. );
  313. }
  314. if ( 'inactive' !== $request['status'] ) {
  315. $can_change_status = $this->plugin_status_permission_check( $file, $request['status'], 'inactive' );
  316. if ( is_wp_error( $can_change_status ) ) {
  317. return $can_change_status;
  318. }
  319. $changed_status = $this->handle_plugin_status( $file, $request['status'], 'inactive' );
  320. if ( is_wp_error( $changed_status ) ) {
  321. return $changed_status;
  322. }
  323. }
  324. // Install translations.
  325. $installed_locales = array_values( get_available_languages() );
  326. /** This filter is documented in wp-includes/update.php */
  327. $installed_locales = apply_filters( 'plugins_update_check_locales', $installed_locales );
  328. $language_packs = array_map(
  329. static function( $item ) {
  330. return (object) $item;
  331. },
  332. $api->language_packs
  333. );
  334. $language_packs = array_filter(
  335. $language_packs,
  336. static function( $pack ) use ( $installed_locales ) {
  337. return in_array( $pack->language, $installed_locales, true );
  338. }
  339. );
  340. if ( $language_packs ) {
  341. $lp_upgrader = new Language_Pack_Upgrader( $skin );
  342. // Install all applicable language packs for the plugin.
  343. $lp_upgrader->bulk_upgrade( $language_packs );
  344. }
  345. $path = WP_PLUGIN_DIR . '/' . $file;
  346. $data = get_plugin_data( $path, false, false );
  347. $data['_file'] = $file;
  348. $response = $this->prepare_item_for_response( $data, $request );
  349. $response->set_status( 201 );
  350. $response->header( 'Location', rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, substr( $file, 0, - 4 ) ) ) );
  351. return $response;
  352. }
  353. /**
  354. * Checks if a given request has access to update a specific plugin.
  355. *
  356. * @since 5.5.0
  357. *
  358. * @param WP_REST_Request $request Full details about the request.
  359. * @return true|WP_Error True if the request has access to update the item, WP_Error object otherwise.
  360. */
  361. public function update_item_permissions_check( $request ) {
  362. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  363. if ( ! current_user_can( 'activate_plugins' ) ) {
  364. return new WP_Error(
  365. 'rest_cannot_manage_plugins',
  366. __( 'Sorry, you are not allowed to manage plugins for this site.' ),
  367. array( 'status' => rest_authorization_required_code() )
  368. );
  369. }
  370. $can_read = $this->check_read_permission( $request['plugin'] );
  371. if ( is_wp_error( $can_read ) ) {
  372. return $can_read;
  373. }
  374. $status = $this->get_plugin_status( $request['plugin'] );
  375. if ( $request['status'] && $status !== $request['status'] ) {
  376. $can_change_status = $this->plugin_status_permission_check( $request['plugin'], $request['status'], $status );
  377. if ( is_wp_error( $can_change_status ) ) {
  378. return $can_change_status;
  379. }
  380. }
  381. return true;
  382. }
  383. /**
  384. * Updates one plugin.
  385. *
  386. * @since 5.5.0
  387. *
  388. * @param WP_REST_Request $request Full details about the request.
  389. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  390. */
  391. public function update_item( $request ) {
  392. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  393. $data = $this->get_plugin_data( $request['plugin'] );
  394. if ( is_wp_error( $data ) ) {
  395. return $data;
  396. }
  397. $status = $this->get_plugin_status( $request['plugin'] );
  398. if ( $request['status'] && $status !== $request['status'] ) {
  399. $handled = $this->handle_plugin_status( $request['plugin'], $request['status'], $status );
  400. if ( is_wp_error( $handled ) ) {
  401. return $handled;
  402. }
  403. }
  404. $this->update_additional_fields_for_object( $data, $request );
  405. $request['context'] = 'edit';
  406. return $this->prepare_item_for_response( $data, $request );
  407. }
  408. /**
  409. * Checks if a given request has access to delete a specific plugin.
  410. *
  411. * @since 5.5.0
  412. *
  413. * @param WP_REST_Request $request Full details about the request.
  414. * @return true|WP_Error True if the request has access to delete the item, WP_Error object otherwise.
  415. */
  416. public function delete_item_permissions_check( $request ) {
  417. if ( ! current_user_can( 'activate_plugins' ) ) {
  418. return new WP_Error(
  419. 'rest_cannot_manage_plugins',
  420. __( 'Sorry, you are not allowed to manage plugins for this site.' ),
  421. array( 'status' => rest_authorization_required_code() )
  422. );
  423. }
  424. if ( ! current_user_can( 'delete_plugins' ) ) {
  425. return new WP_Error(
  426. 'rest_cannot_manage_plugins',
  427. __( 'Sorry, you are not allowed to delete plugins for this site.' ),
  428. array( 'status' => rest_authorization_required_code() )
  429. );
  430. }
  431. $can_read = $this->check_read_permission( $request['plugin'] );
  432. if ( is_wp_error( $can_read ) ) {
  433. return $can_read;
  434. }
  435. return true;
  436. }
  437. /**
  438. * Deletes one plugin from the site.
  439. *
  440. * @since 5.5.0
  441. *
  442. * @param WP_REST_Request $request Full details about the request.
  443. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  444. */
  445. public function delete_item( $request ) {
  446. require_once ABSPATH . 'wp-admin/includes/file.php';
  447. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  448. $data = $this->get_plugin_data( $request['plugin'] );
  449. if ( is_wp_error( $data ) ) {
  450. return $data;
  451. }
  452. if ( is_plugin_active( $request['plugin'] ) ) {
  453. return new WP_Error(
  454. 'rest_cannot_delete_active_plugin',
  455. __( 'Cannot delete an active plugin. Please deactivate it first.' ),
  456. array( 'status' => 400 )
  457. );
  458. }
  459. $filesystem_available = $this->is_filesystem_available();
  460. if ( is_wp_error( $filesystem_available ) ) {
  461. return $filesystem_available;
  462. }
  463. $prepared = $this->prepare_item_for_response( $data, $request );
  464. $deleted = delete_plugins( array( $request['plugin'] ) );
  465. if ( is_wp_error( $deleted ) ) {
  466. $deleted->add_data( array( 'status' => 500 ) );
  467. return $deleted;
  468. }
  469. return new WP_REST_Response(
  470. array(
  471. 'deleted' => true,
  472. 'previous' => $prepared->get_data(),
  473. )
  474. );
  475. }
  476. /**
  477. * Prepares the plugin for the REST response.
  478. *
  479. * @since 5.5.0
  480. *
  481. * @param mixed $item Unmarked up and untranslated plugin data from {@see get_plugin_data()}.
  482. * @param WP_REST_Request $request Request object.
  483. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  484. */
  485. public function prepare_item_for_response( $item, $request ) {
  486. $item = _get_plugin_data_markup_translate( $item['_file'], $item, false );
  487. $marked = _get_plugin_data_markup_translate( $item['_file'], $item, true );
  488. $data = array(
  489. 'plugin' => substr( $item['_file'], 0, - 4 ),
  490. 'status' => $this->get_plugin_status( $item['_file'] ),
  491. 'name' => $item['Name'],
  492. 'plugin_uri' => $item['PluginURI'],
  493. 'author' => $item['Author'],
  494. 'author_uri' => $item['AuthorURI'],
  495. 'description' => array(
  496. 'raw' => $item['Description'],
  497. 'rendered' => $marked['Description'],
  498. ),
  499. 'version' => $item['Version'],
  500. 'network_only' => $item['Network'],
  501. 'requires_wp' => $item['RequiresWP'],
  502. 'requires_php' => $item['RequiresPHP'],
  503. 'textdomain' => $item['TextDomain'],
  504. );
  505. $data = $this->add_additional_fields_to_object( $data, $request );
  506. $response = new WP_REST_Response( $data );
  507. $response->add_links( $this->prepare_links( $item ) );
  508. /**
  509. * Filters plugin data for a REST API response.
  510. *
  511. * @since 5.5.0
  512. *
  513. * @param WP_REST_Response $response The response object.
  514. * @param array $item The plugin item from {@see get_plugin_data()}.
  515. * @param WP_REST_Request $request The request object.
  516. */
  517. return apply_filters( 'rest_prepare_plugin', $response, $item, $request );
  518. }
  519. /**
  520. * Prepares links for the request.
  521. *
  522. * @since 5.5.0
  523. *
  524. * @param array $item The plugin item.
  525. * @return array[]
  526. */
  527. protected function prepare_links( $item ) {
  528. return array(
  529. 'self' => array(
  530. 'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, substr( $item['_file'], 0, - 4 ) ) ),
  531. ),
  532. );
  533. }
  534. /**
  535. * Gets the plugin header data for a plugin.
  536. *
  537. * @since 5.5.0
  538. *
  539. * @param string $plugin The plugin file to get data for.
  540. * @return array|WP_Error The plugin data, or a WP_Error if the plugin is not installed.
  541. */
  542. protected function get_plugin_data( $plugin ) {
  543. $plugins = get_plugins();
  544. if ( ! isset( $plugins[ $plugin ] ) ) {
  545. return new WP_Error( 'rest_plugin_not_found', __( 'Plugin not found.' ), array( 'status' => 404 ) );
  546. }
  547. $data = $plugins[ $plugin ];
  548. $data['_file'] = $plugin;
  549. return $data;
  550. }
  551. /**
  552. * Get's the activation status for a plugin.
  553. *
  554. * @since 5.5.0
  555. *
  556. * @param string $plugin The plugin file to check.
  557. * @return string Either 'network-active', 'active' or 'inactive'.
  558. */
  559. protected function get_plugin_status( $plugin ) {
  560. if ( is_plugin_active_for_network( $plugin ) ) {
  561. return 'network-active';
  562. }
  563. if ( is_plugin_active( $plugin ) ) {
  564. return 'active';
  565. }
  566. return 'inactive';
  567. }
  568. /**
  569. * Handle updating a plugin's status.
  570. *
  571. * @since 5.5.0
  572. *
  573. * @param string $plugin The plugin file to update.
  574. * @param string $new_status The plugin's new status.
  575. * @param string $current_status The plugin's current status.
  576. * @return true|WP_Error
  577. */
  578. protected function plugin_status_permission_check( $plugin, $new_status, $current_status ) {
  579. if ( is_multisite() && ( 'network-active' === $current_status || 'network-active' === $new_status ) && ! current_user_can( 'manage_network_plugins' ) ) {
  580. return new WP_Error(
  581. 'rest_cannot_manage_network_plugins',
  582. __( 'Sorry, you are not allowed to manage network plugins.' ),
  583. array( 'status' => rest_authorization_required_code() )
  584. );
  585. }
  586. if ( ( 'active' === $new_status || 'network-active' === $new_status ) && ! current_user_can( 'activate_plugin', $plugin ) ) {
  587. return new WP_Error(
  588. 'rest_cannot_activate_plugin',
  589. __( 'Sorry, you are not allowed to activate this plugin.' ),
  590. array( 'status' => rest_authorization_required_code() )
  591. );
  592. }
  593. if ( 'inactive' === $new_status && ! current_user_can( 'deactivate_plugin', $plugin ) ) {
  594. return new WP_Error(
  595. 'rest_cannot_deactivate_plugin',
  596. __( 'Sorry, you are not allowed to deactivate this plugin.' ),
  597. array( 'status' => rest_authorization_required_code() )
  598. );
  599. }
  600. return true;
  601. }
  602. /**
  603. * Handle updating a plugin's status.
  604. *
  605. * @since 5.5.0
  606. *
  607. * @param string $plugin The plugin file to update.
  608. * @param string $new_status The plugin's new status.
  609. * @param string $current_status The plugin's current status.
  610. * @return true|WP_Error
  611. */
  612. protected function handle_plugin_status( $plugin, $new_status, $current_status ) {
  613. if ( 'inactive' === $new_status ) {
  614. deactivate_plugins( $plugin, false, 'network-active' === $current_status );
  615. return true;
  616. }
  617. if ( 'active' === $new_status && 'network-active' === $current_status ) {
  618. return true;
  619. }
  620. $network_activate = 'network-active' === $new_status;
  621. if ( is_multisite() && ! $network_activate && is_network_only_plugin( $plugin ) ) {
  622. return new WP_Error(
  623. 'rest_network_only_plugin',
  624. __( 'Network only plugin must be network activated.' ),
  625. array( 'status' => 400 )
  626. );
  627. }
  628. $activated = activate_plugin( $plugin, '', $network_activate );
  629. if ( is_wp_error( $activated ) ) {
  630. $activated->add_data( array( 'status' => 500 ) );
  631. return $activated;
  632. }
  633. return true;
  634. }
  635. /**
  636. * Checks that the "plugin" parameter is a valid path.
  637. *
  638. * @since 5.5.0
  639. *
  640. * @param string $file The plugin file parameter.
  641. * @return bool
  642. */
  643. public function validate_plugin_param( $file ) {
  644. if ( ! is_string( $file ) || ! preg_match( '/' . self::PATTERN . '/u', $file ) ) {
  645. return false;
  646. }
  647. $validated = validate_file( plugin_basename( $file ) );
  648. return 0 === $validated;
  649. }
  650. /**
  651. * Sanitizes the "plugin" parameter to be a proper plugin file with ".php" appended.
  652. *
  653. * @since 5.5.0
  654. *
  655. * @param string $file The plugin file parameter.
  656. * @return string
  657. */
  658. public function sanitize_plugin_param( $file ) {
  659. return plugin_basename( sanitize_text_field( $file . '.php' ) );
  660. }
  661. /**
  662. * Checks if the plugin matches the requested parameters.
  663. *
  664. * @since 5.5.0
  665. *
  666. * @param WP_REST_Request $request The request to require the plugin matches against.
  667. * @param array $item The plugin item.
  668. * @return bool
  669. */
  670. protected function does_plugin_match_request( $request, $item ) {
  671. $search = $request['search'];
  672. if ( $search ) {
  673. $matched_search = false;
  674. foreach ( $item as $field ) {
  675. if ( is_string( $field ) && false !== strpos( strip_tags( $field ), $search ) ) {
  676. $matched_search = true;
  677. break;
  678. }
  679. }
  680. if ( ! $matched_search ) {
  681. return false;
  682. }
  683. }
  684. $status = $request['status'];
  685. if ( $status && ! in_array( $this->get_plugin_status( $item['_file'] ), $status, true ) ) {
  686. return false;
  687. }
  688. return true;
  689. }
  690. /**
  691. * Checks if the plugin is installed.
  692. *
  693. * @since 5.5.0
  694. *
  695. * @param string $plugin The plugin file.
  696. * @return bool
  697. */
  698. protected function is_plugin_installed( $plugin ) {
  699. return file_exists( WP_PLUGIN_DIR . '/' . $plugin );
  700. }
  701. /**
  702. * Determine if the endpoints are available.
  703. *
  704. * Only the 'Direct' filesystem transport, and SSH/FTP when credentials are stored are supported at present.
  705. *
  706. * @since 5.5.0
  707. *
  708. * @return true|WP_Error True if filesystem is available, WP_Error otherwise.
  709. */
  710. protected function is_filesystem_available() {
  711. $filesystem_method = get_filesystem_method();
  712. if ( 'direct' === $filesystem_method ) {
  713. return true;
  714. }
  715. ob_start();
  716. $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() );
  717. ob_end_clean();
  718. if ( $filesystem_credentials_are_stored ) {
  719. return true;
  720. }
  721. return new WP_Error( 'fs_unavailable', __( 'The filesystem is currently unavailable for managing plugins.' ), array( 'status' => 500 ) );
  722. }
  723. /**
  724. * Retrieves the plugin's schema, conforming to JSON Schema.
  725. *
  726. * @since 5.5.0
  727. *
  728. * @return array Item schema data.
  729. */
  730. public function get_item_schema() {
  731. if ( $this->schema ) {
  732. return $this->add_additional_fields_schema( $this->schema );
  733. }
  734. $this->schema = array(
  735. '$schema' => 'http://json-schema.org/draft-04/schema#',
  736. 'title' => 'plugin',
  737. 'type' => 'object',
  738. 'properties' => array(
  739. 'plugin' => array(
  740. 'description' => __( 'The plugin file.' ),
  741. 'type' => 'string',
  742. 'pattern' => self::PATTERN,
  743. 'readonly' => true,
  744. 'context' => array( 'view', 'edit', 'embed' ),
  745. ),
  746. 'status' => array(
  747. 'description' => __( 'The plugin activation status.' ),
  748. 'type' => 'string',
  749. 'enum' => is_multisite() ? array( 'inactive', 'active', 'network-active' ) : array( 'inactive', 'active' ),
  750. 'context' => array( 'view', 'edit', 'embed' ),
  751. ),
  752. 'name' => array(
  753. 'description' => __( 'The plugin name.' ),
  754. 'type' => 'string',
  755. 'readonly' => true,
  756. 'context' => array( 'view', 'edit', 'embed' ),
  757. ),
  758. 'plugin_uri' => array(
  759. 'description' => __( 'The plugin\'s website address.' ),
  760. 'type' => 'string',
  761. 'format' => 'uri',
  762. 'readonly' => true,
  763. 'context' => array( 'view', 'edit' ),
  764. ),
  765. 'author' => array(
  766. 'description' => __( 'The plugin author.' ),
  767. 'type' => 'object',
  768. 'readonly' => true,
  769. 'context' => array( 'view', 'edit' ),
  770. ),
  771. 'author_uri' => array(
  772. 'description' => __( 'Plugin author\'s website address.' ),
  773. 'type' => 'string',
  774. 'format' => 'uri',
  775. 'readonly' => true,
  776. 'context' => array( 'view', 'edit' ),
  777. ),
  778. 'description' => array(
  779. 'description' => __( 'The plugin description.' ),
  780. 'type' => 'object',
  781. 'readonly' => true,
  782. 'context' => array( 'view', 'edit' ),
  783. 'properties' => array(
  784. 'raw' => array(
  785. 'description' => __( 'The raw plugin description.' ),
  786. 'type' => 'string',
  787. ),
  788. 'rendered' => array(
  789. 'description' => __( 'The plugin description formatted for display.' ),
  790. 'type' => 'string',
  791. ),
  792. ),
  793. ),
  794. 'version' => array(
  795. 'description' => __( 'The plugin version number.' ),
  796. 'type' => 'string',
  797. 'readonly' => true,
  798. 'context' => array( 'view', 'edit' ),
  799. ),
  800. 'network_only' => array(
  801. 'description' => __( 'Whether the plugin can only be activated network-wide.' ),
  802. 'type' => 'boolean',
  803. 'readonly' => true,
  804. 'context' => array( 'view', 'edit', 'embed' ),
  805. ),
  806. 'requires_wp' => array(
  807. 'description' => __( 'Minimum required version of WordPress.' ),
  808. 'type' => 'string',
  809. 'readonly' => true,
  810. 'context' => array( 'view', 'edit', 'embed' ),
  811. ),
  812. 'requires_php' => array(
  813. 'description' => __( 'Minimum required version of PHP.' ),
  814. 'type' => 'string',
  815. 'readonly' => true,
  816. 'context' => array( 'view', 'edit', 'embed' ),
  817. ),
  818. 'textdomain' => array(
  819. 'description' => __( 'The plugin\'s text domain.' ),
  820. 'type' => 'string',
  821. 'readonly' => true,
  822. 'context' => array( 'view', 'edit' ),
  823. ),
  824. ),
  825. );
  826. return $this->add_additional_fields_schema( $this->schema );
  827. }
  828. /**
  829. * Retrieves the query params for the collections.
  830. *
  831. * @since 5.5.0
  832. *
  833. * @return array Query parameters for the collection.
  834. */
  835. public function get_collection_params() {
  836. $query_params = parent::get_collection_params();
  837. $query_params['context']['default'] = 'view';
  838. $query_params['status'] = array(
  839. 'description' => __( 'Limits results to plugins with the given status.' ),
  840. 'type' => 'array',
  841. 'items' => array(
  842. 'type' => 'string',
  843. 'enum' => is_multisite() ? array( 'inactive', 'active', 'network-active' ) : array( 'inactive', 'active' ),
  844. ),
  845. );
  846. unset( $query_params['page'], $query_params['per_page'] );
  847. return $query_params;
  848. }
  849. }