/**
 * Get WordPress archive
 *
 * @param array $data Options for the function.
 * @return string|null Post title for the latest,
 * or null if none.
 *   year => months => post names
 */
function ngwp_get_archive($data)
{
    global $wpdb;
    $archive = array();
    $args = array();
    $args['numberposts'] = -1;
    if (isset($data['post_type'])) {
        $args['post_type'] = $data['post_type'];
    } else {
        $args['post_type'] = 'post';
    }
    // Get all published posts (for use later)
    $posts = get_posts($args);
    // Get years that have posts
    $years_query = "SELECT YEAR(post_date) AS year FROM " . $wpdb->prefix . "posts WHERE post_type = '" . $args['post_type'] . "' AND post_status = 'publish' GROUP BY year DESC";
    $years = $wpdb->get_results($years_query);
    // For each year, get months with posts
    foreach ($years as $year) {
        $months_query = "SELECT MONTH(post_date) AS month FROM " . $wpdb->prefix . "posts WHERE post_type = '" . $args['post_type'] . "' AND post_status = 'publish' GROUP BY month DESC";
        $months = $wpdb->get_results($months_query);
        // For each month with posts, get post titles and permalink
        foreach ($months as $key => $month) {
            $month->posts = array();
            $dateObj = DateTime::createFromFormat('!m', $month->month);
            $month->name = $dateObj->format('F');
            foreach ($posts as $post) {
                $post_timestamp = strtotime($post->post_date);
                $post_month = date('n', $post_timestamp);
                $post_year = date('Y', $post_timestamp);
                if ($post_month === $month->month && $post_year == $year->year) {
                    $post_data = array();
                    $post_data['post_title'] = $post->post_title;
                    $post_args = array();
                    $post_args['id'] = $post->ID;
                    $post_args['type'] = $post->post_type;
                    $post_args['slug'] = $post->post_name;
                    $post_data['link'] = ngwp_get_url($post_args);
                    $post_data['date_published'] = ngwp_get_published_date($post_args);
                    $month->posts[] = $post_data;
                }
            }
            if (count($month->posts) === 0) {
                unset($months[$key]);
            }
        }
        $year->months = $months;
    }
    $archive = $years;
    return $archive;
}
/**
 * Get the value of the "starship" field
 *
 * @param array $object Details of current post.
 * @param string $field_name Name of field.
 * @param WP_REST_Request $request Current request
 *
 * @return mixed
 */
function ngwp_get_custom_fields($object, $field_name, $request)
{
    $custom_fields = array();
    $custom_fields['url'] = ngwp_get_url($object);
    $custom_fields['excerpt'] = ngwp_get_excerpt($object, $field_name, $request);
    $custom_fields['meta_title'] = ngwp_get_meta_title($object, $field_name, $request);
    $custom_fields['meta_desc'] = ngwp_get_meta_desc($object, $field_name, $request);
    $custom_fields['cannonical_path'] = ngwp_get_cannonical_path($object, $field_name, $request);
    $custom_fields['robots'] = ngwp_get_robots($object, $field_name, $request);
    $custom_fields['social_image'] = ngwp_get_social_image($object, $field_name, $request);
    $custom_fields['author'] = ngwp_get_author($object, $field_name, $request);
    $custom_fields['date_published'] = ngwp_get_published_date($object);
    $custom_fields['date_modified'] = ngwp_get_modified_date($object);
    $custom_fields['featured_image'] = ngwp_get_featured_image($object);
    $previous = false;
    $custom_fields['next_post'] = ngwp_get_adjacent_post($object['id'], $previous);
    $previous = true;
    $custom_fields['previous_post'] = ngwp_get_adjacent_post($object['id'], $previous);
    return $custom_fields;
}