__( 'Taxonomies', 'airwpsync' ), 'slug' => 'taxonomy', ); } protected function get_mapping_fields() { $whitelist = array( 'category', 'post_tag' ); $excluded = array( 'link_category' ); $taxonomies = get_taxonomies( array( 'show_ui' => 1, ), 'objects' ); $taxonomy_options = array(); foreach ( $taxonomies as $taxonomy ) { if ( ! in_array( $taxonomy->name, $excluded, true ) ) { $enabled = in_array( $taxonomy->name, $whitelist, true ); $taxonomy_options[] = array( 'value' => $taxonomy->name, 'label' => sprintf( '%s (%s)', $taxonomy->labels->singular_name, $taxonomy->name ) . ( ! $enabled ? ' ' . __( '(Pro version, coming soon)', 'airwpsync' ) : '' ), 'enabled' => $enabled, ); } } return $taxonomy_options; } /** * Import terms */ public function import( $importer, $fields, $record, $post_id ) { $this->importer = $importer; $mapped_fields = $this->get_my_mapping( $importer, $fields ); foreach ( $mapped_fields as $mapped_field ) { $value = $fields[ $mapped_field['airtable'] ] ?? false; if ( ! empty( $value ) ) { $this->import_terms( $post_id, $value, $mapped_field['wordpress'] ); } } } /** * Log */ protected function log( $message, $level = 'log' ) { if ( $this->importer ) { $this->importer->log( $message, $level ); } } /** * Import array of values as taxonomy terms */ protected function import_terms( $post_id, $values, $taxonomy ) { $values = is_string( $values ) ? explode( ',', $values ) : $values; $terms = array(); foreach ( $values as $value ) { $value = wp_strip_all_tags( $value ); $term = $this->get_object_term( $post_id, $value, $taxonomy ); if ( is_wp_error( $term ) ) { $this->log( sprintf( '- Cannot get term \'%s\' (taxonomy: \'%s\'), error: %s', $value, $taxonomy, $term->get_error_message() ) ); } else { $terms[] = (int) $term['term_id']; } } wp_set_object_terms( $post_id, $terms, $taxonomy ); } /** * Set terms to object, create term if new */ protected function get_object_term( $object_id, $value, $taxonomy ) { $term = term_exists( $value, $taxonomy ); if ( 0 === $term || null === $term ) { $term = wp_insert_term( $value, $taxonomy ); } return $term; } } new Air_WP_Sync_Taxonomy_Destination();