| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
/** |
| 6 |
* BeyondWords Posts Column. |
| 7 |
* |
| 8 |
* @package Beyondwords\Wordpress |
| 9 |
* @author Stuart McAlpine <stu@beyondwords.io> |
| 10 |
* @since 3.0.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace Beyondwords\Wordpress\Component\Posts\Column; |
| 14 |
|
| 15 |
use Beyondwords\Wordpress\Component\Post\PostMetaUtils; |
| 16 |
use Beyondwords\Wordpress\Component\Settings\SettingsUtils; |
| 17 |
|
| 18 |
/** |
| 19 |
* Column setup |
| 20 |
* |
| 21 |
* @since 3.0.0 |
| 22 |
*/ |
| 23 |
class Column |
| 24 |
{ |
| 25 |
public const OUTPUT_YES = '<span class="dashicons dashicons-yes"></span> '; |
| 26 |
|
| 27 |
public const OUTPUT_NO = '—'; |
| 28 |
|
| 29 |
public const OUTPUT_DISABLED = ' <span class="beyondwords--disabled">Disabled</span>'; |
| 30 |
|
| 31 |
public const OUTPUT_ERROR_PREFIX = '<span class="dashicons dashicons-warning"></span> '; |
| 32 |
|
| 33 |
/** |
| 34 |
* Constructor |
| 35 |
*/ |
| 36 |
public function __construct() |
| 37 |
{ |
| 38 |
add_action('wp_loaded', function () { |
| 39 |
$postTypes = SettingsUtils::getSupportedPostTypes(); |
| 40 |
|
| 41 |
if (is_array($postTypes)) { |
| 42 |
foreach ($postTypes as $postType) { |
| 43 |
add_filter("manage_{$postType}_posts_columns", array($this, 'renderColumnsHead')); |
| 44 |
add_action("manage_{$postType}_posts_custom_column", array($this, 'renderColumnsContent'), 10, 2); |
| 45 |
} |
| 46 |
} |
| 47 |
}); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Add a custom column with player status. |
| 52 |
* |
| 53 |
* @since 3.0.0 |
| 54 |
* |
| 55 |
* @param array $columns Array of <td> headers |
| 56 |
* |
| 57 |
* @return array |
| 58 |
**/ |
| 59 |
public function renderColumnsHead($columns) |
| 60 |
{ |
| 61 |
return array_merge($columns, array( |
| 62 |
'beyondwords' => __('BeyondWords', 'speechkit'), |
| 63 |
)); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Render ✗|✓ in Posts list, under the BeyondWords column. |
| 68 |
* |
| 69 |
* @since 3.0.0 |
| 70 |
* |
| 71 |
* @param string $columnName Column name |
| 72 |
* @param int $postId Post ID |
| 73 |
* |
| 74 |
* @return void |
| 75 |
**/ |
| 76 |
public function renderColumnsContent($columnName, $postId) |
| 77 |
{ |
| 78 |
if ($columnName !== 'beyondwords') { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
$postTypes = SettingsUtils::getSupportedPostTypes(); |
| 83 |
|
| 84 |
if (empty($postTypes)) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
// todo test this for an unsupported post type |
| 89 |
|
| 90 |
$errorMessage = PostMetaUtils::getErrorMessage($postId); |
| 91 |
$contentId = PostMetaUtils::getContentId($postId); |
| 92 |
$disabled = PostMetaUtils::getDisabled($postId); |
| 93 |
|
| 94 |
$allowedTags = array( |
| 95 |
'span' => array( |
| 96 |
'class' => array(), |
| 97 |
), |
| 98 |
); |
| 99 |
|
| 100 |
if (! empty($errorMessage)) { |
| 101 |
echo wp_kses(self::OUTPUT_ERROR_PREFIX . $errorMessage, $allowedTags); |
| 102 |
} elseif (empty($contentId)) { |
| 103 |
echo wp_kses(self::OUTPUT_NO, $allowedTags); |
| 104 |
} else { |
| 105 |
echo wp_kses(self::OUTPUT_YES, $allowedTags); |
| 106 |
} |
| 107 |
|
| 108 |
if (! empty($disabled)) { |
| 109 |
echo wp_kses(self::OUTPUT_DISABLED, $allowedTags); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|