Kod:Tümünü seç Kopyala
<?php
/*
Plugin Name: Custom Post Manager
Description: WordPress yönetici panelinde yazıları listeleme, düzenleme, çöp, kalıcı silme, taslak ve yayınlama işlemlerini durum bazında yönetebileceğiniz eklenti. (Virgülle efektif arama, gösterim sayısı ve sıralama özellikli.)
Version: 1.5
Author: [İsminiz]
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Yönetici menüsüne yeni öğe ekleme
function custom_post_admin_menu() {
add_menu_page(
'Yazılar Yönetimi',
'Yazılar Yönetimi',
'manage_options',
'custom-post-admin',
'custom_post_admin_page',
'dashicons-admin-post',
6
);
}
add_action('admin_menu', 'custom_post_admin_menu');
// Ana yönetici sayfa içeriği
function custom_post_admin_page() {
?>
<div class="wrap">
<h1>Yazılar Yönetimi</h1>
<form method="post" id="bulk-action-form">
<?php wp_nonce_field( 'custom_post_bulk_action', 'custom_post_nonce' ); ?>
<label for="posts_per_page">Sayfa başına yazı sayısı:</label>
<select name="posts_per_page" id="posts_per_page">
<option value="5" <?php selected( isset($_POST['posts_per_page']) ? intval($_POST['posts_per_page']) : 10, 5 ); ?>>5</option>
<option value="10" <?php selected( isset($_POST['posts_per_page']) ? intval($_POST['posts_per_page']) : 10, 10 ); ?>>10</option>
<option value="20" <?php selected( isset($_POST['posts_per_page']) ? intval($_POST['posts_per_page']) : 10, 20 ); ?>>20</option>
<option value="50" <?php selected( isset($_POST['posts_per_page']) ? intval($_POST['posts_per_page']) : 10, 50 ); ?>>50</option>
</select>
<br><br>
<label for="post_status_filter">Yazı Durumu:</label>
<select name="post_status_filter" id="post_status_filter">
<option value="all" <?php selected( isset($_POST['post_status_filter']) ? sanitize_text_field($_POST['post_status_filter']) : 'all', 'all' ); ?>>Tüm Yazılar</option>
<option value="publish" <?php selected( isset($_POST['post_status_filter']) ? sanitize_text_field($_POST['post_status_filter']) : 'all', 'publish' ); ?>>Aktif</option>
<option value="draft" <?php selected( isset($_POST['post_status_filter']) ? sanitize_text_field($_POST['post_status_filter']) : 'all', 'draft' ); ?>>Taslak</option>
<option value="trash" <?php selected( isset($_POST['post_status_filter']) ? sanitize_text_field($_POST['post_status_filter']) : 'all', 'trash' ); ?>>Çöp</option>
</select>
<br><br>
<label for="s">Ara (virgülle ayrılmış terimler, Tüm terimler aranır):</label>
<input type="text" name="s" id="s" value="<?php echo isset($_POST['s']) ? esc_attr($_POST['s']) : ''; ?>">
<input type="submit" value="Filtrele" class="button">
<?php custom_post_list_table(); ?>
<div class="bulk-actions">
<select name="bulk_action" id="bulk_action">
<option value=""><?php _e('Toplu İşlem Seçin'); ?></option>
<option value="delete"><?php _e('Toplu Sil'); ?></option>
<option value="publish"><?php _e('Taslağı Yayınla'); ?></option>
</select>
<input type="submit" value="Uygula" class="button" onclick="return confirm('Seçilen yazılar üzerinde işlemi uygulamak istediğinize emin misiniz?')">
</div>
</form>
</div>
<?php
}
// Yazıların listelendiği tablo (arama, gösterim sayısı ve sıralama özellikli)
function custom_post_list_table() {
global $wpdb;
// Sayfalama ayarları
$posts_per_page = isset($_POST['posts_per_page']) ? intval($_POST['posts_per_page']) : 10;
$paged = isset($_GET['paged']) ? intval($_GET['paged']) : 1;
$offset = ($paged - 1) * $posts_per_page;
// Filtre ve arama değerleri
$post_status = isset($_POST['post_status_filter']) ? sanitize_text_field($_POST['post_status_filter']) : 'all';
$search = isset($_POST['s']) ? sanitize_text_field($_POST['s']) : '';
// SQL sorgusunun temel parçaları
$params = array();
$sql = "FROM {$wpdb->posts} p
LEFT JOIN {$wpdb->postmeta} pm ON (p.ID = pm.post_id AND pm.meta_key = 'post_views_count')
WHERE p.post_type = 'post'";
if ( $post_status !== 'all' ) {
$sql .= " AND p.post_status = %s";
$params[] = $post_status;
}
if ( !empty($search) ) {
// Virgülle ayrılmış arama terimleri (her terim için; başlık ya da içerikte aranacak)
$terms = array_map('trim', explode(',', $search));
$like_conditions = array();
foreach ($terms as $term) {
$like_conditions[] = "(p.post_title LIKE %s OR p.post_content LIKE %s)";
$params[] = '%' . $wpdb->esc_like($term) . '%';
$params[] = '%' . $wpdb->esc_like($term) . '%';
}
// Her terim için koşulların hepsinin sağlanması gerektiğinden AND ile birleştiriyoruz.
$sql .= " AND (" . implode(" AND ", $like_conditions) . ")";
}
// Toplam yazı sayısı (DISTINCT p.ID kullanıyoruz)
$total_posts = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(DISTINCT p.ID) " . $sql, ...$params) );
// Sıralama: URL'de 'orderby' parametresi kontrol ediliyor
$orderby = isset($_GET['orderby']) ? sanitize_text_field($_GET['orderby']) : '';
$order = (isset($_GET['order']) && strtoupper($_GET['order']) == 'ASC') ? 'ASC' : 'DESC';
if ( $orderby == 'view_count' ) {
// Meta değeri string olduğu için sayısal karşılaştırma için CAST kullanıyoruz
$order_by = " ORDER BY CAST(pm.meta_value AS UNSIGNED) $order";
} else {
$order_by = " ORDER BY p.post_date DESC";
}
// Sorguya LIMIT ve OFFSET ekleyerek yazıları çekiyoruz
$params_with_limit = array_merge( $params, array( $posts_per_page, $offset ) );
$query = $wpdb->prepare("SELECT p.*, pm.meta_value as view_count " . $sql . $order_by . " LIMIT %d OFFSET %d", ...$params_with_limit);
$posts = $wpdb->get_results( $query );
// "Gösterim" sütunu başlığı için sıralama bağlantısı
$current_order = $order;
$new_order = ($orderby == 'view_count' && $current_order == 'ASC') ? 'DESC' : 'ASC';
$view_count_link = add_query_arg( array('orderby' => 'view_count', 'order' => $new_order) );
echo '<table class="wp-list-table widefat fixed striped">';
echo '<thead><tr>';
echo '<th><input type="checkbox" id="select-all"></th>';
echo '<th>Başlık</th>';
echo '<th>Durum</th>';
echo '<th>Tarih</th>';
echo '<th><a href="' . esc_url($view_count_link) . '">Gösterim</a></th>';
echo '<th>İşlemler</th>';
echo '</tr></thead>';
echo '<tbody>';
if ( $posts ) {
foreach ( $posts as $post ) {
echo '<tr>';
echo '<td><input type="checkbox" name="post_ids[]" value="' . esc_attr( $post->ID ) . '"></td>';
echo '<td>' . esc_html( $post->post_title ) . '</td>';
echo '<td>' . esc_html( $post->post_status ) . '</td>';
echo '<td>' . esc_html( $post->post_date ) . '</td>';
$view_count = (!empty($post->view_count)) ? intval($post->view_count) : 0;
echo '<td>' . esc_html( $view_count ) . '</td>';
// İşlemler sütunu: Yazının durumuna göre farklı aksiyonlar
echo '<td>';
if ( $post->post_status === 'draft' ) {
// Taslak durumda: Düzenle, Çöp, Sil, Yayınla
$edit_link = get_edit_post_link($post->ID);
echo '<a href="' . esc_url($edit_link) . '" target="_blank">Düzenle</a>';
$trash_nonce = wp_create_nonce( 'custom_post_trash_' . $post->ID );
$trash_link = admin_url( 'admin-post.php?action=custom_post_trash&post_id=' . $post->ID . '&nonce=' . $trash_nonce );
echo ' | <a href="' . esc_url($trash_link) . '">Çöp</a>';
$delete_nonce = wp_create_nonce( 'custom_post_delete_' . $post->ID );
$delete_link = admin_url( 'admin-post.php?action=custom_post_delete&post_id=' . $post->ID . '&nonce=' . $delete_nonce );
echo ' | <a href="' . esc_url($delete_link) . '" onclick="return confirm(\'Bu yazıyı tamamen silmek istediğinize emin misiniz?\')">Sil</a>';
$publish_nonce = wp_create_nonce( 'custom_post_publish_' . $post->ID );
$publish_link = admin_url( 'admin-post.php?action=custom_post_publish&post_id=' . $post->ID . '&nonce=' . $publish_nonce );
echo ' | <a href="' . esc_url($publish_link) . '">Yayınla</a>';
} elseif ( $post->post_status === 'trash' ) {
// Çöp durumunda: Taslak, Sil
$draft_nonce = wp_create_nonce( 'custom_post_draft_' . $post->ID );
$draft_link = admin_url( 'admin-post.php?action=custom_post_draft&post_id=' . $post->ID . '&nonce=' . $draft_nonce );
echo '<a href="' . esc_url($draft_link) . '">Taslak</a>';
$delete_nonce = wp_create_nonce( 'custom_post_delete_' . $post->ID );
$delete_link = admin_url( 'admin-post.php?action=custom_post_delete&post_id=' . $post->ID . '&nonce=' . $delete_nonce );
echo ' | <a href="' . esc_url($delete_link) . '" onclick="return confirm(\'Bu yazıyı tamamen silmek istediğinize emin misiniz?\')">Sil</a>';
} elseif ( $post->post_status === 'publish' ) {
// Yayınlanmış durumda: Düzenle, Ana Sayfa Görüntüsü, Çöp, Taslak
$edit_link = get_edit_post_link($post->ID);
echo '<a href="' . esc_url($edit_link) . '" target="_blank">Düzenle</a>';
$view_link = get_permalink($post->ID);
echo ' | <a href="' . esc_url($view_link) . '" target="_blank">Ana Sayfa Görüntüsü</a>';
$trash_nonce = wp_create_nonce( 'custom_post_trash_' . $post->ID );
$trash_link = admin_url( 'admin-post.php?action=custom_post_trash&post_id=' . $post->ID . '&nonce=' . $trash_nonce );
echo ' | <a href="' . esc_url($trash_link) . '">Çöp</a>';
$draft_nonce = wp_create_nonce( 'custom_post_draft_' . $post->ID );
$draft_link = admin_url( 'admin-post.php?action=custom_post_draft&post_id=' . $post->ID . '&nonce=' . $draft_nonce );
echo ' | <a href="' . esc_url($draft_link) . '">Taslak</a>';
}
echo '</td>';
echo '</tr>';
}
} else {
echo '<tr><td colspan="6">Gösterilecek yazı bulunamadı.</td></tr>';
}
echo '</tbody>';
echo '</table>';
// Sayfalama
$total_pages = ceil( $total_posts / $posts_per_page );
if ( $total_pages > 1 ) {
echo '<div class="tablenav"><div class="tablenav-pages">';
echo paginate_links( array(
'base' => add_query_arg('paged', '%#%'),
'format' => '',
'current' => $paged,
'total' => $total_pages,
) );
echo '</div></div>';
}
}
// Bulk işlemleri: Toplu silme ve toplu yayınlama (taslak yazıları yayınlama)
function custom_post_bulk_actions() {
if ( isset($_POST['bulk_action']) && !empty($_POST['bulk_action']) && !empty($_POST['post_ids']) ) {
if ( ! isset($_POST['custom_post_nonce']) || ! wp_verify_nonce($_POST['custom_post_nonce'], 'custom_post_bulk_action') ) {
wp_die('Güvenlik kontrolü başarısız.');
}
$action = sanitize_text_field( $_POST['bulk_action'] );
foreach ( $_POST['post_ids'] as $post_id ) {
$post_id = intval( $post_id );
if ( $action === 'delete' ) {
wp_delete_post( $post_id, true );
} elseif ( $action === 'publish' ) {
$post = get_post( $post_id );
if ( $post && $post->post_status === 'draft' ) {
wp_update_post( array(
'ID' => $post_id,
'post_status' => 'publish'
) );
}
}
}
wp_redirect( admin_url('admin.php?page=custom-post-admin') );
exit;
}
}
add_action('admin_init', 'custom_post_bulk_actions');
// Tekil "Çöp" işlemi (yazıyı çöpe taşıma)
function custom_post_trash_handler() {
if ( ! isset( $_GET['post_id'] ) || ! isset( $_GET['nonce'] ) ) {
wp_die('Eksik parametreler.');
}
$post_id = intval( $_GET['post_id'] );
$nonce = sanitize_text_field( $_GET['nonce'] );
if ( ! wp_verify_nonce( $nonce, 'custom_post_trash_' . $post_id ) ) {
wp_die('Güvenlik kontrolü başarısız.');
}
if ( ! current_user_can( 'delete_post', $post_id ) ) {
wp_die('Bu işlemi gerçekleştirmek için yeterli izniniz yok.');
}
wp_trash_post( $post_id );
wp_redirect( admin_url('admin.php?page=custom-post-admin') );
exit;
}
add_action( 'admin_post_custom_post_trash', 'custom_post_trash_handler' );
// Tekil "Taslak" işlemi (yazıyı taslak durumuna çekme)
function custom_post_draft_handler() {
if ( ! isset( $_GET['post_id'] ) || ! isset( $_GET['nonce'] ) ) {
wp_die('Eksik parametreler.');
}
$post_id = intval( $_GET['post_id'] );
$nonce = sanitize_text_field( $_GET['nonce'] );
if ( ! wp_verify_nonce( $nonce, 'custom_post_draft_' . $post_id ) ) {
wp_die('Güvenlik kontrolü başarısız.');
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
wp_die('Bu işlemi gerçekleştirmek için yeterli izniniz yok.');
}
wp_update_post( array(
'ID' => $post_id,
'post_status' => 'draft'
) );
wp_redirect( admin_url('admin.php?page=custom-post-admin') );
exit;
}
add_action( 'admin_post_custom_post_draft', 'custom_post_draft_handler' );
// Tekil "Yayınla" işlemi (taslak yazıyı yayına alma)
function custom_post_publish_handler() {
if ( ! isset( $_GET['post_id'] ) || ! isset( $_GET['nonce'] ) ) {
wp_die('Eksik parametreler.');
}
$post_id = intval( $_GET['post_id'] );
$nonce = sanitize_text_field( $_GET['nonce'] );
if ( ! wp_verify_nonce( $nonce, 'custom_post_publish_' . $post_id ) ) {
wp_die('Güvenlik kontrolü başarısız.');
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
wp_die('Bu işlemi gerçekleştirmek için yeterli izniniz yok.');
}
$post = get_post( $post_id );
if ( $post && $post->post_status === 'draft' ) {
wp_update_post( array(
'ID' => $post_id,
'post_status' => 'publish'
) );
}
wp_redirect( admin_url('admin.php?page=custom-post-admin') );
exit;
}
add_action( 'admin_post_custom_post_publish', 'custom_post_publish_handler' );
// Tekil "Sil" işlemi (kalıcı silme)
function custom_post_delete_handler() {
if ( ! isset( $_GET['post_id'] ) || ! isset( $_GET['nonce'] ) ) {
wp_die('Eksik parametreler.');
}
$post_id = intval( $_GET['post_id'] );
$nonce = sanitize_text_field( $_GET['nonce'] );
if ( ! wp_verify_nonce( $nonce, 'custom_post_delete_' . $post_id ) ) {
wp_die('Güvenlik kontrolü başarısız.');
}
if ( ! current_user_can( 'delete_post', $post_id ) ) {
wp_die('Bu işlemi gerçekleştirmek için yeterli izniniz yok.');
}
wp_delete_post( $post_id, true );
wp_redirect( admin_url('admin.php?page=custom-post-admin') );
exit;
}
add_action( 'admin_post_custom_post_delete', 'custom_post_delete_handler' );
// Admin sayfa için script ve stil dosyalarını ekleme
function custom_post_manager_admin_scripts( $hook ) {
if ( $hook != 'toplevel_page_custom-post-admin' ) {
return;
}
wp_enqueue_script( 'custom-post-manager', plugins_url('custom-post-manager.js', __FILE__), array('jquery'), '1.5', true );
wp_enqueue_style( 'custom-post-manager-style', plugins_url('style.css', __FILE__) );
}
add_action('admin_enqueue_scripts', 'custom_post_manager_admin_scripts');
?>