title: Prepare Wordpress Post Revision for Output in WP REST API
body: <p>I built a custom endpoint for the WordPress REST API to get a post revision. The result is in the Wordpress Post syntax:</p>
  
  <pre><code>[ID] =&gt; 478
  [post_author] =&gt; 1
  [post_date] =&gt; 2017-11-20 17:22:11
  [post_date_gmt] =&gt; 2017-11-20 16:22:11
  [post_content] =&gt; My Post content
  </code></pre>
  
  <p>Whereas the Wordpress REST API would provide me something like this: </p>
  
  <pre><code>"author": 1,
  "title": {
      "rendered": "My title"
  },
  "content": {
      "rendered": "",
      "protected": false
  },
  </code></pre>
  
  <p>(one is printed via php the other is in a JSON format, but what is important is that in the first example it says: <code>post_content</code> and in the second it's <code>content</code> which is then separated in <code>rendered</code> and <code>protected</code> for example.</p>
  
  <p>I am pretty sure that it's almost the same thing as described here:
  <a href="https://wordpress.stackexchange.com/questions/236249/wp-api-v2-custom-endpoint-response-formatting?newreg=7edb54e1ae494e528e5e146982469664">https://wordpress.stackexchange.com/questions/236249/wp-api-v2-custom-endpoint-response-formatting?newreg=7edb54e1ae494e528e5e146982469664</a></p>
  
  <p>But in my case I have revisions.</p>
  
  <p>I tried to prepare the Post Object for the REST API.
  I created a new instance of the WP_REST_Revisions_Controller and tried to use its method <code>prepare_item_for_response</code>. $request ist a WP_Rest_Request. (btw: why do I have to write <code>new \WP_REST_Revisions_Controller</code> with a backslash <code>\</code> before).</p>
  
  <pre><code>$latest_revision = wp_get_post_revisions( $id, $args ); //WP Post Object
  
  $postController = new \WP_REST_Revisions_Controller('revision');
  $response = $postController-&gt;prepare_item_for_response( $latest_revision, $request );
  print_r($response);
  </code></pre>
  
  <p>The problem is that I get Notices: </p>
  
  <pre><code>&lt;b&gt;Notice&lt;/b&gt;:  Trying to get property of non-object in &lt;b&gt;/Users/USER/Documents/my-api/wp/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php&lt;/b&gt; on line &lt;b&gt;350&lt;/b&gt;
  
  &lt;b&gt;Notice&lt;/b&gt;:  Trying to get property of non-object in &lt;b&gt;/Users/USER/Documents/my-api/wp/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php&lt;/b&gt; on line &lt;b&gt;354&lt;/b&gt;
  
  &lt;b&gt;Notice&lt;/b&gt;:  Trying to get property of non-object in &lt;b&gt;/Users/USER/Documents/my-api/wp/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php&lt;/b&gt; on line &lt;b&gt;354&lt;/b&gt;
  ....    
  </code></pre>
  
  <p>Which must refer to these lines:
  <a href="https://developer.wordpress.org/reference/classes/wp_rest_revisions_controller/" rel="nofollow noreferrer">https://developer.wordpress.org/reference/classes/wp_rest_revisions_controller/</a> (line 350 and following).</p>
  
  <p>After the notices I print the <code>$response</code> and I get this:</p>
  
  <pre><code>WP_REST_Response Object
  (
      [links:protected] =&gt; Array
          (
          )
  
      [matched_route:protected] =&gt; 
      [matched_handler:protected] =&gt; 
      [data] =&gt; Array
          (
              [author] =&gt; 0
              [date] =&gt; 
              [date_gmt] =&gt; 
              [id] =&gt; 
              [modified] =&gt; 
              [modified_gmt] =&gt; 
              [parent] =&gt; 0
              [slug] =&gt; 
              [guid] =&gt; Array
                  (
                      [rendered] =&gt; 
                      [raw] =&gt; 
                  )
  
          )
  
      [headers] =&gt; Array
          (
          )
  
      [status] =&gt; 200
  )
  </code></pre>
  
  <p>.. somehow the data is missing or something else went wrong.</p>
  
  <p>And here is the whole php script to see what I am doing:</p>
  
  <pre><code>&lt;?php
  
  /**
   * Add a new API route for a post or pages preview
   */
  
  class Preview_To_REST_API_Controller extends WP_REST_Controller {
  
      //The namespace and version for the REST SERVER
      var $namespace = 'previews/v';
      var $version   = '1';
  
      public function register_routes() {
          $namespace = $this-&gt;namespace . $this-&gt;version;
          $base      = 'preview';
          register_rest_route( $namespace, '/' . $base, array(
              array(
                      'methods'         =&gt; WP_REST_Server::READABLE,
                      'callback'        =&gt; array( $this, 'get_preview' ),
                      'permission_callback'   =&gt; array( $this, 'get_permission' )
                  )
          )  );
      }
  
      // Register our REST Server
      public function hook_rest_server(){
          add_action( 'rest_api_init', array( $this, 'register_routes' ) );
      }
  
      public function get_permission(){
          if ( ! current_user_can( 'edit_posts' ) ) {
              return new WP_Error( 'rest_forbidden', esc_html__( 'You do not have permissions to view this data.', 'my-text-domain' ), array( 'status' =&gt; 401 ) );
          }
  
          // This approach blocks the endpoint operation. You could alternatively do this by an un-blocking approach, by returning false here and changing the permissions check.
          return true;
      }
  
      public function get_preview( WP_REST_Request $request ){
          // use the helper methods to get the parameters
          $id = $request-&gt;get_param( 'id' );
  
          // Only return the newest
          $args = array (
              'order' =&gt; 'DESC',
              'orderby' =&gt; 'date',
              'posts_per_page' =&gt; '1'
          );
  
          // Preview version is saved in the latest revision of the page/post
          $latest_revision = wp_get_post_revisions( $id, $args );
  
          print_r($latest_revision);
  
          $postController = new \WP_REST_Revisions_Controller('revision');
          $response = $postController-&gt;prepare_item_for_response( $latest_revision, $request );
          print_r($response);
  
          if ($latest_revision) {
              // Use the current method to get the only element in the revisions array
              // [0] does not return anything, because the Array's value is saved
              // as the ID key: Array[$id], but this ID we need to find out first
              $revision_id = current($latest_revision) -&gt; ID;
              $acf_fields = get_fields($revision_id);
  
              if ( empty($latest_revision) ) {
                  return null;
              }
  
              // Add acf fields to latest revision
              $latest_revision[$revision_id] -&gt; acf = $acf_fields;
  
              return $latest_revision;
  
          } else {
              return null;
          }
      }
  }
  
  $preview_controller = new Preview_To_REST_API_Controller();
  $preview_controller-&gt;hook_rest_server();
  </code></pre>
  
  <p>I would be really glad to receive any hints for solving this problem.
  Cheers</p>