Skip to content

Commit 5be9833

Browse files
author
hsehszroc
committed
Initial commit
0 parents  commit 5be9833

File tree

10 files changed

+4631
-0
lines changed

10 files changed

+4631
-0
lines changed

Includes/API/API.php

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
<?php
2+
/**
3+
* Custom Post Type and Taxonomy Registration API.
4+
*
5+
* API functions easier for namespacing.
6+
*
7+
* @package TheWebSolver\Core\CPT_Framework\API
8+
*/
9+
10+
namespace TheWebSolver\Register;
11+
12+
use TheWebSolver\Core\Cpt\Post_Type;
13+
use TheWebSolver\Core\Cpt\Taxonomy;
14+
use TheWebSolver\Core\Helper\CPT_Factory;
15+
16+
/**
17+
* Registers new post type.
18+
*
19+
* Alias for:
20+
* ```
21+
* (new TheWebSolver\Core\Cpt\Post_Type($names))
22+
* ->set_labels($labels)
23+
* ->set_args($args)
24+
* ->assign_objects($taxonomies)
25+
* ->set_redirect($redirect_frontend)
26+
* ->set_filter($filter_keys)
27+
* ->manage_columns($manage_columns)
28+
* ->start_registration();
29+
* ```
30+
*
31+
* TODO: Integrate migrate codes: {@link https://plugins.trac.wordpress.org/browser/post-type-switcher/trunk/post-type-switcher.php}
32+
*
33+
* @param array $names **required** An array of names.
34+
* * @type string `key` - Post Type Name/Key
35+
* #### - Highly recommended to use `-` (dash) or `_` (underscore) between words and not ` ` (space).
36+
* #### - _POST TYPE NAME/KEY SHOULD NOT BE CHANGED ONCE SET_. If changed, another CPT will get registered. Set {@see @param `$migrate`} value to `true` if you changed post type name/key and want to transfer all posts created from this post type to newly changed post type name/key.
37+
* * @type string `singular_name` - default value for `$args['labels']['singular_name']`
38+
* * @type string `name` - default value for `$args['labels']['name']`
39+
* * @type string `slug` - default value for `$args['rewrite']['slug']`
40+
*
41+
* @param array $labels {@see @method `CPT_Factory::get_labels()`}.\
42+
* {@link https://developer.wordpress.org/reference/functions/get_post_type_labels/}:
43+
*
44+
* @param array $args {@see @method `CPT_Factory::get_args()`}.\
45+
* {@link https://developer.wordpress.org/reference/functions/register_post_type/}.
46+
* #### - `show_ui`, `show_in_menu` needs to be set to `true` for admin use. Defaults to `public`.
47+
* #### - `show_in_rest => true`, `supports => ['editor']` to be set for using "Gutenberg Blocks".
48+
*
49+
* @param string|string[] $taxonomies Taxonomy key in string or array of keys to assign to post type.
50+
* #### Keep in mind, when supplying taxonomy, it should already be registered.
51+
*
52+
* @param array $redirect_frontend Whether to redirect posts url in frontend.
53+
* Redirection happens if set to `true`.\
54+
* `$args` value will also be overridden as below:
55+
* > `public => false`\
56+
* > `publicly_queryable => true`\
57+
* > `has_archive => true`\
58+
* > `query_var => false`
59+
* * @type `bool` `$enabled` Frontend redirection enabled or not. Defaults to `false`.
60+
* * @type `int` `response_code` HTTP Status Response Code for redirection. Defaults to `302 (moved temporarily)`.
61+
*
62+
* @param string|string[] $filter_keys Key in string or array of keys for filtering admin table. {@see @method `CPT_Filter_List::with_keys()`}
63+
*
64+
* @param array $manage_columns An array of column data to add/remove/sort admin table. {@see @method `CPT_Column::apply()`}
65+
*
66+
* @return WP_Post_Type|WP_Error|false
67+
*
68+
* @link https://developer.wordpress.org/reference/functions/register_post_type/
69+
*
70+
* @since 1.0
71+
*/
72+
function post_type(
73+
array $names,
74+
$labels = [],
75+
$args = [],
76+
$taxonomies = '',
77+
$redirect_frontend = ['enabled' => false, 'response_code' => 302],
78+
$filter_keys = '',
79+
$manage_columns = [],
80+
$migrate = false
81+
) {
82+
83+
// TWS Post Type API.
84+
$post_type = new Post_Type($names);
85+
$post_type
86+
->set_labels($labels)
87+
->set_args($args)
88+
->assign_objects($taxonomies)
89+
->set_redirect($redirect_frontend)
90+
->set_filter($filter_keys)
91+
->manage_columns($manage_columns)
92+
->start_registration();
93+
}
94+
95+
/**
96+
* Registers new taxonomy.
97+
*
98+
* Alias for:
99+
* ```
100+
* (new TheWebSolver\Core\Cpt\Taxonomy($names))
101+
* ->set_labels($labels)
102+
* ->set_args($args)
103+
* ->assign_objects($post_types)
104+
* ->set_redirect($redirect_frontend)
105+
* ->set_filter($as_filter)
106+
* ->manage_columns($manage_columns)
107+
* ->start_registration();
108+
* ```
109+
*
110+
* @param array $names **required** An array of names.
111+
* * @type string `key` - Taxonomy Name/Key
112+
* #### - Highly recommended to use `-` (dash) or `_` (underscore) between words and not ` ` (space).
113+
* #### - _TAXONOMY NAME/KEY SHOULD NOT BE CHANGED ONCE SET_.
114+
* * @type string `singular_name` - default value for `$args['labels']['singular_name']`
115+
* * @type string `name` - default value for `$args['labels']['name']`
116+
* * @type string `slug` - default value for `$args['rewrite']['slug']`
117+
*
118+
* @param array $labels {@see @method `CPT_Factory::get_labels()`}.\
119+
* {@link https://developer.wordpress.org/reference/functions/get_taxonomy_labels/}:
120+
*
121+
* @param array $args {@see @method `CPT_Factory::get_args()`}.\
122+
* {@link https://developer.wordpress.org/reference/functions/register_taxonomy/}.
123+
*
124+
* @param string|string[] $post_types Post type key in string or array of keys to assign this taxonomy to.
125+
*
126+
* @param array $redirect_frontend Whether to redirect taxonomy archives in frontend.
127+
* Redirection happens if set to `true`.\
128+
* `$args` value will also be overridden as below:
129+
* > `public => false`\
130+
* > `publicly_queryable => true`\
131+
* > `query_var => false`
132+
* * @type `bool` `$enabled` Frontend redirection enabled or not. Defaults to `false`.
133+
* * @type `string/string[]` `$terms` Taxonomy terms where redirect to happen. Defaults to empty `string` (redirects on all terms page).
134+
* * @type `int` `response_code` HTTP Status Response Code for redirection. Defaults to `302 (moved temporarily)`.
135+
*
136+
* @param bool $as_filter Whether to use this taxonomy as filter for admin table.\
137+
* {@see @method `CPT_Factory::get_args()`}.
138+
*
139+
* @param array $manage_columns An array of column data to add/remove/sort admin table. {@see @method CPT_Column::apply()}
140+
*
141+
* @return WP_Taxonomy|WP_Error|false
142+
*
143+
* @since 1.0
144+
*/
145+
function taxonomy(
146+
array $names,
147+
$labels = [],
148+
$args = [],
149+
$post_types = '',
150+
$redirect_frontend = ['enabled' => false, 'terms' => '', 'response_code' => 302],
151+
$as_filter = true,
152+
$manage_columns = []
153+
) {
154+
155+
// TWS Taxonomy API.
156+
$taxonomy = new Taxonomy($names);
157+
$taxonomy
158+
->set_labels($labels)
159+
->set_args($args)
160+
->assign_objects($post_types)
161+
->set_redirect($redirect_frontend)
162+
->set_filter($as_filter)
163+
->manage_columns($manage_columns)
164+
->start_registration();
165+
}
166+
167+
/**
168+
* Sets post type and it's filtration keys to global vars.
169+
*
170+
* Alias for:
171+
* ```
172+
* CPT_Factory::set_post_type_filters( string $post_type, $keys = '');
173+
* ```
174+
*
175+
* @param string $post_type **required** The current post type admin page.
176+
* @param string|string[] $keys **required** The filtration taxonomy/post_meta keys.
177+
*
178+
* @return void
179+
*
180+
* @global array $tws_post_type_filters
181+
*
182+
* @since 1.0
183+
*/
184+
function filter_post_list_by( string $post_type, $keys ) {
185+
// TWS Post Type Filter API.
186+
CPT_Factory::set_post_type_filters( $post_type, $keys );
187+
}
188+
189+
/**
190+
* Manage columns for post_type/taxonomy admin table.
191+
*
192+
* Alias for:
193+
* ```
194+
* // If `$is_post` is `true`.
195+
* CPT_Factory::set_post_type_columns( $key, $args );
196+
* // If `$is_post` is `false`.
197+
* CPT_Factory::set_taxonomy_columns( $key, $args );
198+
* ```
199+
*
200+
* @param string $key **required** Post type key|Taxonomy key.
201+
* @param array $args **required** Column data in an array {@see @example _below_}.
202+
* @param bool $is_post _optional_ True for post type, false for taxonomy. Defaults to `true`.
203+
*
204+
* @return void
205+
*
206+
* @since 1.0
207+
*
208+
* @example usage:
209+
* #### For setting args:
210+
* ```
211+
* $args = [
212+
* 'column_key' => [
213+
* // Set as human friendly table header name.
214+
* 'label' => __( 'Column label', HZFEX_TEXTDOMAIN ),
215+
*
216+
* // Set `sortable` value to `true` if `meta_key_used_in_callback` is same as `column_key` and other as default.
217+
* // Set `sortable` value to `false` to disable sorting of the column.
218+
* // Set `sortable` value to `meta_key_used_in_callback` (as `string`) if other as default.
219+
* // Set `sortable` value to `array` as shown below if value is different than default:
220+
* // `key` - meta key used in callback function. Defaults to `column_key`.
221+
* // `type` - meta type. Defaults to "CHAR".
222+
* // `compare` - compare operator. Defaults to "=".
223+
* 'sortable' => [
224+
* 'key' => 'column_key',
225+
* 'type' => 'CHAR',
226+
* 'compare' => '=',
227+
* ],
228+
*
229+
* // Function to display content in the `column_key` column.
230+
* // Passed parameters ($is_post = true): $column` and `$post_id`.
231+
* // Passed parameters ($is_post = false): `$content`, `$column` and `$term_id`.
232+
* 'callback' => 'function_name_that_display_content',
233+
* 'priority' => 3, // "0" will be first, "1" will be second, and so on. If want to order all columns properly, all columns must be set with this key/value.
234+
* ],
235+
* ];
236+
* ```
237+
*/
238+
function manage_column_by( string $key, array $args, bool $is_post = true ) {
239+
if( $is_post ) {
240+
CPT_Factory::set_post_type_columns( $key, $args );
241+
} else {
242+
CPT_Factory::set_taxonomy_columns( $key, $args );
243+
}
244+
}

0 commit comments

Comments
 (0)