/*
Plugin Name: Keyword Blog Analyzer & Google Docs Exporter
Description: An advanced blog management tool for listing blogs based on keywords, estimating page count, exporting to Google Docs, and marking blog usage status.
Version: 1.0
Author: SpiritMeaning Dev
*/
if (!defined('ABSPATH')) exit;
class Keyword_Blog_Analyzer_Exporter {
private $option_name = 'kbae_blog_status';
public function __construct() {
add_action('admin_menu', [$this, 'register_admin_menu']);
add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_scripts']);
add_action('wp_ajax_kbae_update_status', [$this, 'handle_status_update']);
add_action('wp_ajax_kbae_export_doc', [$this, 'export_to_google_docs']);
}
public function register_admin_menu() {
add_menu_page('Blog Keyword Analyzer', 'Blog Analyzer', 'manage_options', 'blog-keyword-analyzer', [$this, 'render_admin_page']);
}
public function enqueue_admin_scripts() {
wp_enqueue_script('kbae-script', plugin_dir_url(__FILE__) . 'assets/js/kbae-script.js', ['jquery'], null, true);
wp_localize_script('kbae-script', 'kbae_ajax', [
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('kbae_nonce')
]);
}
public function render_admin_page() {
if (!current_user_can('manage_options')) return;
ob_start();
$status_data = get_option($this->option_name, []);
$keyword = isset($_POST['kbae_keyword']) ? sanitize_text_field($_POST['kbae_keyword']) : '';
echo '
Blog Keyword Analyzer
';
echo '
';
if ($keyword) {
$query = new WP_Query([
'post_type' => 'post',
'posts_per_page' => -1,
's' => $keyword
]);
if ($query->have_posts()) {
echo '
';
wp_reset_postdata();
} else {
echo '
No posts found.
';
}
}
echo '
';
ob_end_flush();
}
public function handle_status_update() {
check_ajax_referer('kbae_nonce', 'nonce');
$post_id = intval($_POST['post_id']);
$lock = filter_var($_POST['lock'], FILTER_VALIDATE_BOOLEAN);
$book = sanitize_text_field($_POST['book']);
$data = get_option($this->option_name, []);
$data[$post_id] = [
'locked' => $lock,
'book' => $book
];
update_option($this->option_name, $data);
wp_send_json_success();
}
public function export_to_google_docs() {
require_once __DIR__ . '/vendor/autoload.php';
$client = new Google_Client();
$client->setApplicationName('KBAE Exporter');
$client->setScopes([Google_Service_Drive::DRIVE_FILE, Google_Service_Docs::DOCUMENTS]);
$client->setAuthConfig(__DIR__ . '/client_secret.json');
$client->setAccessType('offline');
$tokenPath = __DIR__ . '/google/token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
if ($client->isAccessTokenExpired()) {
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
$authUrl = $client->createAuthUrl();
wp_send_json_error(['message' => 'Please authorize via: ' . $authUrl]);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
$post_id = intval($_POST['post_id']);
$post = get_post($post_id);
$content = strip_tags($post->post_content);
$title = $post->post_title;
$docsService = new Google_Service_Docs($client);
$doc = $docsService->documents->create([
'title' => $title
]);
$requests = [[
'insertText' => [
'location' => ['index' => 1],
'text' => $content
]
]];
$docsService->documents->batchUpdate($doc->documentId, new Google_Service_Docs_BatchUpdateDocumentRequest([
'requests' => $requests
]));
wp_send_json_success(['message' => 'Exported to Google Docs: ' . $doc->title]);
}
}
new Keyword_Blog_Analyzer_Exporter();