api = $api; add_action('wp_ajax_aicp_refresh_statistics', array($this, 'ajax_refresh_statistics')); } /** * Get statistics data * * @param bool $force_refresh Force refresh from API * @return array|WP_Error Statistics data or error */ public function get_statistics($force_refresh = false) { return $this->api->get_statistics($force_refresh); } /** * Get articles published this month * * @return int */ public function get_articles_this_month() { $stats = $this->get_statistics(); if (is_wp_error($stats)) { return 0; } return isset($stats['articlesPublishedThisMonth']) ? (int) $stats['articlesPublishedThisMonth'] : 0; } /** * Get total articles all time * * @return int */ public function get_total_articles() { $stats = $this->get_statistics(); if (is_wp_error($stats)) { return 0; } return isset($stats['totalArticlesAllTime']) ? (int) $stats['totalArticlesAllTime'] : 0; } /** * Get publishing history * * @return array */ public function get_publishing_history() { $stats = $this->get_statistics(); if (is_wp_error($stats)) { return array(); } return isset($stats['publishingHistory']) ? $stats['publishingHistory'] : array(); } /** * Format history for chart display * * @return array Chart-ready data */ public function get_chart_data() { $history = $this->get_publishing_history(); $labels = array(); $data = array(); foreach ($history as $item) { // Convert YYYY-MM to readable format $date = DateTime::createFromFormat('Y-m', $item['month']); $labels[] = $date ? $date->format('M Y') : $item['month']; $data[] = (int) $item['count']; } // Reverse to show oldest first return array( 'labels' => array_reverse($labels), 'data' => array_reverse($data) ); } /** * AJAX: Refresh statistics */ public function ajax_refresh_statistics() { check_ajax_referer('aicp_stats_nonce', 'nonce'); if (!current_user_can('manage_aicp_settings')) { wp_send_json_error(array('message' => __('Permission denied.', 'ai-content-plan'))); } $stats = $this->get_statistics(true); // Force refresh if (is_wp_error($stats)) { wp_send_json_error(array('message' => $stats->get_error_message())); } wp_send_json_success(array( 'articles_this_month' => isset($stats['articlesPublishedThisMonth']) ? (int) $stats['articlesPublishedThisMonth'] : 0, 'total_articles' => isset($stats['totalArticlesAllTime']) ? (int) $stats['totalArticlesAllTime'] : 0, 'chart_data' => $this->get_chart_data() )); } }