| 1 |
<?php |
| 2 |
/** |
| 3 |
* Set of REST API routes used in WPCOM Unified Importer. |
| 4 |
* |
| 5 |
* @package automattic/jetpack-import |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Automattic\Jetpack\Import; |
| 9 |
|
| 10 |
use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 11 |
use Automattic\Jetpack\Connection\Rest_Authentication; |
| 12 |
|
| 13 |
/** |
| 14 |
* This class will provide endpoint for the Unified Importer. |
| 15 |
*/ |
| 16 |
class Main { |
| 17 |
|
| 18 |
/** |
| 19 |
* Package version. |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
const PACKAGE_VERSION = '0.10.0'; |
| 24 |
|
| 25 |
/** |
| 26 |
* A list of all the routes. |
| 27 |
* |
| 28 |
* @var \WP_REST_Controller[] |
| 29 |
*/ |
| 30 |
private static $routes = array(); |
| 31 |
|
| 32 |
/** |
| 33 |
* Before everything else starts getting initalized, we need to initialize Jetpack using the |
| 34 |
* Config object. |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public static function configure() { |
| 39 |
if ( did_action( 'jetpack_import_initialized' ) ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
$connection = new Connection_Manager(); |
| 44 |
|
| 45 |
// Initialize the REST API only if the user is connected. |
| 46 |
if ( $connection->has_connected_owner() ) { |
| 47 |
add_action( 'rest_api_init', array( __CLASS__, 'initialize_rest_api' ) ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Runs right after the Jetpack Import package is initialized. |
| 52 |
* |
| 53 |
* @since 0.1.0 |
| 54 |
*/ |
| 55 |
do_action( 'jetpack_import_initialized' ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Register import related REST routes. |
| 60 |
* |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
public static function initialize_rest_api() { |
| 64 |
// Set up the REST authentication hooks. |
| 65 |
Rest_Authentication::init(); |
| 66 |
|
| 67 |
$routes = array( |
| 68 |
'blocks' => new Endpoints\Block(), |
| 69 |
'categories' => new Endpoints\Category(), |
| 70 |
'comments' => new Endpoints\Comment(), |
| 71 |
'custom-css' => new Endpoints\Custom_CSS(), |
| 72 |
'end' => new Endpoints\End(), |
| 73 |
'global-styles' => new Endpoints\Global_Style(), |
| 74 |
'media' => new Endpoints\Attachment(), |
| 75 |
'menu-items' => new Endpoints\Menu_Item(), |
| 76 |
'menus' => new Endpoints\Menu(), |
| 77 |
'navigation' => new Endpoints\Navigation(), |
| 78 |
'pages' => new Endpoints\Page(), |
| 79 |
'posts' => new Endpoints\Post(), |
| 80 |
'start' => new Endpoints\Start(), |
| 81 |
'tags' => new Endpoints\Tag(), |
| 82 |
'template-parts' => new Endpoints\Template_Part(), |
| 83 |
'templates' => new Endpoints\Template(), |
| 84 |
); |
| 85 |
|
| 86 |
/** |
| 87 |
* Allow other plugins to modify import routes. |
| 88 |
* |
| 89 |
* @since 0.1.0 |
| 90 |
* |
| 91 |
* @param array $routes Array of import routes. |
| 92 |
*/ |
| 93 |
self::$routes = apply_filters( 'jetpack_import_types', $routes ); |
| 94 |
|
| 95 |
// Register all the routes. |
| 96 |
foreach ( self::$routes as $route ) { |
| 97 |
$route->register_routes(); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|