| 1 |
<?php |
| 2 |
|
| 3 |
namespace NotificationX\Core; |
| 4 |
use NotificationX\GetInstance; |
| 5 |
|
| 6 |
/** |
| 7 |
* @method static Locations get_instance($args = null) |
| 8 |
*/ |
| 9 |
class Locations { |
| 10 |
/** |
| 11 |
* Instance of Locations |
| 12 |
* |
| 13 |
* @var Locations |
| 14 |
*/ |
| 15 |
use GetInstance; |
| 16 |
|
| 17 |
public function __construct(){ |
| 18 |
|
| 19 |
|
| 20 |
} |
| 21 |
|
| 22 |
public function get_locations( $type = 'global' ) { |
| 23 |
$locations = array( |
| 24 |
'is_front_page' => __( 'Front page', 'notificationx' ), |
| 25 |
'is_home' => __( 'Blog page', 'notificationx' ), |
| 26 |
'is_singular' => __( 'All posts, pages and custom post types', 'notificationx' ), |
| 27 |
'is_single' => __( 'All posts', 'notificationx' ), |
| 28 |
'is_page' => __( 'All pages', 'notificationx' ), |
| 29 |
'is_attachment' => __( 'All attachments', 'notificationx' ), |
| 30 |
'is_search' => __( 'Search results', 'notificationx' ), |
| 31 |
'is_404' => __( '404 error page', 'notificationx' ), |
| 32 |
'is_archive' => __( 'All archives', 'notificationx' ), |
| 33 |
'is_category' => __( 'All category archives', 'notificationx' ), |
| 34 |
'is_tag' => __( 'All tag archives', 'notificationx' ), |
| 35 |
); |
| 36 |
|
| 37 |
if ( 'global' == $type ) { |
| 38 |
return $locations; |
| 39 |
} |
| 40 |
|
| 41 |
$post_types = Helper::post_types(); |
| 42 |
$taxonomies = Helper::taxonomies(); |
| 43 |
|
| 44 |
if ( ! empty( $post_types ) ) { |
| 45 |
|
| 46 |
unset( $post_types['post'] ); |
| 47 |
unset( $post_types['page'] ); |
| 48 |
unset( $post_types['elementor_library'] ); |
| 49 |
|
| 50 |
foreach ( $post_types as $slug => $type ) { |
| 51 |
|
| 52 |
// translators: %s: Post Type label |
| 53 |
$locations[ 'is_singular-' . $slug ] = sprintf( __('All %s posts', 'notificationx'), $type->label ); |
| 54 |
|
| 55 |
if ( $type->has_archive ) { |
| 56 |
// translators: %s: Post Type label |
| 57 |
$locations[ 'is_archive-' . $slug ] = sprintf( __('All %s archives', 'notificationx'), $type->label ); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
foreach ( $taxonomies as $slug => $tax ) { |
| 62 |
// translators: %s: Taxonomy label |
| 63 |
$locations[ 'is_tax-' . $slug ] = sprintf( __('All %s taxonomy archives', 'notificationx'), $tax->label ); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
return $locations; |
| 68 |
} |
| 69 |
|
| 70 |
public function check_location( $locations = array(), $custom_ids = '', $taxonomy_ids = '' ) { |
| 71 |
if ( empty( $locations ) ) { |
| 72 |
return true; |
| 73 |
} |
| 74 |
if ( !is_array( $locations ) ) { |
| 75 |
$locations = [$locations]; |
| 76 |
} |
| 77 |
|
| 78 |
$status = array( |
| 79 |
'is_front_page' => is_front_page(), |
| 80 |
'is_home' => is_home(), |
| 81 |
'is_singular' => is_singular(), |
| 82 |
'is_single' => is_singular( 'post' ), |
| 83 |
'is_page' => ( is_page() && ! is_front_page() ), |
| 84 |
'is_attachment' => is_attachment(), |
| 85 |
'is_search' => is_search(), |
| 86 |
'is_404' => is_404(), |
| 87 |
'is_archive' => is_archive(), |
| 88 |
'is_category' => is_category(), |
| 89 |
'is_tag' => is_tag(), |
| 90 |
); |
| 91 |
|
| 92 |
$status = apply_filters('nx_location_status', $status, $custom_ids); |
| 93 |
|
| 94 |
$post_types = Helper::post_types(); |
| 95 |
$taxonomies = Helper::taxonomies(); |
| 96 |
|
| 97 |
if ( ! empty( $post_types ) ) { |
| 98 |
|
| 99 |
unset( $post_types['post'] ); |
| 100 |
unset( $post_types['page'] ); |
| 101 |
unset( $post_types['elementor_library'] ); |
| 102 |
|
| 103 |
foreach ( $post_types as $slug => $type ) { |
| 104 |
|
| 105 |
$status[ 'is_singular-' . $slug ] = is_singular( $slug ); |
| 106 |
|
| 107 |
if ( $type->has_archive ) { |
| 108 |
$status[ 'is_archive-' . $slug ] = is_post_type_archive( $slug ); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
foreach ( $taxonomies as $slug => $tax ) { |
| 113 |
$status[ 'is_tax-' . $slug ] = is_tax( $slug ); |
| 114 |
} |
| 115 |
} |
| 116 |
// Check for specific taxonomy ID |
| 117 |
if ( in_array( 'is_taxonomy', $locations ) && ! empty($taxonomy_ids ) ) { |
| 118 |
$current_term_id = get_queried_object_id(); |
| 119 |
$ids_array = explode(',',$taxonomy_ids ); |
| 120 |
foreach ($ids_array as $id) { |
| 121 |
if ( $current_term_id == $id ) { |
| 122 |
$status['is_taxonomy'] = ( $current_term_id == $id ); |
| 123 |
break; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
$status_flag = false; |
| 129 |
foreach ( $locations as $location ) { |
| 130 |
if ( ! isset( $status[$location] ) || ! $status[$location] ) { |
| 131 |
continue; |
| 132 |
} else { |
| 133 |
$status_flag = true; |
| 134 |
} |
| 135 |
} |
| 136 |
return $status_flag; |
| 137 |
} |
| 138 |
} |