term_id, '_vc_image_url', true );
?>
|
|
array(
'name' => $plural,
'singular_name' => $singular,
),
'public' => true,
'hierarchical' => $hierarchical,
'rewrite' => array( 'slug' => $slug ),
'show_in_rest' => true,
)
);
}
public static function get_or_create_destination( $item ) {
$name = self::term_name( $item );
if ( '' === $name ) {
return 0;
}
$id = vc_first_non_empty( array( vc_array_get( $item, 'destinationId' ), vc_array_get( $item, 'id' ) ) );
$ref = vc_first_non_empty( array( vc_array_get( $item, 'destinationRef' ), vc_array_get( $item, 'ref' ) ) );
return self::get_or_create_term( 'viator_destination', $name, '_vc_viator_destination_id', $id, array( '_vc_viator_destination_ref' => $ref ) );
}
public static function get_or_create_tag( $item ) {
$name = self::term_name( $item );
$id = vc_first_non_empty( array( vc_array_get( $item, 'tagId' ), vc_array_get( $item, 'id' ) ) );
return self::get_or_create_term( 'viator_tag', $name, '_vc_viator_tag_id', $id );
}
/**
* Resolve a destination term from a numeric Viator destination id, using
* a name looked up from the destinations dictionary.
*/
public static function get_or_create_destination_by_id( $id, $name = '' ) {
$id = (string) $id;
if ( '' === $id ) {
return 0;
}
$name = sanitize_text_field( (string) $name );
if ( '' === $name ) {
/* translators: %s: Viator destination id. */
$name = sprintf( __( 'Destination %s', 'viator-collector' ), $id );
}
return self::get_or_create_term( 'viator_destination', $name, '_vc_viator_destination_id', $id );
}
/**
* Resolve a tag term from a numeric Viator tag id, using a name looked up
* from the tags dictionary.
*/
public static function get_or_create_tag_by_id( $id, $name = '' ) {
$id = (string) $id;
if ( '' === $id ) {
return 0;
}
$name = sanitize_text_field( (string) $name );
if ( '' === $name ) {
/* translators: %s: Viator tag id. */
$name = sprintf( __( 'Tag %s', 'viator-collector' ), $id );
}
return self::get_or_create_term( 'viator_tag', $name, '_vc_viator_tag_id', $id );
}
public static function get_or_create_category( $item ) {
$name = self::term_name( $item );
$id = vc_first_non_empty( array( vc_array_get( $item, 'categoryId' ), vc_array_get( $item, 'id' ) ) );
return self::get_or_create_term( 'viator_category', $name, '_vc_viator_category_id', $id );
}
private static function term_name( $item ) {
if ( is_scalar( $item ) ) {
return sanitize_text_field( (string) $item );
}
if ( ! is_array( $item ) ) {
return '';
}
return sanitize_text_field(
vc_first_non_empty(
array(
vc_array_get( $item, 'name' ),
vc_array_get( $item, 'destinationName' ),
vc_array_get( $item, 'tagName' ),
vc_array_get( $item, 'categoryName' ),
vc_array_get( $item, 'title' ),
)
)
);
}
private static function get_or_create_term( $taxonomy, $name, $meta_key, $viator_id = '', $extra_meta = array() ) {
if ( '' === $name ) {
return 0;
}
if ( '' !== (string) $viator_id ) {
$found = get_terms(
array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
'number' => 1,
'meta_query' => array(
array(
'key' => $meta_key,
'value' => (string) $viator_id,
),
),
)
);
if ( ! is_wp_error( $found ) && ! empty( $found ) ) {
self::update_term_meta( $found[0]->term_id, $meta_key, $viator_id, $extra_meta );
return (int) $found[0]->term_id;
}
}
$term = term_exists( $name, $taxonomy );
if ( 0 === $term || null === $term ) {
$term = wp_insert_term( $name, $taxonomy );
}
if ( is_wp_error( $term ) ) {
VC_Logger::log( 'Term error: ' . $term->get_error_message(), true );
return 0;
}
$term_id = is_array( $term ) ? (int) $term['term_id'] : (int) $term;
self::update_term_meta( $term_id, $meta_key, $viator_id, $extra_meta );
return $term_id;
}
private static function update_term_meta( $term_id, $meta_key, $viator_id, $extra_meta ) {
if ( '' !== (string) $viator_id ) {
update_term_meta( $term_id, $meta_key, sanitize_text_field( (string) $viator_id ) );
}
foreach ( $extra_meta as $key => $value ) {
if ( '' !== (string) $value ) {
update_term_meta( $term_id, $key, sanitize_text_field( (string) $value ) );
}
}
}
}