<?php
/**
 * pena-lite functions and definitions
 *
 * @package Pena Lite
 */

/**
 * Set the content width in pixels, based on the theme's design and stylesheet.
 *
 * Priority 0 to make it available to lower priority callbacks.
 *
 * @global int $content_width
 */
if ( ! isset( $content_width ) )
	$content_width = 870; /* pixels */

/**
 * Adjusts content_width value for full-width page and grid page.
 */
if ( ! function_exists( 'pena_lite_content_width' ) ) :

function pena_lite_content_width() {
	global $content_width;
	if ( is_page_template( 'page-templates/main-template.php' )
	|| is_page_template( 'page-templates/fullwidth-template.php' ))
		$content_width = 1280;
}
add_action( 'template_redirect', 'pena_lite_content_width' );

endif; // if ! function_exists( 'pena_lite_content_width' )

if ( ! function_exists( 'pena_lite_setup' ) ) :
/**
 * Sets up theme defaults and registers support for various WordPress features.
 *
 * Note that this function is hooked into the after_setup_theme hook, which
 * runs before the init hook. The init hook is too late for some features, such
 * as indicating support for post thumbnails.
 */
function pena_lite_setup() {
	/*
	 * Make theme available for translation.
	 * Translations can be filed in the /languages/ directory.
	 * If you're building a theme based on pena_lite, use a find and replace
	 * to change 'pena lite' to the name of your theme in all the template files
	 */
	load_theme_textdomain( 'pena-lite', get_template_directory() . '/languages' );

	// Add default posts and comments RSS feed links to head.
	add_theme_support( 'automatic-feed-links' );

	// Activate support for featured images
	add_theme_support( 'post-thumbnails' );
	
	// Front Page Child Page thumbnail
	add_image_size( 'pena-lite-front-child-page-thumbnail', 430, 9999 );
	
	// Post Page thumbnail
	add_image_size( 'pena-lite-post-thumbnail', 870, 9999 );
	
	/*
	 * Enable support for custom logo.
	 *
	 *  @since Pena Lite 1.0
	 */
	add_theme_support( 'custom-logo', array(
		'height'      => 9999,
		'width'       => 9999,
		'flex-height' => true,
	) );

	add_filter( 'excerpt_more', 'pena_lite_continue_reading_link' );
	
	/*
	 * Let WordPress manage the document title.
	 * By adding theme support, we declare that this theme does not use a
	 * hard-coded <title> tag in the document head, and expect WordPress to
	 * provide it for us.
	 */
	add_theme_support( 'title-tag' );

	/* Add support for editor styles */
	add_editor_style( array( 'editor-style.css', pena_lite_fonts_url() ) );

	// This theme uses wp_nav_menu() in one location.
	register_nav_menus( array(
		'primary' => esc_html__( 'Primary Menu', 'pena-lite' ),
	) );

	/*
	 * Switch default core markup for search form, comment form, and comments
	 * to output valid HTML5.
	 */
	add_theme_support( 'html5', array(
		'search-form',
		'comment-form',
		'gallery',
		'caption',
	) );

	// Set up the WordPress core custom background feature.
	add_theme_support( 'custom-background', apply_filters( 'pena_lite_custom_background_args', array(
		'default-color' => 'ffffff',
	) ) );
	
	// Adding support for core block visual styles.
	add_theme_support( 'wp-block-styles' );
	
	// Add support for full and wide align images.
	add_theme_support( 'align-wide' );
		
	// Add support for custom color scheme.
	add_theme_support( 'editor-color-palette', array(
			array(
				'name'  => esc_html__( 'Purple', 'pena-lite' ),
				'slug'  => 'purple',
				'color' => '#980560',
			),
			array(
				'name'  => esc_html__( 'Blue', 'pena-lite' ),
				'slug'  => 'blue',
				'color' => '#20c9f3',
			),
	) );
}
endif; // pena_lite_setup
add_action( 'after_setup_theme', 'pena_lite_setup' );

/**
 * Register widget area.
 *
 * @link http://codex.wordpress.org/Function_Reference/register_sidebar
 */
function pena_lite_widgets_init() {
	register_sidebar( array(
		'name'          => esc_html__( 'Sidebar', 'pena-lite' ),
		'id'            => 'sidebar-1',
		'description'   => '',
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );

	register_sidebar( array(
		'name'          => esc_html__( 'Footer Widgets 1', 'pena-lite' ),
		'id'            => 'footer-1',
		'description'   => '',
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );

	register_sidebar( array(
		'name'          => esc_html__( 'Footer Widgets 2', 'pena-lite' ),
		'id'            => 'footer-2',
		'description'   => '',
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );

	register_sidebar( array(
		'name'          => esc_html__( 'Footer Widgets 3', 'pena-lite' ),
		'id'            => 'footer-3',
		'description'   => '',
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );
}
add_action( 'widgets_init', 'pena_lite_widgets_init' );

if ( ! function_exists( 'pena_lite_fonts_url' ) ) :
/**
 * Define Google Fonts
 */
function pena_lite_fonts_url() {
	$fonts_url = '';

	/* Translators: If there are characters in your language that are not
	* supported by Droid, translate this to 'off'. Do not translate
	* into your own language.
	*/
	$droid = esc_html_x( 'on', 'Droid Serif: on or off', 'pena-lite' );

	/* Translators: If there are characters in your language that are not
	* supported by Pacifico, translate this to 'off'. Do not translate
	* into your own language.
	*/
	$montserrat = esc_html_x( 'on', 'Montserrat font: on or off', 'pena-lite' );

	if ( 'off' !== $droid || 'off' !== $montserrat ) {
		$font_families = array();

		if ( 'off' !== $droid ) {
			$font_families[] = 'Merriweather:400,300,300italic,400italic,700,700italic,900,900italic';
		}

		if ( 'off' !== $montserrat ) {
			$font_families[] = 'Montserrat:400,700';
		}

		$query_args = array(
			'family' => urlencode( implode( '|', $font_families ) ),
			'subset' => urlencode( 'latin,latin-ext' ),
			'display' => 'swap',
		);

		$fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
	}

	require_once get_theme_file_path( 'inc/wptt-webfont-loader.php' );

	return wptt_get_webfont_url( esc_url_raw( $fonts_url ) );
}
endif;

/**
 * Enqueue scripts and styles.
 */
function pena_lite_scripts() {
	// Add custom fonts, used in the main stylesheet.
	wp_enqueue_style( 'pena-lite-fonts', pena_lite_fonts_url(), array(), null );

	wp_enqueue_style( 'pena-lite-style', get_stylesheet_uri() );

	wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.2' );
	
	if ( is_home()
	|| is_archive() ) {
		wp_enqueue_script( 'pena-lite-masonry', get_template_directory_uri() . '/js/grid.js', array( 'jquery', 'masonry' ), '', true );
	}
	wp_enqueue_script( 'pena-lite-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20120206', true );

	wp_enqueue_script( 'pena-lite-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20130115', true );

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'pena_lite_scripts' );

/**
 * Enqueue theme styles within Gutenberg.
 */
function pena_lite_gutenberg_styles() {

	// Load the theme styles within Gutenberg.
	wp_enqueue_style( 'pena-lite-gutenberg', get_theme_file_uri( '/editor.css' ), false, '1.1.8', 'all' );

	// Add custom fonts to Gutenberg.
	wp_enqueue_style( 'pena-lite-fonts', pena_lite_fonts_url(), array(), null );
}
add_action( 'enqueue_block_editor_assets', 'pena_lite_gutenberg_styles' );

if (!function_exists('pena_lite_admin_scripts')) {
	function pena_lite_admin_scripts($hook) {
		if ('appearance_page_charity' === $hook) {
			wp_enqueue_style('pena-lite-admin', get_template_directory_uri() . '/admin/admin.css');
		}
	}
}
add_action('admin_enqueue_scripts', 'pena_lite_admin_scripts');

/*
 * Filters the Categories archive widget to add a span around the post count
 */

function pena_lite_cat_count_span( $links ) {
	$links = str_replace( '</a> (', '</a><span class="post-count">(', $links );
	$links = str_replace( ')', ')</span>', $links );
	return $links;
}
add_filter( 'wp_list_categories', 'pena_lite_cat_count_span' );

/*
 * Add a span around the post count in the Archives widget
 */

function pena_lite_archive_count_span( $links ) {
  $links = str_replace( '</a>&nbsp;(', '</a><span class="post-count">(', $links );
  $links = str_replace( ')', ')</span>', $links );
  return $links;
}
add_filter( 'get_archives_link', 'pena_lite_archive_count_span' );

if ( ! function_exists( 'pena_lite_continue_reading_link' ) ) :
/**
 * Returns an ellipsis and "Continue reading" plus off-screen title link for excerpts
 */
function pena_lite_continue_reading_link() {
	return '<a class="more-link" href="'. esc_url( get_permalink() ) . '">' . sprintf( wp_kses_post( __( 'Continue reading <span class="screen-reader-text">%1$s</span> <span class="meta-nav" aria-hidden="true">&rarr;</span>', 'pena-lite' ) ), esc_attr( strip_tags( get_the_title() ) ) ) . '</a>';
}
endif; // pena_lite_continue_reading_link
/**
 * Adds a pretty "Continue Reading" link to custom post excerpts.
 *
 * To override this link in a child theme, remove the filter and add your own
 * function tied to the get_the_excerpt filter hook.
 */
function pena_lite_custom_excerpt_more( $output ) {
	if ( has_excerpt() && ! is_attachment() ) {
		$output .= pena_lite_continue_reading_link();
	}
	return $output;
}
add_filter( 'get_the_excerpt', 'pena_lite_custom_excerpt_more' );
add_filter('the_excerpt', 'do_shortcode');

/*
 * Custom comments display to move Reply link,
 * used in comments.php
 */
function pena_lite_comments( $comment, $args, $depth ) {
?>
		<li id="comment-<?php comment_ID(); ?>" <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>>
			<article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
				<footer class="comment-meta">
					<div class="comment-metadata">
						<span class="comment-author vcard">
							<?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>

							<?php printf( '<b class="fn">%s</b>', get_comment_author_link() ); ?>
						</span>
						<a href="<?php echo esc_url( get_comment_link( $comment->comment_ID, $args ) ); ?>">
							<time datetime="<?php comment_time( 'c' ); ?>">
								<?php printf( '<span class="comment-date">%1$s</span><span class="comment-time screen-reader-text">%2$s</span>', get_comment_date(), get_comment_time() ); ?>
							</time>
						</a>
						<?php
						comment_reply_link( array_merge( $args, array(
							'add_below' => 'div-comment',
							'depth'     => $depth,
							'max_depth' => $args['max_depth'],
							'before'    => '<span class="reply">',
							'after'     => '</span>'
						) ) );
						?>
						<?php edit_comment_link( esc_html__( 'Edit', 'pena-lite' ), '<span class="edit-link">', '</span>' ); ?>

					</div><!-- .comment-metadata -->

					<?php if ( '0' == $comment->comment_approved ) : ?>
					<p class="comment-awaiting-moderation"><?php esc_html_e( 'Your comment is awaiting moderation.', 'pena-lite' ); ?></p>
					<?php endif; ?>
				</footer><!-- .comment-meta -->

				<div class="comment-content">
					<?php comment_text(); ?>
				</div><!-- .comment-content -->

			</article><!-- .comment-body -->
<?php
}
/***** Include Admin *****/

if (is_admin()) {
	require_once('admin/admin.php');
}

/**
 * Implement the Custom Header feature.
 */
require get_template_directory() . '/inc/custom-header.php';
/**
 * Custom template tags for this theme.
 */
require get_template_directory() . '/inc/template-tags.php';

/**
 * Custom functions that act independently of the theme templates.
 */
require get_template_directory() . '/inc/extras.php';

/**
 * Customizer additions.
 */
require get_template_directory() . '/inc/customizer.php';

/**
 * Load Jetpack compatibility file.
 */
require get_template_directory() . '/inc/jetpack.php';
/**
 * Block patterns.
 */
if ( function_exists( 'register_block_pattern' ) ) {
	require get_template_directory() . '/functions/block-patterns.php';
}