| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace TheFrosty; |
| 6 |
|
| 7 |
use function add_action; |
| 8 |
use function add_filter; |
| 9 |
use function add_options_page; |
| 10 |
use function apply_filters; |
| 11 |
use function array_map; |
| 12 |
use function dirname; |
| 13 |
use function esc_attr; |
| 14 |
use function esc_html__; |
| 15 |
use function esc_url; |
| 16 |
use function get_option; |
| 17 |
use function get_plugin_data; |
| 18 |
use function get_post_types; |
| 19 |
use function get_the_post_thumbnail; |
| 20 |
use function get_the_title; |
| 21 |
use function has_post_thumbnail; |
| 22 |
use function plugin_dir_url; |
| 23 |
use function plugins_url; |
| 24 |
use function post_type_supports; |
| 25 |
use function register_setting; |
| 26 |
use function sprintf; |
| 27 |
use function update_option; |
| 28 |
use function wp_enqueue_style; |
| 29 |
use function wp_register_style; |
| 30 |
|
| 31 |
/** |
| 32 |
* Class FeatureImageColumn |
| 33 |
* @package TheFrosty |
| 34 |
*/ |
| 35 |
class FeatureImageColumn |
| 36 |
{ |
| 37 |
|
| 38 |
protected const ID = 'featured-image'; |
| 39 |
|
| 40 |
/** |
| 41 |
* FeatureImageColumn constructor. |
| 42 |
* @param string $file |
| 43 |
*/ |
| 44 |
public function __construct(private string $file) |
| 45 |
{ |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Ensures that the rest of the code only runs on edit.php pages |
| 50 |
* @since 0.3 |
| 51 |
*/ |
| 52 |
public function addHooks(): void |
| 53 |
{ |
| 54 |
add_action('admin_menu', [$this, 'adminMenu']); |
| 55 |
add_action('admin_init', [$this, 'adminInit']); |
| 56 |
add_action('admin_enqueue_scripts', [$this, 'style']); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Activation hook, to add our default settings. |
| 61 |
*/ |
| 62 |
public function activationHook(): void |
| 63 |
{ |
| 64 |
$post_types = []; |
| 65 |
foreach ($this->getPostTypes() as $post_type) { |
| 66 |
if (!post_type_supports($post_type, 'thumbnail')) { |
| 67 |
continue; |
| 68 |
} |
| 69 |
$post_types[$post_type] = $post_type; |
| 70 |
} |
| 71 |
update_option('featured_image_column', $post_types); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Register our settings page. |
| 76 |
*/ |
| 77 |
public function adminMenu(): void |
| 78 |
{ |
| 79 |
add_options_page( |
| 80 |
'Featured Image Column Settings', |
| 81 |
'Featured Image Col', |
| 82 |
'manage_options', |
| 83 |
'featured-image-column', |
| 84 |
function (): void { |
| 85 |
$this->settingsPage(); |
| 86 |
} |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Since the load-edit.php hook is too early for checking the post type, hook the rest |
| 92 |
* of the code to the wp action to allow the query to be run first. |
| 93 |
*/ |
| 94 |
public function adminInit(): void |
| 95 |
{ |
| 96 |
global $pagenow; |
| 97 |
|
| 98 |
$this->registerSettings(); |
| 99 |
|
| 100 |
// Only continue if we're on the 'edit.php' page(s) |
| 101 |
if (empty($pagenow) || ($pagenow !== 'edit.php' && \defined('DOING_AJAX') && !\DOING_AJAX)) { |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
// Make sure we've got some post_types saved via our settings. |
| 106 |
$post_types = $this->getSettings(); |
| 107 |
if (empty($post_types)) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
// Add out custom column and column data |
| 112 |
foreach ($post_types as $post_type) { |
| 113 |
if (!post_type_supports($post_type, 'thumbnail')) { |
| 114 |
continue; |
| 115 |
} |
| 116 |
add_filter("manage_{$post_type}_posts_columns", [$this, 'columns']); |
| 117 |
add_action("manage_{$post_type}_posts_custom_column", [$this, 'columnData'], 10, 2); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Enqueue our stylesheet on the edit.php page. |
| 123 |
*/ |
| 124 |
public function style(): void |
| 125 |
{ |
| 126 |
global $pagenow; |
| 127 |
$version = get_plugin_data($this->file, false, false)['Version'] ?? '20170625'; |
| 128 |
wp_register_style('featured-image-column', plugin_dir_url($this->file) . 'css/column.css', [], $version); |
| 129 |
|
| 130 |
if ($pagenow === 'edit.php') { |
| 131 |
wp_enqueue_style('featured-image-column'); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Filter the image in before the 'title' |
| 137 |
* @param array $columns |
| 138 |
* @return array |
| 139 |
*/ |
| 140 |
public function columns(array $columns): array |
| 141 |
{ |
| 142 |
$new_columns = []; |
| 143 |
foreach ($columns as $key => $title) { |
| 144 |
// Put the Thumbnail column before the Title column |
| 145 |
if ($key === 'title') { |
| 146 |
$new_columns[self::ID] = esc_html__('Image', 'featured-image-column'); |
| 147 |
} |
| 148 |
|
| 149 |
$new_columns[$key] = $title; |
| 150 |
} |
| 151 |
|
| 152 |
return $new_columns; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Output the image |
| 157 |
* |
| 158 |
* @param string $column_name |
| 159 |
* @param int $post_id |
| 160 |
*/ |
| 161 |
public function columnData(string $column_name, int $post_id): void |
| 162 |
{ |
| 163 |
if (self::ID !== $column_name) { |
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
$featured_image = $this->getTheImage($post_id); |
| 168 |
|
| 169 |
if (!empty($featured_image)) { |
| 170 |
echo $featured_image; |
| 171 |
|
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
echo " "; // This helps prevent issues with empty cells |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Output our settings. |
| 180 |
* @since 0.2.2 |
| 181 |
*/ |
| 182 |
protected function settingsPage(): void |
| 183 |
{ |
| 184 |
$post_types = $this->getSettings(); |
| 185 |
if (empty($post_types)) { |
| 186 |
$post_types = []; |
| 187 |
foreach ($this->getPostTypes() as $key => $post_type) { |
| 188 |
if (post_type_supports($post_type, 'thumbnail')) { |
| 189 |
$post_types[$post_type] = $post_type; |
| 190 |
} |
| 191 |
} |
| 192 |
update_option('featured_image_column', $post_types); |
| 193 |
} |
| 194 |
|
| 195 |
include dirname($this->file) . '/views/settings.php'; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Register the plugins settings. |
| 200 |
*/ |
| 201 |
protected function registerSettings(): void |
| 202 |
{ |
| 203 |
register_setting( |
| 204 |
'featured_image_column_post_types', |
| 205 |
'featured_image_column', |
| 206 |
static function (mixed $input): array { |
| 207 |
if (!\is_array($input)) { |
| 208 |
$input = (array) $input; |
| 209 |
} |
| 210 |
|
| 211 |
return array_map('sanitize_key', $input); |
| 212 |
} |
| 213 |
); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Function to get the image. |
| 218 |
* @param int|null $post_id |
| 219 |
* @return string |
| 220 |
*/ |
| 221 |
protected function getTheImage(?int $post_id): string |
| 222 |
{ |
| 223 |
if (has_post_thumbnail($post_id)) { |
| 224 |
$size = apply_filters('featured_image_post_thumbnail_size', [50, 50], $post_id); |
| 225 |
return get_the_post_thumbnail($post_id, $size); |
| 226 |
} |
| 227 |
|
| 228 |
$default = plugins_url('images/default.png', $this->file); |
| 229 |
$image = apply_filters('featured_image_column_default_image', $default, $post_id); |
| 230 |
|
| 231 |
return sprintf('<img alt="%1$s" src="%2$s">', esc_attr(get_the_title($post_id)), esc_url($image)); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Helper function to return all public post ty`pes |
| 236 |
* @return array |
| 237 |
*/ |
| 238 |
protected function getPostTypes(): array |
| 239 |
{ |
| 240 |
return get_post_types(['public' => true]); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Get our settings from the DB with the defaults set to an array. |
| 245 |
* @return array |
| 246 |
*/ |
| 247 |
protected function getSettings(): array |
| 248 |
{ |
| 249 |
return get_option('featured_image_column', []); |
| 250 |
} |
| 251 |
} |
| 252 |
|