(int) $term->term_id, 'name' => html_entity_decode( $term->name, ENT_QUOTES, 'UTF-8' ), 'slug' => $term->slug, 'description' => $term->description, 'icon' => (string) get_term_meta( $term->term_id, 'kdocs_icon', true ), 'color' => (string) get_term_meta( $term->term_id, 'kdocs_color', true ), 'cover' => Covers::get( $term->term_id ), 'layout' => (string) get_term_meta( $term->term_id, 'kdocs_layout', true ), 'announcement' => array( 'text' => (string) get_term_meta( $term->term_id, 'kdocs_announcement', true ), 'link' => (string) get_term_meta( $term->term_id, 'kdocs_announcement_link', true ), 'tone' => (string) get_term_meta( $term->term_id, 'kdocs_announcement_tone', true ), ), 'order' => (int) get_term_meta( $term->term_id, 'kdocs_order', true ), 'link' => get_term_link( $term ), 'articles' => self::article_count( $term->term_id ), 'categories' => self::category_count( $term->term_id ), ); /** * Filters KB data sent to the admin app (Pro adds access control fields). * * @param array $data Data. * @param \WP_Term $term Term. */ return apply_filters( 'kdocs_kb_data', $data, $term ); } /** * Creates or updates a KB from request data. * * @param array $input Data. * @param int|null $id Existing id. * @return int|\WP_Error */ public static function save( array $input, $id = null ) { $args = array(); if ( isset( $input['slug'] ) && '' !== $input['slug'] ) { $args['slug'] = sanitize_title( $input['slug'] ); } if ( isset( $input['description'] ) ) { $args['description'] = sanitize_textarea_field( $input['description'] ); } if ( $id ) { if ( isset( $input['name'] ) ) { $args['name'] = sanitize_text_field( $input['name'] ); } $result = wp_update_term( $id, Taxonomies::KB, $args ); } else { $name = isset( $input['name'] ) ? sanitize_text_field( $input['name'] ) : ''; $result = wp_insert_term( $name ? $name : __( 'Untitled Knowledge Base', 'karma-docs' ), Taxonomies::KB, $args ); } if ( is_wp_error( $result ) ) { return $result; } $term_id = (int) $result['term_id']; if ( isset( $input['icon'] ) ) { update_term_meta( $term_id, 'kdocs_icon', Icons::sanitize( $input['icon'] ) ); } if ( isset( $input['color'] ) ) { update_term_meta( $term_id, 'kdocs_color', sanitize_hex_color( $input['color'] ) ); } if ( isset( $input['layout'] ) ) { $layout = in_array( $input['layout'], Settings::CHOICES['home_layout'], true ) ? $input['layout'] : ''; update_term_meta( $term_id, 'kdocs_layout', $layout ); } if ( isset( $input['announcement'] ) && is_array( $input['announcement'] ) ) { $announce = $input['announcement']; update_term_meta( $term_id, 'kdocs_announcement', sanitize_text_field( (string) ( $announce['text'] ?? '' ) ) ); update_term_meta( $term_id, 'kdocs_announcement_link', esc_url_raw( (string) ( $announce['link'] ?? '' ) ) ); update_term_meta( $term_id, 'kdocs_announcement_tone', in_array( $announce['tone'] ?? '', array( 'info', 'success', 'warning' ), true ) ? $announce['tone'] : 'info' ); } if ( isset( $input['cover'] ) ) { update_term_meta( $term_id, Covers::META, Covers::sanitize( $input['cover'] ) ); } if ( ! $id ) { update_term_meta( $term_id, 'kdocs_order', count( Taxonomies::get_kbs() ) ); } /** * Fires after a KB was saved. * * @param int $term_id KB id. * @param array $input Raw input. */ do_action( 'kdocs_kb_saved', $term_id, $input ); return $term_id; } /** * Number of articles in a KB. * * @param int $kb_id KB. * @return int */ public static function article_count( $kb_id ) { $q = new \WP_Query( array( 'post_type' => PostTypes::ARTICLE, 'post_status' => array( 'publish', 'draft', 'pending', 'private', 'future' ), 'posts_per_page' => 1, 'fields' => 'ids', 'tax_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery array( 'taxonomy' => Taxonomies::KB, 'terms' => (int) $kb_id, ), ), ) ); return (int) $q->found_posts; } /** * Number of categories in a KB. * * @param int $kb_id KB. * @return int */ public static function category_count( $kb_id ) { return (int) wp_count_terms( array( 'taxonomy' => Taxonomies::CATEGORY, 'hide_empty' => false, 'meta_key' => 'kdocs_kb', // phpcs:ignore WordPress.DB.SlowDBQuery 'meta_value' => (int) $kb_id, // phpcs:ignore WordPress.DB.SlowDBQuery ) ); } }