<?php

namespace Reach\App\Posts;

use Reach\App\Interfaces\WordPressHooks;
use Reach\App\Fields\SiteOptions;

/**
 * Class BlogPosts
 *
 * @package Reach\App
 */
class BlogPosts implements WordPressHooks {

    /**
     * Adds hooks.
     */
    public function addHooks() {
        add_action( 'init', [ $this, 'getPostPagination' ] );
        add_action( 'wp_enqueue_scripts', [ $this, 'dequeuePaginationCSS' ], 25 );
    }

    /**
     * Remove default pagination CSS file from being enqueued on the page
     */
    public function dequeuePaginationCSS() {
        wp_dequeue_style( 'wp-pagenavi' );
    }

    /**
     * Get blog layout option value
     *
     * @return string|void
     */
    public static function getBlogLayout() {
        $options = SiteOptions::getSiteOptions();

        return ( ! empty( $options['blog_layout'] ) ? esc_attr( $options['blog_layout'] ) : 'default' );
    }

    /**
     * Check that we have a posts page set and include pagination
     */
    public function getPostPagination() {
        // get blog ID
        $blog_id = get_option( 'page_for_posts' );
        if ( $blog_id ) {
            // load ACF Fields
            require_once AD_THEME_PATH . 'vendor/wp-pagenavi/wp-pagenavi.php';
        }
    }

    /**
     * Output post navigation links on single post pages
     */
    public static function getPostNextPreviousLinks() {
        printf(
            '<div class="blog__post-links">%1$s %2$s</div>',
            get_previous_post_link(
                '<div class="blog__post-prev">%link</div>',
                '<svg class="icon icon-arrow-chev-left"><use xlink:href="#icon-arrow-chev-left"></use></svg>' .
                __( 'Previous Post', 'reach' )
            ),
            get_next_post_link(
                '<div class="blog__post-next">%link</div>',
                __( 'Next Post', 'Article' ) .
                '<svg class="icon icon-arrow-chev-right"><use xlink:href="#icon-arrow-chev-right"></use></svg>'
            )
        );
    }

    /**
     * Get post content and build our excerpt length string for archive views.
     *
     * @param int $char_length
     *
     * @return bool|string
     */
    public static function getPostExcerpt( $char_length = 150 ) {
        $excerpt = get_the_content();
        $excerpt = strip_shortcodes( $excerpt );
        $excerpt = strip_tags( $excerpt );
        $excerpt = substr( $excerpt, 0, $char_length );
        $excerpt = substr( $excerpt, 0, strripos( $excerpt, " " ) );
        $excerpt = $excerpt . '&hellip;';

        return apply_filters( 'the_excerpt', $excerpt );
    }

    /**
     * Query related posts to current post ID and output the archive post markup
     *
     * @param $post_id
     */
    public static function getRelatedArticles( $post_id ) {
        // get post categories
        $categories = wp_get_post_categories( $post_id );
        $args       = [
            'post_types'     => 'posts',
            'posts_per_page' => 2,
            'no_found_rows'  => true,
            'post__not_in' => [ $post_id ],
            'category__in'   => $categories
        ];

        $related = new \WP_Query( $args );
        if ( $related->have_posts() ) {
            echo '<div class="blog__related-articles">';

            printf( '<header class="blog__headline">%s</header>', __( 'What to Read Next', 'reach' ) );

            while ( $related->have_posts() ) {
                $related->the_post();

                // Loads the content/singular/page.php template.
                get_template_part( 'content/archive/content' );
            }

            echo '</div>';
        }
    }

    /**
     * Strip content string to the nearest word by character length and return the excerpt
     *
     * @param $content
     * @param int $length
     *
     * @return string|bool
     */
    public static function getContentLimited( $content, $length = 120 ) {
        
        if ( ! $content ) {
            return false;
        }

        // strip tags to avoid breaking any html
        $excerpt = strip_tags( $content );
        
        if ( strlen( $excerpt ) > $length ) {
            // truncate string
            $stringCut = substr( $excerpt, 0, $length );
            // make sure it ends in a word so assassinate doesn't become ass...
            $excerpt = substr( $stringCut, 0, strrpos( $stringCut, ' ' ) );
            $excerpt = $excerpt . '&hellip;';
        }
        return $excerpt;
    }
}