WP_REST_Server::READABLE,
'callback' => array( __CLASS__, 'search_suggestions' ),
'permission_callback' => '__return_true',
'args' => array(
'term' => array(
'type' => 'string',
'maxLength' => 100,
'required' => true,
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => function ( $term ) {
return is_string( $term ) && strlen( trim( $term ) ) >= 2 && strlen( $term ) <= 100;
},
),
),
)
);
}
public static function search_suggestions( WP_REST_Request $request ) {
$term = sanitize_text_field( $request->get_param( 'term' ) );
$hidden_ids = self::get_hidden_category_ids();
$query_args = array(
'post_type' => 'docsstation_doc',
'post_status' => 'publish',
'has_password' => false,
'posts_per_page' => 6,
's' => $term,
'no_found_rows' => true,
'ignore_sticky_posts' => true,
'update_post_meta_cache' => false,
);
if ( ! empty( $hidden_ids ) ) {
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Excludes hidden categories from global search results.
$query_args['tax_query'] = array(
array(
'taxonomy' => 'docsstation_category',
'field' => 'term_id',
'terms' => $hidden_ids,
'include_children' => true,
'operator' => 'NOT IN',
),
);
}
$query = new WP_Query( $query_args );
$posts = $query->posts;
$term_args = array(
'taxonomy' => 'docsstation_category',
'search' => $term,
'hide_empty' => true,
'number' => 3,
);
if ( ! empty( $hidden_ids ) ) {
$term_args['exclude'] = $hidden_ids;
}
$terms = get_terms( $term_args );
if ( ! is_wp_error( $terms ) && $terms ) {
$term_ids = wp_list_pluck( $terms, 'term_id' );
if ( ! empty( $hidden_ids ) ) {
$term_ids = array_diff( $term_ids, $hidden_ids );
}
if ( ! empty( $term_ids ) ) {
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Queries documents belonging to matched categories.
$category_posts = get_posts(
array(
'post_type' => 'docsstation_doc',
'post_status' => 'publish',
'has_password' => false,
'posts_per_page' => 6,
'no_found_rows' => true,
'update_post_meta_cache' => false,
'tax_query' => array(
array(
'taxonomy' => 'docsstation_category',
'field' => 'term_id',
'terms' => $term_ids,
),
),
)
);
$posts = array_merge( $posts, $category_posts );
}
}
$results = array();
$seen = array();
foreach ( $posts as $post ) {
if ( isset( $seen[ $post->ID ] ) || count( $results ) >= 6 ) {
continue;
}
$seen[ $post->ID ] = true;
$terms = get_the_terms( $post->ID, 'docsstation_category' );
$results[] = array(
'title' => get_the_title( $post ),
'url' => esc_url_raw( get_permalink( $post ) ),
'excerpt' => wp_trim_words( wp_strip_all_tags( $post->post_excerpt ? $post->post_excerpt : $post->post_content ), 14 ),
'category' => ! empty( $terms ) && ! is_wp_error( $terms ) ? $terms[0]->name : '',
);
}
return rest_ensure_response( $results );
}
public static function get_hidden_category_ids() {
static $cached_hidden = null;
if ( null !== $cached_hidden ) {
return $cached_hidden;
}
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Necessary to retrieve category IDs flagged as hidden from global navigation.
$terms = get_terms(
array(
'taxonomy' => 'docsstation_category',
'hide_empty' => false,
'fields' => 'ids',
'meta_query' => array(
array(
'key' => '_docsstation_hide_from_global',
'value' => '1',
),
),
)
);
if ( is_wp_error( $terms ) || empty( $terms ) ) {
$cached_hidden = array();
return $cached_hidden;
}
$all_hidden = (array) $terms;
foreach ( $terms as $term_id ) {
$children = get_term_children( (int) $term_id, 'docsstation_category' );
if ( ! is_wp_error( $children ) && ! empty( $children ) ) {
$all_hidden = array_merge( $all_hidden, $children );
}
}
$cached_hidden = array_unique( array_map( 'absint', $all_hidden ) );
return $cached_hidden;
}
public static function is_category_isolated( $term_id ) {
$term = get_term( $term_id, 'docsstation_category' );
$isolated_root = false;
while ( $term && ! is_wp_error( $term ) ) {
if ( '1' === get_term_meta( $term->term_id, '_docsstation_isolated_sidebar', true ) ) {
$isolated_root = $term;
}
if ( ! $term->parent ) {
break;
}
$term = get_term( $term->parent, 'docsstation_category' );
}
return $isolated_root;
}
public static function get_current_isolated_term( $post_id = 0 ) {
$post_id = $post_id ? $post_id : get_the_ID();
if ( ! $post_id ) {
return false;
}
$terms = get_the_terms( $post_id, 'docsstation_category' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$isolated = self::is_category_isolated( $term->term_id );
if ( $isolated ) {
return $isolated;
}
}
}
return false;
}
public static function get_active_isolated_term() {
if ( is_singular( 'docsstation_doc' ) ) {
return self::get_current_isolated_term();
}
if ( is_tax( 'docsstation_category' ) ) {
$term = get_queried_object();
if ( $term && isset( $term->term_id ) ) {
return self::is_category_isolated( $term->term_id );
}
}
return false;
}
public static function is_docs_query() {
return is_singular( 'docsstation_doc' ) || is_post_type_archive( 'docsstation_doc' ) || is_tax( 'docsstation_category' ) || self::is_docs_search();
}
public static function body_class( $classes ) {
if ( self::is_docs_query() ) {
$classes[] = 'docsstation-page';
if ( self::get_active_isolated_term() ) {
$classes[] = 'docsstation-has-isolated-sidebar';
}
}
return $classes;
}
public static function query_vars( $vars ) {
$vars[] = 'docsstation_category';
$vars[] = 'docsstation_search';
return $vars;
}
public static function enqueue_assets() {
if ( ! self::is_docs_query() ) {
return;
}
wp_enqueue_style( 'docsstation', DOCSSTATION_URL . 'assets/css/docsstation.css', array(), DOCSSTATION_VERSION );
wp_enqueue_style( 'docsstation-learning-blocks', DOCSSTATION_URL . 'assets/css/learning-blocks.css', array( 'docsstation' ), DOCSSTATION_VERSION );
wp_enqueue_script( 'docsstation', DOCSSTATION_URL . 'assets/js/docsstation.js', array(), DOCSSTATION_VERSION, true );
wp_localize_script(
'docsstation',
'DocsStationLearning',
array(
'restUrl' => esc_url_raw( rest_url( 'docsstation/v1/' ) ),
'nonce' => wp_create_nonce( 'wp_rest' ),
'loginUrl' => wp_login_url(),
)
);
}
public static function template_include( $template ) {
if ( is_singular( 'docsstation_doc' ) ) {
return DOCSSTATION_DIR . 'templates/single-docsstation_doc.php';
}
if ( is_post_type_archive( 'docsstation_doc' ) || is_tax( 'docsstation_category' ) || self::is_docs_search() ) {
return DOCSSTATION_DIR . 'templates/archive-docsstation_doc.php';
}
return $template;
}
public static function search_query( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( $query->is_search() && '1' === (string) $query->get( 'docsstation_search' ) ) {
$hidden_ids = self::get_hidden_category_ids();
$search = sanitize_text_field( (string) $query->get( 's' ) );
if ( strlen( $search ) > 400 ) {
$search = wp_check_invalid_utf8( substr( $search, 0, 400 ), true );
}
$query->set( 's', $search );
$query->set( 'post_type', 'docsstation_doc' );
$query->set( 'has_password', false );
$tax_query = (array) $query->get( 'tax_query' );
$value = $query->get( 'docsstation_category' );
if ( $value ) {
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Filter search by requested category slug.
$tax_query[] = array(
'taxonomy' => 'docsstation_category',
'field' => 'slug',
'terms' => sanitize_title( $value ),
);
} elseif ( ! empty( $hidden_ids ) ) {
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Exclude hidden categories from main search query.
$tax_query[] = array(
'taxonomy' => 'docsstation_category',
'field' => 'term_id',
'terms' => $hidden_ids,
'include_children' => true,
'operator' => 'NOT IN',
);
}
if ( ! empty( $tax_query ) ) {
$tax_query['relation'] = 'AND';
$query->set( 'tax_query', $tax_query );
}
} elseif ( $query->is_post_type_archive( 'docsstation_doc' ) || $query->is_tax( 'docsstation_category' ) ) {
$query->set( 'has_password', false );
if ( ! $query->is_post_type_archive( 'docsstation_doc' ) ) {
return;
}
$hidden_ids = self::get_hidden_category_ids();
$value = $query->get( 'docsstation_category' );
if ( ! $value && ! empty( $hidden_ids ) ) {
$tax_query = (array) $query->get( 'tax_query' );
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Exclude hidden categories from archive main query.
$tax_query[] = array(
'taxonomy' => 'docsstation_category',
'field' => 'term_id',
'terms' => $hidden_ids,
'include_children' => true,
'operator' => 'NOT IN',
);
$tax_query['relation'] = 'AND';
$query->set( 'tax_query', $tax_query );
}
}
}
private static function is_docs_search( $query = null ) {
if ( is_admin() || ( $query && ! $query->is_main_query() ) ) {
return false;
}
$search_query = $query ? $query : ( isset( $GLOBALS['wp_query'] ) ? $GLOBALS['wp_query'] : null );
return $search_query instanceof WP_Query && $search_query->is_search() && '1' === (string) $search_query->get( 'docsstation_search' );
}
public static function search_join( $join, $query ) {
global $wpdb;
if ( ! self::is_docs_search( $query ) || false !== strpos( $join, 'docsstation_search_terms' ) ) {
return $join;
}
$join .= " LEFT JOIN {$wpdb->term_relationships} AS docsstation_search_relationships ON {$wpdb->posts}.ID = docsstation_search_relationships.object_id";
$join .= " LEFT JOIN {$wpdb->term_taxonomy} AS docsstation_search_taxonomy ON docsstation_search_relationships.term_taxonomy_id = docsstation_search_taxonomy.term_taxonomy_id";
$join .= " LEFT JOIN {$wpdb->terms} AS docsstation_search_terms ON docsstation_search_taxonomy.term_id = docsstation_search_terms.term_id";
return $join;
}
public static function search_terms( $search, $query ) {
global $wpdb;
if ( ! self::is_docs_search( $query ) || '' === trim( (string) get_search_query() ) ) {
return $search;
}
$term = '%' . $wpdb->esc_like( (string) get_search_query() ) . '%';
$taxonomy_search = $wpdb->prepare(
" OR (docsstation_search_taxonomy.taxonomy = 'docsstation_category' AND docsstation_search_terms.name LIKE %s)",
$term
);
$search = ltrim( $search );
if ( 0 === strpos( $search, 'AND ' ) ) {
$search = substr( $search, 4 );
}
return ' AND (' . $search . $taxonomy_search . ')';
}
public static function search_groupby( $groupby, $query ) {
global $wpdb;
if ( ! self::is_docs_search( $query ) ) {
return $groupby;
}
return $wpdb->posts . '.ID';
}
public static function topbar( $isolated_term = null ) {
if ( null === $isolated_term ) {
$isolated_term = self::get_active_isolated_term();
}
$archive_url = get_post_type_archive_link( 'docsstation_doc' );
$is_archive = is_post_type_archive( 'docsstation_doc' ) && ! is_search();
$is_single_doc = is_singular( 'docsstation_doc' );
?>
' . esc_html__( 'No categories yet.', 'docsstation' ) . '
'; } return; } echo '