<?php 

    global $syrus;
    $current_date = current_datetime()->format('Y-m-d H:i:s');
    $one_month_ago = gmdate('Y-m-d H:i:s', strtotime($current_date . ' -1 month'));

    $older_posts_with_open_comments = new WP_Query([
        'posts_per_page'    => -1,
        'post_type'         => 'post',
        'post_status'       => 'publish',
        'comment_status'    => 'open',
        'date_query'        => [
            'before'    => $one_month_ago,
            'inclusive' => true,
        ]
    ]);

    $all_posts = new WP_Query([
        'posts_per_page'    => -1,
        'post_type'         => 'post',
        'post_status'       => 'publish',
        'date_query'        => [
            'before'    => $one_month_ago,
            'inclusive' => true,
        ]
    ]);

    $olders_revisions = new WP_Query([
        'posts_per_page'    => -1,
        'post_type'         => 'revision',
        'post_status'       => 'inherit',
        'date_query'        => [
            'before'    => $one_month_ago,
            'inclusive' => true,
        ]
    ]);

    $spam_and_pending_comments = new WP_Comment_Query();
    $spam_and_pending_comments = $spam_and_pending_comments->query([
        'type'      => 'comment',
        'status'    => ['hold', 'spam'],
    ]);


	$table_info = $syrus->get_posts_table_info();
    $olders_revisions_size = number_format($olders_revisions->found_posts * $table_info['record_size'], 2);

?>
<h2><?php esc_html_e("Clean your site",'syrus') ?></h2>
<p class="m-0"><?php esc_html_e("Here you can clean and optimize your site",'syrus') ?></p>
<hr class="mt-1 mb-4">

<div class="row">

    <div class="col-md-4">
        <div class="card p-0">
            <form onsubmit="return confirm('<?php esc_html_e('Are you sure to close comments on posts older than a month', 'syrus') ?>')" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" method="POST">
                <input type="hidden" name="action" value="close_comments_on_older_posts">
                <?php wp_nonce_field('close_comments_on_older_posts') ?>
                <img src="<?php echo SYRUS_THEME_URI . '/assets/img/wp-admin/close-comments.png' ?>" class="card-img-top" alt="...">
                <div class="card-body d-flex flex-column justify-content-start align-items-start" style="height: 200px">
                    <h5 class="card-title"><?php esc_html_e("Close comments on older posts",'syrus') ?></h5>
                    <p class="card-subtitle mb-4 text-body-secondary"><?php esc_html_e("Close comments on posts older than a month", 'syrus') ?></p>

                    <div class="d-flex justify-content-start align-items-end">
                        <h4 class="mb-0 comments_to_close_count me-2"><?php echo $older_posts_with_open_comments->found_posts ?></h4>
                        <p class="mb-0"> / <?php echo $all_posts->found_posts ?> <?php esc_html_e("Comments to close", 'syrus') ?> </p>
                    </div>
                    
                    <div class="clearfix w-100 mt-auto">
                        <button class="btn btn-outline-primary rounded-4 btn-sm float-end"><?php esc_html_e('Close comments', 'syrus') ?></button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="col-md-4">
        <div class="card p-0">
            <form onsubmit="return confirm('<?php esc_html_e('Are you sure to remove revisions older than a month, this action is irreversible', 'syrus') ?>')" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" method="POST">
                <input type="hidden" name="action" value="remove_revisions_on_older_posts">
                <?php wp_nonce_field('remove_revisions_on_older_posts') ?>
                <img src="<?php echo SYRUS_THEME_URI . '/assets/img/wp-admin/remove-revisions.png' ?>" class="card-img-top" alt="...">
                <div class="card-body d-flex flex-column justify-content-start align-items-start" style="height: 200px">
                    <h5 class="card-title"><?php esc_html_e("Remove revisions on older posts",'syrus') ?></h5>
                    <p class="card-subtitle mb-4 text-body-secondary"><?php esc_html_e("Remove revisions on posts older than a month", 'syrus') ?></p>

                    <div class="d-flex justify-content-start align-items-end">
                        <h4 class="mb-0 revision_count me-2"><?php echo $olders_revisions->found_posts ?></h4>
                        <p class="mb-0"><?php esc_html_e("Revisions to remove", 'syrus') ?> (~ <?php echo $olders_revisions_size . ' kb' ?>)</p>
                    </div>

                    <div class="clearfix w-100 mt-auto">
                        <button class="btn btn-outline-primary rounded-4 btn-sm float-end"><?php esc_html_e('Remove revisions', 'syrus') ?></button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="col-md-4">
        <div class="card p-0">
            <form onsubmit="return confirm('<?php esc_html_e('Are you sure to delete all spam and pending comments, this action is irreversible', 'syrus') ?>')" action="<?php echo esc_url(admin_url('admin-post.php')); ?>" method="POST">
                <input type="hidden" name="action" value="remove_spam_and_pending_comments">
                <?php wp_nonce_field('remove_spam_and_pending_comments') ?>
                <img src="<?php echo SYRUS_THEME_URI . '/assets/img/wp-admin/remove-spam.png' ?>" class="card-img-top" alt="...">
                <div class="card-body d-flex flex-column justify-content-start align-items-start" style="height: 200px">
                    <h5 class="card-title"><?php esc_html_e("Remove spam and pending comments",'syrus') ?></h5>
                    <p class="card-subtitle mb-4 text-body-secondary"><?php esc_html_e("Remove spam and pending comments", 'syrus') ?></p>

                    <div class="d-flex justify-content-start align-items-end">
                        <h4 class="mb-0 comment_count me-2"><?php echo count($spam_and_pending_comments) ?></h4>
                        <p class="mb-0"><?php esc_html_e("Comments to remove", 'syrus') ?></p>
                    </div>

                    <div class="clearfix w-100 mt-auto">
                        <button class="btn btn-outline-primary rounded-4 btn-sm float-end"><?php esc_html_e('Remove comments', 'syrus') ?></button>
                    </div>

                </div>
            </form>
        </div>
    </div>
</div>