PluginProbe
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts / 2.7.6
Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts v2.7.6
2.8.14 2.8.13 2.8.12 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.10 2.8.11 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 trunk 1.0.1 1.0.2 1.1.0 1.1.1 All 147 releases
content-blocks-builder / includes / post-type.php

post-type.php in Content Blocks Builder – Create blocks, repeater blocks with carousel, grid, popup layouts 2.7.6, at includes/post-type.php

254 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * A dummy 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 _doing_it_wrong( __CLASS__ . '->' . __FUNCTION__, __( 'name, singular_name labels are required', 'content-blocks-builder' ) );
68 return;
69 }
70
71 $name = $labels['name'];
72 $singular_name = $labels['singular_name'];
73
74 // Post type args.
75 $labels = wp_parse_args(
76 $labels,
77 [
78 'name' => $name,
79 'singular_name' => $singular_name,
80 'menu_name' => $name,
81 'name_admin_bar' => $singular_name,
82 'add_new' => sprintf( __( 'Add New %s', 'content-blocks-builder' ), $singular_name ),
83 'add_new_item' => sprintf( __( 'Add New %s', 'content-blocks-builder' ), $singular_name ),
84 'new_item' => sprintf( __( 'New %s', 'content-blocks-builder' ), $singular_name ),
85 'edit_item' => sprintf( __( 'Edit %s', 'content-blocks-builder' ), $singular_name ),
86 'view_item' => sprintf( __( 'View %s', 'content-blocks-builder' ), $singular_name ),
87 'all_items' => sprintf( __( 'All %s', 'content-blocks-builder' ), $name ),
88 'search_items' => sprintf( __( 'Search %s', 'content-blocks-builder' ), $name ),
89 'parent_item_colon' => __( 'Parent item:', 'content-blocks-builder' ),
90 'not_found' => __( 'No items found.', 'content-blocks-builder' ),
91 'not_found_in_trash' => __( 'No items found in trash.', 'content-blocks-builder' ),
92 ]
93 );
94
95 $args = wp_parse_args(
96 $args,
97 [
98 'labels' => $labels,
99 'description' => sprintf( __( 'All %s', 'content-blocks-builder' ), $name ),
100 'public' => true,
101 'publicly_queryable' => false,
102 'exclude_from_search' => true,
103 'show_ui' => true,
104 'show_in_menu' => true,
105 'show_in_nav_menus' => false,
106 'show_in_admin_bar' => true,
107 'show_in_rest' => true,
108 'query_var' => false,
109 'rewrite' => false,
110 'capability_type' => 'post',
111 'has_archive' => false,
112 'hierarchical' => false,
113 'menu_position' => 5,
114 'supports' => [ 'title', 'editor', 'author', 'custom-fields', 'revisions', 'page-attributes' ],
115 'can_export' => true,
116 'template_lock' => 'insert',
117 ]
118 );
119
120 // Register post type.
121 register_post_type( $post_type, $args );
122 }
123
124 /**
125 * Register a custom taxonomy
126 *
127 * @param string $taxonomy_name
128 * @param string|array $post_type
129 * @param array $args
130 * @param array $labels
131 * @return void
132 */
133 public function register_taxonomy( $taxonomy_name, $post_type, $args = [], $labels = [] ) {
134 if ( empty( $labels['name'] ) || empty( $labels['singular_name'] ) ) {
135 _doing_it_wrong( __CLASS__ . '->' . __FUNCTION__, __( 'name, singular_name labels are required', 'content-blocks-builder' ) );
136 return;
137 }
138
139 $name = $labels['name'];
140 $singular_name = $labels['singular_name'];
141
142 // Taxonomy args.
143 $labels = wp_parse_args(
144 $labels,
145 [
146 'name' => $name,
147 'singular_name' => $singular_name,
148 'menu_name' => $name,
149 'all_items' => __( 'All Items', 'content-blocks-builder' ),
150 'new_item_name' => __( 'New Item Name', 'content-blocks-builder' ),
151 'add_new_item' => __( 'Add New Item', 'content-blocks-builder' ),
152 'edit_item' => __( 'Edit Item', 'content-blocks-builder' ),
153 'update_item' => __( 'Update Item', 'content-blocks-builder' ),
154 'view_item' => __( 'View Item', 'content-blocks-builder' ),
155 'separate_items_with_commas' => __( 'Separate items with commas', 'content-blocks-builder' ),
156 'add_or_remove_items' => __( 'Add or remove items', 'content-blocks-builder' ),
157 'choose_from_most_used' => __( 'Choose from the most used', 'content-blocks-builder' ),
158 'popular_items' => __( 'Popular Items', 'content-blocks-builder' ),
159 'search_items' => __( 'Search Items', 'content-blocks-builder' ),
160 'not_found' => __( 'Not Found', 'content-blocks-builder' ),
161 ]
162 );
163
164 $args = wp_parse_args(
165 $args,
166 [
167 'labels' => $labels,
168 'hierarchical' => false,
169 'public' => false,
170 'show_ui' => true,
171 'show_in_menu' => false,
172 'show_admin_column' => true,
173 'show_in_nav_menus' => false,
174 'show_tagcloud' => false,
175 'query_var' => false,
176 'show_in_rest' => true,
177 ]
178 );
179
180 register_taxonomy( $taxonomy_name, $post_type, $args );
181 }
182
183 /**
184 * Register custom meta field
185 *
186 * @param string $post_type
187 * @param string $meta_key
188 * @param array $args
189 * @return void
190 */
191 public function register_meta( $post_type, $meta_key, $args = [] ) {
192 register_meta(
193 'post',
194 $meta_key,
195 wp_parse_args(
196 $args,
197 [
198 'object_subtype' => $post_type,
199 'show_in_rest' => true,
200 'single' => true,
201 'type' => 'string',
202 'default' => '',
203 ]
204 )
205 );
206 }
207
208 /**
209 * Add custom thumbnail column to custom post type
210 *
211 * @param array $columns The array of columns.
212 * @return array
213 */
214 public function add_thumbnail_custom_columns( $columns ) {
215 // Thumbnail column.
216 $thumbnail_column = [
217 'thumbnail' => esc_html__( 'Thumbnail', 'content-blocks-builder' ),
218 ];
219
220 // Put the thumbnail column right after the first column.
221 $return = array_slice( $columns, 0, 1, true ) + $thumbnail_column + array_slice( $columns, 1, count( $columns ) - 1, true );
222
223 return $return;
224 }
225
226 /**
227 * Manage custom columns.
228 *
229 * @param string $column the column name.
230 * @param int $post_id the post id.
231 */
232 public function manage_thunbnail_custom_columns( $column, $post_id ) {
233 switch ( $column ) {
234 case 'thumbnail':
235 echo get_the_post_thumbnail( $post_id, [ 60, 60 ] );
236 break;
237
238 // Just break out of the switch statement for everything else.
239 default:
240 break;
241 }
242 }
243
244 /**
245 * Add style to custom columns
246 *
247 * @return void
248 */
249 public function add_thumbnail_custom_columns_style() {
250 echo '<style>.column-thumbnail {width: 70px}</style>';
251 }
252 }
253 endif;
254