File: /home2/tucom/minecraftmienphi.com/wp-content/plugins/apk-info-box/apk-info-box.php
<?php
/*
Plugin Name: APK Info Box
Description: Hiển thị box thông tin APK cho Page và Post (Hỗ trợ Shortcode cho Flatsome UX Builder)
Version: 1.3
Author: Custom
*/
if(!defined('ABSPATH')) exit;
/* =========================
META BOX FORM
========================= */
add_action('add_meta_boxes','apk_add_meta_box');
function apk_add_meta_box(){
// Mảng chứa các loại bài muốn hiển thị Meta Box (hỗ trợ cả page và post)
$screens = ['page', 'post'];
foreach ($screens as $screen) {
add_meta_box(
'apk_info_box',
'🗂️ APK Information',
'apk_meta_box_html',
$screen,
'normal',
'high'
);
}
}
function apk_meta_box_html($post){
wp_nonce_field('apk_meta_save','apk_meta_nonce');
// Thêm style cho form trong Admin
echo '<style>
.apk-meta-table { width: 100%; border-collapse: collapse; }
.apk-meta-table th { width: 220px; text-align: left; padding: 15px 10px; border-bottom: 1px solid #eee; font-weight: 600; vertical-align: top; }
.apk-meta-table td { padding: 15px 10px; border-bottom: 1px solid #eee; vertical-align: middle; }
.apk-meta-table tr:last-child th, .apk-meta-table tr:last-child td { border-bottom: none; }
.apk-meta-input { width: 100%; max-width: 100%; }
.apk-meta-desc { font-size: 12px; color: #666; margin-top: 4px; display: block; font-style: italic; }
</style>';
// Định nghĩa loại input, placeholder và mô tả thêm (nếu có)
$fields = [
'apk_version' => ['label' => 'Phiên bản', 'type' => 'text', 'placeholder' => 'VD: 1.0.5'],
'apk_category' => ['label' => 'Chủ đề chính', 'type' => 'text', 'placeholder' => 'VD: Hành động, Giải đố...'],
'apk_developer' => ['label' => 'Nhà phát triển', 'type' => 'text', 'placeholder' => 'VD: Publisher Name'],
'apk_update' => ['label' => 'Cập nhật mới', 'type' => 'text', 'placeholder' => 'VD: 07/03/2026'],
'apk_fix' => ['label' => 'Khắc phục lỗi & Cải tiến', 'type' => 'textarea', 'placeholder' => 'VD: Sửa lỗi văng game, tối ưu FPS...'],
'apk_compat' => ['label' => 'Tương thích', 'type' => 'text', 'placeholder' => 'VD: Android 5.0 trở lên'],
'apk_download' => ['label' => 'Link tải APK', 'type' => 'url', 'placeholder' => 'https://...', 'desc' => '* Bắt buộc nhập để hiển thị Box trên Page/Post'],
'apk_versions' => ['label' => 'Link xem phiên bản khác', 'type' => 'url', 'placeholder' => 'https://...', 'desc' => 'Để trống sẽ tự động ẩn nút Xem thêm']
];
echo '<table class="apk-meta-table">';
foreach($fields as $key => $data){
$value = get_post_meta($post->ID, $key, true);
echo '<tr>';
echo '<th><label for="' . esc_attr($key) . '">' . esc_html($data['label']) . '</label></th>';
echo '<td>';
// Render Textarea hoặc Text/URL form
if ($data['type'] === 'textarea') {
echo '<textarea id="' . esc_attr($key) . '" name="' . esc_attr($key) . '" class="large-text" rows="3" placeholder="' . esc_attr($data['placeholder']) . '">' . esc_textarea($value) . '</textarea>';
} else {
echo '<input type="' . esc_attr($data['type']) . '" id="' . esc_attr($key) . '" name="' . esc_attr($key) . '" value="' . esc_attr($value) . '" class="regular-text apk-meta-input" placeholder="' . esc_attr($data['placeholder']) . '">';
}
// Hiển thị mô tả field nếu có
if (isset($data['desc'])) {
echo '<span class="apk-meta-desc">' . esc_html($data['desc']) . '</span>';
}
echo '</td>';
echo '</tr>';
}
echo '</table>';
}
/* =========================
SAVE DATA
========================= */
add_action('save_post','apk_save_meta_data');
function apk_save_meta_data($post_id){
if(!isset($_POST['apk_meta_nonce'])) return;
if(!wp_verify_nonce($_POST['apk_meta_nonce'],'apk_meta_save')) return;
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
// Kiểm tra post_type hợp lệ
$post_type = get_post_type($post_id);
if (!in_array($post_type, ['page', 'post'])) return;
// Kiểm tra quyền lưu meta box
if('page' == $post_type) {
if(!current_user_can('edit_page', $post_id)) return;
} else {
if(!current_user_can('edit_post', $post_id)) return;
}
$fields = [
'apk_version' => 'text',
'apk_category' => 'text',
'apk_developer' => 'text',
'apk_update' => 'text',
'apk_fix' => 'textarea',
'apk_compat' => 'text',
'apk_download' => 'url',
'apk_versions' => 'url'
];
foreach($fields as $field => $type){
if(isset($_POST[$field])){
$value = $_POST[$field];
// Validate & Sanitize đúng theo loại dữ liệu
if($type === 'url') {
$sanitized_value = esc_url_raw($value);
} elseif($type === 'textarea') {
$sanitized_value = sanitize_textarea_field($value);
} else {
$sanitized_value = sanitize_text_field($value);
}
update_post_meta($post_id, $field, $sanitized_value);
}
}
}
/* =========================
APK BOX DISPLAY (HTML RENDER)
========================= */
function apk_box_display(){
global $post;
if(!$post) return '';
$download = get_post_meta($post->ID,'apk_download',true);
// Nếu không có link download thì ẩn toàn bộ box
if(!$download) return '';
$version = get_post_meta($post->ID,'apk_version',true);
$category = get_post_meta($post->ID,'apk_category',true);
$developer = get_post_meta($post->ID,'apk_developer',true);
$update = get_post_meta($post->ID,'apk_update',true);
$fix = get_post_meta($post->ID,'apk_fix',true);
$compat = get_post_meta($post->ID,'apk_compat',true);
$versions = get_post_meta($post->ID,'apk_versions',true);
ob_start();
?>
<div class="apk-box-wrapper">
<div class="apk-box">
<div class="apk-head">
<div class="apk-thumb">
<?php if(has_post_thumbnail()) { the_post_thumbnail('medium'); } else { echo '<div class="apk-no-thumb">APK</div>'; } ?>
</div>
<div class="apk-title">
<h4><?php echo esc_html(get_the_title()); ?> <?php if($version) echo '<span class="apk-version-badge">' . esc_html($version) . '</span>'; ?></h4>
<div class="apk-tags">
<span>APK</span>
<span>Miễn Phí</span>
<span>Tiếng Việt</span>
</div>
</div>
</div>
<div class="apk-body">
<h3 class="apk-heading">Thông tin Ứng dụng</h3>
<ul class="apk-list">
<?php if($version) { ?><li><strong>Phiên bản</strong> <span><?php echo esc_html($version); ?></span></li><?php } ?>
<?php if($category) { ?><li><strong>Chủ đề</strong> <span><?php echo esc_html($category); ?></span></li><?php } ?>
<?php if($developer) { ?><li><strong>Nhà phát triển</strong> <span><?php echo esc_html($developer); ?></span></li><?php } ?>
<?php if($update) { ?><li><strong>Cập nhật mới</strong> <span><?php echo esc_html($update); ?></span></li><?php } ?>
<?php if($fix) { ?><li><strong>Khắc phục lỗi</strong> <span><?php echo nl2br(esc_html($fix)); ?></span></li><?php } ?>
<?php if($compat) { ?><li><strong>Tương thích</strong> <span><?php echo esc_html($compat); ?></span></li><?php } ?>
</ul>
<div class="apk-actions">
<a class="apk-button is-primary" href="<?php echo esc_url($download); ?>" target="_blank" rel="noopener noreferrer">
⬇ Tải về APK ngay
</a>
<?php if($versions){ ?>
<a class="apk-button is-outline" href="<?php echo esc_url($versions); ?>" target="_blank" rel="noopener noreferrer">
Xem thêm phiên bản khác
</a>
<?php } ?>
</div>
</div>
</div>
</div>
<?php
return ob_get_clean();
}
/* =========================
SHORTCODE DÀNH CHO UX BUILDER
========================= */
// Sử dụng shortcode [apk_info_box] để chèn tùy ý vào Page/Post bằng UX Builder
add_shortcode('apk_info_box', 'apk_box_display');
/* =========================
AUTO DISPLAY (TRƯỜNG HỢP KHÔNG DÙNG SHORTCODE)
========================= */
add_filter('the_content','apk_auto_display');
function apk_auto_display($content){
if(is_singular(['page', 'post']) && !is_front_page() && !is_home() && is_main_query()){
if ( !has_shortcode($content, 'apk_info_box') ) {
$content = apk_box_display() . $content;
}
}
return $content;
}
/* =========================
STYLE
========================= */
add_action('wp_head','apk_box_style');
function apk_box_style(){
?>
<style>
/* Wrapper để giới hạn 700px và căn giữa */
.apk-box-wrapper {
width: 100%;
max-width: 700px;
margin: 0 auto 30px auto;
clear: both;
}
/* Giao diện boxed phong cách Flatsome */
.apk-box {
background: #fff;
padding: 20px 10px;
font-family: inherit;
position: relative;
overflow: hidden;
}
/* Hiệu ứng hover nhè nhẹ */
.apk-box:hover {
box-shadow: 0 3px 12px rgba(0,0,0,0.08);
border-color: #e0e0e0;
transition: all 0.3s ease;
}
.apk-head {
display: flex;
gap: 20px;
align-items: center;
padding-bottom: 20px;
margin-bottom: 20px;
border-bottom: 1px solid #ececec;
}
.apk-thumb img {
width: 90px;
height: 90px;
object-fit: cover;
border-radius: 12px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.apk-no-thumb {
width: 90px;
height: 90px;
border-radius: 12px;
background: #f7f7f7;
display: flex;
align-items: center;
justify-content: center;
font-weight: 700;
color: #bbb;
font-size: 20px;
border: 1px solid #eee;
}
.apk-title h4 {
margin: 0 0 8px 0 !important;
font-size: 1.25em !important;
font-weight: 700;
line-height: 1.3;
color: #333;
text-transform: none; /* Tránh Flatsome tự in hoa */
}
.apk-version-badge {
font-size: 13px;
background: #f1f1f1;
color: #666;
padding: 2px 8px;
border-radius: 20px;
vertical-align: middle;
font-weight: normal;
margin-left: 5px;
}
.apk-tags span {
background: rgba(0,0,0,0.04);
border: 1px solid rgba(0,0,0,0.05);
color: #666;
padding: 3px 10px;
border-radius: 3px;
margin-right: 6px;
font-size: 12px;
font-weight: 600;
display: inline-block;
margin-bottom: 3px;
text-transform: uppercase;
}
/* Tiêu đề mục thông tin */
.apk-heading {
margin: 0 0 15px 0 !important;
font-size: 16px !important;
font-weight: 700;
text-transform: uppercase;
border-left: 3px solid #000; /* Nét bên trái đặc trưng của list widget ở Flatsome */
padding-left: 10px;
color: #333;
line-height: 1.2;
}
/* Chuyển sang ul list để Flatsome responsive tốt hơn */
.apk-list {
list-style: none !important;
margin: 0 0 20px 0 !important;
padding: 0 !important;
}
.apk-list li {
display: flex;
justify-content: space-between;
padding: 10px 0;
border-bottom: 1px dashed #ececec;
font-size: 14.5px;
color: #555;
margin: 0 !important;
}
.apk-list li:last-child {
border-bottom: none;
}
.apk-list li strong {
font-weight: 600;
color: #333;
width: 40%;
max-width: 200px;
flex-shrink: 0;
}
.apk-list li span {
text-align: right;
flex-grow: 1;
color: #111;
font-weight: 500;
}
/* Nút bấm */
.apk-actions {
display: flex;
flex-direction: column;
gap: 10px;
}
.apk-button {
display: block;
padding: 12px 20px;
text-align: center;
border-radius: 3px; /* Border radius nhẹ */
font-weight: 700;
font-size: 14px;
text-transform: uppercase;
text-decoration: none !important;
letter-spacing: 0.03em;
transition: all 0.3s;
cursor: pointer;
}
.apk-button.is-primary {
background-color: #000;
background-color: var(--primary-color, #000); /* Kế thừa biến màu Flatsome */
color: #fff !important;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.apk-button.is-primary:hover {
background-color: #333;
background-color: var(--primary-color-dark, #333);
box-shadow: 0 4px 10px rgba(0,0,0,0.2);
opacity: 0.9;
}
.apk-button.is-outline {
background-color: transparent;
color: #666 !important;
border: 2px solid #ddd;
padding: 10px 20px;
}
.apk-button.is-outline:hover {
border-color: #aaa;
color: #333 !important;
background-color: #f9f9f9;
}
/* Mobile */
@media (max-width: 500px) {
.apk-box {
padding: 15px;
}
.apk-head {
flex-direction: row;
gap: 15px;
}
.apk-thumb img, .apk-no-thumb {
width: 70px;
height: 70px;
}
.apk-title h4 {
font-size: 16px !important;
}
.apk-tags span {
font-size: 10px;
padding: 2px 6px;
}
.apk-list li {
flex-direction: column;
gap: 4px;
padding: 12px 0;
}
.apk-list li strong {
width: 100%;
max-width: none;
}
.apk-list li span {
text-align: left;
}
.apk-button {
padding: 10px 15px;
}
}
</style>
<?php
}