<?php
/**
 * Clearfy quick start options
 *
 * @author Alex Kovalev <alex.kovalevv@gmail.com>
 * @copyright (c) 03.06.2020, Webcraftic
 * @version 1.0
 */

add_filter("wbcr_clearfy_group_options", function ($options) {
	$options[] = array(
		'name' => 'ajs_enabled',
		'title' => __('Enable Async JavaScript', 'async-javascript'),
		'tags' => array('optimize_performance', 'optimize_code')
	);

	/*$options[] = array(
		'name' => 'ajs_method',
		'title' => __('Javascript load mode', 'async-javascript'),
		'tags' => array('optimize_code'),
		'values' => ['optimize_code' => 'defer']
	);

	$options[] = array(
		'name' => 'ajs_jquery',
		'title' => __('jQuery load', 'async-javascript'),
		'tags' => array('optimize_code'),
		'values' => ['optimize_code' => 'exclude']
	);*/

	$options[] = array(
		'name' => 'ajs_exclusions',
		'title' => __('Exclude scripts', 'async-javascript'),
		'tags' => array()
	);

	/*$options[] = array(
		'name' => 'ajs_async',
		'title' => __('Scripts to async', 'async-javascript'),
		'tags' => array()
	);

	$options[] = array(
		'name' => 'ajs_defer',
		'title' => __('Scripts to defer', 'async-javascript'),
		'tags' => array()
	);

	$options[] = array(
		'name' => 'ajs_plugin_exclusions',
		'title' => __('Plugin Exclusions', 'async-javascript'),
		'tags' => array()
	);

	$options[] = array(
		'name' => 'ajs_theme_exclusions',
		'title' => __('Theme Exclusions', 'async-javascript'),
		'tags' => array()
	);*/

	return $options;
});

add_filter('wmac/pages/settings_form_options', function ($options) {

	$plugins = [];
	$installed_plugins = get_plugins();
	foreach($installed_plugins as $key => $plugin) {
		$tmp = explode('/', $key);
		if( isset($tmp[0]) && count($tmp) >= 2 ) {
			$slug = $tmp[0];
			$plugins[] = [$slug, $plugin['Name']];
		} else {
			break;
		}
	}

	$themes = [];
	$installed_themes = wp_get_themes();
	foreach($installed_themes as $key => $theme) {
		$themes[] = [$key, $theme];
	}

	$options[] = [
		'type' => 'html',
		'html' => '<div class="wbcr-factory-page-group-header"><strong>' . __('Load JavaScript deferred', 'async-javascript') . '</strong><p>' . __('Enabling/disabling Async JavaScript, as well as basic operating mode settings', 'async-javascript') . '</p></div>'
	];

	$options[] = [
		'type' => 'checkbox',
		'way' => 'buttons',
		'name' => 'ajs_enabled',
		'title' => __('Load JavaScript deferred', 'async-javascript'),
		'layout' => ['hint-type' => 'icon', 'hint-icon-color' => 'red'],
		'hint' => __('Enabling / disabling Async JavaScript', 'async-javascript'),
		'default' => false
	];

	/*$options[] = [
		'type' => 'dropdown',
		'way' => 'buttons',
		'name' => 'ajs_method',
		'title' => __('Javascript load mode', 'async-javascript'),
		'data' => [
			['async', __('Async', 'async-javascript')],
			['defer', __('Defer', 'async-javascript')],
		],
		'default' => 'async',
		'layout' => ['hint-type' => 'icon', 'hint-icon-color' => 'grey'],
		'hint' => __("Select the method that you wish to enable.", 'async-javascript')
	];

	$options[] = [
		'type' => 'dropdown',
		'way' => 'buttons',
		'name' => 'ajs_jquery',
		'title' => __('jQuery load', 'async-javascript'),
		'data' => [
			['async', __('Async', 'async-javascript')],
			['defer', __('Defer', 'async-javascript')],
			['exclude', __('Exclude', 'async-javascript')],
		],
		'default' => 'exclude',
		'layout' => ['hint-type' => 'icon', 'hint-icon-color' => 'grey'],
		'hint' => __("Often if jQuery is loaded with async or defer it can break some jQuery functions, specifically inline scripts which require jQuery to be loaded before the scripts are run. Sometimes choosing a different method (async or defer) will work, otherwise it may be necessary to exclude jQuery from having async or defer applied.", 'async-javascript')
	];

	$options[] = [
		'type' => 'html',
		'html' => '<div class="wbcr-factory-page-group-header"><strong>' . __('Exclusions for Async JavaScript', 'async-javascript') . '</strong><p>' . __('Scripts, plugins, and themes that should not be handled by the Async Javascript component', 'async-javascript') . '</p></div>'
	];*/

	$options[] = [
		'type' => 'textarea',
		'name' => 'ajs_exclusions',
		'title' => __('Exclude scripts', 'async-javascript'),
		'hint' => __('A comma-separated list of scripts you want to exclude, for example \'whatever.js, another.js\' (without the quotes) to exclude those scripts', 'async-javascript'),
		'default' => ''
	];

	/*$options[] = [
		'type' => 'textarea',
		'name' => 'ajs_async',
		'title' => __('Scripts to async', 'async-javascript'),
		'hint' => __('Please list any scripts which you would like to apply the \'async\' attribute to. (comma seperated list eg: whatever.js, another.js)', 'async-javascript'),
		'default' => ''
	];

	$options[] = [
		'type' => 'textarea',
		'name' => 'ajs_defer',
		'title' => __('Scripts to defer', 'async-javascript'),
		'hint' => __('Please list any scripts which you would like to apply the \'defer\' attribute to. (comma seperated list eg: whatever.js, another.js)', 'async-javascript'),
		'default' => ''
	];

	$options[] = [
		'type' => 'list',
		'way' => 'checklist',
		'name' => 'ajs_plugin_exclusions',
		'title' => __('Plugin Exclusions', 'async-javascript'),
		'data' => $plugins,
		'layout' => ['hint-type' => 'icon', 'hint-icon-color' => 'grey'],
		'hint' => __('Select one or more plugins. Scripts contained within the plugin will not have async/defer applied.<br><br>' . 'Please Note: This will exclude any JavaScript files which are contained within the files of the selected Plugin(s). External JavaScripts loaded by the selected Plugin(s) are not affected.<br><br>
				 For Example: If a plugin is installed in path /wp-content/plugins/some-plugin/ then and JavaScripts contained within this path will be excluded. If the plugin loads a JavaScript which is countained elsewhere then the Global Method will be used (ie async or defer)', 'async-javascript'),
		'default' => '',
		'cssClass' => 'wbcr_clearfy_ajs_multiple_list',
	];

	$options[] = [
		'type' => 'list',
		'way' => 'checklist',
		'name' => 'ajs_theme_exclusions',
		'title' => __('Theme Exclusions', 'async-javascript'),
		'data' => $themes,
		'layout' => ['hint-type' => 'icon', 'hint-icon-color' => 'grey'],
		'hint' => __('Select one or more themes. Scripts contained within the theme will not have async/defer applied.<br><br>' . 'Please Note: This will exclude any JavaScript files which are contained within the files of the selected Theme(s). External JavaScripts loaded by the selected Theme(s) are not affected.<br><br>
				 For Example: If a theme is installed in path /wp-content/themes/some-theme/ then and JavaScripts contained within this path will be excluded. If the theme loads a JavaScript which is countained elsewhere then the Global Method will be used (ie async or defer)', 'async-javascript'),
		'default' => '',
		'cssClass' => 'wbcr_clearfy_ajs_multiple_list',
	];*/

	return $options;
});
