<?php

if (!class_exists('SEPPlugin')) {
	class SEPPlugin
	{
		public function __construct()
		{
			add_action('plugins_loaded', [$this, 'load_dependencies']);
			add_action('admin_enqueue_scripts', [$this, 'sepAdminScripts']);
			add_shortcode('section-separator', [$this, 'sepShortcode']);
		}

		public function load_dependencies()
		{
			require_once SEP_DIR_PATH . 'includes/functions.php';
			require_once SEP_DIR_PATH . 'section-separator-block.php';
			require_once SEP_DIR_PATH . 'includes/class-sepAdmin.php';
		}

		public function sepAdminScripts($screen): void
		{
			global $typenow;


			if ('section-separator' == $typenow) {
				$assets = include SEP_DIR_PATH . 'build/admin-dashboard.asset.php';
				wp_enqueue_script('admin-post-js', SEP_DIR_URL . 'build/admin-post.js', [], SEP_VERSION, true);
				wp_enqueue_style('admin-post-css', SEP_DIR_URL . 'build/admin-post.css', [], SEP_VERSION);
				wp_enqueue_script('sep-admin-dashboard-js', SEP_DIR_URL . 'build/admin-dashboard.js', array_merge($assets['dependencies'], ['wp-util']), SEP_VERSION, true);
				wp_enqueue_style('sep-admin-dashboard-css', SEP_DIR_URL . 'build/admin-dashboard.css', [], SEP_VERSION);
			}
		}


		function sepShortcode($atts)
		{
			$post_id = $atts['id'];
			$post = get_post($post_id);

			if (!$post) {
				return '';
			}

			if (post_password_required($post)) {
				return get_the_password_form($post);
			}

			switch ($post->post_status) {
				case 'publish':
					return $this->displayContent($post);

				case 'private':
					if (current_user_can('read_private_posts')) {
						return $this->displayContent($post);
					}
					return '';

				case 'draft':
				case 'pending':
				case 'future':
					if (current_user_can('edit_post', $post_id)) {
						return $this->displayContent($post);
					}
					return '';

				default:
					return '';
			}
		}

		function displayContent($post)
		{
			$content = apply_filters('the_content', $post->post_content);
			return $content;
		}
	}
}
