diff --git a/api/newstop.php b/api/newstop.php new file mode 100644 index 0000000..8a0daed --- /dev/null +++ b/api/newstop.php @@ -0,0 +1,93 @@ + WP_REST_Server::READABLE, + 'callback' => 'my_api_news_top', + 'permission_callback' => '__return_true', // при необходимости замените на проверку доступа + 'args' => [ + 'per_page' => [ + 'required' => false, + 'default' => 20, + 'sanitize_callback' => 'absint', + ], + ], + ]); + +}); + +function my_api_news_top(WP_REST_Request $request) { + + $per_page = (int) $request->get_param('per_page'); + if ($per_page <= 0) { + $per_page = 20; + } + if ($per_page > 100) { + $per_page = 100; // защита от слишком тяжелых запросов + } + + // 1) news_query + $news_query = new WP_Query([ + 'post_type' => ['anew', 'yellow'], + 'ignore_sticky_posts' => true, + 'posts_per_page' => $per_page, + 'order' => 'DESC', + 'orderby' => 'post_date', + ]); + + // 2) top_query (ids берём из опции ppp_options) + $top_raw = get_option('ppp_options'); + $top = json_decode($top_raw); + + $ids = []; + if (is_array($top)) { + $ids = array_values(array_filter(array_map(static function ($item) { + return isset($item->id) ? absint($item->id) : 0; + }, $top))); + } + + $top_query = null; + $top_items = []; + + if (!empty($ids)) { + $top_query = new WP_Query([ + 'post_type' => ['anew', 'yellow'], + 'ignore_sticky_posts' => true, + 'post__in' => $ids, + 'posts_per_page' => -1, + 'order' => 'DESC', + 'orderby' => 'post_date', + ]); + + $top_items = array_map('my_api_map_post', $top_query->posts); + } + + $news_items = array_map('my_api_map_post', $news_query->posts); + + return rest_ensure_response([ + 'news' => [ + 'found' => (int) $news_query->found_posts, + 'items' => $news_items, + ], + 'top' => [ + 'ids' => $ids, + 'found' => $top_query ? (int) $top_query->found_posts : 0, + 'items' => $top_items, + ], + ]); +} + +/** + * Приводим WP_Post к простому массиву. + */ +function my_api_map_post($post) { + return [ + 'id' => (int) $post->ID, + 'type' => $post->post_type, + 'title' => get_the_title($post), + 'date' => get_the_date('c', $post), + 'uri' => wp_parse_url(get_permalink($post), PHP_URL_PATH), + 'link' => get_permalink($post), + ]; +} diff --git a/functions.php b/functions.php index 78665b0..9903539 100644 --- a/functions.php +++ b/functions.php @@ -13,11 +13,9 @@ require 'inc/article-rss-feed.php'; //rss ленты статей по дата require "inc/meta_keywords.php"; require "inc/journal_issue.php"; //номера журналов require "inc/rank-description.php"; //подменяем дескрипшт от соцсетей - require "inc/graphql.php"; //graphql - require "inc/replace_old_copyright.php"; //подменяем фото для старых картинок с "плохим" копирайтом //scheduler @@ -31,15 +29,17 @@ if ( defined( 'WP_CLI' ) && WP_CLI ) { include "inc/get_cached_alm.php"; - if (is_admin()) { require "inc/add_adv_checked.php"; // рекламная статья require "inc/admin/auto_check.php"; // помечакм не отправлять в Дзен require "inc/admin/lock-terms.php"; // banned и keys - require "inc/admin/coauthors_filter.php"; //поиск по соавторам - + require "inc/admin/coauthors_filter.php"; // фильтры авторов } +#api +require "api/newstop.php"; // управление REST API + + //TODO: Убрать все эти is_yn, is_zen и прочее и внутри плагинов регулировать вывод @@ -300,8 +300,9 @@ function add_article_in_main_query( $query ) { //var_dump( get_option('layf_custom_url')); if (is_admin()){ if (get_query_var('author_name')){ - $query->set('post_type', array('profile_article','anew','yellow')); + //$query->set('post_type', array('profile_article','anew','yellow')); //echo ''; + return $query; }else if (filter_input(INPUT_GET,'page') == 'wps_authors_page'){ $query->set('post_type', array('profile_article','anew','yellow')); $query->set('posts_per_page', -1); diff --git a/inc/admin/coauthors_filter.php b/inc/admin/coauthors_filter.php index ce87be2..3458868 100644 --- a/inc/admin/coauthors_filter.php +++ b/inc/admin/coauthors_filter.php @@ -1,5 +1,4 @@ '; + // Добавляем скрытое поле для сохранения post_type + echo ''; + echo '