Admin
2 weeks ago
Ajax
2 weeks ago
Asset
2 weeks ago
Context
2 weeks ago
Customizer
2 weeks ago
Debug_Bar
2 weeks ago
Dialog
2 weeks ago
Documentation
2 weeks ago
Duplicate
2 weeks ago
Editor
2 weeks ago
Image
2 weeks ago
JSON_LD
2 weeks ago
Languages
2 weeks ago
Log
2 weeks ago
Meta
2 weeks ago
Models
2 weeks ago
PUE
2 weeks ago
Process
2 weeks ago
Promoter
2 weeks ago
REST
2 weeks ago
Repository
2 weeks ago
Service_Providers
2 weeks ago
Shortcode
2 weeks ago
Support
2 weeks ago
Tabbed_View
2 weeks ago
Tooltip
2 weeks ago
Traits
2 weeks ago
Utils
2 weeks ago
Validator
2 weeks ago
Widget
2 weeks ago
Abstract_Deactivation.php
2 weeks ago
Abstract_Plugin_Register.php
2 weeks ago
App_Shop.php
2 weeks ago
Assets.php
2 weeks ago
Assets_Pipeline.php
2 weeks ago
Autoloader.php
2 weeks ago
Cache.php
2 weeks ago
Cache_Listener.php
2 weeks ago
Changelog_Reader.php
2 weeks ago
Container.php
2 weeks ago
Context.php
2 weeks ago
Cost_Utils.php
2 weeks ago
Credits.php
2 weeks ago
Customizer.php
2 weeks ago
DB_Lock.php
2 weeks ago
Data.php
2 weeks ago
Date_Utils.php
2 weeks ago
Db.php
2 weeks ago
Debug.php
2 weeks ago
Dependency.php
2 weeks ago
Deprecation.php
2 weeks ago
Editor.php
2 weeks ago
Error.php
2 weeks ago
Exception.php
2 weeks ago
Extension.php
2 weeks ago
Extension_Loader.php
2 weeks ago
Feature_Detection.php
2 weeks ago
Field.php
2 weeks ago
Field_Conditional.php
2 weeks ago
Freemius.php
2 weeks ago
Log.php
2 weeks ago
Main.php
2 weeks ago
Notices.php
2 weeks ago
Plugin_Meta_Links.php
2 weeks ago
Plugins.php
2 weeks ago
Plugins_API.php
2 weeks ago
Post_History.php
2 weeks ago
Post_Transient.php
2 weeks ago
Promise.php
2 weeks ago
Repository.php
2 weeks ago
Rewrite.php
2 weeks ago
Settings.php
2 weeks ago
Settings_Manager.php
2 weeks ago
Settings_Tab.php
2 weeks ago
Simple_Table.php
2 weeks ago
Support.php
2 weeks ago
Tabbed_View.php
2 weeks ago
Template.php
2 weeks ago
Template_Factory.php
2 weeks ago
Template_Part_Cache.php
2 weeks ago
Templates.php
2 weeks ago
Terms.php
2 weeks ago
Timezones.php
2 weeks ago
Tracker.php
2 weeks ago
Updater.php
2 weeks ago
Validate.php
2 weeks ago
View_Helpers.php
2 weeks ago
Terms.php
57 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | class Tribe__Terms { |
| 5 | |
| 6 | /** |
| 7 | * Translates an array or list of `term_id`s or `slug`s to an array of `term_id`s; if a term is missing and specified by `slug` it |
| 8 | * will be created. |
| 9 | * |
| 10 | * @param array|string $terms An array or comma separated list of term `term_id` or `slug` or a single `term_id` or `slug`. |
| 11 | * @param string $taxonomy |
| 12 | * @param bool $create_missing Whether terms that could not be found by `term_id` or `slug` should be creater or not. |
| 13 | * |
| 14 | * @return array An array containing the `term_id`s of the created terms. |
| 15 | */ |
| 16 | public static function translate_terms_to_ids( $terms, $taxonomy, $create_missing = true ) { |
| 17 | $terms = is_string( $terms ) ? preg_split( '/\\s*,\\s*/', $terms ) : (array) $terms; |
| 18 | |
| 19 | $term_ids = []; |
| 20 | foreach ( $terms as $term ) { |
| 21 | if ( ! $term instanceof WP_Term && ! strlen( trim( $term ) ) ) { |
| 22 | continue; |
| 23 | } |
| 24 | |
| 25 | if ( $term instanceof WP_Term ) { |
| 26 | $term_info = $term->to_array(); |
| 27 | } elseif ( is_numeric( $term ) ) { |
| 28 | $term = absint( $term ); |
| 29 | $term_info = get_term( $term, $taxonomy, ARRAY_A ); |
| 30 | } else { |
| 31 | $term_info = term_exists( $term, $taxonomy ); |
| 32 | } |
| 33 | |
| 34 | if ( ! $term_info ) { |
| 35 | // Skip if a non-existent term ID is passed. |
| 36 | if ( is_numeric( $term ) ) { |
| 37 | continue; |
| 38 | } |
| 39 | |
| 40 | if ( true == $create_missing ) { |
| 41 | $term_info = wp_insert_term( $term, $taxonomy ); |
| 42 | } else { |
| 43 | continue; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | if ( is_wp_error( $term_info ) ) { |
| 48 | continue; |
| 49 | } |
| 50 | |
| 51 | $term_ids[] = $term_info['term_id']; |
| 52 | } |
| 53 | |
| 54 | return array_unique( $term_ids ); |
| 55 | } |
| 56 | } |
| 57 |