wp/wp-includes/rest-api/endpoints/class-wp-rest-plugins-controller.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
   296 				),
   296 				),
   297 			)
   297 			)
   298 		);
   298 		);
   299 
   299 
   300 		if ( is_wp_error( $api ) ) {
   300 		if ( is_wp_error( $api ) ) {
   301 			if ( false !== strpos( $api->get_error_message(), 'Plugin not found.' ) ) {
   301 			if ( str_contains( $api->get_error_message(), 'Plugin not found.' ) ) {
   302 				$api->add_data( array( 'status' => 404 ) );
   302 				$api->add_data( array( 'status' => 404 ) );
   303 			} else {
   303 			} else {
   304 				$api->add_data( array( 'status' => 500 ) );
   304 				$api->add_data( array( 'status' => 500 ) );
   305 			}
   305 			}
   306 
   306 
   379 		$installed_locales = array_values( get_available_languages() );
   379 		$installed_locales = array_values( get_available_languages() );
   380 		/** This filter is documented in wp-includes/update.php */
   380 		/** This filter is documented in wp-includes/update.php */
   381 		$installed_locales = apply_filters( 'plugins_update_check_locales', $installed_locales );
   381 		$installed_locales = apply_filters( 'plugins_update_check_locales', $installed_locales );
   382 
   382 
   383 		$language_packs = array_map(
   383 		$language_packs = array_map(
   384 			static function( $item ) {
   384 			static function ( $item ) {
   385 				return (object) $item;
   385 				return (object) $item;
   386 			},
   386 			},
   387 			$api->language_packs
   387 			$api->language_packs
   388 		);
   388 		);
   389 
   389 
   390 		$language_packs = array_filter(
   390 		$language_packs = array_filter(
   391 			$language_packs,
   391 			$language_packs,
   392 			static function( $pack ) use ( $installed_locales ) {
   392 			static function ( $pack ) use ( $installed_locales ) {
   393 				return in_array( $pack->language, $installed_locales, true );
   393 				return in_array( $pack->language, $installed_locales, true );
   394 			}
   394 			}
   395 		);
   395 		);
   396 
   396 
   397 		if ( $language_packs ) {
   397 		if ( $language_packs ) {
   569 	/**
   569 	/**
   570 	 * Prepares the plugin for the REST response.
   570 	 * Prepares the plugin for the REST response.
   571 	 *
   571 	 *
   572 	 * @since 5.5.0
   572 	 * @since 5.5.0
   573 	 *
   573 	 *
   574 	 * @param mixed           $item    Unmarked up and untranslated plugin data from {@see get_plugin_data()}.
   574 	 * @param array           $item    Unmarked up and untranslated plugin data from {@see get_plugin_data()}.
   575 	 * @param WP_REST_Request $request Request object.
   575 	 * @param WP_REST_Request $request Request object.
   576 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
   576 	 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
   577 	 */
   577 	 */
   578 	public function prepare_item_for_response( $item, $request ) {
   578 	public function prepare_item_for_response( $item, $request ) {
       
   579 		$fields = $this->get_fields_for_response( $request );
       
   580 
   579 		$item   = _get_plugin_data_markup_translate( $item['_file'], $item, false );
   581 		$item   = _get_plugin_data_markup_translate( $item['_file'], $item, false );
   580 		$marked = _get_plugin_data_markup_translate( $item['_file'], $item, true );
   582 		$marked = _get_plugin_data_markup_translate( $item['_file'], $item, true );
   581 
   583 
   582 		$data = array(
   584 		$data = array(
   583 			'plugin'       => substr( $item['_file'], 0, - 4 ),
   585 			'plugin'       => substr( $item['_file'], 0, - 4 ),
   598 		);
   600 		);
   599 
   601 
   600 		$data = $this->add_additional_fields_to_object( $data, $request );
   602 		$data = $this->add_additional_fields_to_object( $data, $request );
   601 
   603 
   602 		$response = new WP_REST_Response( $data );
   604 		$response = new WP_REST_Response( $data );
   603 		$response->add_links( $this->prepare_links( $item ) );
   605 
       
   606 		if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
       
   607 			$response->add_links( $this->prepare_links( $item ) );
       
   608 		}
   604 
   609 
   605 		/**
   610 		/**
   606 		 * Filters plugin data for a REST API response.
   611 		 * Filters plugin data for a REST API response.
   607 		 *
   612 		 *
   608 		 * @since 5.5.0
   613 		 * @since 5.5.0
   623 	 * @return array[]
   628 	 * @return array[]
   624 	 */
   629 	 */
   625 	protected function prepare_links( $item ) {
   630 	protected function prepare_links( $item ) {
   626 		return array(
   631 		return array(
   627 			'self' => array(
   632 			'self' => array(
   628 				'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, substr( $item['_file'], 0, - 4 ) ) ),
   633 				'href' => rest_url(
       
   634 					sprintf(
       
   635 						'%s/%s/%s',
       
   636 						$this->namespace,
       
   637 						$this->rest_base,
       
   638 						substr( $item['_file'], 0, - 4 )
       
   639 					)
       
   640 				),
   629 			),
   641 			),
   630 		);
   642 		);
   631 	}
   643 	}
   632 
   644 
   633 	/**
   645 	/**
   795 
   807 
   796 		if ( $search ) {
   808 		if ( $search ) {
   797 			$matched_search = false;
   809 			$matched_search = false;
   798 
   810 
   799 			foreach ( $item as $field ) {
   811 			foreach ( $item as $field ) {
   800 				if ( is_string( $field ) && false !== strpos( strip_tags( $field ), $search ) ) {
   812 				if ( is_string( $field ) && str_contains( strip_tags( $field ), $search ) ) {
   801 					$matched_search = true;
   813 					$matched_search = true;
   802 					break;
   814 					break;
   803 				}
   815 				}
   804 			}
   816 			}
   805 
   817