<?php
/**
 * Admin functions.
 *
 * @package Radiant Blogger
 */

define('RADIANT_BLOGGER_SUPPORT',__('https://wordpress.org/support/theme/radiant-blogger/','radiant-blogger'));
define('RADIANT_BLOGGER_REVIEW',__('https://wordpress.org/support/theme/radiant-blogger/reviews/#new-post','radiant-blogger'));
define('RADIANT_BLOGGER_BUY_NOW',__('https://www.wpradiant.net/products/blog-wordpress-theme','radiant-blogger'));
define('RADIANT_BLOGGER_DOC_URL',__('https://preview.wpradiant.net/tutorial/radiant-blogger/','radiant-blogger'));
define('RADIANT_BLOGGER_LIVE_DEMO',__('https://preview.wpradiant.net/radiant-blogger/','radiant-blogger'));
define('RADIANT_BLOGGER_PRO_DOC',__('https://preview.wpradiant.net/tutorial/radiant-blogger-pro/','radiant-blogger'));

/**
 * Register admin page.
 *
 * @since 1.0.0
 */

function radiant_blogger_admin_menu_page() {

	$radiant_blogger_theme = wp_get_theme( get_template() );

	add_theme_page(
		$radiant_blogger_theme->display( 'Name' ),
		$radiant_blogger_theme->display( 'Name' ),
		'manage_options',
		'radiant-blogger',
		'radiant_blogger_do_admin_page'
	);

}
add_action( 'admin_menu', 'radiant_blogger_admin_menu_page' );

function radiant_blogger_admin_theme_style() {
	wp_enqueue_style('radiant-blogger-custom-admin-style', esc_url(get_template_directory_uri()) . '/get-started/getstart.css');
	wp_enqueue_script( 'admin-notice-script', get_template_directory_uri() . '/get-started/js/admin-notice-script.js', array( 'jquery' ) );
    wp_localize_script('admin-notice-script', 'example_ajax_obj', array('ajaxurl' => admin_url('admin-ajax.php')));
}
add_action('admin_enqueue_scripts', 'radiant_blogger_admin_theme_style');

/**
 * Render admin page.
 *
 * @since 1.0.0
 */
function radiant_blogger_do_admin_page() {

	$radiant_blogger_theme = wp_get_theme( get_template() );
	?>
	<div class="radiant-blogger-appearence wrap about-wrap">
		<div class="head-btn">
			<div><h1><?php echo $radiant_blogger_theme->display( 'Name' ); ?></h1></div>
			<div class="demo-btn">
				<span>
					<a class="button button-pro" href="<?php echo esc_url( RADIANT_BLOGGER_BUY_NOW ); ?>" target="_blank"><?php esc_html_e( 'Buy Now', 'radiant-blogger' ); ?></a>
				</span>
				<span>
					<a class="button button-demo" href="<?php echo esc_url( RADIANT_BLOGGER_LIVE_DEMO ); ?>" target="_blank"><?php esc_html_e( 'Demo', 'radiant-blogger' ); ?></a>
				</span>
        <span>
					<a class="button btn-bundle" href="<?php echo esc_url( RADIANT_BLOGGER_BUY_BUNDLE ); ?>" target="_blank"><?php esc_html_e( 'Buy Bundle', 'radiant-blogger' ); ?></a>
				</span>
				<span>
					<a class="button button-doc" href="<?php echo esc_url( RADIANT_BLOGGER_PRO_DOC ); ?>" target="_blank"><?php esc_html_e( 'Documentation', 'radiant-blogger' ); ?></a>
				</span>
			</div>
		</div>
		
		<div class="two-col">

			<div class="about-text">
				<?php
					$description_raw = $radiant_blogger_theme->display( 'Description' );
					$main_description = explode( 'Official', $description_raw );
					?>
				<?php echo wp_kses_post( $main_description[0] ); ?>
        <p>
			    <a class="button button-primary" href="<?php echo esc_url( home_url() ); ?>" target="_blank"><?php esc_html_e( 'Visit Site', 'radiant-blogger' ); ?></a>
			    <a class="button button-primary" id="form-button" href="<?php echo esc_url( 'https://forms.gle/RhBK4jcPYRm1z38N6' ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Free Theme Installation Support', 'radiant-blogger' ); ?></a>
		    </p>
			</div><!-- .col -->

			<div class="about-img">
				<a href="<?php echo esc_url( $radiant_blogger_theme->display( 'ThemeURI' ) ); ?>" target="_blank"><img src="<?php echo trailingslashit( get_template_directory_uri() ); ?>screenshot.png" alt="<?php echo esc_attr( $radiant_blogger_theme->display( 'Name' ) ); ?>" /></a>
			</div><!-- .col -->

		</div><!-- .two-col -->

  <nav class="nav-tab-wrapper wp-clearfix" aria-label="<?php esc_attr_e( 'Secondary menu', 'radiant-blogger' ); ?>">
    <a href="<?php echo esc_url( admin_url( add_query_arg( array( 'page' => 'radiant-blogger' ), 'themes.php' ) ) ); ?>" class="nav-tab<?php echo ( isset( $_GET['page'] ) && 'radiant-blogger' === $_GET['page'] && ! isset( $_GET['tab'] ) ) ?' nav-tab-active' : ''; ?>"><?php esc_html_e( 'About', 'radiant-blogger' ); ?></a>

    <a href="<?php echo esc_url( admin_url( add_query_arg( array( 'page' => 'radiant-blogger', 'tab' => 'free_vs_pro' ), 'themes.php' ) ) ); ?>" class="nav-tab<?php echo ( isset( $_GET['tab'] ) && 'free_vs_pro' === $_GET['tab'] ) ?' nav-tab-active' : ''; ?>"><?php esc_html_e( 'Compare free Vs Pro', 'radiant-blogger' ); ?></a>

    <a href="<?php echo esc_url( admin_url( add_query_arg( array( 'page' => 'radiant-blogger', 'tab' => 'changelog' ), 'themes.php' ) ) ); ?>" class="nav-tab<?php echo ( isset( $_GET['tab'] ) && 'changelog' === $_GET['tab'] ) ?' nav-tab-active' : ''; ?>"><?php esc_html_e( 'Changelog', 'radiant-blogger' ); ?></a>
  </nav>

    <?php
      radiant_blogger_main_screen();

      radiant_blogger_changelog_screen();

      radiant_blogger_free_vs_pro();
}
/**
 * Output the main about screen.
 */
function radiant_blogger_main_screen() {
  if ( isset( $_GET['page'] ) && 'radiant-blogger' === $_GET['page'] && ! isset( $_GET['tab'] ) ) {
  ?>
    
<div class="four-col">

	<div class="col">

		<h3><i class="dashicons dashicons-book-alt"></i><?php esc_html_e( 'Free Theme Directives', 'radiant-blogger' ); ?></h3>

		<p>
			<?php esc_html_e( 'This article will walk you through the different phases of setting up and handling your WordPress website.', 'radiant-blogger' ); ?>
		</p>

		<p>
			<a class="button green button-primary" href="<?php echo esc_url( RADIANT_BLOGGER_DOC_URL ); ?>" target="_blank"><?php esc_html_e( 'Free Documentation', 'radiant-blogger' ); ?></a>
		</p>

	</div><!-- .col -->

	<div class="col">

		<h3><i class="dashicons dashicons-admin-customizer"></i><?php esc_html_e( 'Full Site Editing', 'radiant-blogger' ); ?></h3>

		<p>
			<?php esc_html_e( 'We have used Full Site Editing which will help you preview your changes live and fast.', 'radiant-blogger' ); ?>
		</p>

		<p>
			<a class="button button-primary" href="<?php echo esc_url( admin_url( 'site-editor.php' ) ); ?>" ><?php esc_html_e( 'Use Site Editor', 'radiant-blogger' ); ?></a>
		</p>

	</div><!-- .col -->

	<div class="col">

		<h3><i class="dashicons dashicons-book-alt"></i><?php esc_html_e( 'Leave us a review', 'radiant-blogger' ); ?></h3>
		<p>
			<?php esc_html_e( 'We would love to hear your feedback.', 'radiant-blogger' ); ?>
		</p>

		<p>
			<a class="button button-primary" href="<?php echo esc_url( RADIANT_BLOGGER_REVIEW ); ?>" target="_blank"><?php esc_html_e( 'Review', 'radiant-blogger' ); ?></a>
		</p>

	</div><!-- .col -->


	<div class="col">

		<h3><i class="dashicons dashicons-sos"></i><?php esc_html_e( 'Help &amp; Support', 'radiant-blogger' ); ?></h3>

		<p>
			<?php esc_html_e( 'If you have any question/feedback regarding theme, please post in our official support forum.', 'radiant-blogger' ); ?>
		</p>

		<p>
			<a class="button button-primary" href="<?php echo esc_url( RADIANT_BLOGGER_SUPPORT ); ?>" target="_blank"><?php esc_html_e( 'Get Support', 'radiant-blogger' ); ?></a>
		</p>

	</div><!-- .col -->
<div class="col">

		<h3><i class="dashicons dashicons-visibility"></i><?php esc_html_e( 'Live Demo', 'radiant-blogger' ); ?></h3>

		<p>
			<?php esc_html_e( 'Preview the live demo to explore the homepage, inner pages, and overall design flow before setup.', 'radiant-blogger' ); ?>
		</p>

		<p>
			<a class="button button-primary" href="<?php echo esc_url( RADIANT_BLOGGER_LIVE_DEMO ); ?>" target="_blank"><?php esc_html_e( 'View Live Demo', 'radiant-blogger' ); ?></a>
		</p>

	</div><!-- .col -->

	<?php $theme_slug = get_stylesheet(); ?>

	<div class="col">
		<h3><i class="dashicons dashicons-admin-links"></i><?php esc_html_e( 'Quick Link', 'radiant-blogger' ); ?></h3>

		<div class="radiant-blogger-card-body">
			<div class="radiant-blogger-card-btn-grp">
				<a class="button button-hero btn-col"
					href="<?php echo esc_url( admin_url( 'site-editor.php?postType=wp_template_part&postId=' . $theme_slug . '//header&canvas=edit' ) ); ?>"
					target="_blank">
					<?php esc_html_e( 'Edit Header', 'radiant-blogger' ); ?>
				</a>

				<a class="button button-hero btn-col"
					href="<?php echo esc_url( admin_url( 'site-editor.php?postType=wp_template_part&postId=' . $theme_slug . '//footer&canvas=edit' ) ); ?>"
					target="_blank">
					<?php esc_html_e( 'Edit Footer', 'radiant-blogger' ); ?>
				</a>

				<a class="button button-hero btn-col"
					href="<?php echo esc_url( admin_url( 'site-editor.php?postType=wp_template_part&postId=' . $theme_slug . '//sidebar&canvas=edit' ) ); ?>"
					target="_blank">
					<?php esc_html_e( 'Edit Sidebar', 'radiant-blogger' ); ?>
				</a>

				<a class="button button-hero btn-col"
					href="<?php echo esc_url( admin_url( 'site-editor.php?postType=wp_template_part' ) ); ?>"
					target="_blank">
					<?php esc_html_e( 'All Template Parts', 'radiant-blogger' ); ?>
				</a>

				<a class="button button-hero btn-col"
					href="<?php echo esc_url( admin_url( 'site-editor.php?postType=wp_template&postId=' . $theme_slug . '//front-page&canvas=edit' ) ); ?>"
					target="_blank">
					<?php esc_html_e( 'Edit Frontpage', 'radiant-blogger' ); ?>
				</a>

				<a class="button button-hero btn-col"
					href="<?php echo esc_url( admin_url( 'site-editor.php?postType=wp_template&postId=' . $theme_slug . '//archive&canvas=edit' ) ); ?>"
					target="_blank">
					<?php esc_html_e( 'Edit Archive Page', 'radiant-blogger' ); ?>
				</a>
			</div>
		</div>
	</div>
</div>
</div><!-- .four-col -->
  <?php
  }
}

/**
 * Output the changelog screen.
 */
function radiant_blogger_changelog_screen() {
  if ( ! ( isset( $_GET['tab'] ) && 'changelog' === $_GET['tab'] ) ) {
    return;
  }

  $changelog_entries = radiant_blogger_get_changelog_entries();
  ?>
  <div class="wrap about-wrap">
    <p class="about-description"><?php esc_html_e( 'Want to know whats been happening with the latest changes?', 'radiant-blogger' ); ?></p>
    <?php
      if ( false === $changelog_entries ) {
        echo '<p>' . esc_html__( 'Changelog file does not exist or is not readable.', 'radiant-blogger' ) . '</p>';
      } elseif ( empty( $changelog_entries ) ) {
        echo '<p>' . esc_html__( 'No changelog entries found.', 'radiant-blogger' ) . '</p>';
      } else {
        echo radiant_blogger_render_changelog_entries( $changelog_entries );
      }
    ?>
  </div>
  <?php
}

/**
 * Retrieve changelog entries from the README.
 *
 * @return array|false List of versions keyed by heading label, false if file missing.
 */
function radiant_blogger_get_changelog_entries() {
  $readme_file = get_template_directory() . '/README.txt';

  if ( ! file_exists( $readme_file ) || ! is_readable( $readme_file ) ) {
    return false;
  }

  $content = file_get_contents( $readme_file );

  if ( false === $content ) {
    return false;
  }

  $header = '== changelog ==';
  $start  = stripos( $content, $header );

  if ( false === $start ) {
    return [];
  }

  $start += strlen( $header );
  $end    = stripos( $content, "\n==", $start );

  if ( false === $end ) {
    $end = strlen( $content );
  }

  $section = substr( $content, $start, $end - $start );
  $section = trim( str_replace( array( "\r\n", "\r" ), "\n", $section ) );
  $lines   = explode( "\n", $section );
  $entries = [];
  $version = '';

  foreach ( $lines as $line ) {
    $line = trim( $line );
    if ( '' === $line ) {
      continue;
    }

    if ( preg_match( '/^=+\s*(.+?)\s*=*\s*$/', $line, $label ) ) {
      $version = $label[1];
      $entries[ $version ] = [];
      continue;
    }

    if ( '' === $version ) {
      continue;
    }

    if ( preg_match( '/^[\-\*]\s*(.+)$/', $line, $item ) ) {
      $entries[ $version ][] = $item[1];
      continue;
    }

    $entries[ $version ][] = $line;
  }

  return $entries;
}

/**
 * Render changelog HTML from parsed entries.
 *
 * @param array $entries Parsed changelog entries.
 * @return string Safe HTML block.
 */
function radiant_blogger_render_changelog_entries( array $entries ) {
  $output = '<div class="changelog">';

  foreach ( $entries as $version => $items ) {
    $output .= '<article class="changelog-item">';
    $output .= '<h3 class="changelog-version">' . esc_html( $version ) . '</h3>';

    if ( ! empty( $items ) ) {
      $output .= '<ul class="changelog-list">';
      foreach ( $items as $item ) {
        $output .= '<li>' . esc_html( $item ) . '</li>';
      }
      $output .= '</ul>';
    }

    $output .= '</article>';
  }

  $output .= '</div>';

  return $output;
}
/**
 * Import Demo data for theme using catch themes demo import plugin
 */
function radiant_blogger_free_vs_pro() {
  if ( isset( $_GET['tab'] ) && 'free_vs_pro' === $_GET['tab'] ) {
  ?>
    <div class="wrap about-wrap">

      <h3 class="about-description"><?php esc_html_e( 'Compare Free Vs Pro', 'radiant-blogger' ); ?></h3>
      <div class="vs-theme-table">
        <table>
          <thead>
            <tr><th class="head" scope="col"><?php esc_html_e( 'Theme Features', 'radiant-blogger' ); ?></th>
              <th class="head" scope="col"><?php esc_html_e( 'Free Theme', 'radiant-blogger' ); ?></th>
              <th class="head" scope="col"><?php esc_html_e( 'Pro Theme', 'radiant-blogger' ); ?></th>
            </tr>
          </thead>
          <tbody>
            <tr class="odd" scope="row">
              <td headers="features" class="feature"><span><?php esc_html_e( 'Responsive Design', 'radiant-blogger' ); ?></span></td>
              <td><span class="dashicons dashicons-saved"></span></td>
              <td><span class="dashicons dashicons-saved"></span></td>
            </tr>
            <tr class="odd" scope="row">
              <td headers="features" class="feature"><?php esc_html_e( 'Painless Setup', 'radiant-blogger' ); ?></td>
              <td><span class="dashicons dashicons-saved"></span></td>
              <td><span class="dashicons dashicons-saved"></span></td>
            </tr>
            <tr class="odd" scope="row">
              <td headers="features" class="feature"><?php esc_html_e( 'Color Options', 'radiant-blogger' ); ?></td>
              <td><span class="dashicons dashicons-saved"></span></td>
              <td><span class="dashicons dashicons-saved"></span></td>
            </tr>
            <tr class="odd" scope="row">
              <td headers="features" class="feature"><?php esc_html_e( 'Premium site demo', 'radiant-blogger' ); ?></td>
              <td><span class="dashicons dashicons-no-alt"></span></td>
              <td><span class="dashicons dashicons-saved"></span></td>
            </tr>
            <tr class="odd" scope="row">
              <td headers="features" class="feature"><?php esc_html_e( 'Multiple Block Layout', 'radiant-blogger' ); ?></td>
              <td><span class="dashicons dashicons-no-alt"></span></td>
              <td><span class="dashicons dashicons-saved"></span></td>
            </tr>
            <tr class="odd" scope="row">
              <td headers="features" class="feature"><?php esc_html_e( 'Premium Patterns', 'radiant-blogger' ); ?></td>
              <td><span class="dashicons dashicons-no-alt"></span></td>
              <td><span class="dashicons dashicons-saved"></span></td>
            </tr>
            <tr class="odd" scope="row">
              <td headers="features" class="feature"><?php esc_html_e( 'Multiple Fonts', 'radiant-blogger' ); ?></td>
              <td><span class="dashicons dashicons-no-alt"></span></td>
              <td><span class="dashicons dashicons-saved"></span></td>
            </tr>
            <tr class="odd" scope="row">
              <td headers="features" class="feature"><?php esc_html_e( 'Slider Block', 'radiant-blogger' ); ?></td>
              <td><span class="dashicons dashicons-no-alt"></span></td>
              <td><span class="dashicons dashicons-saved"></span></td>
            </tr>
            <tr class="odd" scope="row">
              <td headers="features" class="feature"><?php esc_html_e( 'Post Listing Block', 'radiant-blogger' ); ?></td>
              <td><span class="dashicons dashicons-no-alt"></span></td>
              <td><span class="dashicons dashicons-saved"></span></td>
            </tr>
            <tr class="odd" scope="row">
              <td headers="features" class="feature"><?php esc_html_e( 'WooCommerce Filter Block', 'radiant-blogger' ); ?></td>
              <td><span class="dashicons dashicons-no-alt"></span></td>
              <td><span class="dashicons dashicons-saved"></span></td>
            </tr>
            <tr class="odd" scope="row">
              <td headers="features" class="feature"><?php esc_html_e( 'Gallery Block', 'radiant-blogger' ); ?></td>
              <td><span class="dashicons dashicons-no-alt"></span></td>
              <td><span class="dashicons dashicons-saved"></span></td>
            </tr>
            <tr class="odd" scope="row">
              <td headers="features" class="feature"><?php esc_html_e( 'Post Carousel Block', 'radiant-blogger' ); ?></td>
              <td><span class="dashicons dashicons-no-alt"></span></td>
              <td><span class="dashicons dashicons-saved"></span></td>
            </tr>
            <tr class="odd" scope="row">
              <td class="feature feature--empty"></td>
              <td class="feature feature--empty"></td>
              <td headers="comp-2" class="td-btn-2"><a target="_blank" href="<?php echo esc_url( RADIANT_BLOGGER_BUY_NOW ); ?>" class="sidebar-button single-btn" target="_blank"><?php esc_html_e( 'Buy It Now', 'radiant-blogger' ); ?></a>

              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  <?php
  }
}