'bcal_aliases', 'label' => __( 'Aliases', 'bearius-calendar' ), 'type' => 'textarea', 'help' => __( 'Other names used in import files, one per line or comma separated. Imports matching an alias are assigned to this term instead of creating a duplicate.', 'bearius-calendar' ), ); return array( 'bcal_venue' => array( array( 'key' => 'bcal_address', 'label' => __( 'Street address', 'bearius-calendar' ), 'type' => 'text', 'help' => '', ), array( 'key' => 'bcal_postal', 'label' => __( 'Postal code', 'bearius-calendar' ), 'type' => 'text', 'help' => '', ), array( 'key' => 'bcal_city_id', 'label' => __( 'City', 'bearius-calendar' ), 'type' => 'city', 'help' => __( 'Events at this venue without a city get this one.', 'bearius-calendar' ), ), array( 'key' => 'bcal_lat', 'label' => __( 'Latitude', 'bearius-calendar' ), 'type' => 'text', 'help' => __( 'Optional. Makes directions links exact. Copy it from any map app.', 'bearius-calendar' ), ), array( 'key' => 'bcal_lng', 'label' => __( 'Longitude', 'bearius-calendar' ), 'type' => 'text', 'help' => '', ), $aliases, ), 'bcal_organizer' => array( array( 'key' => 'bcal_url', 'label' => __( 'Website', 'bearius-calendar' ), 'type' => 'url', 'help' => '', ), $aliases, ), 'bcal_city' => array( $aliases ), 'bcal_category' => array( array( 'key' => 'bcal_color', 'label' => __( 'Colour', 'bearius-calendar' ), 'type' => 'color', 'help' => __( 'Marks this category in the calendar views. Subcategories without their own colour use this one.', 'bearius-calendar' ), ), $aliases, ), ); } /** * Hooks for every taxonomy. */ function bcal_register_term_field_hooks() { foreach ( array_keys( bcal_term_fields() ) as $taxonomy ) { add_action( $taxonomy . '_add_form_fields', 'bcal_term_add_fields' ); add_action( $taxonomy . '_edit_form_fields', 'bcal_term_edit_fields', 10, 2 ); add_action( 'created_' . $taxonomy, 'bcal_term_save_fields' ); add_action( 'edited_' . $taxonomy, 'bcal_term_save_fields' ); } } add_action( 'admin_init', 'bcal_register_term_field_hooks' ); /** * Input markup. * * @param array $field Field. * @param string $value Value. * @return string */ function bcal_term_input( $field, $value ) { $id = 'bcal-term-' . $field['key']; if ( 'color' === $field['type'] ) { $hex = bcal_normalize_hex( (string) $value ); return ' '; } if ( 'city' === $field['type'] ) { $cities = get_terms( array( 'taxonomy' => 'bcal_city', 'hide_empty' => false, 'number' => 1000, 'orderby' => 'name', ) ); $html = ''; } if ( 'textarea' === $field['type'] ) { return ''; } return ''; } /** * "Add term" screen. * * @param string $taxonomy Taxonomy. */ function bcal_term_add_fields( $taxonomy ) { $fields = bcal_term_fields(); wp_nonce_field( 'bcal_term', 'bcal_term_nonce' ); foreach ( $fields[ $taxonomy ] as $field ) { echo '
'; echo wp_kses( bcal_term_input( $field, '' ), bcal_kses_html() ); if ( $field['help'] ) { echo '

' . esc_html( $field['help'] ) . '

'; } echo '
'; } } /** * "Edit term" screen. * * @param WP_Term $term Term. * @param string $taxonomy Taxonomy. */ function bcal_term_edit_fields( $term, $taxonomy ) { $fields = bcal_term_fields(); wp_nonce_field( 'bcal_term', 'bcal_term_nonce' ); foreach ( $fields[ $taxonomy ] as $field ) { $value = (string) get_term_meta( $term->term_id, $field['key'], true ); echo ''; echo wp_kses( bcal_term_input( $field, $value ), bcal_kses_html() ); if ( $field['help'] ) { echo '

' . esc_html( $field['help'] ) . '

'; } echo ''; } } /** * Saves term fields. * * @param int $term_id Term ID. */ function bcal_term_save_fields( $term_id ) { if ( ! isset( $_POST['bcal_term_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['bcal_term_nonce'] ) ), 'bcal_term' ) ) { return; } $term = get_term( $term_id ); if ( ! $term || is_wp_error( $term ) ) { return; } if ( ! current_user_can( 'edit_term', $term_id ) ) { return; } $fields = bcal_term_fields(); if ( ! isset( $fields[ $term->taxonomy ] ) ) { return; } $input = isset( $_POST['bcal_term'] ) && is_array( $_POST['bcal_term'] ) ? wp_unslash( $_POST['bcal_term'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized foreach ( $fields[ $term->taxonomy ] as $field ) { $raw = isset( $input[ $field['key'] ] ) && is_scalar( $input[ $field['key'] ] ) ? (string) $input[ $field['key'] ] : ''; if ( 'bcal_aliases' === $field['key'] ) { bcal_save_aliases( $term_id, $raw ); continue; } if ( 'city' === $field['type'] ) { $city_id = absint( $raw ); if ( $city_id && term_exists( $city_id, 'bcal_city' ) ) { update_term_meta( $term_id, $field['key'], $city_id ); } else { delete_term_meta( $term_id, $field['key'] ); } continue; } if ( in_array( $field['key'], array( 'bcal_lat', 'bcal_lng' ), true ) ) { $raw = str_replace( ',', '.', trim( $raw ) ); $raw = is_numeric( $raw ) ? $raw : ''; } if ( 'color' === $field['type'] ) { $raw = empty( $input[ $field['key'] . '_none' ] ) ? (string) bcal_normalize_hex( $raw ) : ''; } $value = 'url' === $field['type'] ? esc_url_raw( $raw ) : sanitize_text_field( $raw ); if ( '' === $value ) { delete_term_meta( $term_id, $field['key'] ); } else { update_term_meta( $term_id, $field['key'], $value ); } } }