/** * Endpoint: /app/home/ * Свежие новости, анонсы мероприятий и статьи для главного экрана мобильного * приложения — те же блоки, что на главной https://rgbs.ru * (post_type news / adv / information). */ function rgbs_app_home_items($post_type, $limit = 6) { $q = new WP_Query(array( 'post_type' => $post_type, 'posts_per_page' => $limit, 'orderby' => 'date', 'order' => 'DESC', 'post_status' => 'publish', )); $items = array(); while ($q->have_posts()) { $q->the_post(); $img = ''; if (function_exists('get_field')) { $pv = get_field('new_preview'); if (is_string($pv) && $pv !== '') $img = $pv; } if ($img === '' && has_post_thumbnail()) { $img = (string) get_the_post_thumbnail_url(get_the_ID(), 'medium'); } if ($img === '' && function_exists('catch_that_image')) { $ci = catch_that_image(); if (is_string($ci) && strpos($ci, '/path/to/default') === false) $img = $ci; } $items[] = array( 'id' => get_the_ID(), 'title' => html_entity_decode(wp_strip_all_tags(get_the_title()), ENT_QUOTES, 'UTF-8'), 'url' => get_permalink(), 'image' => $img, 'date' => get_the_date('c'), ); } wp_reset_postdata(); return $items; } add_action('init', 'rgbs_app_home_endpoint'); function rgbs_app_home_endpoint() { $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); if (!preg_match('#^/app/home/?$#', (string)$path)) return; nocache_headers(); header('Content-Type: application/json; charset=utf-8'); header('Access-Control-Allow-Origin: *'); echo wp_json_encode(array( 'news' => rgbs_app_home_items('news', 6), 'adv' => rgbs_app_home_items('adv', 6), 'information' => rgbs_app_home_items('information', 6), ), JSON_UNESCAPED_UNICODE); exit; }