Skip to content

Commit

Permalink
Merge pull request #158 from digitoimistodude/cpt-tax-register-simpli…
Browse files Browse the repository at this point in the history
…fication

Simplify CPT and taxonomy registration by using the class name also as a slug
  • Loading branch information
ronilaukkarinen authored Nov 3, 2022
2 parents 9068a58 + 658af4f commit 0d18586
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
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

0 comments on commit 0d18586

Please sign in to comment.