<?php
/**
 * WP REST API Content routes
 * https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
 *
 * Version:     0.0.0
 *
 * Author:      Simon Cereska
 *
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

if ( ! class_exists( 'Content_Endpoint' ) ) :

    /**
     * WP REST Content class.
     *
     * WP API Content support for WP API v2.
     */
    class Content_Endpoint {
        function __construct() {
        }

        /**
         * Grab posts for home page content
         */
        function getHomePage( $data ) {
            $posts = get_posts( array(
                // 'author' => $data['id'],
                'lang' => 'en'
            ) );

            echo '<h1>This is Content endpoint</h1>';

            echo $posts;

            if ( empty( $posts ) ) {
                return null;
            }

            return $posts;
            // return $posts[0]->post_title;
        }

        function init() {
            add_action( 'rest_api_init', function () {
                register_rest_route( 'biuro/v1', '/content-home/(?P<id>\d+)', array(
                    'methods' => 'GET',
                    'callback' => 'getHomePage',
                ) );
            } );
        }

    }

endif;




// add_action( 'rest_api_init', function () {
//     register_rest_route( 'biuro/v1', '/content/(?P<id>\d+)', array(
//         'methods' => 'GET',
//         'callback' => 'getHomeContent',
//     ) );
// } );







// // Add Advanced Custom Fields Fields to the WP REST API
// // https://gist.github.com/rileypaulsen/9b4505cdd0ac88d5ef51

// function wp_api_encode_acf($data,$post,$context){
//     $data['meta'] = array_merge($data['meta'],get_fields($post['ID']));
//     return $data;
// }

// if( function_exists('get_fields') ){
//     add_filter('json_prepare_post', 'wp_api_encode_acf', 10, 3);
// }




// // Adding Custom Endpoints
// // https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/

// /**
//  * Grab latest post title by an author!
//  *
//  * @param array $data Options for the function.
//  * @return string|null Post title for the latest,  * or null if none.
//  */
// function getHomePosts( $data ) {
//     $posts = get_posts( array(
//         'author' => $data['id'],
//         'lang' => 'en'
//     ) );

//     echo $posts;

//     if ( empty( $posts ) ) {
//         return null;
//     }

//     return $posts[0]->post_title;
// }


// add_action( 'rest_api_init', function () {
//   register_rest_route( 'myplugin/v1', '/author/(?P<id>\d+)', array(
//     'methods' => 'GET',
//     'callback' => 'getHomePosts',
//   ) );
// } );


// ?>
