Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify CPT and taxonomy registration by using the class name also as a slug #158

Merged
merged 2 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### [Unreleased]

* Fix incorrect textdomain in external-link JS module
* Simplify CPT and taxonomy registration by using the class name also as a slug

### 9.2.1: 2022-10-28

Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ Air-light can register your CPT:s automatically.

```
'post_types' => [
'your-post-type' => 'Your_Post_Type'
'Your_Post_Type'
]
```

Expand All @@ -248,9 +248,8 @@ Air-light can register your Taxonomies automatically.
1. Add your taxonomy to theme settings under taxonomies, located in `functions.php` like this:

```
'your-taxonomy' => [
'name' => 'Your_Taxonomy'
'post_types' => 'post, page'
'taxonomies' => [
'Your_Taxonomy' => [ 'post', 'page', 'your-post-type' ]
]
```

Expand Down
11 changes: 4 additions & 7 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* own files under /inc and just require here.
*
* @Date: 2019-10-15 12:30:02
* @Last Modified by: Roni Laukkarinen
* @Last Modified time: 2022-10-28 10:17:27
* @Last Modified by: Timi Wahalahti
* @Last Modified time: 2022-11-03 16:44:56
*
* @package air-light
*/
Expand Down Expand Up @@ -92,10 +92,7 @@
* https://github.com/digitoimistodude/air-light#custom-taxonomies
*/
'taxonomies' => [
// 'your-taxonomy' => [
// 'name' => 'Your_Taxonomy',
// 'post_types' => [ 'post', 'page' ],
// ],
// 'Your_Taxonomy' => [ 'post', 'page' ],
],

/**
Expand All @@ -105,7 +102,7 @@
* https://github.com/digitoimistodude/air-light#custom-post-types
*/
'post_types' => [
// 'your-post-type' => 'Your_Post_Type',
// 'Your_Post_Type',
],

/**
Expand Down
32 changes: 18 additions & 14 deletions inc/includes/theme-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
/**
* Theme setup
*
* @Author: Niku Hietanen
* @Author: Timi Wahalahti
* @Date: 2020-02-20 13:46:50
* @Last Modified by: Roni Laukkarinen
* @Last Modified time: 2021-05-04 11:13:01
* @Last Modified by: Timi Wahalahti
* @Last Modified time: 2022-11-03 16:44:39
*
* @package air-light
* @package vierityspalkki
**/

namespace Air_Light;
Expand Down Expand Up @@ -45,21 +45,23 @@ function build_taxonomies() {
return;
}

foreach ( THEME_SETTINGS['taxonomies'] as $slug => $args ) {
$classname = __NAMESPACE__ . '\\' . $args['name'];
$file_path = get_theme_file_path( '/inc/taxonomies/' . $slug . '.php' );
foreach ( THEME_SETTINGS['taxonomies'] as $name => $post_types ) {
$slug = strtolower( $name );

$classname = __NAMESPACE__ . '\\' . $name;
$file_path = get_theme_file_path( '/inc/taxonomies/' . str_replace('_', '-', $slug ) . '.php' );

if ( ! file_exists( $file_path ) ) {
return new \WP_Error( 'invalid-taxonomy', __( 'The taxonomy class file does not exist.', 'air-light' ), $classname );
return new \WP_Error( 'invalid-taxonomy', __( 'The taxonomy class file does not exist.', 'vierityspalkki' ), $classname );
}
require $file_path;

if ( ! class_exists( $classname ) ) {
return new \WP_Error( 'invalid-taxonomy', __( 'The taxonomy you attempting to create does not have a class to instance. Possible problems: your configuration does not match the class file name; the class file name does not exist.', 'air-light' ), $classname );
return new \WP_Error( 'invalid-taxonomy', __( 'The taxonomy you attempting to create does not have a class to instance. Possible problems: your configuration does not match the class file name; the class file name does not exist.', 'vierityspalkki' ), $classname );
}

$taxonomy_class = new $classname( $slug );
$taxonomy_class->register( $args['post_types'] );
$taxonomy_class->register( $post_types );
}
}

Expand All @@ -71,18 +73,20 @@ function build_post_types() {
return;
}

foreach ( THEME_SETTINGS['post_types'] as $slug => $name ) {
foreach ( THEME_SETTINGS['post_types'] as $name ) {
$slug = strtolower( $name );

$classname = __NAMESPACE__ . '\\' . $name;
$file_path = get_theme_file_path( '/inc/post-types/' . $slug . '.php' );
$file_path = get_theme_file_path( '/inc/post-types/' . str_replace('_', '-', $slug ) . '.php' );

if ( ! file_exists( $file_path ) ) {
return new \WP_Error( 'invalid-taxonomy', __( 'The taxonomy class file does not exist.', 'air-light' ), $classname );
return new \WP_Error( 'invalid-cpt', __( 'The custom post type class file does not exist.', 'vierityspalkki' ), $classname );
}
// Get the class file
require $file_path;

if ( ! class_exists( $classname ) ) {
return new \WP_Error( 'invalid-taxonomy', __( 'The taxonomy you attempting to create does not have a class to instance. Possible problems: your configuration does not match the class file name; the class file name does not exist.', 'air-light' ), $classname );
return new \WP_Error( 'invalid-cpt', __( 'The custom post type you attempting to create does not have a class to instance. Possible problems: your configuration does not match the class file name; the class file name does not exist.', 'vierityspalkki' ), $classname );
}

$post_type_class = new $classname( $slug );
Expand Down