rest_api_init(); } /** * Add new query vars. * * @since 2.0 * @param array $vars * @return string[] */ public function add_query_vars( $vars ) { $vars[] = 'appmaker-wp-api'; return $vars; } public static function add_endpoint() { add_rewrite_endpoint( 'appmaker-wp-api', EP_ALL ); } /** * API request - Trigger any API requests. * * @since 2.0 * @version 2.4 */ public function handle_api_requests() { global $wp; if ( ! empty( $_GET['appmaker-wp-api'] ) ) { $wp->query_vars['appmaker-wp-api'] = $_GET['appmaker-wp-api']; } // appmaker-wc-api endpoint requests. if ( ! empty( $wp->query_vars['appmaker-wp-api'] ) ) { // Buffer, we won't want any output here. ob_start(); // No cache headers. nocache_headers(); // Clean the API request. $api_request = strtolower( wc_clean( $wp->query_vars['appmaker-wp-api'] ) ); // Trigger generic action before request hook. do_action( 'appmaker_wp_api_request', $api_request ); // Is there actually something hooked into this API request? If not trigger 400 - Bad request. status_header( has_action( 'appmaker_wp_api_' . $api_request ) ? 200 : 400 ); // Trigger an action which plugins can hook into to fulfill the request. do_action( 'appmaker_wp_api_' . $api_request ); // Done, clear buffer and exit. ob_end_clean(); die( '-1' ); } } private function rest_api_init() { global $wp_version; // REST API was included starting WordPress 4.4. if ( version_compare( $wp_version, 4.4, '<' ) ) { return; } $this->rest_api_includes(); // Init REST API routes. add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) ); } public function rest_api_includes() { // WP-API classes and functions. include_once( 'vendor/wp-rest-functions.php' ); if ( ! class_exists( 'WP_REST_Controller' ) ) { include_once( 'vendor/class-wp-rest-controller.php' ); } // Abstract Classes include_once( 'abstracts/abstract-appmaker-wp-rest-controller.php'); // Endpoints Classes include_once( 'endpoints/class-appmaker-wp-rest-posts-controller.php'); include_once( 'endpoints/class-appmaker-wp-rest-terms-controller.php'); include_once( 'endpoints/appmaker/class-appmaker-wp-rest-backend-media-controller.php'); include_once( 'endpoints/appmaker/class-appmaker-wp-rest-backend-inapppage-controller.php'); include_once( 'endpoints/appmaker/class-appmaker-wp-rest-frontend-controller.php'); include_once( 'endpoints/appmaker/class-appmaker-wp-rest-backend-posts-controller.php'); include_once( 'endpoints/appmaker/class-appmaker-wp-rest-backend-terms-controller.php'); include_once( 'endpoints/appmaker/class-appmaker-wp-rest-backend-nav-controller.php'); // Format Converter include_once( 'class-appmaker-wp-converter.php'); } public function register_rest_routes() { global $wp_post_types; global $wp_taxonomies; if (isset($wp_post_types['post'])) { $wp_post_types['post']->show_in_rest = true; $wp_post_types['post']->rest_base = 'posts'; } if (isset($wp_post_types['page'])) { $wp_post_types['page']->show_in_rest = true; $wp_post_types['page']->rest_base = 'pages'; } if (isset($wp_taxonomies['category'])) { $wp_taxonomies['category']->show_in_rest = true; $wp_taxonomies['category']->rest_base = 'categories'; } if (isset($wp_taxonomies['post_tag'])) { $wp_taxonomies['post_tag']->show_in_rest = true; $wp_taxonomies['post_tag']->rest_base = 'tags'; } $controllers = array( "APPMAKER_WP_REST_Posts_Controller" => array("APPMAKER_WP_REST_Posts_Controller","post"), "APPMAKER_WP_REST_Pages_Controller" => array("APPMAKER_WP_REST_Posts_Controller","page"), "APPMAKER_WP_REST_Category_Controller" => array("APPMAKER_WP_REST_Terms_Controller","category"), "APPMAKER_WP_REST_Tag_Controller" => array("APPMAKER_WP_REST_Terms_Controller","post_tag"), "APPMAKER_WP_REST_BACKEND_INAPPPAGE_Controller" , "APPMAKER_WP_REST_BACKEND_NAV_Controller", "APPMAKER_WP_REST_BACKEND_MEDIA_Controller", "APPMAKER_WP_REST_BACKEND_Posts_Controller", "APPMAKER_WP_REST_BACKEND_Terms_Controller", "APPMAKER_WP_REST_FRONTEND_INAPPPAGE_Controller" => array("APPMAKER_WP_REST_FRONTEND_Controller","inAppPages"), "APPMAKER_WP_REST_FRONTEND_NAV_Controller" => array("APPMAKER_WP_REST_FRONTEND_Controller","navigationMenu") ); foreach ( $controllers as $key => $controller ) { if(is_array($controller)){ $this->{$key} = new $controller[0]($controller[1]); $this->$key->register_routes(); }else{ $this->$controller = new $controller(); $this->$controller->register_routes(); } } } }