feat: give projects a repository, package and status

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
chrnxandClaude Opus 5 committed 2026-08-15 14:04:03 -05:00
1 parent 7895e4e927
commit 062c3951fa
2 files changed
+188 -21

No files matched your search

+133 -21
View File
@@ -1,37 +1,149 @@
<?php
/**
* Project post metadata.
* Project term metadata.
*
* @package CHRNX
*/
/**
* Require permission to edit the post owning project metadata.
* Return the outbound link slots a project can carry.
*
* @param bool $allowed Previously calculated permission.
* @param string $meta_key Metadata key.
* @param int $post_id Post ID.
* @return bool Whether the current user can edit the post.
* One generic package slot rather than one field per registry: Green Tea ships
* to npm, a Rust project would ship to crates.io, a Python one to PyPI. A
* per-registry list would need editing every time a new language shows up.
*
* @return array<string,array<string,string>> Slot key to label and icon.
*/
function chrnx_auth_project_meta( $allowed, $meta_key, $post_id ) {
return current_user_can( 'edit_post', $post_id );
function chrnx_get_project_link_slots() {
return array(
'repo' => array(
'label' => __( 'Repository', 'chrnx' ),
'icon' => '<path d="M9 19c-5 1.5-5-2.5-7-3"/><path d="M15 22v-4a3.4 3.4 0 0 0-1-2.6c3-.3 6-1.5 6-6.6a5 5 0 0 0-1.4-3.5 4.6 4.6 0 0 0-.1-3.5s-1.1-.3-3.5 1.3a12 12 0 0 0-6 0C6.6 1.5 5.5 1.8 5.5 1.8a4.6 4.6 0 0 0-.1 3.5A5 5 0 0 0 4 8.8c0 5.1 3 6.3 6 6.6a3.4 3.4 0 0 0-1 2.6v4"/>',
),
'package' => array(
'label' => __( 'Package', 'chrnx' ),
'icon' => '<path d="m7.5 4.3 9 5.2"/><path d="M21 16V8a2 2 0 0 0-1-1.7l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.7l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16"/><path d="M3.3 7 12 12l8.7-5"/><path d="M12 22V12"/>',
),
'site' => array(
'label' => __( 'Website', 'chrnx' ),
'icon' => '<circle cx="12" cy="12" r="10"/><path d="M12 2a15 15 0 0 1 0 20"/><path d="M12 2a15 15 0 0 0 0 20"/><path d="M2 12h20"/>',
),
);
}
/**
* Register project metadata for posts.
* Return the project term meta keys and their sanitizers.
*
* @return array<string,string> Meta key to sanitize callback.
*/
function chrnx_get_project_meta_definitions() {
$definitions = array(
'chrnx_project_cover' => 'absint',
'chrnx_project_status' => 'sanitize_text_field',
);
foreach ( array_keys( chrnx_get_project_link_slots() ) as $slot ) {
$definitions[ 'chrnx_project_' . $slot ] = 'chrnx_sanitize_https_url';
}
return $definitions;
}
/**
* Register project term metadata.
*/
function chrnx_register_project_meta() {
register_post_meta(
'post',
'chrnx_project_featured',
array(
'type' => 'boolean',
'single' => true,
'default' => false,
'show_in_rest' => array( 'schema' => array( 'type' => 'boolean' ) ),
'sanitize_callback' => 'rest_sanitize_boolean',
'auth_callback' => 'chrnx_auth_project_meta',
)
);
foreach ( chrnx_get_project_meta_definitions() as $meta_key => $sanitize ) {
register_term_meta(
'chrnx_project',
$meta_key,
array(
'type' => 'chrnx_project_cover' === $meta_key ? 'integer' : 'string',
'single' => true,
'show_in_rest' => true,
'sanitize_callback' => $sanitize,
'auth_callback' => 'chrnx_auth_release_meta',
)
);
}
}
add_action( 'init', 'chrnx_register_project_meta' );
/**
* Return the stored outbound links for one project.
*
* @param int $term_id Project term ID.
* @return array<string,string> Slot key to URL, omitting empty ones.
*/
function chrnx_get_project_links( $term_id ) {
$links = array();
foreach ( array_keys( chrnx_get_project_link_slots() ) as $slot ) {
$url = chrnx_sanitize_https_url( get_term_meta( $term_id, 'chrnx_project_' . $slot, true ) );
if ( '' !== $url ) {
$links[ $slot ] = $url;
}
}
return $links;
}
/**
* Render the project metadata fields on the term edit screen.
*
* @param WP_Term $term Project term.
*/
function chrnx_render_project_edit_fields( $term ) {
chrnx_print_release_field(
'chrnx_project_status',
__( 'Status', 'chrnx' ),
(string) get_term_meta( $term->term_id, 'chrnx_project_status', true ),
'text',
__( 'Short and honest, for example Beta, Activo or Archivado.', 'chrnx' )
);
foreach ( chrnx_get_project_link_slots() as $slot => $definition ) {
chrnx_print_release_field(
'chrnx_project_' . $slot,
$definition['label'],
(string) get_term_meta( $term->term_id, 'chrnx_project_' . $slot, true ),
'url',
__( 'HTTPS only. Leave empty to hide this icon.', 'chrnx' )
);
}
chrnx_print_release_field(
'chrnx_project_cover',
__( 'Cover attachment ID', 'chrnx' ),
(string) absint( get_term_meta( $term->term_id, 'chrnx_project_cover', true ) ),
'text',
__( 'Leave empty to use the shared placeholder art.', 'chrnx' )
);
}
add_action( 'chrnx_project_edit_form_fields', 'chrnx_render_project_edit_fields' );
/**
* Persist project term metadata from the term edit screen.
*
* @param int $term_id Project term ID.
*/
function chrnx_save_project_meta( $term_id ) {
if ( ! chrnx_auth_release_meta() ) {
return;
}
// Core supplies and verifies the add/update term nonce on these screens.
$nonce = isset( $_POST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ) : '';
if ( ! wp_verify_nonce( $nonce, 'update-tag_' . $term_id ) && ! wp_verify_nonce( $nonce, 'add-tag' ) ) {
return;
}
foreach ( chrnx_get_project_meta_definitions() as $meta_key => $sanitize ) {
if ( ! isset( $_POST[ $meta_key ] ) ) {
continue;
}
$value = call_user_func( $sanitize, wp_unslash( $_POST[ $meta_key ] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized by the mapped callback.
if ( '' === $value || 0 === $value ) {
delete_term_meta( $term_id, $meta_key );
continue;
}
update_term_meta( $term_id, $meta_key, $value );
}
}
add_action( 'edited_chrnx_project', 'chrnx_save_project_meta' );
add_action( 'created_chrnx_project', 'chrnx_save_project_meta' );
+55
View File
@@ -0,0 +1,55 @@
<?php
/**
* Tests for project term metadata.
*
* @package CHRNX
*/
/**
* Verify the project term meta contract.
*/
class ProjectMetaTest extends WP_UnitTestCase {
/**
* Register taxonomies and meta before each test.
*/
public function set_up() {
parent::set_up();
do_action( 'init' );
}
/**
* Ensure every project meta key is registered for the taxonomy.
*/
public function test_project_meta_keys_are_registered() {
$registered = get_registered_meta_keys( 'term', 'chrnx_project' );
foreach ( array_keys( chrnx_get_project_meta_definitions() ) as $meta_key ) {
$this->assertArrayHasKey( $meta_key, $registered );
}
}
/**
* Ensure a plain http repository URL is rejected.
*/
public function test_project_links_reject_insecure_urls() {
$term_id = self::factory()->term->create( array( 'taxonomy' => 'chrnx_project' ) );
update_term_meta( $term_id, 'chrnx_project_repo', 'http://example.com/repo' );
$this->assertArrayNotHasKey( 'repo', chrnx_get_project_links( $term_id ) );
}
/**
* Ensure stored https links come back keyed by their slot.
*/
public function test_project_links_return_stored_https_urls() {
$term_id = self::factory()->term->create( array( 'taxonomy' => 'chrnx_project' ) );
update_term_meta( $term_id, 'chrnx_project_repo', 'https://github.com/Expressive-Tea/green-tea' );
update_term_meta( $term_id, 'chrnx_project_package', 'https://www.npmjs.com/package/@green-tea/core' );
$links = chrnx_get_project_links( $term_id );
$this->assertSame( 'https://github.com/Expressive-Tea/green-tea', $links['repo'] );
$this->assertSame( 'https://www.npmjs.com/package/@green-tea/core', $links['package'] );
$this->assertArrayNotHasKey( 'site', $links );
}
}