<?php
/**
 * Collection of theme functions.
 *
 * @package Masonry_Blog
 */

/**
 * Funtion To Get Google Fonts
 */
if ( ! function_exists( 'masonry_blog_google_fonts_url' ) ) {
	/**
	 * Return Font's URL.
	 *
	 * @since 1.0.0
	 * @return string Fonts URL.
	 */
	function masonry_blog_google_fonts_url() {

		$fonts_url = '';
		$fonts = array();
		$subsets = 'latin,latin-ext';

		$site_title_font_family = masonry_blog_get_option( 'masonry_blog_site_title_font_family' );

		if ( ! masonry_blog_is_standard_fonts( $site_title_font_family ) ) {

			$fonts[] = $site_title_font_family;
		}

		$headings_font_family = masonry_blog_get_option( 'masonry_blog_headings_font_family' );

		if ( ! masonry_blog_is_standard_fonts( $headings_font_family ) ) {

			$fonts[] = $headings_font_family;
		}

		$body_font_family = masonry_blog_get_option( 'masonry_blog_body_font_family' );

		if ( ! masonry_blog_is_standard_fonts( $body_font_family ) ) {

			$fonts[] = $body_font_family;
		}

		$fonts = array_unique( $fonts );

		foreach ( $fonts as $f) {

			$f_family = explode(':', $f);

			$f_family = str_replace('+', ' ', $f_family);

			$font_family = ( ! empty( $f_family[1]) ) ? $f_family[1] : '';

			$fonts[] = $f_family[0].':'.$font_family;

		}

		if ( $fonts ) {
			$fonts_url = add_query_arg( array(
				'family' => rawurlencode( implode( '|', $fonts ) ),
				'subset' => rawurlencode( $subsets ),
			), 'https://fonts.googleapis.com/css' );
		}

		return $fonts_url;
	}
}


if ( ! function_exists( 'masonry_blog_navigation_fallback' ) ) {
	/**
	 * Fallback callback for primary navigation menu.
	 */
	function masonry_blog_navigation_fallback() {
		?>
		<ul class="primary-menu">
			<?php
			wp_list_pages(
				array(
					'title_li' => '',
					'depth'    => 4,
				)
			);
			?>
		</ul><!-- .primary-menu -->
		<?php
	}
}



if ( ! function_exists( 'masonry_blog_excerpt_length' ) ) {
	/**
	 * Filters For Excerpt Length.
	 *
	 * @param int $length Excerpt length.
	 */
	function masonry_blog_excerpt_length( $length ) {

		if ( is_admin() ) {

			return $length;
		}

		$excerpt_length = masonry_blog_get_option( 'masonry_blog_excerpt_length' );

		if ( absint( $excerpt_length ) > 0 ) {

			$excerpt_length = absint( $excerpt_length );
		}

		return $excerpt_length;
	}
}
add_filter( 'excerpt_length', 'masonry_blog_excerpt_length' );



if ( ! function_exists( 'masonry_blog_excerpt_more' ) ) {
	/**
	 * Filter For Excerpt More.
	 *
	 * @param string $more The string shown within the more link.
	 */
	function masonry_blog_excerpt_more( $more ) {

		return '...';
	}
}
add_filter( 'excerpt_more', 'masonry_blog_excerpt_more' );



if ( ! function_exists( 'masonry_blog_main_query_filter' ) ) {
	/**
	 * Filter For Main Query.
	 *
	 * @param object $query Query instance.
	 */
	function masonry_blog_main_query_filter( $query ) {

		if ( is_admin() ) {

			return $query;
		}

		if (
			$query->is_search &&
			masonry_blog_get_option( 'masonry_blog_search_exclude_page_from_result' )
		) {

			$query->set( 'post_type', 'post' );
		}

		return $query;
	}
}
add_filter( 'pre_get_posts', 'masonry_blog_main_query_filter' );


if ( ! function_exists( 'masonry_blog_search_form' ) ) {
	/**
	 * Search form of the theme.
	 *
	 * @since 1.0.0
	 *
	 * @param string $form Search form HTML.
	 */
	function masonry_blog_search_form( $form ) {

		$form = '<form role="search" method="get" class="search-form" action="' . esc_url( home_url( '/' ) ) . '"><label class="screen-reader-text" for="s">' . esc_html__( 'Search for:', 'masonry-blog' ) . '</label><input type="search" name="s" placeholder="' . esc_html_x( 'Type here to search', 'placeholder', 'masonry-blog' ) . '" value="' . get_search_query() . '"><button type="submit"><i class="fa fa-search" aria-hidden="true"></i></button></form>';

		return $form;
	}
}
add_filter( 'get_search_form', 'masonry_blog_search_form', 10 );


if ( ! function_exists( 'masonry_blog_nav_description' ) ) {
	/**
	 * Adds menu description in primary menu.
	 *
	 * @since 1.0.0
	 *
	 * @param string $item_output The menu item's starting HTML output.
	 * @param object $item Menu item data object.
	 * @param int    $depth Depth of menu item. Used for padding.
	 * @param object $args An object of 'wp_nav_menu()' arguments.
	 */
	function masonry_blog_nav_description( $item_output, $item, $depth, $args ) {

		if ( ! empty( $item->description ) ) {

			$item_output = str_replace( $args->link_after . '</a>', '<span class="menu-item-description">' . $item->description . '</span>' . $args->link_after . '</a>', $item_output );
		}

		return $item_output;
	}
}
add_filter( 'walker_nav_menu_start_el', 'masonry_blog_nav_description', 10, 4 );


if ( ! function_exists( 'masonry_blog_sidebar_position' ) ) {
	/**
	 * Returns sidebar position.
	 *
	 * @since 1.0.0
	 */
	function masonry_blog_sidebar_position() {

		if ( ! is_active_sidebar( 'sidebar-1' ) ) {

			return 'none';
		}

		if ( masonry_blog_get_option( 'masonry_blog_enable_global_sidebar_position' ) ) {

			return masonry_blog_get_option( 'masonry_blog_global_sidebar_position' );
		} else {

			if ( is_front_page() && is_home() ) {

				return masonry_blog_get_option( 'masonry_blog_blog_sidebar_position' );

			} elseif ( is_home() && ! is_front_page() ) {

				return masonry_blog_get_option( 'masonry_blog_blog_sidebar_position' );

			} else {

				if ( is_front_page() && ! is_home() ) {

					return masonry_blog_get_option( 'masonry_blog_display_static_page_sidebar_position' );
				}
			}

			if ( is_archive() ) {

				return masonry_blog_get_option( 'masonry_blog_archive_sidebar_position' );
			}

			if ( is_search() ) {

				return masonry_blog_get_option( 'masonry_blog_search_sidebar_position' );
			}

			if ( is_single() ) {

				if ( masonry_blog_get_option( 'masonry_blog_enable_common_post_sidebar_position' ) ) {

					return masonry_blog_get_option( 'masonry_blog_common_post_sidebar_position' );
				} else {

					$sidebar_position = get_post_meta( get_the_ID(), 'masonry_blog_sidebar_position', true );

					if ( ! empty( $sidebar_position ) ) {

						return $sidebar_position;
					} else {

						return 'right';
					}
				}
			}

			if ( is_page() ) {

				if ( masonry_blog_get_option( 'masonry_blog_enable_common_page_sidebar_position' ) ) {

					return masonry_blog_get_option( 'masonry_blog_common_page_sidebar_position' );
				} else {

					$sidebar_position = get_post_meta( get_the_ID(), 'masonry_blog_sidebar_position', true );

					if ( ! empty( $sidebar_position ) ) {

						return $sidebar_position;
					} else {

						return 'right';
					}
				}
			}
		}
	}
}


if ( ! function_exists( 'masonry_blog_related_posts_query' ) ) {
	/**
	 * Queries posts for related posts.
	 *
	 * @since 1.0.0
	 *
	 * @return array
	 */
	function masonry_blog_related_posts_query() {

		$related_posts_no = masonry_blog_get_option( 'masonry_blog_related_posts_no' );

		$related_posts_query_args = array(
			'no_found_rows'       => true,
			'ignore_sticky_posts' => true,
		);

		if ( absint( $related_posts_no ) > 0 ) {

			$related_posts_query_args['posts_per_page'] = absint( $related_posts_no );
		} else {

			$related_posts_query_args['posts_per_page'] = 4;
		}

		$current_object = get_queried_object();

		if ( $current_object instanceof WP_Post ) {

			$current_id = $current_object->ID;

			if ( absint( $current_id ) > 0 ) {

				// Exclude current post.
				$related_posts_query_args['post__not_in'] = array( absint( $current_id ) );

				// Include current posts categories.
				$categories = wp_get_post_categories( $current_id );

				if ( ! empty( $categories ) ) {

					$related_posts_query_args['tax_query'] = array( // phpcs:ignore
						array(
							'taxonomy' => 'category',
							'field'    => 'term_id',
							'terms'    => $categories,
							'operator' => 'IN',
						),
					);
				}
			}
		}

		$related_posts_query = new WP_Query( $related_posts_query_args );

		return $related_posts_query;
	}
}


if ( ! function_exists( 'masonry_blog_banner_posts_query' ) ) {
	/**
	 * Queries posts for banner/slider.
	 *
	 * @since 1.0.0
	 *
	 * @return array
	 */
	function masonry_blog_banner_posts_query() {

		$banner_post_category = masonry_blog_get_option( 'masonry_blog_banner_category' );

		$no_of_banner_items = masonry_blog_get_option( 'masonry_blog_banner_item_no' );

		$banner_query_args = array(
			'post_type'           => 'post',
			'ignore_sticky_posts' => true,
			'posts_per_page'      => $no_of_banner_items,
		);

		if ( ! empty( $banner_post_category ) ) {

			$banner_query_args['category_name'] = $banner_post_category;
		}

		$banner_query = new WP_Query( $banner_query_args );

		return $banner_query;
	}
}


if ( ! function_exists( 'masonry_blog_comments_callback' ) ) {
	/**
	 * Queries posts for banner/slider.
	 *
	 * @since 1.0.0
	 *
	 * @param object $comment Comment object instance.
	 * @param array  $args Comment args.
	 * @param int    $depth Comments depth.
	 */
	function masonry_blog_comments_callback( $comment, $args, $depth ) {

		if ( 'div' === $args['style'] ) {

			$tag       = 'div';
			$add_below = 'comment';
		} else {
			$tag       = 'li';
			$add_below = 'div-comment';
		}
		?>
		<<?php echo $tag; // phpcs:ignore ?>
			<?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>
			id="comment-<?php comment_ID(); ?>"
		>
		<?php
		if ( 'div' !== $args['style'] ) {
			?>
			<div id="div-comment-<?php comment_ID(); ?>" class="comment-body">
			<?php
		}
		?>
		<div class="comment-author vcard">
			<div class="comment-author-avatar">
				<?php
				if ( 0 !== $args['avatar_size'] ) {
					?>
					<figure class="avatar-container">
						<img
							class="avatar"
							alt="<?php echo esc_attr( get_comment_author() ); ?>"
							src="<?php echo esc_url( get_avatar_url( $comment->user_id, array( 'size' => '75' ) ) ); ?>"
							height="75"
							width="75"
						>
					</figure>
					<?php
				}
				?>
			</div>
			<div class="comment-author-content">
				<div class="comment-author-info">
					<?php
					printf(
						/* translators: %s: author link */
						__( '<cite class="fn">%s</cite> <span class="says">says:</span>', 'masonry-blog' ), // phpcs:ignore
						wp_kses_post( get_comment_author_link() )
					);
					?>
					<div class="comment-meta commentmetadata">
						<a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
							<?php
							printf(
								/* translators: 1: date, 2: time */
								esc_html__( '%1$s at %2$s', 'masonry-blog' ), // phpcs:ignore
								get_comment_date(), // phpcs:ignore
								get_comment_time() // phpcs:ignore
							);
							?>
						</a>
						<?php edit_comment_link( esc_html__( '(Edit)', 'masonry-blog' ), '', '' ); ?>
					</div>
				</div>
				<?php
				if ( '0' === $comment->comment_approved ) {
					?>
					<em class="comment-awaiting-moderation"><?php esc_html_e( 'Your comment is awaiting moderation.', 'masonry-blog' ); ?></em><br/>
					<?php
				}
				?>
				<div class="comment-author-comment">
					<?php comment_text(); ?>
				</div>
				<div class="reply">
					<?php
					comment_reply_link(
						array_merge(
							$args,
							array(
								'add_below' => $add_below,
								'depth'     => $depth,
								'max_depth' => $args['max_depth'],
							)
						)
					);
					?>
				</div>
			</div>
		</div>
		<?php
		if ( 'div' !== $args['style'] ) {
			?>
			</div>
			<?php
		}
		?>
		</<?php echo $tag; // phpcs:ignore ?>>
		<?php
	}
}

if ( ! function_exists( 'masonry_blog_excerpt' ) ) {
	/**
	 * Get post content, trim it, and print the trimmed content.
	 */
	function masonry_blog_excerpt() {

		$content = apply_filters( 'the_content', get_the_content() );

		$excerpt_length = masonry_blog_get_option( 'masonry_blog_excerpt_length' );
		?>
		<p><?php echo wp_kses_post( wp_trim_words( $content, $excerpt_length, '...' ) ); ?></p>
		<?php
	}
}



if ( ! function_exists( 'masonry_blog_thumbnail_alt_text' ) ) {
	/**
	 * Function to get post thumbnail alt text value.
	 */
	function masonry_blog_thumbnail_alt_text() {

		$post_thumbnail_id = get_post_thumbnail_id( get_the_ID() );

		$alt_text = '';

		if ( ! empty( $post_thumbnail_id ) ) {

			$alt_text = get_post_meta( $post_thumbnail_id, '_wp_attachment_image_alt', true );
		}

		if ( ! empty( $alt_text ) ) {

			echo esc_attr( $alt_text );
		} else {

			the_title_attribute();
		}
	}
}


if ( ! function_exists( 'masonry_blog_alignment_class' ) ) {
	/**
	 * Returns CSS class according to the alignment selected.
	 *
	 * @param string $align Alignment.
	 */
	function masonry_blog_alignment_class( $align ) {

		switch ( $align ) {
			case 'left':
				return 'left-aligned';
			case 'right':
				return 'right-aligned';
			default:
				return 'center-aligned';
		}
	}
}

if ( ! function_exists( 'masonry_blog_ajax_posts_loading' ) ) {
	/**
	 * Checks if posts are loaded using AJAX.
	 */
	function masonry_blog_ajax_posts_loading() {

		if (
			masonry_blog_get_option( 'masonry_blog_pagination' ) === 'infinite_scroll' ||
			masonry_blog_get_option( 'masonry_blog_pagination' ) === 'button_click'
		) {

			return true;
		}
	}
}


if ( ! function_exists( 'masonry_blog_display_carousel' ) ) {
	/**
	 * Displays banner on blog and front page.
	 */
	function masonry_blog_display_carousel() {

		if ( ! masonry_blog_get_option( 'masonry_blog_display_banner' ) ) {

			return false;
		}

		$display_page = masonry_blog_get_option( 'masonry_blog_display_banner_on' );

		switch ( $display_page ) {

			case 'blog_page':
				if ( is_home() && is_front_page() ) {
					return true;
				} else {
					if ( is_home() && ! is_front_page() ) {
						return true;
					}
				}
				break;
			case 'front_page':
				if ( ! is_home() && is_front_page() ) {
					return true;
				}
				break;
			case 'blog_n_front':
				if ( is_home() || is_front_page() ) {
					return true;
				}
				break;
			default:
				return false;
		}

		return false;
	}
}
