'', 'post_type' => '', 'taxonomy' => '', ); if ( function_exists( 'get_current_screen' ) ) { $screen = get_current_screen(); if ( $screen && ! empty( $screen->id ) ) { $context['screen_id'] = $screen->id; } if ( $screen && ! empty( $screen->post_type ) ) { $context['post_type'] = $screen->post_type; } if ( $screen && ! empty( $screen->taxonomy ) ) { $context['taxonomy'] = $screen->taxonomy; } } // Best-effort fallback for post type / taxonomy. if ( empty( $context['post_type'] ) && isset( $_GET['post_type'] ) ) { $context['post_type'] = sanitize_text_field( wp_unslash( $_GET['post_type'] ) ); } if ( empty( $context['taxonomy'] ) && isset( $_GET['taxonomy'] ) ) { $context['taxonomy'] = sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ); } return $context; } public function get_guidance_for_context( $context, $limit = 3 ) { if ( ! empty( AIT_AIAA_Settings::get( 'guidance_disabled' ) ) ) { return array(); } $limit = max( 1, (int) $limit ); $role_keys = $this->get_current_user_roles(); $meta_query = array( 'relation' => 'AND', array( 'key' => '_ait_ag_enabled', 'value' => '1', 'compare' => '=', ), ); // We query by target type in OR blocks. For multi-select match types // (post_type, taxonomy), we filter in PHP after querying. $or = array( 'relation' => 'OR' ); // Global. $or[] = array( 'relation' => 'AND', array( 'key' => '_ait_ag_target_type', 'value' => 'global', 'compare' => '=', ), ); // Screen match. if ( ! empty( $context['screen_id'] ) ) { $or[] = array( 'relation' => 'AND', array( 'key' => '_ait_ag_target_type', 'value' => 'screen', 'compare' => '=', ), array( 'key' => '_ait_ag_screen_id', 'value' => $context['screen_id'], 'compare' => '=', ), ); } // Post type (filter exact match in PHP). if ( ! empty( $context['post_type'] ) ) { $or[] = array( 'relation' => 'AND', array( 'key' => '_ait_ag_target_type', 'value' => 'post_type', 'compare' => '=', ), ); } // Taxonomy (filter exact match in PHP). if ( ! empty( $context['taxonomy'] ) ) { $or[] = array( 'relation' => 'AND', array( 'key' => '_ait_ag_target_type', 'value' => 'taxonomy', 'compare' => '=', ), ); } $meta_query[] = $or; $q = new WP_Query( array( 'post_type' => AIT_AIAA_Guidance_CPT::CPT, 'post_status' => 'publish', 'posts_per_page' => $limit * 6, // fetch extra; filters happen in PHP 'orderby' => 'meta_value_num', 'meta_key' => '_ait_ag_priority', 'order' => 'ASC', 'meta_query' => $meta_query, 'no_found_rows' => true, ) ); $items = array(); if ( $q->have_posts() ) { foreach ( $q->posts as $post ) { // Hide for roles gate. $hide_roles = get_post_meta( $post->ID, '_ait_ag_hide_roles', true ); if ( is_array( $hide_roles ) && ! empty( $hide_roles ) ) { foreach ( $role_keys as $rk ) { if ( in_array( $rk, $hide_roles, true ) ) { continue 2; // skip this guidance item } } } // Role visibility gate. $visible_roles = get_post_meta( $post->ID, '_ait_ag_role_visibility', true ); if ( is_array( $visible_roles ) && ! empty( $visible_roles ) ) { $ok = false; foreach ( $role_keys as $rk ) { if ( in_array( $rk, $visible_roles, true ) ) { $ok = true; break; } } if ( ! $ok ) { continue; } } // Target filter for post_type. $tt = get_post_meta( $post->ID, '_ait_ag_target_type', true ); if ( 'post_type' === $tt && ! empty( $context['post_type'] ) ) { $pts = get_post_meta( $post->ID, '_ait_ag_post_types', true ); // Back-compat. if ( ( empty( $pts ) || ! is_array( $pts ) ) ) { $old = get_post_meta( $post->ID, '_ait_ag_post_type', true ); if ( ! empty( $old ) && is_string( $old ) ) { $pts = array( $old ); } } if ( ! is_array( $pts ) ) { $pts = array(); } if ( ! in_array( $context['post_type'], $pts, true ) ) { continue; } } // Target filter for taxonomy. if ( 'taxonomy' === $tt && ! empty( $context['taxonomy'] ) ) { $txs = get_post_meta( $post->ID, '_ait_ag_taxonomies', true ); if ( ! is_array( $txs ) ) { $txs = array(); } if ( ! in_array( $context['taxonomy'], $txs, true ) ) { continue; } } $items[] = $post; if ( count( $items ) >= $limit ) { break; } } } $items = apply_filters( 'ait_aiaa_guidance_items', $items, $context, $role_keys ); return $items; } protected function get_current_user_roles() { $user = wp_get_current_user(); if ( empty( $user->roles ) ) { return array(); } return $user->roles; } }