diff --git a/inc/project-meta.php b/inc/project-meta.php index 86c7141..dcf23b4 100644 --- a/inc/project-meta.php +++ b/inc/project-meta.php @@ -1,37 +1,149 @@ > 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' => '', + ), + 'package' => array( + 'label' => __( 'Package', 'chrnx' ), + 'icon' => '', + ), + 'site' => array( + 'label' => __( 'Website', 'chrnx' ), + 'icon' => '', + ), + ); } /** - * Register project metadata for posts. + * Return the project term meta keys and their sanitizers. + * + * @return array 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 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' ); diff --git a/tests/php/ProjectMetaTest.php b/tests/php/ProjectMetaTest.php new file mode 100644 index 0000000..d47e38f --- /dev/null +++ b/tests/php/ProjectMetaTest.php @@ -0,0 +1,55 @@ +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 ); + } +}