-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Knowledge: Introduce the wp_knowledge custom post type
#12201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gziolo
wants to merge
20
commits into
WordPress:trunk
Choose a base branch
from
gziolo:feature/knowledge-cpt
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
7508b55
Knowledge: Introduce the wp_knowledge custom post type.
gziolo 84b21a4
Knowledge: Address PR feedback on naming and capability filter.
gziolo 1a05ea2
Knowledge: Use plural/singular capability bases per convention.
gziolo e7d4f1c
Knowledge: Strengthen REST controller permission test coverage.
gziolo 5c459e8
Knowledge: Keep author control of their own trashed rows.
gziolo a65e9d8
Knowledge: Align the collection read error code with core controllers.
gziolo a65c11c
Knowledge: Drop the no-op show_admin_column from wp_knowledge_type.
gziolo a155832
Fix type issues with wp_maybe_grant_knowledge_caps()
westonruter 3733630
Fix typing with wp_knowledge_maybe_map_term_label()
westonruter 3180f8e
Fix typing on prepare_items_query()
westonruter 2c6665b
Add types to wp_maybe_grant_knowledge_caps()
westonruter c021f5a
Guard against post type not being registered in get_items_permissions…
westonruter 9781410
Knowledge: Resolve type-term labels in the site locale.
gziolo a924b44
Fix param docs indentation
westonruter 7bcf1e4
Improve docblock formatting
westonruter 52235ad
Fix most phpstan issues in tests and use native type hints
westonruter b8aa419
Fix phpstan errors in tests
westonruter 2b6582c
Knowledge: Enforce the default type term after all REST term writes.
gziolo 8d32e60
Knowledge: Move @since tags from 7.1.0 to 7.2.0.
gziolo c6023eb
Knowledge: Drop the `memory` default type.
gziolo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| <?php | ||
| /** | ||
| * Knowledge API: Public functions for the `wp_knowledge` post type. | ||
| * | ||
| * The Knowledge post type is a private-by-default storage primitive. Individual | ||
| * rows are classified by one or more terms in the `wp_knowledge_type` taxonomy | ||
| * (for example "guideline" or "note"). This file holds the type | ||
| * registry, the default-term fallback applied on save, and the helper that | ||
| * gives lazily created type terms a human-readable label. | ||
| * | ||
| * @package WordPress | ||
| * @subpackage Knowledge | ||
| * @since 7.2.0 | ||
| */ | ||
|
|
||
| /** | ||
| * Retrieves the registered knowledge types, keyed by slug. | ||
| * | ||
| * Plugins can register their own types via the {@see 'wp_knowledge_types'} filter. | ||
| * | ||
| * @since 7.2.0 | ||
| * | ||
| * @return array { | ||
| * Slug-keyed map of knowledge types. | ||
| * | ||
| * @type array ...$0 { | ||
| * Data for a single knowledge type. | ||
| * | ||
| * @type string $title The human-readable label for the type. | ||
| * } | ||
| * } | ||
| * @phpstan-return array<non-empty-string, array{title: non-empty-string}> | ||
| */ | ||
| function wp_knowledge_types(): array { | ||
| /** | ||
| * Filters the knowledge types available on this site. | ||
| * | ||
| * @since 7.2.0 | ||
| * | ||
| * @param array $types { | ||
| * Slug-keyed map of knowledge types. | ||
| * | ||
| * @type array ...$0 { | ||
| * Data for a single knowledge type. | ||
| * | ||
| * @type string $title The human-readable label for the type. | ||
| * } | ||
| * } | ||
| * @phpstan-param array<non-empty-string, array{title: non-empty-string}> $types | ||
| */ | ||
| return apply_filters( | ||
| 'wp_knowledge_types', | ||
| array( | ||
| 'guideline' => array( | ||
| 'title' => _x( 'Guideline', 'knowledge type' ), | ||
| ), | ||
| 'note' => array( | ||
| 'title' => _x( 'Note', 'knowledge type' ), | ||
| ), | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Ensures every knowledge row carries at least one type term. | ||
| * | ||
| * Knowledge rows are classified by their `wp_knowledge_type` terms, so a row | ||
| * saved without one would belong to no type at all. This assigns the `note` | ||
| * fallback in that case, keeping every row classified. | ||
| * | ||
| * Hooked to `wp_after_insert_post` so it runs after the row's terms are saved, | ||
| * when the final set of terms is known. | ||
| * | ||
| * @since 7.2.0 | ||
| * @access private | ||
| * | ||
| * @param int $post_id Saved post ID. | ||
| * @param WP_Post $post Saved post object. | ||
| */ | ||
| function wp_knowledge_ensure_default_type_term( int $post_id, WP_Post $post ): void { | ||
| if ( 'wp_knowledge' !== $post->post_type ) { | ||
| return; | ||
| } | ||
|
|
||
| $terms = get_the_terms( $post_id, 'wp_knowledge_type' ); | ||
| if ( is_wp_error( $terms ) || ! empty( $terms ) ) { | ||
| return; | ||
| } | ||
|
|
||
| /* | ||
| * Resolve to a term ID up front, creating the term on first use: | ||
| * wp_set_object_terms() interprets strings as names for hierarchical | ||
| * taxonomies, not slugs. | ||
| */ | ||
| $term = term_exists( 'note', 'wp_knowledge_type' ); | ||
| if ( ! $term ) { | ||
| $term = wp_insert_term( 'note', 'wp_knowledge_type' ); | ||
| if ( is_wp_error( $term ) ) { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| wp_set_object_terms( $post_id, (int) $term['term_id'], 'wp_knowledge_type' ); | ||
| } | ||
|
|
||
| /** | ||
| * Swaps a raw knowledge-type slug for its registered label on term creation. | ||
| * | ||
| * Hooked to the `wp_insert_term_data` filter. When `wp_set_object_terms()` is | ||
| * called with a slug that does not yet exist, wp_insert_term() fires and this | ||
| * filter runs after WordPress has computed both `name` and `slug`. A `name` | ||
| * equal to `slug` indicates the term was created from a raw slug (for example by | ||
| * `wp_set_object_terms()`) rather than from a user-provided label, so the label is | ||
| * replaced with the title from {@see wp_knowledge_types()}. | ||
| * | ||
| * The name is written once and shared by every user, so it is resolved in the | ||
| * site locale, not the locale of the request that happens to create the term. | ||
| * | ||
| * @since 7.2.0 | ||
| * @access private | ||
| * | ||
| * @param array $data Term data to be inserted (keyed by column name). | ||
| * @param string $taxonomy Taxonomy slug. | ||
| * @return array Possibly modified term data. | ||
| * | ||
| * @phpstan-param array{ name: string, slug: string } $data | ||
| * @phpstan-return array{ name: string, slug: string } | ||
| */ | ||
| function wp_knowledge_maybe_map_term_label( $data, string $taxonomy ): array { | ||
| if ( ! is_array( $data ) ) { | ||
| $data = array_fill_keys( array( 'name', 'slug' ), '' ); | ||
| } | ||
|
|
||
| if ( 'wp_knowledge_type' !== $taxonomy ) { | ||
| return $data; | ||
| } | ||
|
|
||
| if ( $data['name'] !== $data['slug'] ) { | ||
| return $data; | ||
| } | ||
|
|
||
| // Type titles may be translatable, so resolve them under the site locale. | ||
| $switched_locale = switch_to_locale( get_locale() ); | ||
|
|
||
| $types = wp_knowledge_types(); | ||
| if ( isset( $types[ $data['slug'] ] ) ) { | ||
| $data['name'] = $types[ $data['slug'] ]['title']; | ||
| } | ||
|
|
||
| if ( $switched_locale ) { | ||
| restore_previous_locale(); | ||
| } | ||
|
|
||
| return $data; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.