150 lines
4.9 KiB
PHP
150 lines
4.9 KiB
PHP
<?php
|
|
/**
|
|
* Project term metadata.
|
|
*
|
|
* @package CHRNX
|
|
*/
|
|
|
|
/**
|
|
* Return the outbound link slots a project can carry.
|
|
*
|
|
* 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_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"/>',
|
|
),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* 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() {
|
|
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' );
|