星期二 , 21 1 月 2025

下载文件统计相关

// Add meta box to posts
function add_download_file_meta_box() {
    add_meta_box(
        'download_file_meta',               // Meta box ID
        'Download File',                    // Meta box title
        'render_download_file_meta_box',    // Callback to render the content
        'post',                             // Post type
        'normal',                           // Context
        'high'                              // Priority
    );
}
add_action('add_meta_boxes', 'add_download_file_meta_box');

// Render the file input field inside the meta box
function render_download_file_meta_box($post) {
    // Retrieve current value of the custom field (if any)
    $download_file = get_post_meta($post->ID, '_download_file', true);

    // Nonce field for security
    wp_nonce_field('download_file_nonce', 'download_file_nonce_field');

    // Output file input field
    echo '<label for="download_file">Upload ZIP File:</label>';
    echo '<input type="file" id="download_file" name="download_file" accept=".zip" />';
    if ($download_file) {
        echo '<p><strong>Current File:</strong> <a href="' . esc_url($download_file) . '" target="_blank">Download</a></p>';
    }
}

// Save the uploaded ZIP file URL as meta data
function save_download_file_meta($post_id) {
    // Check if nonce is set and valid
    if (!isset($_POST['download_file_nonce_field']) || !wp_verify_nonce($_POST['download_file_nonce_field'], 'download_file_nonce')) {
        return;
    }

    // Check if the user has permission to save the data
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
    if (!current_user_can('edit_post', $post_id)) return;

    // Check if file was uploaded and handle the file
    if (isset($_FILES['download_file']) && !empty($_FILES['download_file']['tmp_name'])) {
        // Upload the file
        $uploaded_file = $_FILES['download_file'];
        $upload = wp_handle_upload($uploaded_file, ['test_form' => false]);


        if (isset($upload['url'])) {
            // Save the file URL as a meta value
            update_post_meta($post_id, '_download_file', $upload['url']);
        }
    }
}
add_action('save_post', 'save_download_file_meta');


// Add the custom column to the posts list
function add_download_file_column($columns) {
    $columns['download_file'] = 'Download File'; // Add new column
    return $columns;
}
add_filter('manage_posts_columns', 'add_download_file_column');

// Display value for the new column
function show_download_file_column_value($column_name, $post_id) {
    if ('download_file' === $column_name) {
        // Check if there is a file uploaded
        $download_file = get_post_meta($post_id, '_download_file', true);
        if ($download_file) {
            echo 'Yes'; // Display 'Yes' if the file is uploaded
        } else {
            echo 'No';  // Display 'No' if no file is uploaded
        }
    }
}
add_action('manage_posts_custom_column', 'show_download_file_column_value', 10, 2);


// Add some basic styles to the admin page
function download_file_column_styles() {
    echo '<style>
        .column-download_file { width: 100px; text-align: center; }
        .column-download_file:empty { color: #ccc; }
        .column-download_file.Yes { color: green; font-weight: bold; }
        .column-download_file.No { color: red; font-weight: bold; }
    </style>';
}
add_action('admin_head', 'download_file_column_styles');


// Append download link to post content if download_file field is not empty
function append_download_file_to_content($content) {
    // Get the current post ID
    $post_id = get_the_ID();

    // Get the value of the download_file custom field
    $download_file = get_post_meta($post_id, '_download_file', true);
    $download_count = get_post_meta( $post_id, 'dp_download_count', true ) ?: 0;
    // If the download_file custom field has a value (a file URL)
    if (!empty($download_file)) {
        // Add the download button or link at the end of the content
        $content .= '<div class="download-section">';
        $content .= '<a href="' . esc_url(home_url('?file_url=' .$download_file .'&post_id=' . $post_id)) . '" class="download-button" target="_blank">点击下载文件</a>';
        $content .= '</div>';
        $content .= '下载次数:' . $download_count;
    }

    return $content;
}

add_filter('the_content', 'append_download_file_to_content');

// Function to handle downloads and count
function handle_file_download() {
    if (!isset($_GET['file_url']) || !isset($_GET['post_id'])) {
        return;
    }

    $attachment_id = intval($_GET['file_id']);
    $post_id = intval($_GET['post_id']);
    //$file_url = wp_get_attachment_url($attachment_id);
    $file_url = esc_url($_GET['file_url'] );

    if (!$file_url) {
        wp_die('File not found');
    }
    
    // Increment download count
    $current_count = get_post_meta($post_id, 'dp_download_count') ?: 0;
    $new_count = (int)$current_count + 1;
    update_post_meta($post_id, 'dp_download_count', $new_count);

    // Redirect to actual file
    wp_redirect($file_url);
    exit;
}
add_action('init', 'handle_file_download');