/* ===== HIỂN THỊ AUDIO ===== */ add_filter('the_content', function ($content) { if (!is_single()) return $content; global $wpdb; $radio = $wpdb->get_row($wpdb->prepare( "SELECT * FROM " . EASY_RADIO_TABLE . " WHERE post_id=%d AND active=1", get_the_ID() )); if ($radio) { return $content.'
'; } return $content; }); /* ===== API /radio ===== */ add_action('init', function () { add_rewrite_rule('^easy-radio-api/?$', 'index.php?easy_radio_api=1', 'top'); }); add_filter('query_vars', fn($v)=>array_merge($v,['easy_radio_api'])); function easy_radio_get_cached_json() { $cache = get_transient('easy_radio_json_cache'); if ($cache !== false) return $cache; $data = easy_radio_build_json(); set_transient('easy_radio_json_cache', $data, 15 * MINUTE_IN_SECONDS); return $data; } add_action('template_redirect', function () { if (get_query_var('easy_radio_api') == 1) { if (!defined('EASY_RADIO_API_TOKEN') || !isset($_GET['token']) || $_GET['token'] !== EASY_RADIO_API_TOKEN) { status_header(403); echo json_encode(['error'=>'Invalid token']); exit; } header('Content-Type: application/json; charset=utf-8'); echo json_encode(easy_radio_get_cached_json(), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); exit; } }); add_action('save_post', fn()=>delete_transient('easy_radio_json_cache')); /* ===== BUILD JSON ===== */ function easy_radio_build_json() { global $wpdb; $out = []; $cats = get_categories([ 'hide_empty' => false ]); foreach ($cats as $cat) { $node = [ "id" => (string)$cat->term_id, "word" => $cat->name, "type" => "list", "phonetic" => "", "definitions" => ["en"=>"","ja"=>"","vi"=>""], "example" => "", "media" => ["image"=>"","audio"=>""], "children" => [] ]; // Lấy post + radio trong 1 query $rows = $wpdb->get_results($wpdb->prepare(" SELECT p.ID, p.post_title, r.audio_url, r.text_url, r.waveform_url FROM {$wpdb->posts} p INNER JOIN ".EASY_RADIO_TABLE." r ON p.ID = r.post_id INNER JOIN {$wpdb->term_relationships} tr ON p.ID = tr.object_id INNER JOIN {$wpdb->term_taxonomy} tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.term_id = %d AND p.post_status = 'publish' AND r.active = 1 ORDER BY r.created_at DESC LIMIT 10 ", $cat->term_id)); foreach ($rows as $row) { $node['children'][] = [ "id" => (string)$row->ID, "word" => $row->post_title, "type" => "word", "phonetic" => "", "definitions" => ["en"=>"","ja"=>"","vi"=>""], "example" => "", "media" => [ "image" => get_the_post_thumbnail_url($row->ID, 'full') ?: "", "audio" => $row->audio_url ?: "", "text" => $row->text_url ?: "", "waveform" => $row->waveform_url ?: "" ], "children" => [] ]; } // Chỉ push category nếu có post hợp lệ if (!empty($node['children'])) { $out[] = $node; } } return $out; } /* ===== CỘT RADIO TRONG DANH SÁCH BÀI VIẾT ===== */ add_filter('manage_posts_columns', function ($cols) { $cols['easy_radio'] = 'Radio'; return $cols; }); add_action('manage_posts_custom_column', function ($col, $post_id) { if ($col === 'easy_radio') { $url = admin_url('admin.php?page=easy-radio&post_id=' . $post_id); echo 'Link'; } }, 10, 2); やさしい日本語ニュース | Trang 6 trên 40 | EASY NEWS JAPAN - Tin tức Nhật Bản dễ hiểu - Có furigana & giải thích nghĩa tiếng Việt