<?php
/*
Plugin Name: DOCman Pro
Plugin URI: https://www.joomlatools.com/wordpress
Description: Document Management Plugin for Wordpress
Version: 2.4.4
Author: Joomlatools
Author URI: https://www.joomlatools.com/
Requires at least: 6.3
Requires PHP: 7.3
License: GNUGPLv3
License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/

/**
 * @package     DOCman
 * @copyright   Copyright (C) 2019 Timble CVBA. (http://www.timble.net)
 * @license     GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link        http://www.joomlatools.com
 */

defined( 'ABSPATH' ) or die();

require_once __DIR__.'/base/resources/install/install.php';

$helper = \Foliolabs\Docman\InstallHelper::getInstance(__FILE__);

// Do not register the plugin if a FW install is pending

if (!$helper->hasFrameworkFolderToInstall())
{
	if ($helper->canRegister())
	{
		require_once WP_CONTENT_DIR.'/plugins/foliokit/wordpress.php'; // Load custom WordPress functions

		\Foliolabs\WP::add_action('foliokit_before_bootstrap', function() {
			foliokit_register_plugin(__FILE__);
		});

		\Foliolabs\WP::add_action('foliokit_after_bootstrap', function()
		{
			try {
				$manager = \Foliokit::getObject('manager');

				$manager->setObject('user.provider', $manager->getObject('user.provider')->decorate('com:docman.user.provider'));
				$manager->setObject('user', $manager->getObject('user')->decorate('com:docman.user'));

				// Load the language file so menu items are translated in backend at all times
				\Foliokit::getObject('translator')->load('com:docman');

				// Sync role changes
				\Foliolabs\WP::add_action('updated_option', function($option_name, $old_roles, $new_roles) use ($manager)
				{
					global $wpdb;

					if ($option_name == sprintf('%suser_roles', $wpdb->prefix))
					{
						$model = $manager->getObject('com:docman.model.usergroups');

						$usergroups = $model->internal(1)->fetch();

						foreach ($usergroups as $usergroup)
						{
							if (!isset($new_roles[$usergroup->name]))
							{
								$usergroup->delete();
								$usergroups->remove($usergroup);
							}
						}

						foreach ($new_roles as $name => $role)
						{
							if (!$usergroups->find(array('name' => $name))->count()) {
								$model->create(array('name' => $name, 'internal' => 1))->save();
							}
						}
					}
				}, 10, 3);

				$sync_user = function($user_id, $hash_check = true) {
					\Foliokit::getObject('com:docman.controller.user')->sync(['user_id' => $user_id, 'hash_check' => $hash_check]);
				};

				if ($user_id = \Foliolabs\WP::get_current_user_id()) {
					$sync_user($user_id);  // Perform a "soft" roles/groups sync
				}

				// Sync user roles changes
				\Foliolabs\WP::add_action( 'set_user_role', function($user_id, $new_role, $old_roles) use ($sync_user) {
					$sync_user($user_id);
				}, 10, 3 );

				// Sync user roles changes
				\Foliolabs\WP::add_action( 'add_user_role', function($user_id, $role) use ($sync_user) {
					$sync_user($user_id);
				}, 10, 2 );

				// Sync user roles changes
				\Foliolabs\WP::add_action( 'remove_user_role', function($user_id, $role) use ($sync_user) {
					$sync_user($user_id);
				}, 10, 2 );

				// Cleanup tables upon user deletes
				\Foliolabs\WP::add_action('deleted_user', function($user_id)
				{
					$driver = \Foliokit::getObject('lib:database.driver.mysqli');

					$query = \Foliokit::getObject('lib:database.query.delete')->table('docman_usergroups_users')
									->where('wp_user_id = :user_id')->bind(['user_id' => $user_id]);

					$driver->delete($query);

					\Foliokit::getObject('com:docman.model.users')->id($user_id)->fetch()->delete(); // Delete corresponding user data
				}, 10, 1);


				$adminEditorView = function()
				{
					global $hook_suffix;

					$query = \Foliokit::getObject('request')->getQuery();
					if ($query->get('component', 'cmd') !== 'docman' || $query->get('view', 'cmd') !== 'editor') {
						return;
					}

					$hook_suffix = 'docman-form';
					
					\Foliolabs\WP::add_filter('admin_footer_text', function () { return ''; }, PHP_INT_MAX, 1);
					\Foliolabs\WP::add_filter('update_footer', function () { return ''; }, PHP_INT_MAX, 1);

					\Foliolabs\WP::remove_action( 'in_admin_header', 'wp_admin_bar_render', 0 );
					\Foliolabs\WP::add_filter( 'admin_title', function(){ $GLOBALS['wp_query']->is_embed=true;  add_action('admin_xml_ns', function(){ $GLOBALS['wp_query']->is_embed=false; } ); } );

					$style = 'foliokit-admin-hide-sidebar';
					\Foliolabs\WP::wp_register_style($style, '', []);
					\Foliolabs\WP::wp_enqueue_style($style);
					\Foliolabs\WP::wp_add_inline_style($style, "
							#adminmenumain, #wpfooter { display: none; }
							#wpcontent { margin: 5px; padding-left: 0; }
							#wpbody-content { padding-bottom: 0; }
							body { height: revert; /* avoid the scrollbar */ }
							");

					require_once \Foliolabs\WP\ABSPATH . 'wp-admin/admin-header.php';

					if (\Foliokit::getObject('request')->getQuery()->has('component')) {
						// Dispatch action ran already. Return the result
						echo \Foliokit::getObject('com:base.dispatcher.page')->render();
					}

					require_once \Foliolabs\WP\ABSPATH . 'wp-admin/admin-footer.php';
					die;
				};

				$siteEditorView = function() {
					if (is_admin()) { return; }

					$query = \Foliokit::getObject('request')->getQuery();

					// Dirty hack to support endpoint requests
					if (strpos(urldecode(Foliokit::getObject('request')->getUrl()->getPath()), 'route:/editor') !== false) {
						$query->set('component', 'docman');
						$query->set('view', 'editor');

						\Foliokit::getObject('com:base.dispatcher.page')->dispatch();
					}

					if ($query->get('component', 'cmd') !== 'docman' || $query->get('view', 'cmd') !== 'editor') {
						return;
					}

					?><!DOCTYPE html>
					<html class="no-js" <?php language_attributes(); ?>>
					<head>
						<meta charset="<?php bloginfo( 'charset' ); ?>">
						<meta name="viewport" content="width=device-width, initial-scale=1.0" >
						<link rel="profile" href="https://gmpg.org/xfn/11">
						<?php wp_head(); ?>
						<style>body { padding: 5px; }</style>
					</head>
					<body <?php body_class(); ?>>
					<?php
					wp_body_open();

					if (\Foliokit::getObject('request')->getQuery()->has('component')) {
						// Dispatch action ran already. Return the result
						echo \Foliokit::getObject('com:base.dispatcher.page')->render();
					}
					wp_footer();
					echo '</body></html>';
					die();
				};

				\Foliolabs\WP::add_action('admin_init', $adminEditorView);
				\Foliolabs\WP::add_action('init', $siteEditorView, PHP_INT_MAX);

				// Siteground Optimizer script exclussions 

				\Foliolabs\WP::add_filter( 'sgo_javascript_combine_exclude', function ($exclude_list)
				{
					$exclude_list = array_merge($exclude_list, \Foliolabs\WP::getEnqueuedScripts());

					return $exclude_list;
				});

				// Siteground Optimizer style exclussions 

				\Foliolabs\WP::add_filter( 'sgo_css_combine_exclude', function($exclude_list)
				{
					$exclude_list = array_merge($exclude_list, \Foliolabs\WP::getEnqueuedStyles());
				
					return $exclude_list;
				});

				\Foliolabs\WP::add_action('permalink_structure_changed', function() use ($manager)
				{
					// Force categories cache (tree) to re-generate itself

					$manager->getObject('com:docman.database.table.categories')->clearCache();
				});

			} catch (\Exception $e){
				if (\Foliokit::isDebug()) throw $e;
			}
		});
	}
	else $helper->deactivate();
}
