<?php

namespace ReadJutsu;

use ReadJutsu\Admin;
use ReadJutsu\Frontend;

/**
 * Plugin Name:     ReadJutsu – Reading Time & Scroll Progress Bar
 * Plugin URI:      https://www.wp-jutsu.com/plugins/readjutsu/
 * Description:     Display estimated reading time and a beautiful scroll progress bar. Native block theme support, Gutenberg block included.
 * Version:         1.0.0
 * Requires at least: 6.2
 * Requires PHP:    7.4
 * Author:          WPJutsu
 * Author URI:      https://www.wp-jutsu.com
 * License:         GPL-3.0-or-later
 * License URI:     https://www.gnu.org/licenses/gpl-3.0.html
 * Text Domain:     readjutsu
 */

defined('ABSPATH') or die('Something went wrong.');

// Free and premium builds can be loaded in the same request (during the
// premium activation), so everything below is guarded against a double load.
if (!defined('READJUTSU_PLUGIN_PATH')) {
  define('READJUTSU_PLUGIN_URL',  plugin_dir_url(__FILE__));
  define('READJUTSU_PLUGIN_PATH', plugin_dir_path(__FILE__));
  define('READJUTSU_SLUG',    'readjutsu');
  define('READJUTSU_VERSION', '1.0.0');
}

require_once READJUTSU_PLUGIN_PATH . 'freemius-init.php';

if (!class_exists(__NAMESPACE__ . '\\ReadJutsu', false)) {

class ReadJutsu
{

  public function __construct()
  {
    $this->load_dependencies();
    add_action('init', [$this, 'register_block']);
  }

  private function load_dependencies()
  {
    if (is_admin()) {
      include_once READJUTSU_PLUGIN_PATH . 'admin/class-readjutsu-admin.php';
    } else {
      include_once READJUTSU_PLUGIN_PATH . 'includes/class-readjutsu-frontend.php';
    }
  }

  public function register_block()
  {
    // Register the public stylesheet so block.json can reference it as a "style"
    // handle — WordPress then loads it in both the front end and the editor
    // canvas (iframe), no manual <style> dump needed.
    wp_register_style(
      'readjutsu-public',
      READJUTSU_PLUGIN_URL . 'public/css/readjutsu-public.css',
      [],
      READJUTSU_VERSION
    );

    if (function_exists('register_block_type')) {
      register_block_type(READJUTSU_PLUGIN_PATH . 'block/reading-time/');
      register_block_type(READJUTSU_PLUGIN_PATH . 'block/progress-bar/');
    }
  }
}

function readjutsu_run_plugin()
{
  new ReadJutsu();
  if (is_admin()) {
    new Admin();
  } else {
    new Frontend();
  }

  // Needed in both admin (meta box) and front (render filters).
  $override_file = READJUTSU_PLUGIN_PATH . 'includes/class-readjutsu-post-override-pro__premium_only.php';
  if (\readjutsu_can_use_premium() && file_exists($override_file)) {
    require_once $override_file;
    new Post_Override_Pro();
  }

  $analytics_file = READJUTSU_PLUGIN_PATH . 'includes/class-readjutsu-analytics-pro__premium_only.php';
  if (\readjutsu_can_use_premium() && file_exists($analytics_file)) {
    require_once $analytics_file;
    new Analytics_Pro();
  }
}
add_action('plugins_loaded', 'ReadJutsu\\readjutsu_run_plugin');

}
