| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Modules\DisplayConditions\Conditions; |
| 4 |
|
| 5 |
class Singular extends Condition { |
| 6 |
protected $sub_conditions = [ |
| 7 |
'front', |
| 8 |
'page', |
| 9 |
'error' |
| 10 |
]; |
| 11 |
|
| 12 |
public function get_type(): string { |
| 13 |
return 'singular'; |
| 14 |
} |
| 15 |
|
| 16 |
public function get_priority(): int { |
| 17 |
return 60; |
| 18 |
} |
| 19 |
|
| 20 |
public function get_label(): string { |
| 21 |
return __( 'Singular', 'templately' ); |
| 22 |
} |
| 23 |
|
| 24 |
public function get_all_label(): string { |
| 25 |
return __( 'All Singular', 'templately' ); |
| 26 |
} |
| 27 |
|
| 28 |
public function get_name(): string { |
| 29 |
return 'singular'; |
| 30 |
} |
| 31 |
|
| 32 |
public function check( $args = [] ): bool { |
| 33 |
return ( is_singular() && ! is_embed() ) || is_404(); |
| 34 |
} |
| 35 |
|
| 36 |
protected function register_sub_conditions() { |
| 37 |
$post_types = $this->builder->get_public_post_types(); |
| 38 |
|
| 39 |
foreach ( $post_types as $post_type => $label ) { |
| 40 |
// Do NOT gate on get_post_type_archive_link() here — that guard belongs to |
| 41 |
// Archive (an archive only exists when the CPT publishes one). A SINGLE |
| 42 |
// entry exists regardless of has_archive, so gating it wrongly excluded |
| 43 |
// has_archive=false CPTs (e.g. testimonials, team members) from ever having |
| 44 |
// a Single template. Skip only post types already registered via the static |
| 45 |
// $sub_conditions list ('page' → the Page condition) to avoid a duplicate. |
| 46 |
if ( in_array( $post_type, $this->sub_conditions, true ) ) { |
| 47 |
continue; |
| 48 |
} |
| 49 |
|
| 50 |
// A post type only belongs here if it actually has viewable single pages. |
| 51 |
// Cheap before ThemeBuilder gated on `show_in_nav_menus`; load-bearing now |
| 52 |
// that it gates on `public`, which admits post types with no front-end |
| 53 |
// single view at all. |
| 54 |
if ( ! is_post_type_viewable( $post_type ) ) { |
| 55 |
continue; |
| 56 |
} |
| 57 |
|
| 58 |
$condition = new Post( [ |
| 59 |
'post_type' => $post_type, |
| 60 |
] ); |
| 61 |
|
| 62 |
$this->register_sub_condition( $condition ); |
| 63 |
} |
| 64 |
} |
| 65 |
} |