<?php
/**
 * Related Post Features
 *
 * Author: Gian MR - http://www.gianmr.com
 *
 * @since 1.0.0
 * @package Majalahpro Core
 */

/* Exit if accessed directly */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! function_exists( 'majalahpro_related_post' ) ) {
	/**
	 * Adding the related post to the end of your single post
	 *
	 * @since 1.0.0
	 * @return string
	 */
	function majalahpro_related_post() {
		global $post;

		$majpro_relpost = get_option( 'majpro_relpost' );

		if ( isset( $majpro_relpost['relpost_number'] ) && ! empty( $majpro_relpost['relpost_number'] ) ) {
			/* option section default */
			$number = intval( $majpro_relpost['relpost_number'] );
		} else {
			$number = 5;
		}

		if ( isset( $majpro_relpost['relpost_taxonomy'] ) && 'category' === $majpro_relpost['relpost_taxonomy'] ) {
			$categories = get_the_category( $post->ID );
			if ( $categories ) {
				$category_ids = array();
				foreach ( $categories as $individual_category ) {
					$category_ids[] = $individual_category->term_id;
					$args           = array(
						'category__in'           => $category_ids,
						'post__not_in'           => array( $post->ID ),
						'posts_per_page'         => $number,
						// start improve performance.
						'no_found_rows'          => true,
						'update_post_term_cache' => false,
						'update_post_meta_cache' => false,
						'cache_results'          => false,
						// End improve performance.
						'ignore_sticky_posts'    => 1,
					);
				}
			}
		} elseif ( isset( $majpro_relpost['relpost_taxonomy'] ) && 'topics' === $majpro_relpost['relpost_taxonomy'] ) {
			$topics = wp_get_object_terms( $post->ID, 'newstopic', array( 'fields' => 'ids' ) );
			if ( $topics ) {
				$args = array(
					'post__not_in'           => array( $post->ID ),
					'posts_per_page'         => $number,
					'tax_query'              => array(
						array(
							'taxonomy' => 'newstopic',
							'field'    => 'id',
							'terms'    => $topics,
						),
					),
					// start improve performance.
					'no_found_rows'          => true,
					'update_post_term_cache' => false,
					'update_post_meta_cache' => false,
					'cache_results'          => false,
					// End improve performance.
					'ignore_sticky_posts'    => 1,
				);
			}
		} else {
			$tags = wp_get_post_tags( $post->ID );
			if ( $tags ) {
				$tag_ids = array();
				foreach ( $tags as $individual_tag ) {
					$tag_ids[] = $individual_tag->term_id;
					$args      = array(
						'tag__in'                => $tag_ids,
						'post__not_in'           => array( $post->ID ),
						'posts_per_page'         => $number,
						// start improve performance.
						'no_found_rows'          => true,
						'update_post_term_cache' => false,
						'update_post_meta_cache' => false,
						'cache_results'          => false,
						// End improve performance.
						'ignore_sticky_posts'    => 1,
					);
				}
			}
		}

		if ( ! isset( $args ) ) {
			$args = '';
		}

		$majalahpro_query = new WP_Query( $args );

		$content = '';
		if ( $majalahpro_query->have_posts() ) {
			if ( isset( $majpro_relpost['relpost_text'] ) && ! empty( $majpro_relpost['relpost_text'] ) ) {
				/* option section default */
				$titletext = esc_attr( $majpro_relpost['relpost_text'] );
			} else {
				$titletext = __( 'Related Posts', 'majalahpro-core' );
			}

			$content .= '<div class="related-title-first"><span>' . $titletext . '</span></div>';

			$content .= '<div class="majalahpro-core-first-related-post gmr-single gmr-list-related">';

			$content .= '<ul>';

			while ( $majalahpro_query->have_posts() ) :
				$majalahpro_query->the_post();

				$categories_list = get_the_category_list( esc_html__( ', ', 'majalahpro-core' ) );

				$content .= '<li>';
				$content .= '<div class="majalahpro-core-related-title">';
				$content .= '<a href="' . get_permalink() . '" itemprop="url" title="' . the_title_attribute(
					array(
						'before' => __( 'Permalink to: ', 'majalahpro-core' ),
						'after'  => '',
						'echo'   => false,
					)
				) . '" rel="bookmark">' . get_the_title() . '</a>';
				$content .= '</div>';
				$content .= '</li>';

			endwhile;
			wp_reset_postdata();
			$content .= '</ul>';

			$content .= '</div>';
		} // if have posts

		return $content;
	}
}

if ( ! function_exists( 'majalahpro_core_related_post' ) ) :
	/**
	 * Returning Related Posts
	 */
	function majalahpro_core_related_post() {
		$related = majalahpro_related_post();
		return $related;
	}
endif; // endif majalahpro_core_related_post.

if ( ! function_exists( 'majalahpro_core_add_related_the_content' ) ) :
	/**
	 * Insert content after box content single
	 *
	 * @since 1.0.0
	 * @link https://jetpack.com/support/related-posts/customize-related-posts/#delete
	 * @return void
	 */
	function majalahpro_core_add_related_the_content() {
		if ( is_singular( array( 'post' ) ) && in_the_loop() ) {
			$majpro_relpost = get_option( 'majpro_relpost' );
			if ( isset( $majpro_relpost['enable_relpost'] ) && ! empty( $majpro_relpost['enable_relpost'] ) ) {
				/* option section default */
				$option = $majpro_relpost['enable_relpost'];
			} else {
				$option = 'on';
			}

			if ( 'on' === $option ) :
				echo majalahpro_core_related_post();  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			endif;
		}
	}
endif; // endif majalahpro_core_add_related_the_content.
add_action( 'majalahpro_core_add_related_the_content', 'majalahpro_core_add_related_the_content', 40 );

if ( ! function_exists( 'majalahpro_related_post_second' ) ) {
	/**
	 * Adding the related post to the end of single post in box before menu
	 *
	 * @since 1.0.0
	 * @return string
	 */
	function majalahpro_related_post_second() {
		global $post;

		$majpro_relpost = get_option( 'majpro_relpost' );

		if ( isset( $majpro_relpost['relpost_number_2'] ) && ! empty( $majpro_relpost['relpost_number_2'] ) ) {
			/* option section, default */
			$number = intval( $majpro_relpost['relpost_number_2'] );
		} else {
			$number = 5;
		}

		if ( isset( $majpro_relpost['relpost_taxonomy_2'] ) && 'tags' === $majpro_relpost['relpost_taxonomy_2'] ) {
			$tags = wp_get_post_tags( $post->ID );
			if ( $tags ) {
				$tag_ids = array();
				foreach ( $tags as $individual_tag ) {
					$tag_ids[] = $individual_tag->term_id;
					$args      = array(
						'tag__in'                => $tag_ids,
						'post__not_in'           => array( $post->ID ),
						'posts_per_page'         => $number,
						// start improve performance.
						'no_found_rows'          => true,
						'update_post_term_cache' => false,
						'update_post_meta_cache' => false,
						'cache_results'          => false,
						// End improve performance.
						'ignore_sticky_posts'    => 1,
					);
				}
			}
		} elseif ( isset( $majpro_relpost['relpost_taxonomy_2'] ) && 'topics' === $majpro_relpost['relpost_taxonomy_2'] ) {
			$topics = wp_get_object_terms( $post->ID, 'newstopic', array( 'fields' => 'ids' ) );
			if ( $topics ) {
				$args = array(
					'post__not_in'           => array( $post->ID ),
					'posts_per_page'         => $number,
					'tax_query'              => array(
						array(
							'taxonomy' => 'newstopic',
							'field'    => 'id',
							'terms'    => $topics,
						),
					),
					// start improve performance.
					'no_found_rows'          => true,
					'update_post_term_cache' => false,
					'update_post_meta_cache' => false,
					'cache_results'          => false,
					// End improve performance.
					'ignore_sticky_posts'    => 1,
				);
			}
		} else {
			$categories = get_the_category( $post->ID );
			if ( $categories ) {
				$category_ids = array();
				foreach ( $categories as $individual_category ) {
					$category_ids[] = $individual_category->term_id;
					$args           = array(
						'category__in'           => $category_ids,
						'post__not_in'           => array( $post->ID ),
						'posts_per_page'         => $number,
						// start improve performance.
						'no_found_rows'          => true,
						'update_post_term_cache' => false,
						'update_post_meta_cache' => false,
						'cache_results'          => false,
						// End improve performance.
						'ignore_sticky_posts'    => 1,
					);
				}
			}
		}
		if ( ! isset( $args ) ) {
			$args = '';
		}

		$majalahpro_query = new WP_Query( $args );

		$content = '';
		if ( $majalahpro_query->have_posts() ) {
			if ( isset( $majpro_relpost['relpost_text_2'] ) && ! empty( $majpro_relpost['relpost_text_2'] ) ) {
				/* option section default */
				$titletext = esc_attr( $majpro_relpost['relpost_text_2'] );
			} else {
				$titletext = __( 'Don\'t Miss', 'majalahpro-core' );
			}

			$content .= '<h3 class="related-title"><span>' . $titletext . '</span></h3>';

			$content .= '<div class="majalahpro-core-related-post site-main gmr-box-content gmr-single gmr-gallery-related">';

			$content .= '<ul>';

			while ( $majalahpro_query->have_posts() ) :
				$majalahpro_query->the_post();

				$categories_list = get_the_category_list( esc_html__( ', ', 'majalahpro-core' ) );

				$content .= '<li>';
				$featured_image_url = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) );
				if ( ! empty( $featured_image_url ) ) :
					$content .= '<div class="other-content-thumbnail">';
					$content .= '<a href="' . get_permalink() . '" itemprop="url" title="' . the_title_attribute(
						array(
							'before' => __( 'Permalink to: ', 'majalahpro-core' ),
							'after'  => '',
							'echo'   => false,
						)
					) . '" class="image-related" rel="bookmark">';
					$content .= get_the_post_thumbnail( $post->ID, 'medium' );
					$content .= '</a>';
					if ( has_post_format( 'gallery' ) ) {
						$content .= '<span class="icon_image"></span>';
					} elseif ( has_post_format( 'video' ) ) {
						$content .= '<span class="arrow_triangle-right"></span>';
					}
					$content .= '</div>';
				endif;
				$content .= '<div class="majalahpro-core-related-title">';
				$content .= '<a href="' . get_permalink() . '" itemprop="url" title="' . the_title_attribute(
					array(
						'before' => __( 'Permalink to: ', 'majalahpro-core' ),
						'after'  => '',
						'echo'   => false,
					)
				) . '" rel="bookmark">' . get_the_title() . '</a>';
				$content .= '</div>';
				$content .= '</li>';

			endwhile;
			wp_reset_postdata();

			$content .= '</ul>';

			$content .= '</div>';
		} // if have posts

		return $content;
	}
}

if ( ! function_exists( 'majalahpro_core_add_second_related_the_content' ) ) :
	/**
	 * Insert content after box content single
	 *
	 * @since 1.0.0
	 * @link https://jetpack.com/support/related-posts/customize-related-posts/#delete
	 * @return void
	 */
	function majalahpro_core_add_second_related_the_content() {
		if ( is_singular( array( 'post' ) ) && in_the_loop() ) {
			$majpro_relpost = get_option( 'majpro_relpost' );
			if ( isset( $majpro_relpost['enable_relpost_2'] ) && ! empty( $majpro_relpost['enable_relpost_2'] ) ) {
				/* option section default */
				$option = $majpro_relpost['enable_relpost_2'];
			} else {
				$option = 'on';
			}
			if ( 'on' === $option ) :
				echo majalahpro_related_post_second(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			endif;
		}
	}
endif; // endif majalahpro_core_add_second_related_the_content.
add_action( 'majalahpro_core_add_second_related_the_content', 'majalahpro_core_add_second_related_the_content', 40 );

if ( ! function_exists( 'majalahpro_related_post_infinite' ) ) {
	/**
	 * Adding the related post to the end of single post in box before menu
	 *
	 * @since 1.0.0
	 * @return string
	 */
	function majalahpro_related_post_infinite() {
		global $post;
		global $wp_rewrite;
		global $paged;
		global $wp_query;

		$majpro_relpost = get_option( 'majpro_relpost' );

		if ( isset( $majpro_relpost['relpost_number_3'] ) && ! empty( $majpro_relpost['relpost_number_3'] ) ) {
			/* option section default */
			$number = intval( $majpro_relpost['relpost_number_3'] );
		} else {
			$number = 2;
		}

		$paged = isset( $_REQUEST['page28332434234'] ) ? $_REQUEST['page28332434234'] : 1;

		if ( isset( $majpro_relpost['relpost_taxonomy_3'] ) && 'tags' === $majpro_relpost['relpost_taxonomy_3'] ) {
			$tags = wp_get_post_tags( $post->ID );
			if ( $tags ) {
				$tag_ids = array();
				foreach ( $tags as $individual_tag ) {
					$tag_ids[] = $individual_tag->term_id;
					$args      = array(
						'tag__in'                => $tag_ids,
						'post__not_in'           => array( $post->ID ),
						'posts_per_page'         => $number,
						// start improve performance.
						'paged'                  => $paged,
						'update_post_term_cache' => false,
						'update_post_meta_cache' => false,
						'cache_results'          => false,
						// End improve performance.
						'ignore_sticky_posts'    => 1,
					);
				}
			}
		} elseif ( isset( $majpro_relpost['relpost_taxonomy_3'] ) && 'topics' === $majpro_relpost['relpost_taxonomy_3'] ) {
			$topics = wp_get_object_terms( $post->ID, 'newstopic', array( 'fields' => 'ids' ) );
			if ( $topics ) {
				$args = array(
					'post__not_in'           => array( $post->ID ),
					'posts_per_page'         => $number,
					'tax_query'              => array(
						array(
							'taxonomy' => 'newstopic',
							'field'    => 'id',
							'terms'    => $topics,
						),
					),
					// start improve performance.
					'paged'                  => $paged,
					'update_post_term_cache' => false,
					'update_post_meta_cache' => false,
					'cache_results'          => false,
					// End improve performance.
					'ignore_sticky_posts'    => 1,
				);
			}
		} else {
			$categories = get_the_category( $post->ID );
			if ( $categories ) {
				$category_ids = array();
				foreach ( $categories as $individual_category ) {
					$category_ids[] = $individual_category->term_id;
					$args           = array(
						'category__in'           => $category_ids,
						'post__not_in'           => array( $post->ID ),
						'posts_per_page'         => $number,
						// start improve performance.
						'paged'                  => $paged,
						'update_post_term_cache' => false,
						'update_post_meta_cache' => false,
						'cache_results'          => false,
						// End improve performance.
						'ignore_sticky_posts'    => 1,
					);
				}
			}
		}

		if ( ! isset( $args ) ) {
			$args = '';
		}

		$majalahpro_query = new WP_Query( $args );

		$orig_post  = $post;
		$temp_query = $wp_query;
		$wp_query   = NULL;
		$wp_query   = $majalahpro_query;

		$content = '';
		if ( $majalahpro_query->have_posts() ) {

			$content .= '<div class="majalahpro-core-related-post gmr-infinite-selector gmr-related-infinite">';

			$content .= '<div id="gmr-main-load">';

			while ( $majalahpro_query->have_posts() ) :
				$majalahpro_query->the_post();

				$categories_list = get_the_category_list( esc_html__( ', ', 'majalahpro-core' ) );

				$content .= '<div class="item-infinite">';
				$content .= '<div class="item-box clearfix">';

				$featured_image_url = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) );
				if ( ! empty( $featured_image_url ) ) :
					$content .= '<div class="majalahpro-core-related-image pull-right">';
					$content .= '<a href="' . get_permalink() . '" itemprop="url" title="' . the_title_attribute(
						array(
							'before' => __( 'Permalink to: ', 'majalahpro-core' ),
							'after'  => '',
							'echo'   => false,
						)
					) . '" class="image-related" rel="bookmark">';
					$content .= get_the_post_thumbnail( $post->ID, 'medium' );
					$content .= '</a>';
					if ( has_post_format( 'gallery' ) ) {
						$content .= '<span class="icon_image"></span>';
					} elseif ( has_post_format( 'video' ) ) {
						$content .= '<span class="arrow_triangle-right"></span>';
					}
					$content .= '</div>';
				endif;

				$content .= '<div class="majalahpro-core-related-title">';
				$content .= '<a href="' . get_permalink() . '" itemprop="url" title="' . the_title_attribute(
					array(
						'before' => __( 'Permalink to: ', 'majalahpro-core' ),
						'after'  => '',
						'echo'   => false,
					)
				) . '" rel="bookmark">' . get_the_title() . '</a>';
				$content .= '</div>';
				$content .= '</div>';
				$content .= '</div>';

			endwhile;

			$content .= '</div>';

			$paged_qp = 'page28332434234'; // the query parameter for pagination.
			$base     = remove_query_arg( $paged_qp, get_pagenum_link() ) . '%_%'; // remove current lists query parameter from base, other lists qr will be kept.
			$base     = str_replace( '#038;', '&', $base );

			// if base already have a query parameter, use &.
			if ( strpos( $base, '?' ) ) {
				$format = '&' . $paged_qp . '=%#%';
			} else {
				$format = '?' . $paged_qp . '=%#%';
			}

			$paged = isset( $_REQUEST['page28332434234'] ) ? $_REQUEST['page28332434234'] : 1;

			$navigation = paginate_links(
				apply_filters(
					'gmr_get_pagination_args',
					array(
						'base'      => $base,
						'show_all'  => false,
						'format'    => $format,
						'current'   => $paged,
						'total'     => $majalahpro_query->max_num_pages,
						'prev_text' => __( 'Prev', 'majalahpro' ),
						'next_text' => __( 'Next', 'majalahpro' ),
						'add_args'  => false,
						'type'      => 'list',
					)
				)
			);

			$navigation = str_replace( '<a ', '<a rel="nofollow" ', $navigation );

			$content .= $navigation;

			$content .= '</div>';
		} // if have posts.
		$post = $orig_post;
		// Reset main query object.
		$wp_query = NULL;
		$wp_query = $temp_query;
		wp_reset_postdata(); // reset the query.

		return $content;
	}
}

if ( ! function_exists( 'majalahpro_core_add_related_post_infinite' ) ) :
	/**
	 * Insert content after box content single
	 *
	 * @since 1.0.0
	 * @link https://jetpack.com/support/related-posts/customize-related-posts/#delete
	 * @return void
	 */
	function majalahpro_core_add_related_post_infinite() {
		if ( is_singular( array( 'post' ) ) ) {
			$majpro_relpost = get_option( 'majpro_relpost' );
			if ( isset( $majpro_relpost['enable_relpost_3'] ) && ! empty( $majpro_relpost['enable_relpost_3'] ) ) {
				/* option section default */
				$option = $majpro_relpost['enable_relpost_3'];
			} else {
				$option = 'on';
			}

			if ( 'on' === $option ) :
				echo majalahpro_related_post_infinite(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			endif;
		}
	}
endif; // endif majalahpro_core_add_related_post_infinite.
add_action( 'majalahpro_core_add_related_post_infinite', 'majalahpro_core_add_related_post_infinite', 40 );
