<?php
/**
 * UAEL Posts Helper.
 *
 * @package UAEL
 */

namespace UltimateElementor\Classes;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Class UAEL_Posts_Helper.
 */
class UAEL_Posts_Helper {

	/**
	 * Get Post Types.
	 *
	 * @since 1.5.2
	 * @access public
	 */
	public static function get_post_types() {

		$post_types = get_post_types(
			array(
				'public' => true,
			),
			'objects'
		);

		$options = array();

		foreach ( $post_types as $post_type ) {
			$options[ $post_type->name ] = $post_type->label;
		}

		// Deprecated 'Media' post type.
		$key = array_search( 'Media', $options, true );
		if ( 'attachment' === $key ) {
			unset( $options[ $key ] );
		}

		return apply_filters( 'uael_loop_post_types', $options );
	}

	/**
	 * Get Post Taxonomies.
	 *
	 * @since 1.5.2
	 * @param string $post_type Post type.
	 * @access public
	 */
	public static function get_taxonomy( $post_type ) {

		$taxonomies = get_object_taxonomies( $post_type, 'objects' );
		$data       = array();

		foreach ( $taxonomies as $tax_slug => $tax ) {
			if ( ! $tax->public || ! $tax->show_ui ) {
				continue;
			}

			$data[ $tax_slug ] = $tax;
		}

		return apply_filters( 'uael_post_loop_taxonomies', $data, $taxonomies, $post_type );
	}

	/**
	 * Get size information for all currently-registered image sizes.
	 *
	 * @global $_wp_additional_image_sizes
	 * @uses   get_intermediate_image_sizes()
	 * @link   https://codex.wordpress.org/Function_Reference/get_intermediate_image_sizes
	 * @since 1.5.2
	 * @return array $sizes Data for all currently-registered image sizes.
	 */
	public static function get_image_sizes() {

		global $_wp_additional_image_sizes;

		$sizes  = get_intermediate_image_sizes(); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.get_intermediate_image_sizes_get_intermediate_image_sizes
		$result = array();

		foreach ( $sizes as $size ) {
			if ( in_array( $size, array( 'thumbnail', 'medium', 'medium_large', 'large' ), true ) ) {
				$result[ $size ] = ucwords( trim( str_replace( array( '-', '_' ), array( ' ', ' ' ), $size ) ) );
			} else {
				$result[ $size ] = sprintf(
					'%1$s (%2$sx%3$s)',
					ucwords( trim( str_replace( array( '-', '_' ), array( ' ', ' ' ), $size ) ) ),
					$_wp_additional_image_sizes[ $size ]['width'],
					$_wp_additional_image_sizes[ $size ]['height']
				);
			}
		}

		$result = array_merge(
			array(
				'full' => esc_html__( 'Full', 'uael' ),
			),
			$result
		);

		$result['custom'] = esc_html__( 'Custom', 'uael' );

		$result = apply_filters( 'uael_post_featured_image_sizes', $result );

		return $result;
	}

	/**
	 * Get list of users.
	 *
	 * @uses   get_users()
	 * @link   https://codex.wordpress.org/Function_Reference/get_users
	 * @since 1.5.2
	 * @return array $users Data for all users.
	 */
	public static function get_users() {
		$users     = get_users( array( 'role__in' => array( 'administrator', 'editor', 'author', 'contributor' ) ) );
		$user_list = array();

		if ( empty( $users ) ) {
			return $user_list;
		}

		foreach ( $users as $key => $value ) {
			$user_list[ $value->ID ] = $value->data->user_login;
		}

		return apply_filters( 'uael_post_loop_user_list', $user_list );
	}

	/**
	 * Get list of categories.
	 *
	 * @since 1.36.0
	 * @param array $args Optional. Arguments to pass to get_categories().
	 * @access public
	 * @return array Categories array with term_id as key and name as value.
	 */
	public static function get_categories( $args = array() ) {
		$default_args = array(
			'hide_empty' => false,
		);

		$args       = wp_parse_args( $args, $default_args );
		$categories = get_categories( $args );
		$options    = array();

		if ( empty( $categories ) ) {
			return $options;
		}

		foreach ( $categories as $category ) {
			$options[ $category->term_id ] = $category->name;
		}

		return apply_filters( 'uael_categories_list', $options );
	}

	/**
	 * Get list of tags.
	 *
	 * @since 1.36.0
	 * @param array $args Optional. Arguments to pass to get_tags().
	 * @access public
	 * @return array Tags array with term_id as key and name as value.
	 */
	public static function get_tags( $args = array() ) {
		$default_args = array(
			'hide_empty' => false,
		);

		$args    = wp_parse_args( $args, $default_args );
		$tags    = get_tags( $args );
		$options = array();

		if ( empty( $tags ) ) {
			return $options;
		}

		foreach ( $tags as $tag ) {
			$options[ $tag->term_id ] = $tag->name;
		}

		return apply_filters( 'uael_tags_list', $options );
	}

	/**
	 * Get terms for a specific taxonomy.
	 *
	 * @since 1.36.0
	 * @param string $taxonomy The taxonomy name.
	 * @param array  $args Optional. Arguments to pass to get_terms().
	 * @access public
	 * @return array Terms array with term_id as key and name as value.
	 */
	public static function get_terms_by_taxonomy( $taxonomy, $args = array() ) {
		$default_args = array(
			'taxonomy'   => $taxonomy,
			'hide_empty' => false,
		);

		$args    = wp_parse_args( $args, $default_args );
		$terms   = get_terms( $args );
		$options = array();

		if ( empty( $terms ) || is_wp_error( $terms ) ) {
			return $options;
		}

		foreach ( $terms as $term ) {
			$options[ $term->term_id ] = $term->name;
		}

		return apply_filters( 'uael_terms_list_' . $taxonomy, $options );
	}
}
