Taxonomies::CATEGORY, 'hide_empty' => false, 'meta_key' => 'kdocs_kb', // phpcs:ignore WordPress.DB.SlowDBQuery 'meta_value' => (int) $kb_id, // phpcs:ignore WordPress.DB.SlowDBQuery ) ); if ( is_wp_error( $terms ) ) { return array(); } $by_parent = array(); foreach ( $terms as $term ) { $by_parent[ (int) $term->parent ][] = $term; } $out = array(); $walk = static function ( $parent, $depth ) use ( &$walk, &$out, $by_parent ) { if ( empty( $by_parent[ $parent ] ) ) { return; } $list = $by_parent[ $parent ]; usort( $list, static function ( $a, $b ) { return (int) get_term_meta( $a->term_id, 'kdocs_order', true ) <=> (int) get_term_meta( $b->term_id, 'kdocs_order', true ); } ); foreach ( $list as $term ) { $out[] = array( 'id' => (int) $term->term_id, 'name' => html_entity_decode( $term->name, ENT_QUOTES, 'UTF-8' ), 'depth' => $depth, ); $walk( (int) $term->term_id, $depth + 1 ); } }; $walk( 0, 0 ); return $out; } /** * Enqueues the editor plugin on article screens. */ public function enqueue() { $screen = get_current_screen(); $types = Features::is_pro() ? array( PostTypes::ARTICLE, \KarmaDocs\Pro\Changelog::TYPE ) : array( PostTypes::ARTICLE ); if ( ! $screen || ! in_array( $screen->post_type, $types, true ) ) { return; } $asset_file = KDOCS_PATH . 'build/editor.asset.php'; if ( ! file_exists( $asset_file ) ) { return; } $asset = require $asset_file; wp_enqueue_script( 'kdocs-editor', KDOCS_URL . 'build/editor.js', $asset['dependencies'], $asset['version'], true ); wp_set_script_translations( 'kdocs-editor', 'karma-docs', KDOCS_PATH . 'languages' ); $kbs = array(); foreach ( Taxonomies::get_kbs() as $kb ) { $kbs[] = array( 'id' => (int) $kb->term_id, 'name' => html_entity_decode( $kb->name, ENT_QUOTES, 'UTF-8' ), 'categories' => self::category_options( $kb->term_id ), ); } $roles = array(); foreach ( wp_roles()->get_names() as $key => $name ) { $roles[] = array( 'value' => $key, 'label' => translate_user_role( $name ), ); } wp_add_inline_script( 'kdocs-editor', 'window.kdocsEditor = ' . wp_json_encode( array( 'kbs' => $kbs, 'isPro' => Features::is_pro(), 'upgradeUrl' => Features::upgrade_url(), 'roles' => $roles, 'reviewDays' => (int) Settings::get( 'review_interval', 180 ), 'builderUrl' => admin_url( 'admin.php?page=karma-docs-builder' ), 'summaries' => Features::is_pro(), 'aiWriter' => Features::is_pro() && Settings::get( 'ai_enabled' ) && Settings::get( 'ai_writer' ), 'versions' => Features::is_pro() ? \KarmaDocs\Pro\Versions::all() : array(), 'seoPlugin' => \KarmaDocs\Frontend\Schema::has_seo_plugin(), 'owners' => Features::is_pro() ? array_map( static fn( $u ) => array( 'id' => (int) $u->ID, 'name' => $u->display_name, ), get_users( array( 'capability' => 'edit_posts', 'fields' => array( 'ID', 'display_name' ), 'number' => 200, ) ) ) : array(), 'aiReady' => '' !== (string) Settings::get( 'ai_key_' . Settings::get( 'ai_provider', 'anthropic' ), '' ), 'settingsUrl' => admin_url( 'admin.php?page=karma-docs-settings&tab=ai' ), ) ) . ';', 'before' ); } }