-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract search filters to their own classes Use consistent taxonomy
- Loading branch information
Showing
6 changed files
with
288 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace P4\MasterTheme\Search\Filters; | ||
|
||
/** | ||
* Categories used for search. | ||
*/ | ||
class Categories { | ||
/** | ||
* @return array<WP_Term> | ||
*/ | ||
public static function get_all(): array { | ||
return get_categories(); | ||
} | ||
|
||
/** | ||
* @return array{int, array{term_id: int, name: string, results: int}} | ||
*/ | ||
public static function get_filters(): array { | ||
$categories = self::get_all(); | ||
$filters = []; | ||
|
||
foreach ( $categories as $category ) { | ||
if ( 'uncategorised' === $category->slug ) { | ||
continue; | ||
} | ||
|
||
$filters[ $category->term_id ] = [ | ||
'term_id' => $category->term_id, | ||
'name' => $category->name, | ||
'results' => 0, | ||
]; | ||
} | ||
|
||
return $filters; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace P4\MasterTheme\Search\Filters; | ||
|
||
/** | ||
* Content type used for search. | ||
* Native types (post, page, attachment, etc.). | ||
* Custom types (p4_action, etc.). | ||
*/ | ||
class ContentTypes { | ||
public const ACT_PAGE = 'action'; | ||
public const POST = 'post'; | ||
public const PAGE = 'page'; | ||
public const CAMPAIGN = 'campaign'; | ||
public const ATTACHMENT = 'attachment'; | ||
public const ARCHIVE = 'archive'; | ||
|
||
/** | ||
* Get all content types. | ||
* | ||
* @return array<WP_Post_Type> | ||
*/ | ||
public static function get_all(): array { | ||
$config = self::get_config(); | ||
$types = get_post_types( | ||
[ | ||
'public' => true, | ||
'exclude_from_search' => false, | ||
], | ||
false | ||
); | ||
|
||
return array_filter( | ||
$types, | ||
function ( $type ) use ( $config ) { | ||
return isset( $config[ $type->name ] ); | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* @param bool $include_archive Include archive type. | ||
* | ||
* @return array{string, array{id: int, name: string, results: int}} | ||
*/ | ||
public static function get_filters( | ||
bool $include_archive = false | ||
): array { | ||
$types = self::get_all(); | ||
$config = self::get_config(); | ||
|
||
// ACT_PAGE are sub-pages of the Act page and not a real type, adding manually. | ||
$filters = [ | ||
0 => [ | ||
'id' => 0, | ||
'name' => __( 'Action', 'planet4-master-theme' ), | ||
'results' => 0, | ||
], | ||
]; | ||
|
||
foreach ( $types as $name => $type ) { | ||
if ( self::ARCHIVE === $name && ! $include_archive ) { | ||
continue; | ||
} | ||
|
||
$type_data = $config[ $type->name ] ?? null; | ||
if ( ! $type_data ) { | ||
continue; | ||
} | ||
|
||
$filters[ $type_data['id'] ] = [ | ||
'id' => $type_data['id'], | ||
'name' => $type_data['label'], | ||
'results' => 0, | ||
]; | ||
} | ||
|
||
return $filters; | ||
} | ||
|
||
/** | ||
* Get all content type config for search | ||
* | ||
* @return array{string, array{id: int, label: string}} | ||
*/ | ||
public static function get_config(): array { | ||
return [ | ||
self::ACT_PAGE => [ | ||
'id' => 0, | ||
'label' => __( 'Action', 'planet4-master-theme' ), | ||
], | ||
self::ATTACHMENT => [ | ||
'id' => 1, | ||
'label' => __( 'Document', 'planet4-master-theme' ), | ||
], | ||
self::PAGE => [ | ||
'id' => 2, | ||
'label' => __( 'Page', 'planet4-master-theme' ), | ||
], | ||
self::POST => [ | ||
'id' => 3, | ||
'label' => __( 'Post', 'planet4-master-theme' ), | ||
], | ||
self::CAMPAIGN => [ | ||
'id' => 4, | ||
'label' => __( 'Campaign', 'planet4-master-theme' ), | ||
], | ||
self::ARCHIVE => [ | ||
'id' => 5, | ||
'label' => __( 'Archive', 'planet4-master-theme' ), | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace P4\MasterTheme\Search\Filters; | ||
|
||
use WP_Term; | ||
|
||
/** | ||
* Post types used for search (Press release, News, Report, etc.). | ||
* Configured in `Posts > Posts Types` | ||
*/ | ||
class PostTypes { | ||
/** | ||
* @return WP_Term[] | ||
*/ | ||
public static function get_all(): array { | ||
$types = get_terms( | ||
[ | ||
'taxonomy' => 'p4-page-type', | ||
'hide_empty' => false, | ||
] | ||
); | ||
|
||
if ( is_wp_error( $types ) | ||
|| ! is_array( $types ) | ||
|| empty( $types ) | ||
|| ! ( $types[0] instanceof WP_Term ) | ||
) { | ||
return []; | ||
} | ||
|
||
return $types; | ||
} | ||
|
||
/** | ||
* @return array{int, array{term_id: int, name: string, results: int}} | ||
*/ | ||
public static function get_filters(): array { | ||
$post_types = self::get_all(); | ||
$filters = []; | ||
|
||
foreach ( $post_types as $post_type ) { | ||
$filters[ $post_type->term_id ] = [ | ||
'term_id' => $post_type->term_id, | ||
'name' => $post_type->name, | ||
'results' => 0, | ||
]; | ||
} | ||
|
||
return $filters; | ||
} | ||
} |
Oops, something went wrong.