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

use SabaiApps\Directories\Component\Entity\Type\IEntity;
use SabaiApps\Directories\Component\Field;

class PostReferenceFieldRenderer extends Field\Renderer\AbstractRenderer {

	protected function _fieldRendererInfo() {
		return array(
			'label'            => __( 'Link', 'directories-pro' ),
			'field_types'      => array( $this->_name ),
			'default_settings' => array(
				'_separator' => ', ',
			),
			'inlineable'       => true,
		);
	}

	protected function _fieldRendererRenderField( Field\IField $field, array &$settings, IEntity $entity, array $values, ?array $context = null, $more = 0 ) {
		$field_settings = $field->getFieldSettings();
		if ( ! post_type_exists( $field_settings['post_type'] ) ) {
			return;
		}

		$posts = get_posts(
			array(
				'post_type' => $field_settings['post_type'],
				'include'   => $values,
			)
		);
		if ( empty( $posts ) ) {
			return;
		}

		$ret = array();
		foreach ( $posts as $post ) {
			if ( ! $url = get_permalink( $post ) ) {
				continue;
			}

			$ret[] = '<a href="' . esc_url( $url ) . '">' . esc_html( get_the_title( $post ) ) . '</a>';
		}

		return implode( $settings['_separator'], $ret );
	}
}
