<?php

/* ==========================================================================
 *  General
 * ========================================================================== */

function realizer_admin_enqueue_scripts( $hook_suffix ) {
	if ( $hook_suffix === 'appearance_page_realizer' ) {
		wp_enqueue_style( 'realizer-admin', esc_url( get_template_directory_uri() ) . '/assets/css/realizer-admin.css' );
	}
}
add_action( 'admin_enqueue_scripts', 'realizer_admin_enqueue_scripts' );

/* ==========================================================================
 *  Admin notices
 * ========================================================================== */

function realizer_admin_notices() {
	$user_id = get_current_user_id();
	if ( ! get_user_meta( $user_id, 'realizer_admin_notices_dismissed' ) ) {
		global $pagenow;
		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}
		if ( $pagenow === 'themes.php' ) {
			$format				= __( 'Thank you for using the REALIZER. Please configure various settings in the %s. We also have a %s that is compatible with the REALIZER, so please make use of it.', 'realizer' );
			$theme_realizer		= '<a href="' . esc_url( admin_url() ) . 'customize.php' . '">' . __( 'Theme settings', 'realizer' ) . '</a>';
			$pattern_library	= '<a href="' . esc_url( admin_url() ) . 'themes.php?page=realizer' . '">' . __( 'WordPress Block Pattern Library', 'realizer' ) . '</a>';
			echo '<div id="realizer-notice" class="notice notice-success"><p>' . sprintf( $format, $theme_realizer, $pattern_library ) . '</p><p><a href="?realizer-admin-notices-dismissed">' . __( 'Permanently dismiss this notice', 'realizer' ) . '</a></p></div>';
		}
	}
}
add_action( 'admin_notices', 'realizer_admin_notices' );

function realizer_admin_notices_dismissed() {
	$user_id = get_current_user_id();
	if ( isset( $_GET['realizer-admin-notices-dismissed'] ) ) {
		add_user_meta( $user_id, 'realizer_admin_notices_dismissed', 'true', true );
	}
}
add_action( 'admin_init', 'realizer_admin_notices_dismissed' );

function realizer_delete_user_meta() {
	$user_id = get_current_user_id();
	delete_user_meta( $user_id, 'realizer_admin_notices_dismissed' );
}
add_action( 'switch_theme', 'realizer_delete_user_meta' );

/* ==========================================================================
 *  Admin menu - submenu page
 * ========================================================================== */

function realizer_admin_menu() {
	add_submenu_page (
		'themes.php',
		__( 'REALIZER', 'realizer' ),
		__( 'REALIZER', 'realizer' ),
		'manage_options',
		'realizer',
		'realizer_contents'
	);
}
add_action( 'admin_menu', 'realizer_admin_menu' );

function realizer_contents() {
	?>
		<div class="wrap">
			<h1><?php echo __( 'REALIZER', 'realizer' ); ?></h1>
			<table class="form-table" role="presentation">
				<tbody>
					<tr>
						<th scope="row"><?php echo __( 'Theme Editor and Customize', 'realizer' ); ?></th>
						<td>
							<p><?php echo __( 'The WordPress theme "REALIZER" allows you to configure various settings in the following locations.', 'realizer' ); ?></p>
							<p class="submit">
								<a href="<?php echo esc_url( admin_url() ) . 'site-editor.php'; ?>" class="button"><?php echo __( 'Editor', 'realizer' ); ?></a>
								<a href="<?php echo esc_url( admin_url() ) . 'customize.php'; ?>" class="button"><?php echo __( 'Customize', 'realizer' ); ?></a>
							</p>
							<div class="rss">
								<p><?php echo __( 'Recently published updates', 'realizer' ); ?></p>
								<ul>
									<?php
										include_once( ABSPATH . WPINC . '/feed.php' );
										$feeduri	= 'https://demo.fuji-production.jp/wordpress/themes/realizer/news/news-category/update/feed/';
										$rss		= fetch_feed( $feeduri );
										if ( ! is_wp_error( $rss ) ) {
											$maxitems	= $rss->get_item_quantity(3);
											$rss_items	= $rss->get_items( 0, $maxitems );
										}
										foreach ( $rss_items as $item ) :
									?>
										<li>
											<span>
												<?php
													$item_date = $item->get_date( get_option( 'date_format' ) );
													echo esc_html( $item_date );
												?>
											</span>
											<span>
												<a href="<?php echo esc_url( $item->get_permalink() ); ?>" target="_blank" rel="noopener noreferrer">
													<?php
														$title = $item->get_title();
														if ( mb_strlen( $title ) > 60 ) {
															echo mb_substr( $title, 0, 60 ) . '...';
														} else {
															echo esc_html( $title );
														}
													?>
												</a>
											</span>
							 			</li>
									<?php endforeach; ?>
								</ul>
							</div>
						</td>
					</tr>
				</tbody>
			</table>
			<hr>
			<table class="form-table" role="presentation">
				<tbody>
					<tr>
						<th scope="row"><?php echo __( 'WordPress Block Pattern Library', 'realizer' ); ?></th>
						<td>
							<p><?php echo __( 'The WordPress Block Pattern Library is an annual subscription service that allows you to recreate clean, well-structured layouts without hesitation. Simply copy and paste the patterns to instantly implement a stable and polished structure.', 'realizer' ); ?></p>
							<p class="submit">
								<a href="<?php echo esc_url( 'https://www.fuji-production.jp/wordpress-block-pattern-library/' ); ?>" class="button" target="_blank"><?php echo __( 'Learn more', 'realizer' ); ?></a>
							</p>
							<div class="wrap-inner rss">
								<div>
									<p><?php echo __( 'Recently Published Block Patterns', 'realizer' ); ?></p>
									<ul>
										<?php
											include_once( ABSPATH . WPINC . '/feed.php' );
											$feeduri	= 'https://www.fuji-production.jp/pattern/feed/';
											$rss		= fetch_feed( $feeduri );
											if ( ! is_wp_error( $rss ) ) {
												$maxitems	= $rss->get_item_quantity(3);
												$rss_items	= $rss->get_items( 0, $maxitems );
											}
											foreach ( $rss_items as $item ) :
										?>
											<li>
												<span>
													<?php
														$item_date = $item->get_date( get_option( 'date_format' ) );
														echo esc_html( $item_date );
													?>
												</span>
												<span>
													<a href="<?php echo esc_url( $item->get_permalink() ); ?>" target="_blank" rel="noopener noreferrer">
														<?php
															$title = $item->get_title();
															if ( mb_strlen( $title ) > 60 ) {
																echo mb_substr( $title, 0, 60 ) . '...';
															} else {
																echo esc_html( $title );
															}
														?>
													</a>
												</span>
											</li>
										<?php endforeach; ?>
									</ul>
								</div>
								<div>
									<?php
										$locale = get_locale();
										if ( $locale === 'ja' ) {
											$wbpl_banner = '/assets/images/wordpress_block_pattern_library_banner_ja_600x500.png';
										} else {
											$wbpl_banner = '/assets/images/wordpress_block_pattern_library_banner_en-US_600x500.png';
										}
									?>
									<p><a href="<?php echo esc_url( 'https://www.fuji-production.jp/wordpress-block-pattern-library/' )?>" target="_blank"><img src="<?php  echo esc_url( get_template_directory_uri() . $wbpl_banner ); ?>"></a></p>
								</div>
							</div>
						</td>
					</tr>
				</tbody>
			</table>
			<hr>
			<table class="form-table" role="presentation">
				<tbody>
					<tr>
						<th scope="row"><?php echo __( 'BLOGS | Useful information for WordPress and web development', 'realizer' ); ?></th>
						<td>
							<p><?php echo __( 'We provide expert commentary on useful information for website creation, WordPress, and web marketing.<br>Easy to understand even for beginners, we have compiled content that can be used in practice, from basic knowledge to creation methods, costs, and SEO strategies.', 'realizer' ); ?></p>
							<p class="submit">
								<a href="<?php echo esc_url( 'https://www.fuji-production.jp/blogs/' ); ?>" class="button" target="_blank"><?php echo __( 'Learn more', 'realizer' ); ?></a>
							</p>
							<div class="rss">
								<p><?php echo __( 'Recently published articles', 'realizer' ); ?></p>
								<ul>
									<?php
										include_once( ABSPATH . WPINC . '/feed.php' );
										$feeduri	= 'https://www.fuji-production.jp/blogs/feed/';
										$rss		= fetch_feed( $feeduri );
										if ( ! is_wp_error( $rss ) ) {
											$maxitems	= $rss->get_item_quantity(3);
											$rss_items	= $rss->get_items( 0, $maxitems );
										}
										foreach ( $rss_items as $item ) :
									?>
										<li>
											<span>
												<?php
													$item_date = $item->get_date( get_option( 'date_format' ) );
													echo esc_html( $item_date );
												?>
											</span>
											<span>
												<a href="<?php echo esc_url( $item->get_permalink() ); ?>" target="_blank" rel="noopener noreferrer">
													<?php
														$title = $item->get_title();
														if ( mb_strlen( $title ) > 60 ) {
															echo mb_substr( $title, 0, 60 ) . '...';
														} else {
															echo esc_html( $title );
														}
													?>
												</a>
											</span>
										</li>
									<?php endforeach; ?>
								</ul>
							</div>
						</td>
					</tr>
				</tbody>
			</table>
			<hr>
		</div>
	<?php
}