<?php
/**
 * Gallery 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_get_attachment_gallery' ) ) :
	/**
	 * Display Gallery base attachment post
	 *
	 * @param string $orderby Order By.
	 * @param number $numbpost Number Post.
	 * @param string $poststatus  Status Post.
	 * @param bool   $echo Echo or return.
	 * @since 1.0.0
	 * @return string
	 */
	function majalahpro_get_attachment_gallery( $orderby = 'date', $numbpost = -1, $poststatus = 'inherit', $echo = true ) {
		global $post, $attachment;

		$title = get_the_title();

		$orderby = ! empty( $orderby ) ? $orderby : 'date';

		$numbpost = ! empty( $numbpost ) ? $numbpost : -1;

		$postparent = ! empty( $postparent ) ? $postparent : $post->ID;

		$poststatus = ! empty( $poststatus ) ? $poststatus : 'inherit';

		if ( is_attachment() ) :
			$args = array(
				'orderby'        => esc_html( $orderby ),
				'post_type'      => 'attachment',
				'post_status'    => esc_html( $poststatus ),
				'post_parent'    => intval( $post->post_parent ),
				'post_mime_type' => 'image',
				'posts_per_page' => intval( $numbpost ),
			);
		else :
			$args = array(
				'orderby'        => esc_html( $orderby ),
				'post_type'      => 'attachment',
				'post_status'    => esc_html( $poststatus ),
				'post_parent'    => intval( $post->ID ),
				'posts_per_page' => intval( $numbpost ),
				'post_mime_type' => 'image',
				'exclude'        => get_post_thumbnail_id( $post->ID ),
			);
		endif;
		$content = '';
		$images  = get_posts( $args );

		if ( $images ) {
			$content .= '<div class="gmr-gallery"><ul>';

			foreach ( $images as $attach ) {
				$attachment_id  = $attach->ID;
				$img_url        = wp_get_attachment_image_src( $attachment_id, 'medium' );
				$image          = $img_url[0];
				$desc_img       = get_the_title( $attachment_id );
				$attachment_url = get_attachment_link( $attachment_id );

				$content .= '<li>';
				$content .= '<a href="' . $attachment_url . '" title="' . __( 'Image for ', 'majalahpro-core' ) . ' ' . $desc_img . '"><img src="' . $image . '" height="104" width="138" alt="' . $desc_img . '" title="' . $desc_img . '" /></a>';
				$content .= '</li>';
			}
			$content .= '<ul></div>';
		}
		if ( $echo ) {
			echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		} else {
			return $content;
		}
	}
endif; // endif majalahpro_get_attachment_gallery.
add_action( 'majalahpro_get_attachment_gallery', 'majalahpro_get_attachment_gallery', 10, 5 );

if ( ! function_exists( 'majalahpro_display_attachment_gallery' ) ) :
	/**
	 * Insert content after the_content
	 *
	 * @param string $content Post Content.
	 * @since 1.0.0
	 * @return string
	 */
	function majalahpro_display_attachment_gallery( $content ) {
		if ( is_singular( 'post' ) && in_the_loop() ) {

			$majpro_gallery = get_option( 'majpro_gallery' );

			if ( isset( $majpro_gallery['enable_single_gallery'] ) && ! empty( $majpro_gallery['enable_single_gallery'] ) ) {
				/* option section default */
				$option = $majpro_gallery['enable_single_gallery'];
			} else {
				$option = 'on';
			}

			if ( 'on' === $option ) {
				$content = $content . majalahpro_get_attachment_gallery( $orderby = 'date', $numbpost = -1, $poststatus = 'inherit', $echo = true );
			}
		} elseif ( is_attachment() && in_the_loop() ) {

			$majpro_gallery = get_option( 'majpro_gallery' );

			if ( isset( $majpro_gallery['enable_attachment_gallery'] ) && ! empty( $majpro_gallery['enable_attachment_gallery'] ) ) {
				/* option section default */
				$option = $majpro_gallery['enable_attachment_gallery'];
			} else {
				$option = 'on';
			}

			if ( 'on' === $option ) {
				$content = $content . majalahpro_get_attachment_gallery( $orderby = 'date', $numbpost = -1, $poststatus = 'inherit', $echo = true );
			}
		}
		return $content;
	}
endif; // endif majalahpro_display_attachment_gallery.
add_action( 'majalahpro_display_attachment_gallery', 'majalahpro_display_attachment_gallery', 40 );
