* @since 3.0.0 */ namespace Beyondwords\Wordpress\Component\Posts\Column; use Beyondwords\Wordpress\Component\Post\PostMetaUtils; use Beyondwords\Wordpress\Component\Settings\SettingsUtils; /** * Column setup * * @since 3.0.0 */ class Column { public const OUTPUT_YES = ' '; public const OUTPUT_NO = '—'; public const OUTPUT_DISABLED = ' Disabled'; public const OUTPUT_ERROR_PREFIX = ' '; /** * Constructor */ public function __construct() { add_action('wp_loaded', function () { $postTypes = SettingsUtils::getSupportedPostTypes(); if (is_array($postTypes)) { foreach ($postTypes as $postType) { add_filter("manage_{$postType}_posts_columns", array($this, 'renderColumnsHead')); add_action("manage_{$postType}_posts_custom_column", array($this, 'renderColumnsContent'), 10, 2); } } }); } /** * Add a custom column with player status. * * @since 3.0.0 * * @param array $columns Array of headers * * @return array **/ public function renderColumnsHead($columns) { return array_merge($columns, array( 'beyondwords' => __('BeyondWords', 'speechkit'), )); } /** * Render ✗|✓ in Posts list, under the BeyondWords column. * * @since 3.0.0 * * @param string $columnName Column name * @param int $postId Post ID * * @return void **/ public function renderColumnsContent($columnName, $postId) { if ($columnName !== 'beyondwords') { return; } $postTypes = SettingsUtils::getSupportedPostTypes(); if (empty($postTypes)) { return; } // todo test this for an unsupported post type $errorMessage = PostMetaUtils::getErrorMessage($postId); $contentId = PostMetaUtils::getContentId($postId); $disabled = PostMetaUtils::getDisabled($postId); $allowedTags = array( 'span' => array( 'class' => array(), ), ); if (! empty($errorMessage)) { echo wp_kses(self::OUTPUT_ERROR_PREFIX . $errorMessage, $allowedTags); } elseif (empty($contentId)) { echo wp_kses(self::OUTPUT_NO, $allowedTags); } else { echo wp_kses(self::OUTPUT_YES, $allowedTags); } if (! empty($disabled)) { echo wp_kses(self::OUTPUT_DISABLED, $allowedTags); } } }