<?php
namespace SabaiApps\Directories\Component\WordPressContent\FieldRenderer;

use SabaiApps\Directories\Component\Field;
use SabaiApps\Directories\Component\Entity;
use SabaiApps\Directories\Component\Field\IField;

class PostContentFieldRenderer extends Field\Renderer\TextRenderer {

	protected function _fieldRendererInfo() {
		$ret                = parent::_fieldRendererInfo();
		$ret['field_types'] = array( $this->_name );
		$ret['separatable'] = false;
		$ret['default_settings']['disable_excerpt_more'] = true;
		$ret['default_settings']['disable_the_content']  = false;
		return $ret;
	}

	public function fieldRendererSupports( Entity\Model\Bundle $bundle, IField $field ) {
		return empty( $bundle->info['no_content'] );
	}

	protected function _fieldRendererSettingsForm( Field\IField $field, array $settings, array $parents = array() ) {
		$ret                        = parent::_fieldRendererSettingsForm( $field, $settings, $parents );
		$ret['disable_the_content'] = array(
			'#type'          => 'checkbox',
			'#title'         => __( 'Disable the_content filter', 'directories-pro' ),
			'#default_value' => ! empty( $settings['disable_the_content'] ),
			'#states'        => array(
				'visible' => array(
					sprintf( 'input[name="%s[trim]"]', $field_name_prefix = $this->_application->Form_Form_fieldName( $parents ) ) => array(
						'type'  => 'checked',
						'value' => false,
					),
				),
			),
		);
		$ret['disable_excerpt_more'] = array(
			'#type'          => 'checkbox',
			'#title'         => __( 'Disable excerpt_more filter', 'directories-pro' ),
			'#default_value' => ! empty( $settings['disable_excerpt_more'] ),
			'#states'        => array(
				'visible' => array(
					sprintf( 'input[name="%s[trim]"]', $field_name_prefix ) => array(
						'type'  => 'checked',
						'value' => true,
					),
				),
			),
		);
		$visible_state_key           = sprintf( 'input[name="%s[disable_excerpt_more]"]', $field_name_prefix );
		$ret['trim_link']['#states']['visible'][ $visible_state_key ] = $ret['trim_marker']['#states']['visible'][ $visible_state_key ] = array(
			'type'  => 'checked',
			'value' => true,
		);
		return $ret;
	}

	protected function _getContent( $value, array $settings, Entity\Type\IEntity $entity ) {
		if ( ! $post = $entity->post() ) {
			return ''; // this should not happen
		}

		if ( ! empty( $settings['disable_the_content'] ) ) {
			remove_all_filters( 'the_content' );
		}

		// Make sure the following filters are applied which may have been removed by a page builder plugin.
		$filters = array(
			'wptexturize'        => 10,
			'wpautop'            => 10,
			'convert_smilies'    => 10,
			'convert_chars'      => 10,
			'shortcode_unautop'  => 10,
			'prepend_attachment' => 10,
			'do_shortcode'       => 11,
		);
		foreach ( $filters as $filter => $priority ) {
			if ( ! has_filter( 'the_content', $filter ) ) {
				add_filter( 'the_content', $filter, $priority );
			} else {
				unset( $filters[ $filter ] );
			}
		}

		// Process content
		setup_postdata( $post );
		$GLOBALS['post'] = $post;
		$value           = str_replace( ']]>', ']]&gt;', apply_filters( 'the_content', wp_kses_post( get_the_content() ) ) );
		wp_reset_postdata();

		// Remove filters if added
		if ( ! empty( $filters ) ) {
			foreach ( $filters as $filter => $priority ) {
				remove_filter( 'the_content', $filter, $priority );
			}
		}

		return $value;
	}

	protected function _getTrimmedContent( $value, $length, $marker, $link, array $settings, Entity\Type\IEntity $entity ) {
		if ( ! $post = $entity->post() ) {
			return ''; // this should not happen
		}

		$value = apply_filters( 'get_the_excerpt', $post->post_excerpt ? $post->post_excerpt : strip_shortcodes( $post->post_content ), $post );
		// Add WordPress trim marker
		$marker = ' ' . '[&hellip;]'; // set default trim marker of WordPress
		if ( isset( $settings['disable_excerpt_more'] )
			&& ! $settings['disable_excerpt_more']
		) {
			setup_postdata( $post );
			$GLOBALS['post'] = $post;
			$marker          = apply_filters( 'excerpt_more', $marker );
			wp_reset_postdata();
		} else {
			if ( isset( $settings['trim_marker'] ) ) {
				$marker = $settings['trim_marker'];
			}
			$link = ! empty( $settings['trim_link'] );
		}

		return parent::_getTrimmedContent( $value, $length, $marker, $link, $settings, $entity );
	}

	protected function _fieldRendererReadableSettings( Field\IField $field, array $settings ) {
		$ret = (array) parent::_fieldRendererReadableSettings( $field, $settings );
		if ( ! empty( $ret['trim'] ) ) {
			$ret += array(
				'disable_excerpt_more' => array(
					'label'   => __( 'Disable excerpt_more filter', 'directories-pro' ),
					'value'   => ! empty( $settings['disable_excerpt_more'] ),
					'is_bool' => true,
				),
			);
			if ( ! empty( $settings['disable_excerpt_more'] ) ) {
				unset( $ret['trim_marker'], $ret['trim_link'] );
			}
		}
		return $ret;
	}
}
