<?php

/**
 * Basic theme setup
 */

add_filter('use_block_editor_for_post', '__return_false', 10);
add_filter('use_widgets_block_editor', '__return_false');

add_action('admin_head', function () {
?>
	<style>
		#adminmenu #custom-admin-logo {
			display: flex;
			flex-direction: column;
			align-items: flex-start;
			justify-content: flex-start;
			gap: 5px;
			padding: 5px 10px;
			margin-bottom: 5px;
		}

		.folded #adminmenu #custom-admin-logo {
			overflow: hidden;
		}
	</style>
	<script>
		document.addEventListener('DOMContentLoaded', function() {
			const menu = document.getElementById('adminmenu');

			if (menu && !document.getElementById('custom-admin-logo-item')) {

				const li = document.createElement('li');
				li.id = 'custom-admin-logo-item';
				li.className = 'wp-first-item custom-admin-logo-item';

				const link = document.createElement('a');
				link.id = 'custom-admin-logo';
				link.href = 'https://t.me/wpgads_bot';
				link.target = '_blank';
				link.className = 'menu-top';

				link.innerHTML = `
                <span class="made-by">Generated by</span>
                <svg width="100%" height="51" viewBox="0 0 130 51" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
                <path d="M116.539 19.2739C119.442 19.2739 122.038 19.7533 124.325 20.712C126.613 21.6708 128.504 23.0508 130 24.8521L124.457 29.8201C123.401 28.6871 122.258 27.8447 121.026 27.2927C119.824 26.7116 118.475 26.4209 116.979 26.4209C115.659 26.4209 114.457 26.6244 113.372 27.0311C112.287 27.4379 111.363 28.0334 110.601 28.8179C109.838 29.5732 109.237 30.4885 108.797 31.5634C108.386 32.6093 108.181 33.8005 108.181 35.1369C108.181 36.4152 108.386 37.5919 108.797 38.6668C109.237 39.7418 109.838 40.6716 110.601 41.456C111.363 42.2114 112.272 42.807 113.328 43.2428C114.413 43.6495 115.601 43.853 116.891 43.853C118.181 43.853 119.442 43.6495 120.674 43.2428C120.981 43.1342 121.289 43.0074 121.598 42.8626V34.4834H129.296V47.1215L129.13 47.2376C127.409 48.4303 125.426 49.3504 123.182 49.9978C120.865 50.666 118.592 51 116.363 51C113.929 51 111.671 50.6222 109.589 49.8669C107.507 49.0824 105.703 47.9786 104.178 46.555C102.682 45.1313 101.509 43.4606 100.659 41.5431C99.8084 39.5965 99.3833 37.4612 99.3833 35.1369C99.3833 32.8127 99.8083 30.6917 100.659 28.7742C101.509 26.8277 102.697 25.1425 104.222 23.7189C105.747 22.2954 107.551 21.2058 109.633 20.4504C111.744 19.666 114.046 19.2739 116.539 19.2739Z" />
                <path d="M50.6489 39.5373L57.0982 19.8841H65.412L55.4264 50.3898H46.1006L40.2013 31.811L34.0475 50.3898H24.7217L21.5228 41.6411C22.5437 41.8381 23.5126 42.1551 24.0755 42.7115C25.6799 44.2976 25.2692 49.1437 25.7789 49.1508C26.2888 49.157 25.757 44.2985 27.3226 42.7508C28.8882 41.2032 33.8023 41.6806 33.797 41.2249C33.7907 40.7693 28.8874 41.127 27.2828 39.541C25.6781 37.9548 26.0891 33.1077 25.5791 33.1015C25.0692 33.0954 25.601 37.9538 24.0354 39.5014C23.4184 40.1113 22.2814 40.4061 21.1303 40.568L18.0854 32.2402H27.0593L29.7657 39.7676L36.423 19.8841H44.4291L50.6489 39.5373Z" />
                <path fill-rule="evenodd" clip-rule="evenodd" d="M82.6096 19.8841C85.4249 19.8841 87.8446 20.3343 89.8681 21.235C91.9209 22.1356 93.5045 23.443 94.6189 25.1571C95.7333 26.8422 96.2904 28.8469 96.2904 31.1712C96.2904 33.4664 95.7333 35.4566 94.6189 37.1417C93.5045 38.8267 91.9209 40.1341 89.8681 41.0638C87.8446 41.9644 85.4249 42.4149 82.6096 42.4149H77.2431V50.3898H68.533V19.8841H82.6096ZM77.2431 35.6162H82.0819C83.9001 35.6162 85.2492 35.2241 86.129 34.4397C87.0381 33.6553 87.4926 32.5657 87.4926 31.1712C87.4926 29.7476 87.0381 28.6435 86.129 27.859C85.2492 27.0746 83.9001 26.6825 82.0819 26.6825H77.2431V35.6162Z" />
                <path d="M1.18057e-09 22.2349C0.000112377 21.4902 8.02236 22.1728 10.6127 19.6122C13.203 17.0517 12.4325 9.12162 13.2659 9.12162C14.0992 9.12163 13.3287 17.0517 15.919 19.6122C18.5093 22.1728 26.5322 21.4901 26.5317 22.2349C26.5312 22.9797 18.5093 22.297 15.919 24.8575C13.3287 27.4181 14.0992 35.3482 13.2659 35.3481C12.4325 35.3481 13.203 27.4181 10.6127 24.8575C8.02237 22.297 -0.000112374 22.9796 1.18057e-09 22.2349Z" />
                <path d="M17.8441 8.96534C17.8442 8.45619 23.3289 8.92286 25.0998 7.17227C26.8708 5.42168 26.344 -5.24266e-06 26.9138 0C27.4835 5.24267e-06 26.9567 5.42168 28.7277 7.17227C30.4987 8.92286 35.9838 8.45616 35.9834 8.96534C35.9831 9.47453 30.4987 9.00782 28.7277 10.7584C26.9567 12.509 27.4835 17.9307 26.9138 17.9307C26.3441 17.9306 26.8708 12.509 25.0998 10.7584C23.3289 9.00781 17.844 9.47449 17.8441 8.96534Z" />
                </svg>`;

				li.appendChild(link);
				menu.prepend(li);
			}
		});
	</script>
<?php
});

if (! function_exists('theme_setup')) {
	function theme_setup()
	{

		add_theme_support('title-tag');

		add_theme_support('post-thumbnails');

		add_theme_support('html5', array(
			'search-form',
			'comment-form',
			'comment-list',
			'gallery',
			'caption'
		));

		add_theme_support('custom-logo');

		register_nav_menus(array(
			'main-menu' => __('Main Menu', 'theme'),
		));
	}
}
add_action('after_setup_theme', 'theme_setup');

/**
 * Favicon from assets/favicon.svg.
 */
add_action('wp_head', function (): void {
	$path = get_template_directory() . '/assets/favicon.svg';
	if (! is_file($path)) {
		return;
	}

	printf(
		'<link rel="icon" href="%s" type="image/svg+xml">' . "\n",
		esc_url(get_template_directory_uri() . '/assets/favicon.svg')
	);
}, 1);

/**
 * Theme config from assets/theme-config.php (generated by pipeline).
 *
 * @return array<string, string>
 */
function theme_config(): array
{
	static $config = null;

	if ($config !== null) {
		return $config;
	}

	$path = get_template_directory() . '/assets/theme-config.php';
	if (! is_file($path)) {
		$config = [];

		return $config;
	}

	$loaded = require $path;
	$config = is_array($loaded) ? $loaded : [];

	return $config;
}

function theme_google_fonts_url(): string
{
	$import = trim((string) (theme_config()['google_fonts_import'] ?? ''));
	if ($import === '') {
		return '';
	}

	return 'https://fonts.googleapis.com/css2?family=' . $import . '&display=swap';
}

function theme_enqueue_google_fonts(): void
{
	$url = theme_google_fonts_url();
	if ($url === '') {
		return;
	}

	wp_enqueue_style('theme-google-fonts', $url, array(), null);
}

add_action('wp_enqueue_scripts', 'theme_enqueue_google_fonts', 5);

add_filter(
	'wp_resource_hints',
	function (array $urls, string $relation_type): array {
		if ($relation_type !== 'preconnect' || theme_google_fonts_url() === '') {
			return $urls;
		}

		$urls[] = 'https://fonts.googleapis.com';
		$urls[] = [
			'href' => 'https://fonts.gstatic.com',
			'crossorigin' => 'anonymous',
		];

		return $urls;
	},
	10,
	2
);

/**
 * Enqueue theme styles and scripts.
 */
function theme_scripts()
{
	$dir = get_template_directory();
	$uri = get_template_directory_uri();

	$style_deps = array();
	if (theme_google_fonts_url() !== '') {
		$style_deps[] = 'theme-google-fonts';
	}

	/**
	 * Swiper styles
	 */
	wp_enqueue_style(
		'swiper',
		$uri . '/assets/libs/swiper/swiper-bundle.min.css',
		[],
		'12.2.0'
	);
	/**
	 * Swiper script
	 */
	wp_enqueue_script(
		'swiper',
		$uri . '/assets/libs/swiper/swiper-bundle.min.js',
		[],
		'12.2.0',
		true
	);

	$normalize_path = $dir . '/assets/css/normalize.css';
	$base_path = $dir . '/assets/css/base.css';
	$blog_prose_path = $dir . '/assets/css/blog-prose.css';
	$global_path = $dir . '/assets/css/global.css';
	$index_path = $dir . '/assets/css/index.css';

	wp_enqueue_style(
		'theme-normalize',
		$uri . '/assets/css/normalize.css',
		$style_deps,
		is_file($normalize_path) ? (string) filemtime($normalize_path) : null
	);

	wp_enqueue_style(
		'theme-base',
		$uri . '/assets/css/base.css',
		array('theme-normalize'),
		is_file($base_path) ? (string) filemtime($base_path) : null
	);

	wp_enqueue_style(
		'theme-blog-prose',
		$uri . '/assets/css/blog-prose.css',
		array('theme-base'),
		is_file($blog_prose_path) ? (string) filemtime($blog_prose_path) : null
	);

	wp_enqueue_style(
		'theme-global',
		$uri . '/assets/css/global.css',
		array('theme-blog-prose'),
		is_file($global_path) ? (string) filemtime($global_path) : null
	);

	wp_enqueue_style(
		'theme-index',
		$uri . '/assets/css/index.css',
		array('theme-global'),
		is_file($index_path) ? (string) filemtime($index_path) : null
	);
}

add_action('wp_enqueue_scripts', 'theme_scripts');

/**
 * Classic editor
 */

/**
 * Enable visual editor
 */
add_filter(
	'user_can_richedit',
	'__return_true'
);

/**
 * TinyMCE configuration
 */
add_filter(
	'tiny_mce_before_init',
	function ($settings) {

		// Press Enter creates a new paragraph <p>
		$settings['forced_root_block'] = 'p';

		$settings['convert_newlines_to_brs'] = false;

		// Shift + Enter creates <br>
		$settings['force_br_newlines'] = false;
		$settings['force_p_newlines']  = true;

		// Clean unnecessary inline markup
		$settings['cleanup_on_startup']     = true;
		$settings['verify_html']            = true;
		$settings['convert_fonts_to_spans'] = true;

		// Keep clean HTML structure
		$settings['remove_linebreaks']  = false;
		$settings['fix_list_elements']  = true;
		$settings['fix_table_elements'] = true;

		// Remove extra attributes from paragraph tags
		$settings['forced_root_block_attrs'] = '';

		// Main toolbar buttons
		$settings['toolbar1'] =
			'formatselect,bold,italic,link,unlink,bullist,numlist,blockquote,undo,redo';

		// Secondary toolbar buttons
		$settings['toolbar2'] =
			'removeformat,pastetext,charmap,outdent,indent';

		// Available block formats
		$settings['block_formats'] =
			'Paragraph=p; Heading 2=h2; Heading 3=h3; Heading 4=h4';

		return $settings;
	}
);

add_filter(

	'content_save_pre',
	function ($content) {
		return wpautop($content);
	}

);

/**
 * Always show second toolbar row
 */
add_filter(

	'wp_editor_settings',
	function ($settings) {
		if (empty($settings['tinymce']) || ! is_array($settings['tinymce'])) {
			$settings['tinymce'] = [];
		}
		$settings['tinymce']['wordpress_adv_hidden'] = false;

		return $settings;
	}

);
