taxonomy = $taxonomy; $this->fields = $fields; add_action( 'delete_term', [ $this, 'delete_term' ], 5, 3 ); // Add form. add_action( "{$this->taxonomy}_add_form_fields", [ $this, 'add' ] ); add_action( "{$this->taxonomy}_edit_form_fields", [ $this, 'edit' ], 10 ); add_action( 'created_term', [ $this, 'save' ], 10, 3 ); add_action( 'edit_term', [ $this, 'save' ], 10, 3 ); add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); // Add columns. add_filter( "manage_edit-{$this->taxonomy}_columns", [ $this, 'taxonomy_columns' ] ); add_filter( "manage_{$this->taxonomy}_custom_column", [ $this, 'taxonomy_column' ], 10, 3 ); } /** * Adds a column for previewing meta fields. * * @param array $columns Original taxonomy columns. * @return array Modified columns. */ public function taxonomy_columns( $columns ) { $new_columns = []; if ( isset( $columns['cb'] ) ) { $new_columns['cb'] = $columns['cb']; unset( $columns['cb'] ); } $new_columns['rtsb-meta-preview'] = ''; return array_merge( $new_columns, $columns ); } /** * Renders the content for the custom meta preview column. * * @param string $columns Current output (ignored). * @param string $column Column name. * @param int $term_id Term ID. * @return void */ public function taxonomy_column( $columns, $column, $term_id ) { $attribute = SwatchesFns::get_wc_attribute_taxonomy( $this->taxonomy ); $fields = SwatchesFns::get_taxonomy_meta_fields( $attribute->attribute_type ); ?> attribute_type ) { case 'color': $key = $fields[0]['id']; $is_dual_key = isset( $fields[1]['id'] ) ? $fields[1]['id'] : ''; $dual_color_key = isset( $fields[2]['id'] ) ? $fields[2]['id'] : ''; $value = sanitize_hex_color( get_term_meta( $term_id, $key, true ) ); $is_dual = (bool) ( get_term_meta( $term_id, $is_dual_key, true ) === 'yes' ); if ( $is_dual ) { $secondary_color = sanitize_hex_color( get_term_meta( $term_id, $dual_color_key, true ) ); printf( '
', esc_attr( $secondary_color ), esc_attr( $value ) ); } else { printf( '
', esc_attr( $value ) ); } break; case 'image': $key = $fields[0]['id']; $attachment_id = absint( get_term_meta( $term_id, $key, true ) ); $image = wp_get_attachment_image_url( $attachment_id ); printf( '', esc_url( $image ) ); break; case 'button': default: do_action( 'rtsb/variation/attribute/preview', $term_id, $attribute, $fields ); break; } } /** * Delete term meta when a term is deleted. * * @param int $term_id The term ID. * @param int $tt_id Term taxonomy ID. * @param string $taxonomy Taxonomy slug. */ public function delete_term( $term_id, $tt_id, $taxonomy ) { global $wpdb; $term_id = absint( $term_id ); if ( $term_id && $taxonomy === $this->taxonomy ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $wpdb->delete( $wpdb->termmeta, [ 'term_id' => $term_id ], [ '%d' ] ); wp_cache_delete( $term_id, 'term_meta' ); // Optional, clears any remaining cache. } } /** * Enqueues scripts and styles needed for color pickers and media uploads. * * @return void */ public function enqueue_scripts() { wp_enqueue_media(); wp_enqueue_style( 'wp-color-picker' ); wp_enqueue_script( 'wp-color-picker' ); } /** * Saves custom term meta on term creation or update. * * @param int $term_id Term ID. * @param int $tt_id Term taxonomy ID (optional). * @param string $taxonomy Taxonomy name (optional). * @return void */ public function save( $term_id, $tt_id = '', $taxonomy = '' ) { if ( $taxonomy !== $this->taxonomy ) { return; } if ( ! wp_verify_nonce( Fns::get_nonce(), rtsb()->nonceText ) ) { return; // Invalid nonce. } foreach ( $this->fields as $field ) { $field_id = $field['id']; $field_type = $field['type']; // If field is not submitted and is a checkbox, remove its meta. if ( ! isset( $_POST[ $field_id ] ) ) { if ( 'checkbox' === $field_type ) { delete_term_meta( $term_id, $field_id ); } continue; } switch ( $field_type ) { case 'text': case 'color': $post_value = sanitize_text_field( wp_unslash( $_POST[ $field_id ] ) ); break; case 'url': $post_value = esc_url_raw( wp_unslash( $_POST[ $field_id ] ) ); break; case 'image': $post_value = absint( wp_unslash( $_POST[ $field_id ] ) ); break; case 'textarea': $post_value = sanitize_textarea_field( wp_unslash( $_POST[ $field_id ] ) ); break; case 'checkbox': $post_value = sanitize_text_field( wp_unslash( $_POST[ $field_id ] ) ); $post_value = 'yes' === $post_value ? 'yes' : ''; break; case 'editor': $post_value = wp_kses_post( wp_unslash( $_POST[ $field_id ] ) ); break; case 'select': case 'select2': $post_value = sanitize_key( wp_unslash( $_POST[ $field_id ] ) ); break; default: $post_value = sanitize_text_field( wp_unslash( $_POST[ $field_id ] ) ); do_action( 'rtsb/save/term/meta', $term_id, $field, $post_value, $taxonomy ); break; } update_term_meta( $term_id, $field_id, $post_value ); } do_action( 'rtsb/after/term/meta/saved', $term_id, $taxonomy ); } /** * Outputs meta fields on the "Add New Term" screen. * * @return void */ public function add() { $this->generate_fields(); } /** * Outputs meta fields on the "Edit Term" screen. * * @param object $term Term object. * @return void */ public function edit( $term ) { $this->generate_fields( $term ); } /** * Generates form fields for add/edit screens. * * @param object|false $term Optional term object for edit context. * @return void */ private function generate_fields( $term = false ) { $screen = get_current_screen(); if ( ( $screen->post_type == $this->post_type ) && ( $screen->taxonomy == $this->taxonomy ) ) { self::generate_form_fields( $this->fields, $term ); } } /** * Outputs each field's HTML markup. * * @param array $fields Field definitions. * @param object|false $term Term object, or false for "Add New". * @return void */ public static function generate_form_fields( $fields, $term ) { wp_enqueue_script( 'rtsb-variation-swatch-admin' ); $fields = apply_filters( 'rtsb/term/meta/fields', $fields, $term ); if ( empty( $fields ) ) { return; } wp_nonce_field( rtsb()->nonceText, rtsb()->nonceId ); foreach ( $fields as $field ) { $field = apply_filters( 'rtsb/term/meta/field', $field, $term ); $field['id'] = esc_html( $field['id'] ); if ( ! $term ) { $field['value'] = isset( $field['default'] ) ? $field['default'] : ''; } else { $field['value'] = get_term_meta( $term->term_id, $field['id'], true ); } $field['size'] = isset( $field['size'] ) ? $field['size'] : '40'; $field['required'] = ( isset( $field['required'] ) && true === $field['required'] ) ? ' aria-required="true"' : ''; $field['placeholder'] = ( isset( $field['placeholder'] ) ) ? ' placeholder="' . $field['placeholder'] . '" data-placeholder="' . $field['placeholder'] . '"' : ''; $field['desc'] = ( isset( $field['desc'] ) ) ? $field['desc'] : ''; $field['dependency'] = ( isset( $field['dependency'] ) ) ? $field['dependency'] : []; self::field_start( $field, $term ); switch ( $field['type'] ) { case 'text': case 'url': ?> > > 8, 'quicktags' => false, 'media_buttons' => false, ]; wp_editor( $field['value'], $field['id'], $field['settings'] ); break; case 'select': case 'select2': $field['options'] = isset( $field['options'] ) ? $field['options'] : []; $field['multiple'] = isset( $field['multiple'] ) ? ' multiple="multiple"' : ''; $css_class = ( 'select2' === $field['type'] ) ? 'rtsb-selectwoo' : ''; ?>
class="form-field "> class="form-field ">