<?php

/**
 * nextpage functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package nextpage
 */

/**
 * Define Const for theme Dir
 * @since 1.0.0
 * */
define('NEXTPAGE_ROOT_PATH', get_template_directory());
define('NEXTPAGE_ROOT_URL', get_template_directory_uri());
define('NEXTPAGE_CSS', NEXTPAGE_ROOT_URL . '/assets/css');
define('NEXTPAGE_JS', NEXTPAGE_ROOT_URL . '/assets/js');
define('NEXTPAGE_INC', NEXTPAGE_ROOT_PATH . '/inc');
define('NEXTPAGE_THEME_OPTIONS', NEXTPAGE_INC . '/theme-options');
define('NEXTPAGE_THEME_STYLESHEETS', NEXTPAGE_INC . '/theme-stylesheets');
define('NEXTPAGE_THEME_OPTIONS_IMG', NEXTPAGE_ROOT_URL . '/inc/theme-options/img');

/**
 * define theme info
 * @since 1.0.0
 * */
if (is_child_theme()) {
	$nextpage_theme        = wp_get_theme();
	$nextpage_parent_theme = $nextpage_theme->Template;
	$nextpage_theme_info   = wp_get_theme($nextpage_parent_theme);
} else {
	$nextpage_theme_info = wp_get_theme();
}
define('NEXTPAGE_DEV_MODE', false);
$nextpage_version = NEXTPAGE_DEV_MODE ? time() : $nextpage_theme_info->get('Version');
define('NEXTPAGE_NAME', $nextpage_theme_info->get('Name'));
define('NEXTPAGE_VERSION', $nextpage_version);
define('NEXTPAGE_AUTHOR', $nextpage_theme_info->get('Author'));
define('NEXTPAGE_AUTHOR_URI', $nextpage_theme_info->get('AuthorURI'));


/*
 * include theme helper function
 * @since 1.0.0
 * */
if (file_exists(NEXTPAGE_INC . '/template-functions.php') && NEXTPAGE_INC . '/template-tags.php') {
	require_once NEXTPAGE_INC . '/template-functions.php';
	require_once NEXTPAGE_INC . '/template-tags.php';

	function Nextpage_Function($instance)
	{
		$new_instance = false;
		switch ($instance) {
			case ("Functions"):
				$new_instance = class_exists('Nextpage_Functions') ? Nextpage_Functions::getInstance() : false;
				break;
			case ("Tags"):
				$new_instance = class_exists('Nextpage_Tags') ? Nextpage_Tags::getInstance() : false;
				break;
			default:
				$new_instance = false;
				break;
		}

		return $new_instance;
	}
}
/*
* Include codester helper functions
* @since 1.0.0
*/
if (file_exists(NEXTPAGE_INC . '/get-theme-options.php')) {
	require_once NEXTPAGE_INC . '/get-theme-options.php';
}

/*
* Include theme init file
* @since 1.0.0
*/
if (file_exists(NEXTPAGE_INC . '/class-nextpage-init.php')) {
	require_once NEXTPAGE_INC . '/class-nextpage-init.php';
}


function nextpage_move_comment_field_to_bottom($fields)
{
	$comment_field   = $fields['comment'];
	$comment_cookies = $fields['cookies'];
	unset($fields['comment']);
	unset($fields['cookies']);
	$fields['comment'] = $comment_field;
	$fields['cookies'] = $comment_cookies;

	return $fields;
}

add_filter('comment_form_fields', 'nextpage_move_comment_field_to_bottom', 99, 1);

/**
 * Prompt the user to install/activate the Nextpage Helper plugin, which powers
 * the one-click site setup. Once the plugin is active it shows its own setup
 * notice, so this one only appears while the plugin is missing/inactive.
 * @since 1.0.0
 */
function nextpage_helper_required_notice()
{
	if (! current_user_can('activate_plugins')) {
		return;
	}
	if (! function_exists('is_plugin_active')) {
		require_once ABSPATH . 'wp-admin/includes/plugin.php';
	}

	$plugin_file = 'nextpage-helper/nextpage-helper.php';
	if (is_plugin_active($plugin_file)) {
		return; // plugin active: it shows its own "Set up my site" notice
	}

	$installed = file_exists(WP_PLUGIN_DIR . '/' . $plugin_file);
	if ($installed) {
		$url   = wp_nonce_url(admin_url('admin-post.php?action=nextpage_activate_helper'), 'nextpage_activate_helper');
		$label = esc_html__('Activate Nextpage Helper & Set Up', 'nextpage');
	} else {
		// Installed straight from our own server (not the WordPress.org directory).
		$url   = wp_nonce_url(admin_url('admin-post.php?action=nextpage_install_helper'), 'nextpage_install_helper');
		$label = esc_html__('Install Nextpage Helper', 'nextpage');
	}

	if (isset($_GET['nextpage_install']) && 'failed' === $_GET['nextpage_install']) {
		echo '<div class="notice notice-error"><p>' . esc_html__('Could not download Nextpage Helper. Please check the download URL or install it manually.', 'nextpage') . '</p></div>';
	}
	?>
	<div class="notice notice-info" style="border-left-color:#2271b1;padding:14px;">
		<h3 style="margin:.2em 0;"><?php esc_html_e('Set up your Nextpage theme', 'nextpage'); ?></h3>
		<p style="font-size:14px;"><?php esc_html_e('Nextpage uses the free Nextpage Helper plugin to build your header, footer and home page automatically.', 'nextpage'); ?></p>
		<p><a href="<?php echo esc_url($url); ?>" class="button button-primary button-hero"><?php echo $label; // phpcs:ignore ?></a></p>
	</div>
	<?php
}
add_action('admin_notices', 'nextpage_helper_required_notice');

/**
 * Activate the bundled Nextpage Helper plugin, then hand off to its one-click
 * setup so the whole flow is a single button for the user.
 * @since 1.0.0
 */
function nextpage_activate_helper_cb()
{
	if (! current_user_can('activate_plugins')) {
		wp_die(esc_html__('You are not allowed to do this.', 'nextpage'));
	}
	check_admin_referer('nextpage_activate_helper');

	$result = activate_plugin('nextpage-helper/nextpage-helper.php');
	if (is_wp_error($result)) {
		wp_safe_redirect(admin_url('plugins.php'));
		exit;
	}

	// Hand off to the plugin's setup (runs on the next request with the plugin loaded).
	wp_safe_redirect(nextpage_helper_run_setup_url());
	exit;
}

/**
 * Build the one-click setup URL with a raw (non HTML-encoded) nonce, safe for a
 * redirect Location header. wp_nonce_url() esc_html's the ampersand, which would
 * corrupt the query string when used in wp_safe_redirect().
 * @since 1.0.0
 */
function nextpage_helper_run_setup_url()
{
	return add_query_arg(
		array(
			'action'   => 'nextpage_helper_run_setup',
			'_wpnonce' => wp_create_nonce('nextpage_helper_run_setup'),
		),
		admin_url('admin-post.php')
	);
}
add_action('admin_post_nextpage_activate_helper', 'nextpage_activate_helper_cb');

/**
 * Download URL of the Nextpage Helper plugin zip on our own server.
 * Override with the `nextpage_helper_plugin_zip` filter if it ever moves.
 */
if (! defined('NEXTPAGE_HELPER_PLUGIN_ZIP')) {
	define('NEXTPAGE_HELPER_PLUGIN_ZIP', 'https://solverwp.com/plugins/nextpage-helper.zip');
}

/**
 * Install the Nextpage Helper plugin from our own server, then activate it and
 * hand off to its one-click setup — the whole thing behind a single button.
 * @since 1.0.0
 */
function nextpage_install_helper_cb()
{
	if (! current_user_can('install_plugins')) {
		wp_die(esc_html__('You are not allowed to do this.', 'nextpage'));
	}
	check_admin_referer('nextpage_install_helper');

	require_once ABSPATH . 'wp-admin/includes/file.php';
	require_once ABSPATH . 'wp-admin/includes/misc.php';
	require_once ABSPATH . 'wp-admin/includes/plugin.php';
	require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';

	$zip_url = apply_filters('nextpage_helper_plugin_zip', NEXTPAGE_HELPER_PLUGIN_ZIP);

	$skin     = new Automatic_Upgrader_Skin();
	$upgrader = new Plugin_Upgrader($skin);
	$result   = $upgrader->install($zip_url);

	$plugin_file = 'nextpage-helper/nextpage-helper.php';

	if (is_wp_error($result) || ! $result || ! file_exists(WP_PLUGIN_DIR . '/' . $plugin_file)) {
		wp_safe_redirect(add_query_arg('nextpage_install', 'failed', admin_url()));
		exit;
	}

	activate_plugin($plugin_file);

	// Hand off to the plugin's one-click setup.
	wp_safe_redirect(nextpage_helper_run_setup_url());
	exit;
}
add_action('admin_post_nextpage_install_helper', 'nextpage_install_helper_cb');
