<?php
/**
 * Plugin Name: DoC8
 * Description: Show your documented project, user guide, or any other type of project you have that require upfront documentation with this simple but flexible interface.
 * Author: Irene A. Mitchell
 * Version: 1.0.0
 * Text Domain: docu
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit; // No direct access!
}

/**
 * Class DoC8
 *
 * The main DoC8 class used to control the plugin.
 */
final class DoC8 {
    /**
     * Version number control.
     *
     * @var string
     */
    private $version = '1.0.0';

    /**
     * The absolute path of this plugin.
     *
     * @var string
     */
    private $plugin_path = '';

    /**
     * The relative path of this plugin.
     *
     * @var string
     */
    private $plugin_url = '';

    /**
     * An static instance holder of this class.
     *
     * @var null
     */
    static $_instance = null;

    /**
     * A single instance caller.
     *
     * @return GCourse|null
     */
    public static function instance() {
        if ( ! self::$_instance ) {
            self::$_instance = new Doc8();
        }

        return self::$_instance;
    }

    /**
     * GCourse constructor.
     */
    public function __construct() {
        // Define absolute and relative path
        $this->plugin_path = dirname( __FILE__ ) . DIRECTORY_SEPARATOR;
        $this->plugin_url  = plugins_url( 'doc8/' );

        // Register custom post type during activation
	    register_activation_hook( __FILE__, array( $this, 'activate' ) );

        // Initialize this plugin
        add_action( 'plugins_loaded', array( $this, 'init' ) );
    }

    public function get_plugin_url() {
    	return $this->plugin_url;
    }

    public function get_plugin_path() {
    	return $this->plugin_path;
    }

	/**
	 * Called during plugin activation.
	 */
    public function activate() {
    	// Register then flush rewrite rules
	    // so the new custom post type gets activated.
	    if ( ! function_exists( 'doc8_register' ) ) {
	    	require_once $this->plugin_path . 'inc/functions.php';
	    }

    	doc8_register();
    	flush_rewrite_rules();
    }

    public function init() {
        require_once $this->plugin_path . '/inc/functions.php';

        // Load language file
	    load_plugin_textdomain( 'docu', false, $this->plugin_path . 'languages' );

        add_action( 'init', 'doc8_register' );

        if ( is_admin() ) {
        	$this->admin_hooks();
        } else {
        	$this->front_hooks();
        }
    }

    protected function admin_hooks() {
    	// Register admin settings
	    add_action( 'admin_init', array( $this, 'register_settings' ) );

	    // Register admin assets
	    add_action( 'admin_enqueue_scripts', array( $this, 'register_admin_assets' ) );

    	// Set settings page
	    add_action( 'admin_menu', array( $this, 'set_settings_page' ) );

	    // Save attachments
	    add_action( 'save_post', 'doc8_save_code_attachments', 10, 2 );
    }

    protected function front_hooks() {
    	add_action( 'wp_enqueue_scripts', array( $this, 'register_assets' ) );

	     add_filter( 'pre_get_posts', 'doc8_pre_get_documents' );
	     add_filter( 'posts_orderby', 'doc8_documents_orderby', 10, 2 );

	     add_filter( 'template_include', 'doc8_page_template' );
	     add_filter( 'get_the_archive_title', 'doc8_get_heading_title' );
	     add_filter( 'get_the_archive_description', 'doc8_get_heading_description' );
	     add_filter( 'the_content', 'doc8_set_document_attachments' );
    }

    public function register_assets() {
    	$src = $this->plugin_url . 'assets/';
    	$version = $this->version;

    	// Load standard style
	    wp_register_style( 'docu-standard', $src . 'css/standard.min.css', null, $version );
	    wp_register_script( 'docu-ace', $src . 'external/ace/src-min-noconflict/ace.js', false, $version, true );
	    wp_register_script( 'docu-editor-config', $src . 'js/ace-editor-config.min.js', array( 'jquery', 'docu-ace' ), $version, true );

	    if ( ! is_doc8() ) {
	    	// Don't load assets if it is not doc8 pages.
	    	return;
	    }

	    wp_enqueue_style( 'docu-standard' );
	    wp_enqueue_script( 'docu-editor-config' );
    }

    public function register_settings() {
	    require_once $this->plugin_path . 'inc/admin-functions.php';
	    require_once $this->plugin_path . 'inc/class-doc8-settings.php';
    }

    public function set_settings_page() {
    	$label = __( 'Settings', 'docu' );
    	$post_type = doc8_get_post_type();

    	add_submenu_page( 'edit.php?post_type=' . $post_type, $label, $label, 'manage_options', 'settings', 'doc8_admin_settings_page' );
    }

    public function register_admin_assets() {
	    $src = $this->plugin_url . 'assets/';
	    $version = $this->version;

	    wp_register_style( 'doc8-admin-css', $src . 'css/admin.min.css', false, $version );
	    wp_register_script( 'doc8-admin-js', $src . 'js/admin.min.js', false, $version, true );
    }
}

function doc8() {
    return DoC8::instance();
}
$GLOBALS['DoC8'] = doc8();
