|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Knowledge API: Public functions for the `wp_knowledge` post type. |
| 4 | + * |
| 5 | + * The Knowledge post type is a private-by-default storage primitive. Individual |
| 6 | + * rows are classified by one or more terms in the `wp_knowledge_type` taxonomy |
| 7 | + * (for example "guideline", "memory", or "note"). This file holds the type |
| 8 | + * registry, the default-term fallback applied on save, and the helper that |
| 9 | + * gives lazily created type terms a human-readable label. |
| 10 | + * |
| 11 | + * @package WordPress |
| 12 | + * @subpackage Knowledge |
| 13 | + * @since 7.1.0 |
| 14 | + */ |
| 15 | + |
| 16 | +/** |
| 17 | + * Retrieves the registered knowledge types, keyed by slug. |
| 18 | + * |
| 19 | + * Plugins can register their own types via the {@see 'wp_knowledge_types'} filter. |
| 20 | + * |
| 21 | + * @since 7.1.0 |
| 22 | + * |
| 23 | + * @return array { |
| 24 | + * Slug-keyed map of knowledge types. |
| 25 | + * |
| 26 | + * @type array ...$0 { |
| 27 | + * Data for a single knowledge type. |
| 28 | + * |
| 29 | + * @type string $title The human-readable label for the type. |
| 30 | + * } |
| 31 | + * } |
| 32 | + * @phpstan-return array<string, array{title: string}> |
| 33 | + */ |
| 34 | +function wp_knowledge_types(): array { |
| 35 | + /** |
| 36 | + * Filters the knowledge types available on this site. |
| 37 | + * |
| 38 | + * @since 7.1.0 |
| 39 | + * |
| 40 | + * @param array $types { |
| 41 | + * Slug-keyed map of knowledge types. |
| 42 | + * |
| 43 | + * @type array ...$0 { |
| 44 | + * Data for a single knowledge type. |
| 45 | + * |
| 46 | + * @type string $title The human-readable label for the type. |
| 47 | + * } |
| 48 | + * } |
| 49 | + * @phpstan-param array<string, array{title: string}> $types |
| 50 | + */ |
| 51 | + return apply_filters( |
| 52 | + 'wp_knowledge_types', |
| 53 | + array( |
| 54 | + 'guideline' => array( |
| 55 | + 'title' => _x( 'Guideline', 'knowledge type' ), |
| 56 | + ), |
| 57 | + 'memory' => array( |
| 58 | + 'title' => _x( 'Memory', 'knowledge type' ), |
| 59 | + ), |
| 60 | + 'note' => array( |
| 61 | + 'title' => _x( 'Note', 'knowledge type' ), |
| 62 | + ), |
| 63 | + ) |
| 64 | + ); |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Assigns the `note` fallback term when a knowledge post is saved without a type. |
| 69 | + * |
| 70 | + * Hooked to the `save_post_wp_knowledge` action so that every knowledge row has |
| 71 | + * at least one `wp_knowledge_type` term. Uses get_the_terms() so the check is |
| 72 | + * served by the object term cache. |
| 73 | + * |
| 74 | + * @since 7.1.0 |
| 75 | + * @access private |
| 76 | + * |
| 77 | + * @param int $post_id Saved post ID. |
| 78 | + */ |
| 79 | +function _wp_knowledge_ensure_default_type_term( int $post_id ): void { |
| 80 | + if ( wp_is_post_revision( $post_id ) ) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + $terms = get_the_terms( $post_id, 'wp_knowledge_type' ); |
| 85 | + if ( is_wp_error( $terms ) || ! empty( $terms ) ) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + /* |
| 90 | + * Resolve to a term ID up front, creating the term on first use: |
| 91 | + * wp_set_object_terms() interprets strings as names for hierarchical |
| 92 | + * taxonomies, not slugs. |
| 93 | + */ |
| 94 | + $term = term_exists( 'note', 'wp_knowledge_type' ); |
| 95 | + if ( ! $term ) { |
| 96 | + $term = wp_insert_term( 'note', 'wp_knowledge_type' ); |
| 97 | + if ( is_wp_error( $term ) ) { |
| 98 | + return; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + wp_set_object_terms( $post_id, (int) $term['term_id'], 'wp_knowledge_type' ); |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Swaps a raw knowledge-type slug for its registered label on term creation. |
| 107 | + * |
| 108 | + * Hooked to the `wp_insert_term_data` filter. When wp_set_object_terms() is |
| 109 | + * called with a slug that does not yet exist, wp_insert_term() fires and this |
| 110 | + * filter runs after WordPress has computed both `name` and `slug`. A `name` |
| 111 | + * equal to `slug` indicates the term was created from a raw slug (for example by |
| 112 | + * wp_set_object_terms()) rather than from a user-provided label, so the label is |
| 113 | + * replaced with the title from wp_knowledge_types(). Because term names are |
| 114 | + * persisted in the database, the translated title is stored in the locale active |
| 115 | + * when the term is created. |
| 116 | + * |
| 117 | + * @since 7.1.0 |
| 118 | + * @access private |
| 119 | + * |
| 120 | + * @param array $data Term data to be inserted (keyed by column name). |
| 121 | + * @param string $taxonomy Taxonomy slug. |
| 122 | + * @return array Possibly modified term data. |
| 123 | + * |
| 124 | + * @phpstan-param array<string, mixed> $data |
| 125 | + * @phpstan-return array<string, mixed> |
| 126 | + */ |
| 127 | +function _wp_knowledge_maybe_map_term_label( array $data, string $taxonomy ): array { |
| 128 | + if ( 'wp_knowledge_type' !== $taxonomy ) { |
| 129 | + return $data; |
| 130 | + } |
| 131 | + |
| 132 | + if ( $data['name'] !== $data['slug'] ) { |
| 133 | + return $data; |
| 134 | + } |
| 135 | + |
| 136 | + $types = wp_knowledge_types(); |
| 137 | + if ( isset( $types[ $data['slug'] ] ) ) { |
| 138 | + $data['name'] = $types[ $data['slug'] ]['title']; |
| 139 | + } |
| 140 | + |
| 141 | + return $data; |
| 142 | +} |
0 commit comments