← All changes
|
jetpack_vendor/automattic/jetpack-import/src/endpoints/class-start.php
+158
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,158 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Start REST route | |
| 4 | + * | |
| 5 | + * @package automattic/jetpack-import | |
| 6 | + */ | |
| 7 | + | |
| 8 | +namespace Automattic\Jetpack\Import\Endpoints; | |
| 9 | + | |
| 10 | +use Automattic\Jetpack\Import\Main; | |
| 11 | +use WP_Error; | |
| 12 | +use WP_REST_Request; | |
| 13 | +use WP_REST_Response; | |
| 14 | + | |
| 15 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 16 | + exit( 0 ); | |
| 17 | +} | |
| 18 | + | |
| 19 | +/** | |
| 20 | + * Class Start | |
| 21 | + * | |
| 22 | + * This class is used to start the import process. | |
| 23 | + */ | |
| 24 | +class Start extends \WP_REST_Controller { | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * Base class | |
| 28 | + */ | |
| 29 | + use Import; | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * Constructor. | |
| 33 | + */ | |
| 34 | + public function __construct() { | |
| 35 | + $this->rest_base = 'start'; | |
| 36 | + } | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * Get the register route options. | |
| 40 | + * | |
| 41 | + * @see register_rest_route() | |
| 42 | + * | |
| 43 | + * @return array The options. | |
| 44 | + */ | |
| 45 | + protected function get_route_options() { | |
| 46 | + return array( | |
| 47 | + array( | |
| 48 | + 'methods' => \WP_REST_Server::READABLE, | |
| 49 | + 'callback' => array( $this, 'get_item' ), | |
| 50 | + 'permission_callback' => array( $this, 'import_permissions_callback' ), | |
| 51 | + 'args' => array(), | |
| 52 | + ), | |
| 53 | + 'schema' => array( $this, 'get_public_item_schema' ), | |
| 54 | + ); | |
| 55 | + } | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * Retrieves main informations. | |
| 59 | + * | |
| 60 | + * @since 4.7.0 | |
| 61 | + * | |
| 62 | + * @param WP_REST_Request $request Full details about the request. | |
| 63 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. | |
| 64 | + */ | |
| 65 | + public function get_item( $request ) { | |
| 66 | + $items = array( | |
| 67 | + 'max_batch_items' => apply_filters( 'rest_get_max_batch_size', 25 ), | |
| 68 | + 'max_execution_time' => (int) ini_get( 'max_execution_time' ), | |
| 69 | + 'max_input_time' => (int) ini_get( 'max_input_time' ), | |
| 70 | + 'mime_types' => get_allowed_mime_types(), | |
| 71 | + 'posts_max_id' => (int) $this->get_posts_max_id(), | |
| 72 | + 'version' => Main::PACKAGE_VERSION, | |
| 73 | + ); | |
| 74 | + | |
| 75 | + $response = array(); | |
| 76 | + | |
| 77 | + foreach ( $items as $name => $value ) { | |
| 78 | + /** | |
| 79 | + * Filters the value of a item recognized by the REST API. | |
| 80 | + */ | |
| 81 | + $response[ $name ] = apply_filters( 'jetpack_import_rest_get_start', $value, $name, $request ); | |
| 82 | + } | |
| 83 | + | |
| 84 | + return $response; | |
| 85 | + } | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * Retrieves the start values schema, conforming to JSON Schema. | |
| 89 | + * | |
| 90 | + * @return array Item schema data. | |
| 91 | + */ | |
| 92 | + public function get_item_schema() { | |
| 93 | + if ( $this->schema ) { | |
| 94 | + return $this->add_additional_fields_schema( $this->schema ); | |
| 95 | + } | |
| 96 | + | |
| 97 | + $schema = array( | |
| 98 | + '$schema' => 'http://json-schema.org/draft-04/schema#', | |
| 99 | + 'title' => 'import-start', | |
| 100 | + 'type' => 'object', | |
| 101 | + 'properties' => array( | |
| 102 | + 'max_batch_items' => array( | |
| 103 | + 'description' => __( 'Max batch size.', 'jetpack-import' ), | |
| 104 | + 'type' => 'integer', | |
| 105 | + 'context' => array( 'view' ), | |
| 106 | + 'readonly' => true, | |
| 107 | + ), | |
| 108 | + 'max_execution_time' => array( | |
| 109 | + 'description' => __( 'Max execution time.', 'jetpack-import' ), | |
| 110 | + 'type' => 'integer', | |
| 111 | + 'context' => array( 'view' ), | |
| 112 | + 'readonly' => true, | |
| 113 | + ), | |
| 114 | + 'max_input_time' => array( | |
| 115 | + 'description' => __( 'Max execution input time.', 'jetpack-import' ), | |
| 116 | + 'type' => 'integer', | |
| 117 | + 'context' => array( 'view' ), | |
| 118 | + 'readonly' => true, | |
| 119 | + ), | |
| 120 | + 'mime_types' => array( | |
| 121 | + 'description' => __( 'Upload accepted mime types.', 'jetpack-import' ), | |
| 122 | + 'type' => 'array', | |
| 123 | + 'items' => array( 'type' => 'string' ), | |
| 124 | + 'context' => array( 'view' ), | |
| 125 | + 'readonly' => true, | |
| 126 | + ), | |
| 127 | + 'posts_max_id' => array( | |
| 128 | + 'description' => __( 'Last posts autogenerated ID.', 'jetpack-import' ), | |
| 129 | + 'type' => 'integer', | |
| 130 | + 'context' => array( 'view' ), | |
| 131 | + 'readonly' => true, | |
| 132 | + ), | |
| 133 | + 'version' => array( | |
| 134 | + 'description' => __( 'Version of the import package.', 'jetpack-import' ), | |
| 135 | + 'type' => 'string', | |
| 136 | + 'context' => array( 'view' ), | |
| 137 | + 'readonly' => true, | |
| 138 | + ), | |
| 139 | + ), | |
| 140 | + ); | |
| 141 | + | |
| 142 | + $this->schema = $schema; | |
| 143 | + | |
| 144 | + return $this->add_additional_fields_schema( $this->schema ); | |
| 145 | + } | |
| 146 | + | |
| 147 | + /** | |
| 148 | + * Get the last posts autogenerated ID. | |
| 149 | + */ | |
| 150 | + private function get_posts_max_id() { | |
| 151 | + global $wpdb; | |
| 152 | + | |
| 153 | + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching | |
| 154 | + $max_id = $wpdb->get_var( "SELECT MAX(ID) FROM $wpdb->posts" ); | |
| 155 | + | |
| 156 | + return $max_id; | |
| 157 | + } | |
| 158 | +} | |