forked from plank/wcmtl-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwceh-api.php
187 lines (174 loc) · 4.95 KB
/
wceh-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* Plugin Name: WordCamp Canada API Demo
* Plugin URI: https://github.com/cgarofalo/wceh-api
* Description: A plugin demoing custom API endpoints.
* Version: 0.1
* Author: Christina Garofalo
* Author URI: https://profiles.wordpress.org/cold-iron-chef/
*
* @package wceh-api/plugin
**/
// Add the custom Post type
add_action( 'init', 'wcehapi_custom_post_type' );
// Add Taxonomies
add_action( 'init', 'wcehapi_taxonomy_colour' );
add_action( 'init', 'wcehapi_taxonomy_breed' );
add_action( 'init', 'wcehapi_taxonomy_pattern' );
// Load the API (All files in the api directory)
foreach ( glob( __DIR__ . '/api/*.php' ) as $file ) {
require $file;
}
/**
* Register Custom Post Type
*
* @return void
*/
function wcehapi_custom_post_type() {
register_post_type(
'wcehapi_cat',
[
'labels' => [
'name' => __( 'Cats', 'wcehapi' ),
'singular_name' => __( 'Cat', 'wcehapi' ),
'add_new' => __( 'Add New Cat', 'wcehapi' ),
'add_new_item' => __( 'Add New Cat', 'wcehapi' ),
'edit_item' => __( 'Edit Cat', 'wcehapi' ),
'all_items' => __( 'All Cats', 'wcehapi' ),
],
'public' => true, // This needs to be true so Polylang can see the post type.
'show_ui' => true,
'has_archive' => false,
'hierarchical' => false, // This is not a hierarchical post type.
'show_in_rest' => false, // Turn off block for this post type
'exclude_from_search' => true,
'menu_icon' => wcehapi_get_svg( 'cat' ), // Custom SVG icon, but a Dashicon slug can also be used.
'capability_type' => 'post', // Same capabilities as a post.
'rewrite' => [
'slug' => 'cats',
],
'supports' => [
'title',
'editor',
'thumbnail',
],
]
);
}
/**
* Register Custom Taxonomy
*
* @return void
*/
function wcehapi_taxonomy_breed() {
register_taxonomy(
'cat_breed',
'wcehapi_cat',
[
'labels' => [
'name' => __( 'Breeds', 'wcehapi' ),
'singular_name' => __( 'Breed', 'wcehapi' ),
'add_new' => __( 'Add New Breed', 'wcehapi' ),
'add_new_item' => __( 'Add New Breed', 'wcehapi' ),
],
'public' => true, // This needs to be true so Polylang can see the taxonomy.
'show_ui' => true,
'show_in_rest' => false,
'exclude_from_search' => true,
'hierarchical' => false,
'rewrite' => [
'slug' => 'breed',
],
]
);
}
/**
* Register Custom Taxonomy
*
* @return void
*/
function wcehapi_taxonomy_colour() {
register_taxonomy(
'cat_colour',
'wcehapi_cat',
[
'labels' => [
'name' => __( 'Colours', 'wcehapi' ),
'singular_name' => __( 'Colour', 'wcehapi' ),
'add_new' => __( 'Add New Colour', 'wcehapi' ),
'add_new_item' => __( 'Add New Colour', 'wcehapi' ),
],
'public' => true, // This needs to be true so Polylang can see the taxonomy.
'show_ui' => true,
'show_in_rest' => false,
'exclude_from_search' => true,
'hierarchical' => false,
'rewrite' => [
'slug' => 'Colour',
],
]
);
}
/**
* Register Custom Taxonomy
*
* @return void
*/
function wcehapi_taxonomy_pattern() {
register_taxonomy(
'cat_pattern',
'wcehapi_cat',
[
'labels' => [
'name' => __( 'Patterns', 'wcehapi' ),
'singular_name' => __( 'Pattern', 'wcehapi' ),
'add_new' => __( 'Add New Pattern', 'wcehapi' ),
'add_new_item' => __( 'Add New Pattern', 'wcehapi' ),
],
'public' => true, // This needs to be true so Polylang can see the taxonomy.
'show_ui' => true,
'show_in_rest' => false,
'exclude_from_search' => true,
'hierarchical' => false,
'rewrite' => [
'slug' => 'pattern',
],
]
);
}
/**
* Get svg icon for custom post type
*
* @param string $icon The icon name
*
* @return string
*/
function wcehapi_get_svg( $icon ) {
// Fallback to dashicons if the icon can't be found.
$svg = 'dashicons-admin-post';
if ( file_exists( __DIR__ . '/assets/' . $icon . '.svg' ) ) {
// The fill attribute needs to be set to "black" for WordPress to be able to change the colour.
$svg = 'data:image/svg+xml;base64,' . base64_encode( file_get_contents( __DIR__ . '/assets/' . $icon . '.svg' ) ); //phpcs:ignore
}
return $svg;
}
// Bonus: Restrict User endpoints
add_filter( 'rest_endpoints', 'wcehapi_restrict_user_endpoints' );
/**
* Remove user endpoints for unauthorized users.
*
* @param array $endpoints Array of endpoints
*
* @return array $endpoints Filtered list of endpoints
*/
function wcehapi_restrict_user_endpoints( $endpoints ) {
if ( ! is_user_logged_in() ) {
if ( isset( $endpoints['/wp/v2/users'] ) ) {
unset( $endpoints['/wp/v2/users'] );
}
if ( isset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] ) ) {
unset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] );
}
}
return $endpoints;
}