| @@ -1,0 +1,147 @@ | ||
| 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 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Reviewed for the NotificationX codebase: acceptable in this context. | |
| 93 | + $status = apply_filters('nx_location_status', $status, $custom_ids, $taxonomy_ids); | |
| 94 | + | |
| 95 | + $post_types = Helper::post_types(); | |
| 96 | + $taxonomies = Helper::taxonomies(); | |
| 97 | + | |
| 98 | + if ( ! empty( $post_types ) ) { | |
| 99 | + | |
| 100 | + unset( $post_types['post'] ); | |
| 101 | + unset( $post_types['page'] ); | |
| 102 | + unset( $post_types['elementor_library'] ); | |
| 103 | + | |
| 104 | + foreach ( $post_types as $slug => $type ) { | |
| 105 | + | |
| 106 | + $status[ 'is_singular-' . $slug ] = is_singular( $slug ); | |
| 107 | + | |
| 108 | + if ( $type->has_archive ) { | |
| 109 | + $status[ 'is_archive-' . $slug ] = is_post_type_archive( $slug ); | |
| 110 | + } | |
| 111 | + } | |
| 112 | + | |
| 113 | + foreach ( $taxonomies as $slug => $tax ) { | |
| 114 | + $status[ 'is_tax-' . $slug ] = is_tax( $slug ); | |
| 115 | + } | |
| 116 | + } | |
| 117 | + /** | |
| 118 | + * Check for specific taxonomy ID. | |
| 119 | + * | |
| 120 | + * `is_taxonomy` is a Pro-only location, and Pro resolves it through the | |
| 121 | + * `nx_location_status` filter above (taxonomy-aware, with a `has_term()` | |
| 122 | + * fallback for singular pages). This block is only a fallback for sites | |
| 123 | + * running an older Pro that does not set the key yet — without the | |
| 124 | + * `isset()` guard it would run *after* the filter and clobber Pro's value. | |
| 125 | + */ | |
| 126 | + if ( ! isset( $status['is_taxonomy'] ) && in_array( 'is_taxonomy', $locations ) && ! empty( $taxonomy_ids ) ) { | |
| 127 | + $current_term_id = get_queried_object_id(); | |
| 128 | + $ids_array = explode(',',$taxonomy_ids ); | |
| 129 | + foreach ($ids_array as $id) { | |
| 130 | + if ( $current_term_id == trim( $id ) ) { | |
| 131 | + $status['is_taxonomy'] = true; | |
| 132 | + break; | |
| 133 | + } | |
| 134 | + } | |
| 135 | + } | |
| 136 | + | |
| 137 | + $status_flag = false; | |
| 138 | + foreach ( $locations as $location ) { | |
| 139 | + if ( ! isset( $status[$location] ) || ! $status[$location] ) { | |
| 140 | + continue; | |
| 141 | + } else { | |
| 142 | + $status_flag = true; | |
| 143 | + } | |
| 144 | + } | |
| 145 | + return $status_flag; | |
| 146 | + } | |
| 147 | +} | |