<?php

namespace Fab\Controller;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

use Fab\Interfaces\JIT_Interface;

/**
 * Number of Posts
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class Insight_Number_Of_Posts extends Insight_Type_Post implements JIT_Interface {

    /**
     * Constructor.
     *
     * @return void
     */
    public function __construct() {
        parent::__construct();
        $this->insight_id           = 'number_of_posts';
        $this->transient_name       = 'fab_insights_number_of_posts';
        $this->transient_expiration = 24 * HOUR_IN_SECONDS;
        $this->insight_schedule     = 'weekly';
    }

    /**
     * Generate insight.
     *
     * @return int
     */
    public function generate_insight() {
        // Calculate number of posts and it's status.
        $data = wp_count_posts( 'post' );

        // If $data is not an object, early return.
        if ( ! is_object( $data ) ) {
            return array();
        }

        // Get allowed statuses.
        $allowed = array( 'publish', 'draft', 'trash', 'pending', 'private', 'scheduled' );
        $total   = 0;
        foreach ( $data as $status => $count ) {
            if ( ! in_array( $status, $allowed, true ) || 0 === (int) $count ) {
                unset( $data->$status );
                continue;
            }
            $data->$status = (int) $count;
            $total        += $data->$status;
        }

        // Add total to the data.
        $data->total = $total;

        // Set the data to the cache.
        $this->set_transient( $data );

        // Return the data.
        return $data;
    }

    /**
     * Add the insight to the dashboard data.
     *
     * @param array $data The dashboard data.
     * @return array
     */
    public function add_insight_to_dashboard_data( $data ) {
        // If the insight type is not set, set it to an empty array.
        if ( ! isset( $data['labels'][ $this->insight_type ] ) ) {
            $data['labels'][ $this->insight_type ] = array();
        }

        // Add the insight to the dashboard data.
        $data['labels'][ $this->insight_type ][ $this->insight_id ] = __( 'Number of Posts', 'floating-awesome-button' );

        return $data;
    }

    /*
    |--------------------------------------------------------------------------
    | Fulfill implemented interface contracts
    |--------------------------------------------------------------------------
     */

    /**
     * JIT method.
     *
     * @return void
     */
    public static function jit() {
        // Load the insight.
        add_filter( 'fab_insights_dashboard', array( __CLASS__, 'jit_load_insight' ), 10, 2 );

        // Add the insight to the dashboard data.
        add_filter( 'fab_insights_dashboard_data', array( __CLASS__, 'jit_add_insight_to_dashboard_data' ), 10, 2 );

        // Schedule cron to regenerate insight.
        add_action( 'init', array( __CLASS__, 'jit_schedule_regenerate_insight' ) );
        add_action( 'fab_insights_number_of_posts_schedule_action', array( __CLASS__, 'jit_generate_insight' ) );
        add_action( 'fab_insights_number_of_posts_regenerate_insight', array( __CLASS__, 'jit_regenerate_insight' ) );
    }
}
