Collaboration
2 weeks ago
Apps.php
2 weeks ago
Collection.php
1 month ago
Comments.php
2 months ago
DynamicContent.php
1 month ago
ExportImport.php
2 weeks ago
Form.php
2 weeks ago
Media.php
2 weeks ago
Page.php
2 weeks ago
PageSettings.php
2 weeks ago
RBAC.php
2 weeks ago
Symbol.php
2 weeks ago
Taxonomy.php
2 months ago
TemplateExportImport.php
2 months ago
UserData.php
2 weeks ago
Users.php
2 months ago
Walkthrough.php
2 months ago
WordpressData.php
2 months ago
WpAdmin.php
2 months ago
Taxonomy.php
193 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Taxonomy API |
| 4 | * |
| 5 | * @package kirki |
| 6 | */ |
| 7 | |
| 8 | namespace Kirki\Ajax; |
| 9 | |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | exit; // Exit if accessed directly. |
| 12 | } |
| 13 | |
| 14 | use Kirki\HelperFunctions; |
| 15 | use Exception; |
| 16 | |
| 17 | /** |
| 18 | * Taxonomy API Class |
| 19 | */ |
| 20 | class Taxonomy { |
| 21 | public static function get_post_terms() { |
| 22 | try { |
| 23 | $id = HelperFunctions::sanitize_text( isset( $_GET['id'] ) ? $_GET['id'] : null ); |
| 24 | |
| 25 | $taxonomy = HelperFunctions::sanitize_text( isset( $_GET['taxonomy'] ) ? $_GET['taxonomy'] : null ); |
| 26 | |
| 27 | $taxonomies = get_the_terms( $id, $taxonomy ); |
| 28 | |
| 29 | if ( $taxonomies instanceof \WP_Error ) { |
| 30 | throw new Exception( 'Failed!' ); |
| 31 | } |
| 32 | |
| 33 | if ( is_array( $taxonomies ) ) { |
| 34 | wp_send_json_success( |
| 35 | $taxonomies |
| 36 | ); |
| 37 | } else { |
| 38 | wp_send_json_success( array() ); |
| 39 | } |
| 40 | } catch ( Exception $e ) { |
| 41 | wp_send_json_error( |
| 42 | $e->getMessage(), |
| 43 | $e->getCode() ?? 400 |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | die; |
| 48 | } |
| 49 | |
| 50 | public static function get_terms() { |
| 51 | try { |
| 52 | $name = HelperFunctions::sanitize_text( isset( $_GET['name'] ) ? $_GET['name'] : null ); |
| 53 | |
| 54 | $terms = get_terms( |
| 55 | array( |
| 56 | 'taxonomy' => $name, |
| 57 | 'hide_empty' => false, |
| 58 | ) |
| 59 | ); |
| 60 | |
| 61 | if ( $terms instanceof \WP_Error ) { |
| 62 | throw new Exception( 'Error!' ); |
| 63 | } |
| 64 | |
| 65 | wp_send_json_success( $terms ); |
| 66 | } catch ( Exception $e ) { |
| 67 | wp_send_json_error( |
| 68 | $e->getMessage(), |
| 69 | $e->getCode() ?? 400 |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | die; |
| 74 | } |
| 75 | |
| 76 | public static function get_post_type_taxonomies() { |
| 77 | try { |
| 78 | $post_types = get_post_types( array(), 'objects' ); |
| 79 | $response = array(); |
| 80 | |
| 81 | foreach ( $post_types as $post_type => $details ) { |
| 82 | $taxonomies = get_object_taxonomies( $post_type, 'objects' ); |
| 83 | if ( ! empty( $taxonomies ) ) { |
| 84 | // Format taxonomies as array with value and title |
| 85 | $formatted_taxonomies = array(); |
| 86 | foreach ( $taxonomies as $taxonomy ) { |
| 87 | $formatted_taxonomies[] = array( |
| 88 | 'value' => $taxonomy->name, |
| 89 | 'title' => $taxonomy->label, |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | if ( ! empty( $formatted_taxonomies ) ) { |
| 94 | $response[] = array( |
| 95 | 'value' => $post_type, |
| 96 | 'title' => $details->label, |
| 97 | 'taxonomies' => $formatted_taxonomies, |
| 98 | ); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | wp_send_json_success( $response ); |
| 104 | } catch ( Exception $e ) { |
| 105 | wp_send_json_error( |
| 106 | $e->getMessage(), |
| 107 | $e->getCode() ?? 400 |
| 108 | ); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | public static function get_post_type_from_term_id( $term_id ) { |
| 113 | // Get the term object from the term ID |
| 114 | $term = get_term( $term_id ); |
| 115 | // Check if the term exists |
| 116 | if ( ! $term || is_wp_error( $term ) ) { |
| 117 | return null; |
| 118 | } |
| 119 | // Get the taxonomy associated with the term |
| 120 | $taxonomy = $term->taxonomy; |
| 121 | // Get the post types associated with the taxonomy |
| 122 | $post_types = get_post_types( array() ); |
| 123 | foreach ( $post_types as $key => $post_type ) { |
| 124 | // Get all taxonomies associated with the post type |
| 125 | $taxonomies = get_object_taxonomies( $post_type, 'objects' ); |
| 126 | if ( ! empty( $taxonomies ) ) { |
| 127 | foreach ( $taxonomies as $taxonomy_slug => $taxonomy_object ) { |
| 128 | if ( $taxonomy_slug === $taxonomy ) { |
| 129 | return $post_type; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | public static function get_term_by_id( $term_id ) { |
| 138 | $term = get_term( $term_id ); |
| 139 | $term = get_object_vars( $term ); |
| 140 | |
| 141 | $post_type = self::get_post_type_from_term_id( $term_id ); |
| 142 | $term['post_type'] = $post_type; |
| 143 | |
| 144 | return $term; |
| 145 | } |
| 146 | |
| 147 | public static function get_all_terms_by_post_type() { |
| 148 | $post_type = HelperFunctions::sanitize_text( isset( $_GET['post_type'] ) ? $_GET['post_type'] : null ); |
| 149 | |
| 150 | // Get taxonomy objects related to the post type |
| 151 | $taxonomies = get_object_taxonomies( $post_type, 'objects' ); |
| 152 | $result = array(); |
| 153 | |
| 154 | if ( count( $taxonomies ) > 0 ) { |
| 155 | // Loop through each taxonomy |
| 156 | foreach ( $taxonomies as $taxonomy_slug => $taxonomy_obj ) { |
| 157 | // Get all terms for this taxonomy |
| 158 | $terms = get_terms( |
| 159 | array( |
| 160 | 'taxonomy' => $taxonomy_slug, |
| 161 | 'hide_empty' => false, |
| 162 | ) |
| 163 | ); |
| 164 | |
| 165 | // Skip if error or empty |
| 166 | if ( is_wp_error( $terms ) || empty( $terms ) ) { |
| 167 | continue; |
| 168 | } |
| 169 | |
| 170 | // Prepare term list with required fields |
| 171 | $terms_array = array(); |
| 172 | foreach ( $terms as $term ) { |
| 173 | $terms_array[] = array( |
| 174 | 'term_id' => $term->term_id, |
| 175 | 'name' => $term->name, |
| 176 | 'slug' => $term->slug, |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | // Add taxonomy with terms to result |
| 181 | $result[] = array( |
| 182 | 'value' => $taxonomy_slug, |
| 183 | 'title' => $taxonomy_obj->label, |
| 184 | 'terms' => $terms_array, |
| 185 | ); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | wp_send_json( $result ); |
| 190 | } |
| 191 | |
| 192 | } |
| 193 |