public function setUp() { parent::setUp(); // Reset REST server to ensure only our routes are registered $GLOBALS['wp_rest_server'] = null; add_filter('wp_rest_server_class', array($this, 'filter_wp_rest_server_class')); $this->server = rest_get_server(); remove_filter('wp_rest_server_class', array($this, 'filter_wp_rest_server_class')); }
/** * Returns an array holding the defined links of the given response object. * * @since 2.0.0 * * @param WP_REST_Response $response Response object. * * @return array The array holding the defined links of the given response object. */ private function get_links(WP_REST_Response $response) { $server = rest_get_server(); foreach (['get_compact_response_links', 'get_response_links'] as $method) { if (is_callable([$server, $method])) { return (array) call_user_func([$server, $method], $response); } } return []; }
/** * Set up. */ function setUp() { parent::setUp(); $this->plugin = get_plugin_instance(); $this->plugin->customize_snapshot_manager->post_type->register(); $snapshot_data = array(array('status' => 'draft', 'date_gmt' => '2010-01-01 00:00:00'), array('status' => 'pending', 'date_gmt' => '2010-01-01 00:00:00'), array('status' => 'publish', 'date_gmt' => '2010-01-01 00:00:00'), array('status' => 'future', 'date_gmt' => gmdate('Y-m-d H:i:s', time() + 24 * 3600))); foreach ($snapshot_data as $i => $snapshot_params) { $user_id = $this->factory()->user->create(array('role' => 'administrator')); $post_id = $this->plugin->customize_snapshot_manager->post_type->save(array_merge($snapshot_params, array('uuid' => Customize_Snapshot_Manager::generate_uuid(), 'author' => $user_id, 'data' => array('blogname' => array('value' => "Snapshot {$i}"))))); $this->snapshot_by_status[$snapshot_params['status']] = $post_id; } global $wp_rest_server; $wp_rest_server = null; $this->server = rest_get_server(); }
/** * Do a REST request. * * Used primarily to route internal requests through WP_REST_Server. * * @since 4.4.0 * * @global WP_REST_Server $wp_rest_server ResponseHandler instance (usually WP_REST_Server). * * @param WP_REST_Request|string $request Request. * @return WP_REST_Response REST response. */ function rest_do_request($request) { $request = rest_ensure_request($request); return rest_get_server()->dispatch($request); }
/** * Returns the REST server instance (usually WP_REST_Server). * * @see rest_get_server() * * @return \WP_REST_Server */ public function getServer() { return rest_get_server(); }
function wp_enqueue_scripts() { global $post; $rest_server = rest_get_server(); if ($this->has_fee()) { wp_enqueue_style('wp-core-ui', $this->url('/css/wp-core-ui.css'), false, self::VERSION, 'screen'); wp_enqueue_style('wp-core-ui-colors', $this->url('/css/wp-core-ui-colors.css'), false, self::VERSION, 'screen'); wp_enqueue_style('wp-auth-check'); wp_enqueue_script('wp-auth-check'); wp_enqueue_script('fee-tinymce', $this->url('/vendor/tinymce.js'), array(), self::TINYMCE_VERSION, true); wp_enqueue_script('fee-tinymce-image', $this->url('/js/tinymce.image.js'), array('fee-tinymce'), self::VERSION, true); wp_enqueue_script('fee-tinymce-theme', $this->url('/js/tinymce.theme.js'), array('fee-tinymce'), self::VERSION, true); foreach (array('lists', 'paste', 'wordpress', 'wplink', 'wptextpattern', 'wpview') as $plugin) { wp_enqueue_script('fee-' . $plugin, $this->url('/vendor/' . $plugin . '.js'), array('fee-tinymce'), self::VERSION, true); } $tinymce_plugins = array('wordpress', 'feeImage', 'wptextpattern', 'wplink', 'wpview', 'paste', 'lists'); $tinymce_toolbar = array('bold', 'italic', 'strikethrough', 'link'); $tinymce = array('selector' => '.fee-content', 'plugins' => implode(' ', array_unique(apply_filters('fee_tinymce_plugins', $tinymce_plugins))), 'toolbar' => apply_filters('fee_tinymce_toolbar', $tinymce_toolbar), 'theme' => 'fee', 'inline' => true, 'relative_urls' => false, 'convert_urls' => false, 'browser_spellcheck' => true, 'placeholder' => apply_filters('fee_content_placeholder', __('Just write…')), 'wpeditimage_html5_captions' => current_theme_supports('html5', 'caption'), 'end_container_on_empty_block' => true); $request = new WP_REST_Request('GET', '/wp/v2/' . ($post->post_type === 'page' ? 'pages' : 'posts') . '/' . $post->ID); $request->set_query_params(array('context' => 'edit')); $result = $rest_server->dispatch($request); wp_enqueue_script('fee', $this->url('/js/fee.js'), array('fee-tinymce', 'wp-util', 'heartbeat', 'editor', 'wp-api', 'media-views'), self::VERSION, true); wp_localize_script('fee', 'feeData', array('tinymce' => apply_filters('fee_tinymce_config', $tinymce), 'post' => $result->get_data(), 'lock' => !wp_check_post_lock($post->ID) ? implode(':', wp_set_post_lock($post->ID)) : false, 'titlePlaceholder' => apply_filters('enter_title_here', __('Enter title here'), $post), 'editURL' => get_edit_post_link())); $request = new WP_REST_Request('GET', '/wp/v2'); $result = $rest_server->dispatch($request); wp_localize_script('wp-api', 'wpApiSettings', array('root' => esc_url_raw(get_rest_url()), 'nonce' => wp_create_nonce('wp_rest'), 'versionString' => 'wp/v2/', 'schema' => $result->get_data(), 'cacheSchema' => true)); wp_enqueue_media(array('post' => $post)); wp_deregister_script('mce-view'); wp_enqueue_script('mce-view', $this->url('/vendor/mce-view.js'), array('shortcode', 'jquery', 'media-views', 'media-audiovideo'), self::VERSION, true); wp_enqueue_script('mce-view-register', $this->url('/js/mce-view-register.js'), array('mce-view', 'fee'), self::VERSION, true); wp_localize_script('mce-view-register', 'mce_view_register', array('post_id' => $post->ID)); wp_enqueue_style('tinymce-core', $this->url('/css/tinymce.core.css'), false, self::VERSION, 'screen'); wp_enqueue_style('tinymce-view', $this->url('/css/tinymce.view.css'), false, self::VERSION, 'screen'); wp_enqueue_style('fee', $this->url('/css/fee.css'), false, self::VERSION, 'screen'); wp_enqueue_style('dashicons'); } if (current_user_can('edit_posts')) { if (is_singular()) { require_once ABSPATH . '/wp-admin/includes/post.php'; $user_id = wp_check_post_lock($post->ID); $user = get_userdata($user_id); } wp_enqueue_script('fee-adminbar', $this->url('/js/fee-adminbar.js'), array('wp-util', 'wp-api'), self::VERSION, true); wp_localize_script('fee-adminbar', 'fee_adminbar', array('lock' => is_singular() && $user_id ? $user->display_name : false, 'supportedPostTypes' => $this->get_supported_post_types(), 'postNew' => admin_url('post-new.php'), 'nonce' => wp_create_nonce('fee-new'))); } }
/** * Update a single plugin. * * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response|WP_Error Return updated object. */ public function update_item($request) { $plugins = $this->get_plugins(); if (!isset($plugins[$request['id']])) { return new WP_Error('rest_plugin_invalid_id', __('Invalid plugin ID.'), array('status' => 400)); } $plugin = urldecode($request['id']) . '.php'; if (!empty($request['status']) && $plugins[$request['id']]['type'] === 'plugin') { $request['status'] = filter_var($request['status'], FILTER_VALIDATE_BOOLEAN); if ($request['status']) { $status = activate_plugin($plugin); if (is_wp_error($status)) { return new WP_Error('rest_cannot_edit', __('The resource cannot be edited.'), array('status' => 500)); } } else { deactivate_plugins($plugin, false, false); } } // Fetch and return the updated item. $item_request = new WP_REST_Request('GET', '/' . $this->namespace . '/' . $this->rest_base . '/' . $request['id']); $item_request->set_param('context', $request['context']); return rest_get_server()->dispatch($item_request); }
/** * Prepare a response for inserting into a collection. * * @param WP_REST_Response $response Response object. * @return array Response data, ready for insertion into collection data. */ public function prepare_response_for_collection($response) { if (!$response instanceof WP_REST_Response) { return $response; } $data = (array) $response->get_data(); $server = rest_get_server(); if (method_exists($server, 'get_compact_response_links')) { $links = call_user_func(array($server, 'get_compact_response_links'), $response); } else { $links = call_user_func(array($server, 'get_response_links'), $response); } if (!empty($links)) { $data['_links'] = $links; } return $data; }