<?php
/**
 * Class api for working with common fields of custom posts.
 *
 * @package cyberpress
 */

/**
 * Class CyberPress_Custom_Post_API
 */
class CyberPress_Custom_Post_API {

    /**
     * WordPess post id.
     *
     * @var int
     */
    private $post_id;

    /**
     * WordPress post type.
     *
     * @var string
     */
    private $post_type;

    /**
     * Global Prefix for unique options.
     *
     * @var string
     */
    private $global_prefix = 'cyberpress';

    /**
     * CyberPress_Custom_Post_API constructor.
     *
     * @param null $id - post id. If null or not exist - we use global $post->ID.
     * @param null $post_type - post type. If null or not exist - we use global $post->ID.
     */
    public function __construct( $id = null, $post_type = null ) {
        global $post;
        if ( isset( $id ) && ! empty( $id ) ) {
            $this->set_id( $id );
            $this->set_post_type( $post_type );
        } else {
            $this->set_post_type( $post->post_type );
            $this->set_id( $post->ID );
        }
    }

    /**
     * Functions for set Post id.
     *
     * @param int $id - post id.
     */
    public function set_id( $id ) {
        $this->post_id = $id;
    }

    /**
     * Functions for get Post Type.
     *
     * @return string - post id.
     */
    public function get_post_type() {
        return $this->post_type;
    }

    /**
     * Functions for set Post Type.
     *
     * @param string $post_type - post type.
     */
    public function set_post_type( $post_type ) {
        $this->post_type = $post_type;
    }

    /**
     * Functions for get Post id.
     *
     * @return int - post id.
     */
    public function get_id() {
        return $this->post_id;
    }

    /**
     * Functions for get Global prefix.
     *
     * @return string
     */
    public function get_global_prefix() {
        return $this->global_prefix;
    }

    /**
     * Wrapper function to get the post publication time property.
     *
     * @return mixed
     */
    public function get_the_time() {
        // phpcs:ignore
        return apply_filters( $this->get_filter_prefix() . '_get_the_time', get_the_time( '', $this->post_id ) );
    }

    /**
     * Wrapper function to get the date creation of the post.
     *
     * @return mixed
     */
    public function get_the_date() {
        // phpcs:ignore
        return apply_filters( $this->get_filter_prefix() . '_get_the_date', get_the_date( '', $this->post_id ) );
    }

    /**
     * Wrapper function to get the permalink to post.
     *
     * @return mixed
     */
    public function get_url() {
        // phpcs:ignore
        return apply_filters( $this->get_filter_prefix() . '_get_url', get_permalink( $this->post_id ) );
    }

    /**
     * Wrapper function to get the post thumbnail img layout.
     *
     * @param string $attachment_resolution - image resolution.
     * @param array  $image_atts - image args.
     * @return mixed
     */
    public function get_thumbnail( $attachment_resolution = 'medium', $image_atts = array() ) {
        $thumbnail_id = get_post_thumbnail_id( $this->post_id );
        if ( $thumbnail_id ) {
            // phpcs:ignore
            return apply_filters( $this->get_filter_prefix() . '_get_thumbnail', wp_get_attachment_image( $thumbnail_id, $attachment_resolution, false, $image_atts ) );
        }
    }

    /**
     * Check thumbnail exist.
     *
     * @return mixed - thumbnail id if exist or false.
     */
    public function check_thumbnail() {
        // Previously we used the `get_the_post_thumbnail` function,
        // but it is not working properly on the archive pages and returns empty string.
        $thumbnail_id = get_post_thumbnail_id( $this->post_id );
        // phpcs:ignore
        return apply_filters( $this->get_filter_prefix() . '_check_thumbnail', !!$thumbnail_id );
    }

    /**
     * Get post of title.
     *
     * @return mixed - post title or empty string.
     */
    public function get_title() {
        // phpcs:ignore
        return apply_filters( $this->get_filter_prefix() . '_get_title', get_the_title( $this->post_id ) );
    }

    /**
     * Returns the filter prefix depending on the type of post.
     *
     * @return string - filter prefix
     */
    public function get_filter_prefix() {
        $post_type     = $this->get_post_type();
        $global_prefix = $this->get_global_prefix();
        $filter_prefix = $global_prefix . '_posts';

        if ( null !== $post_type ) {
            $filter_prefix = $global_prefix . '_' . $post_type;
        }
        return $filter_prefix;
    }
    /**
     * Display the post content.
     *
     * @param string $more_link_text Optional. Content for when there is more text.
     * @param bool   $strip_teaser   Optional. Strip teaser content before the more text. Default is false.
     */
    public function the_content( $more_link_text = null, $strip_teaser = false ) {
        the_content( $more_link_text, $strip_teaser );
    }

    /**
     * Retrieve the post content.
     *
     * @param string $more_link_text - Content for when there is more text.
     * @param bool   $strip_teaser - Strip teaser content before the more text. Default is false.
     * @return mixed
     */
    public function get_the_content( $more_link_text = null, $strip_teaser = false ) {
        // phpcs:ignore
        return apply_filters( $this->get_filter_prefix() . '_get_the_content', get_the_content( $more_link_text, $strip_teaser ) );
    }

    /**
     * Display the post excerpt
     */
    public function the_excerpt() {
        the_excerpt( $this->post_id );
    }

    /**
     * Retrieves the post excerpt.
     *
     * @return mixed
     */
    public function get_the_excerpt() {
        // phpcs:ignore
        return apply_filters( $this->get_filter_prefix() . '_get_the_excerpt', get_the_excerpt( $this->post_id ) );
    }
}
