<?php

namespace Fab\Controller;

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

use Fab\Interfaces\JIT_Interface;

/**
 * Placeholders
 *
 * @package    Fab
 * @subpackage Fab/Controller
 */
class Placeholders extends Base implements JIT_Interface {

    /**
     * Implement placeholder filters.
     *
     * @param array $placeholders Placeholders.
     * @return array Placeholders.
     */
    public function add_fab_placeholders( $placeholders ) {
        global $wp, $post;

        // Default values that can be overridden by $data.
        $placeholders['{site_title}']       = get_bloginfo( 'name' );
        $placeholders['{site_url}']         = home_url();
        $placeholders['{site_tagline}']     = get_bloginfo( 'description' );
        $placeholders['{current_page_url}'] = home_url( isset( $wp->request ) ? $wp->request : '' );

        // Add post data if available to $defaults.
        if ( $post && isset( $post->ID ) ) {
            $placeholders['{post_id}']            = $post->ID;
            $placeholders['{post_title}']         = $post->post_title;
            $placeholders['{post_slug}']          = $post->post_name;
            $placeholders['{post_status}']        = $post->post_status;
            $placeholders['{post_type}']          = $post->post_type;
            $placeholders['{post_url}']           = get_the_permalink( $post->ID );
            $placeholders['{post_excerpt}']       = $post->post_excerpt;
            $placeholders['{post_content}']       = get_the_content( $post->ID );
            $placeholders['{post_date}']          = get_the_date( 'Y-m-d', $post->ID );
            $placeholders['{post_author}']        = get_the_author_meta( 'display_name', get_post_field( 'post_author', $post->ID ) );
            $placeholders['{post_thumbnail_url}'] = get_the_post_thumbnail_url( $post->ID, 'full' );
        }

        // User.
        if ( is_user_logged_in() ) {
            $user                         = wp_get_current_user();
            $placeholders['{user_id}']    = $user->ID;
            $placeholders['{user_name}']  = $user->display_name;
            $placeholders['{user_email}'] = $user->user_email;
            $placeholders['{user_role}']  = $user->roles[0];
        }

        return $placeholders;
    }

    /**
     * Placeholders description.
     *
     * @param array $placeholders Placeholders.
     * @return array Placeholders.
     */
    public function add_fab_placeholders_description( $placeholders ) {
        // Website.
        $placeholders['{site_title}']       = __( 'Website title', 'floating-awesome-button' );
        $placeholders['{site_url}']         = __( 'Website URL', 'floating-awesome-button' );
        $placeholders['{site_tagline}']     = __( 'Website description', 'floating-awesome-button' );
        $placeholders['{current_page_url}'] = __( 'Current page URL', 'floating-awesome-button' );

        // Post.
        $placeholders['{post_id}']            = __( 'Post ID', 'floating-awesome-button' );
        $placeholders['{post_title}']         = __( 'Post title', 'floating-awesome-button' );
        $placeholders['{post_slug}']          = __( 'Post slug', 'floating-awesome-button' );
        $placeholders['{post_status}']        = __( 'Post status', 'floating-awesome-button' );
        $placeholders['{post_type}']          = __( 'Post type', 'floating-awesome-button' );
        $placeholders['{post_url}']           = __( 'Post URL', 'floating-awesome-button' );
        $placeholders['{post_date}']          = __( 'Post date', 'floating-awesome-button' );
        $placeholders['{post_author}']        = __( 'Post author', 'floating-awesome-button' );
        $placeholders['{post_thumbnail_url}'] = __( 'Post thumbnail URL', 'floating-awesome-button' );

        // User.
        $placeholders['{user_id}']    = __( 'User ID', 'floating-awesome-button' );
        $placeholders['{user_name}']  = __( 'User name', 'floating-awesome-button' );
        $placeholders['{user_email}'] = __( 'User email', 'floating-awesome-button' );
        $placeholders['{user_role}']  = __( 'User role', 'floating-awesome-button' );

        return $placeholders;
    }

    /*
    |--------------------------------------------------------------------------
    | Fulfill implemented interface contracts
    |--------------------------------------------------------------------------
     */

    /**
     * JIT Method.
     *
     * @return void
     */
    public static function jit() {
        // Add placeholders.
        add_filter( 'fab_placeholders', array( __CLASS__, 'jit_add_fab_placeholders' ), 9, 1 );

        // Explain placeholders.
        add_filter( 'fab_placeholders_description', array( __CLASS__, 'jit_add_fab_placeholders_description' ), 9, 1 );
    }
}
