| 1 |
<?php |
| 2 |
/** |
| 3 |
* The PostType helper class |
| 4 |
* |
| 5 |
* @package BoldBlocks |
| 6 |
* @author Phi Phan <mrphipv@gmail.com> |
| 7 |
* @copyright Copyright (c) 2023, Phi Phan |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace BoldBlocks; |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
defined( 'ABSPATH' ) || exit; |
| 14 |
|
| 15 |
if ( ! class_exists( PostType::class ) ) : |
| 16 |
/** |
| 17 |
* A helper class to register post type, taxonomy, and meta fields. |
| 18 |
*/ |
| 19 |
class PostType { |
| 20 |
/** |
| 21 |
* Class instance |
| 22 |
* |
| 23 |
* @var PostType |
| 24 |
*/ |
| 25 |
private static $instance; |
| 26 |
|
| 27 |
/** |
| 28 |
* The constructor |
| 29 |
*/ |
| 30 |
private function __construct() {} |
| 31 |
|
| 32 |
/** |
| 33 |
* Initialize the instance. |
| 34 |
* |
| 35 |
* @return PostType |
| 36 |
*/ |
| 37 |
public static function get_instance() { |
| 38 |
if ( ! isset( self::$instance ) ) { |
| 39 |
self::$instance = new PostType(); |
| 40 |
} |
| 41 |
|
| 42 |
return self::$instance; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Generate thumbnail column for custom post type. |
| 47 |
* |
| 48 |
* @param string $post_type |
| 49 |
* @return void |
| 50 |
*/ |
| 51 |
public function generate_thumbnail_column( $post_type ) { |
| 52 |
add_filter( 'manage_edit-' . $post_type . '_columns', [ $this, 'add_thumbnail_custom_columns' ] ); |
| 53 |
add_action( 'manage_' . $post_type . '_posts_custom_column', [ $this, 'manage_thunbnail_custom_columns' ], 10, 2 ); |
| 54 |
add_action( 'admin_head', [ $this, 'add_thumbnail_custom_columns_style' ] ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Register a custom post type |
| 59 |
* |
| 60 |
* @param string $post_type |
| 61 |
* @param array $args |
| 62 |
* @param array $labels |
| 63 |
* @return void |
| 64 |
*/ |
| 65 |
public function register_post_type( $post_type, $args = [], $labels = [] ) { |
| 66 |
if ( empty( $labels['name'] ) || empty( $labels['singular_name'] ) ) { |
| 67 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 68 |
_doing_it_wrong( __CLASS__ . '->' . __FUNCTION__, __( 'name, singular_name labels are required', 'content-blocks-builder' ) ); |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
$name = $labels['name']; |
| 73 |
$singular_name = $labels['singular_name']; |
| 74 |
|
| 75 |
// Post type args. |
| 76 |
$labels = wp_parse_args( |
| 77 |
$labels, |
| 78 |
[ |
| 79 |
'name' => $name, |
| 80 |
'singular_name' => $singular_name, |
| 81 |
'menu_name' => $name, |
| 82 |
'name_admin_bar' => $singular_name, |
| 83 |
// translators: %s: Post type singular name |
| 84 |
'add_new' => sprintf( __( 'Add New %s', 'content-blocks-builder' ), $singular_name ), |
| 85 |
// translators: %s: Post type singular name |
| 86 |
'add_new_item' => sprintf( __( 'Add New %s', 'content-blocks-builder' ), $singular_name ), |
| 87 |
// translators: %s: Post type singular name |
| 88 |
'new_item' => sprintf( __( 'New %s', 'content-blocks-builder' ), $singular_name ), |
| 89 |
// translators: %s: Post type singular name |
| 90 |
'edit_item' => sprintf( __( 'Edit %s', 'content-blocks-builder' ), $singular_name ), |
| 91 |
// translators: %s: Post type singular name |
| 92 |
'view_item' => sprintf( __( 'View %s', 'content-blocks-builder' ), $singular_name ), |
| 93 |
// translators: %s: Post type name |
| 94 |
'all_items' => sprintf( __( 'All %s', 'content-blocks-builder' ), $name ), |
| 95 |
// translators: %s: Post type name |
| 96 |
'search_items' => sprintf( __( 'Search %s', 'content-blocks-builder' ), $name ), |
| 97 |
'parent_item_colon' => __( 'Parent item:', 'content-blocks-builder' ), |
| 98 |
'not_found' => __( 'No items found.', 'content-blocks-builder' ), |
| 99 |
'not_found_in_trash' => __( 'No items found in trash.', 'content-blocks-builder' ), |
| 100 |
] |
| 101 |
); |
| 102 |
|
| 103 |
$args = wp_parse_args( |
| 104 |
$args, |
| 105 |
[ |
| 106 |
'labels' => $labels, |
| 107 |
// translators: %s: Post type name |
| 108 |
'description' => sprintf( __( 'All %s', 'content-blocks-builder' ), $name ), |
| 109 |
'public' => true, |
| 110 |
'publicly_queryable' => false, |
| 111 |
'exclude_from_search' => true, |
| 112 |
'show_ui' => true, |
| 113 |
'show_in_menu' => true, |
| 114 |
'show_in_nav_menus' => false, |
| 115 |
'show_in_admin_bar' => true, |
| 116 |
'show_in_rest' => true, |
| 117 |
'query_var' => false, |
| 118 |
'rewrite' => false, |
| 119 |
'capability_type' => 'post', |
| 120 |
'has_archive' => false, |
| 121 |
'hierarchical' => false, |
| 122 |
'menu_position' => 5, |
| 123 |
'supports' => [ 'title', 'editor', 'author', 'custom-fields', 'revisions', 'page-attributes' ], |
| 124 |
'can_export' => true, |
| 125 |
'template_lock' => 'insert', |
| 126 |
] |
| 127 |
); |
| 128 |
|
| 129 |
// Register post type. |
| 130 |
register_post_type( $post_type, $args ); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Register a custom taxonomy |
| 135 |
* |
| 136 |
* @param string $taxonomy_name |
| 137 |
* @param string|array $post_type |
| 138 |
* @param array $args |
| 139 |
* @param array $labels |
| 140 |
* @return void |
| 141 |
*/ |
| 142 |
public function register_taxonomy( $taxonomy_name, $post_type, $args = [], $labels = [] ) { |
| 143 |
if ( empty( $labels['name'] ) || empty( $labels['singular_name'] ) ) { |
| 144 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 145 |
_doing_it_wrong( __CLASS__ . '->' . __FUNCTION__, __( 'name, singular_name labels are required', 'content-blocks-builder' ) ); |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
$name = $labels['name']; |
| 150 |
$singular_name = $labels['singular_name']; |
| 151 |
|
| 152 |
// Taxonomy args. |
| 153 |
$labels = wp_parse_args( |
| 154 |
$labels, |
| 155 |
[ |
| 156 |
'name' => $name, |
| 157 |
'singular_name' => $singular_name, |
| 158 |
'menu_name' => $name, |
| 159 |
'all_items' => __( 'All Items', 'content-blocks-builder' ), |
| 160 |
'new_item_name' => __( 'New Item Name', 'content-blocks-builder' ), |
| 161 |
'add_new_item' => __( 'Add New Item', 'content-blocks-builder' ), |
| 162 |
'edit_item' => __( 'Edit Item', 'content-blocks-builder' ), |
| 163 |
'update_item' => __( 'Update Item', 'content-blocks-builder' ), |
| 164 |
'view_item' => __( 'View Item', 'content-blocks-builder' ), |
| 165 |
'separate_items_with_commas' => __( 'Separate items with commas', 'content-blocks-builder' ), |
| 166 |
'add_or_remove_items' => __( 'Add or remove items', 'content-blocks-builder' ), |
| 167 |
'choose_from_most_used' => __( 'Choose from the most used', 'content-blocks-builder' ), |
| 168 |
'popular_items' => __( 'Popular Items', 'content-blocks-builder' ), |
| 169 |
'search_items' => __( 'Search Items', 'content-blocks-builder' ), |
| 170 |
'not_found' => __( 'Not Found', 'content-blocks-builder' ), |
| 171 |
] |
| 172 |
); |
| 173 |
|
| 174 |
$args = wp_parse_args( |
| 175 |
$args, |
| 176 |
[ |
| 177 |
'labels' => $labels, |
| 178 |
'hierarchical' => false, |
| 179 |
'public' => false, |
| 180 |
'show_ui' => true, |
| 181 |
'show_in_menu' => false, |
| 182 |
'show_admin_column' => true, |
| 183 |
'show_in_nav_menus' => false, |
| 184 |
'show_tagcloud' => false, |
| 185 |
'query_var' => false, |
| 186 |
'show_in_rest' => true, |
| 187 |
] |
| 188 |
); |
| 189 |
|
| 190 |
register_taxonomy( $taxonomy_name, $post_type, $args ); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Register custom meta field |
| 195 |
* |
| 196 |
* @param string $post_type |
| 197 |
* @param string $meta_key |
| 198 |
* @param array $args |
| 199 |
* @return void |
| 200 |
*/ |
| 201 |
public function register_meta( $post_type, $meta_key, $args = [] ) { |
| 202 |
register_meta( |
| 203 |
'post', |
| 204 |
$meta_key, |
| 205 |
wp_parse_args( |
| 206 |
$args, |
| 207 |
[ |
| 208 |
'object_subtype' => $post_type, |
| 209 |
'show_in_rest' => true, |
| 210 |
'single' => true, |
| 211 |
'type' => 'string', |
| 212 |
'default' => '', |
| 213 |
] |
| 214 |
) |
| 215 |
); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Add custom thumbnail column to custom post type |
| 220 |
* |
| 221 |
* @param array $columns The array of columns. |
| 222 |
* @return array |
| 223 |
*/ |
| 224 |
public function add_thumbnail_custom_columns( $columns ) { |
| 225 |
// Thumbnail column. |
| 226 |
$thumbnail_column = [ |
| 227 |
'thumbnail' => esc_html__( 'Thumbnail', 'content-blocks-builder' ), |
| 228 |
]; |
| 229 |
|
| 230 |
// Put the thumbnail column right after the first column. |
| 231 |
$return = array_slice( $columns, 0, 1, true ) + $thumbnail_column + array_slice( $columns, 1, count( $columns ) - 1, true ); |
| 232 |
|
| 233 |
return $return; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Manage custom columns. |
| 238 |
* |
| 239 |
* @param string $column the column name. |
| 240 |
* @param int $post_id the post id. |
| 241 |
*/ |
| 242 |
public function manage_thunbnail_custom_columns( $column, $post_id ) { |
| 243 |
switch ( $column ) { |
| 244 |
case 'thumbnail': |
| 245 |
echo get_the_post_thumbnail( $post_id, [ 60, 60 ] ); |
| 246 |
break; |
| 247 |
|
| 248 |
// Just break out of the switch statement for everything else. |
| 249 |
default: |
| 250 |
break; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Add style to custom columns |
| 256 |
* |
| 257 |
* @return void |
| 258 |
*/ |
| 259 |
public function add_thumbnail_custom_columns_style() { |
| 260 |
echo '<style>.column-thumbnail {width: 70px}</style>'; |
| 261 |
} |
| 262 |
} |
| 263 |
endif; |
| 264 |
|