Skip to content

Commit 40eddeb

Browse files
committed
Custom taxonomies + bug fixes
1 parent 1931ddd commit 40eddeb

File tree

4 files changed

+91
-8
lines changed

4 files changed

+91
-8
lines changed

autoload.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,32 @@ public function rest_api() {
298298
}
299299

300300

301+
/**
302+
* Instantiate REST API
303+
*
304+
* @return Void
305+
*/
306+
public function prevent_unauthorized_rest_access( $result ) {
307+
// If a previous authentication check was applied,
308+
// pass that result along without modification.
309+
if ( true === $result || is_wp_error( $result ) ) {
310+
return $result;
311+
}
312+
313+
// No authentication has been performed yet.
314+
// Return an error if user is not logged in.
315+
if ( ! is_user_logged_in() ) {
316+
return new WP_Error(
317+
'rest_not_logged_in',
318+
__( 'You are not currently logged in.' ),
319+
array( 'status' => 401 )
320+
);
321+
}
322+
323+
return $result;
324+
}
325+
326+
301327
/**
302328
* Add the functionality files
303329
* Available classes: PLUGIN_INSTALL, PLUGIN_DB, PLUGIN_METABOX, PLUGIN_QUERY, PLUGIN_SETTINGS, PLUGIN_SHORTCODE, PLUGIN_WIDGET
@@ -353,6 +379,8 @@ public function init() {
353379
register_uninstall_hook( PLUGIN_FILE, array( 'PLUGIN_BUILD', 'db_uninstall' ) );
354380
register_uninstall_hook( PLUGIN_FILE, array( 'PLUGIN_BUILD', 'cron_uninstall' ) );
355381

382+
add_filter( 'rest_authentication_errors', array( $this, 'prevent_unauthorized_rest_access' ) );
383+
356384
add_action( 'init', array( $this, 'installation' ) );
357385
add_action( 'init', array( $this, 'custom_cron_hook_cb' ) );
358386
add_action( 'init', array( $this, 'cpt' ) );

src/class-cpt.php

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,13 @@ public function __construct() {
3737
$this->labels = $this->labels();
3838
$this->args = $this->args( $this->labels );
3939

40+
$this->taxonomy_labels = $this->taxonomy_labels();
41+
$this->taxonomy_args = $this->taxonomy_args( $this->taxonomy_labels );
42+
4043
//register_post_type( 'cpt_name', $this->args );
4144
//add_filter( 'post_updated_messages', array( $this, 'group_updated_messages' ) );
45+
46+
//register_taxonomy( 'custom_tax', array( 'cpt_name' ), $this->taxonomy_args );
4247
}
4348

4449

@@ -70,7 +75,7 @@ public function labels() {
7075

7176

7277
/**
73-
* Define the arguments
78+
* Define the arguments for custom post type
7479
*
7580
* @param Array $labels
7681
*
@@ -83,7 +88,7 @@ public function args( $labels ) {
8388
'description' => __( '', 'textdomain' ),
8489
'labels' => $labels,
8590
'supports' => array( 'title', 'editor', 'thumbnail' ),
86-
'taxonomies' => array( 'topics', 'post_tag' ),
91+
'taxonomies' => array( 'custom_tax', 'post_tag' ),
8792
'hierarchical' => true,
8893
'public' => true,
8994
'rewrite' => array( 'slug' => 'slug_name' ),
@@ -100,7 +105,7 @@ public function args( $labels ) {
100105
'capability_type' => 'post',
101106
'show_in_rest' => true,
102107
//Controls WP REST API behaviour
103-
'rest_controller_class' => 'WP_REST_Terms_Controller',
108+
'rest_controller_class' => 'WP_REST_Posts_Controller',
104109
);
105110

106111
return $args;
@@ -134,5 +139,56 @@ public function cpt_updated_messages( $messages ) {
134139

135140
return $messages;
136141
}
142+
143+
144+
/**
145+
* Taxonomy labels
146+
*
147+
* @return Array
148+
*/
149+
public function taxonomy_labels() {
150+
151+
$labels = array(
152+
'name' => _x( 'Taxonomy', 'taxonomy general name', 'textdomain' ),
153+
'singular_name' => _x( 'Taxonomy', 'taxonomy singular name', 'textdomain' ),
154+
'search_items' => __( 'Search Taxonomy', 'textdomain' ),
155+
'all_items' => __( 'All Taxonomies', 'textdomain' ),
156+
'parent_item' => __( 'Parent Taxonomy', 'textdomain' ),
157+
'parent_item_colon' => __( 'Parent Taxonomy:', 'textdomain' ),
158+
'edit_item' => __( 'Edit Taxonomy', 'textdomain' ),
159+
'update_item' => __( 'Update Taxonomy', 'textdomain' ),
160+
'add_new_item' => __( 'Add New Taxonomy', 'textdomain' ),
161+
'new_item_name' => __( 'New Taxonomy Name', 'textdomain' ),
162+
'menu_name' => __( 'Taxonomy', 'textdomain' ),
163+
);
164+
165+
return $labels;
166+
}
167+
168+
169+
/**
170+
* Define the arguments for custom taxonomy
171+
*
172+
* @param Array $labels
173+
*
174+
* @return Array
175+
*/
176+
public function taxonomy_args( $labels ) {
177+
178+
$args = array(
179+
'hierarchical' => true,
180+
'labels' => $labels,
181+
'show_ui' => true,
182+
'show_admin_column' => true,
183+
'query_var' => true,
184+
'rewrite' => array( 'slug' => 'custom_tax' ),
185+
'show_in_rest' => true,
186+
'rest_base' => 'custom_tax',
187+
//Controls WP REST API behaviour
188+
'rest_controller_class' => 'WP_REST_Terms_Controller',
189+
);
190+
191+
return $args;
192+
}
137193
}
138194
}

src/class-rest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
<?php
32
if ( ! defined( 'ABSPATH' ) ) exit;
43

54
/**
@@ -29,8 +28,8 @@ public function register_routes() {
2928
'methods' => WP_REST_Server::READABLE,
3029
'callback' => array( $this, 'callback' ),
3130
'permission_callback' => array( $this, 'permission' ),
32-
'args' => array('sample', 'list', 'of', 'args'),
33-
),
31+
'args' => array('sample', 'list', 'of', 'args')
32+
));
3433
}
3534

3635

@@ -72,7 +71,7 @@ public function permission() {
7271
*
7372
* @return Array
7473
*/
75-
protected function prepare_item_for_response($items, $request) {
74+
public function prepare_item_for_response($items, $request) {
7675

7776
//Process the data in any way you like
7877
$data = compact($items, $request);

wp-plugin-framework.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Plugin Name: WordPress Plugin Framework
44
Plugin URI: https://github.com/nirjharlo/wp-plugin-framework/
55
Description: Simple and Light WordPress plugin development framework for organized Object Oriented code for Developers.
6-
Version: 1.4
6+
Version: 1.4.1
77
Author: Nirjhar Lo
88
Author URI: http://nirjharlo.com
99
Text Domain: textdomain

0 commit comments

Comments
 (0)