Ticket #38280: 38280.2.diff
File 38280.2.diff, 6.1 KB (added by , 7 years ago) |
---|
-
src/wp-includes/taxonomy.php
2897 2897 return true; 2898 2898 } 2899 2899 2900 /** 2901 * Retrieves the term count for a specific object type. 2902 * 2903 * @param int $term_id Term ID. 2904 * @param string $taxonomy Taxonomy name. 2905 * @param string $object_type Object type. 2906 * 2907 * @return WP_Error|bool|int WP_Error if invalid taxonomy is passed. 2908 * False if object is not in taxonomy. 2909 * Object term count otherwise. 2910 */ 2911 function wp_get_term_count_for_object_type( $term_id, $taxonomy, $object_type ) { 2912 if ( ! taxonomy_exists( $taxonomy ) ) { 2913 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); 2914 } 2915 2916 if ( ! is_object_in_taxonomy( $object_type, $taxonomy ) ) { 2917 return false; 2918 } 2919 2920 $term_count_meta = get_term_meta( $term_id, '_wp_term_counts', true ); 2921 2922 if ( ! $term_count_meta ) { 2923 $term = get_term( $term_id, $taxonomy ); 2924 return (int) $term->count; 2925 } 2926 2927 return (int) $term_count_meta[ $object_type ]; 2928 } 2929 2900 2930 // 2901 2931 // Cache 2902 2932 // … … 3387 3417 3388 3418 foreach ( (array) $terms as $term ) { 3389 3419 $count = 0; 3420 $term_count_meta = (array) get_term_meta( $term, '_wp_term_counts', true ); 3390 3421 3422 // Remove any object types that no longer have taxonomy support. 3423 $term_count_meta = array_intersect( $term_count_meta, $object_types ); 3424 3391 3425 // Attachments can be 'inherit' status, we need to base count off the parent's status if so. 3392 if ( $check_attachments ) 3393 $ count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts p1 WHERE p1.ID = $wpdb->term_relationships.object_id AND ( post_status = 'publish' OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) = 'publish' ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term ) );3426 if ( $check_attachments ) { 3427 $attachment_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts p1 WHERE p1.ID = $wpdb->term_relationships.object_id AND ( post_status = 'publish' OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) = 'publish' ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term ) ); 3394 3428 3395 if ( $object_types ) 3396 $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type IN ('" . implode("', '", $object_types ) . "') AND term_taxonomy_id = %d", $term ) ); 3429 $count += $attachment_count; 3430 $term_count_meta['attachment'] = $attachment_count; 3431 } 3397 3432 3433 if ( $object_types ) { 3434 foreach ( $object_types as $type ) { 3435 $current_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type = '" . $type . "' AND term_taxonomy_id = %d", $term ) ); 3436 3437 $term_count_meta[ $type ] = $current_count; 3438 $count += $current_count; 3439 } 3440 } 3441 3442 if ( 1 < count( $term_count_meta ) ) { 3443 update_term_meta( $term, '_wp_term_counts', $term_count_meta ); 3444 } else { 3445 delete_term_meta( $term, '_wp_term_counts' ); 3446 } 3447 3398 3448 /** This action is documented in wp-includes/taxonomy.php */ 3399 3449 do_action( 'edit_term_taxonomy', $term, $taxonomy->name ); 3400 3450 $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); -
tests/phpunit/tests/term/getTerms.php
228 228 ), $terms_id_slug ); 229 229 } 230 230 231 231 /** 232 232 * @ticket 11823 233 233 */ 234 234 function test_get_terms_include_exclude() { 235 235 global $wpdb; 236 236 … … 2219 2219 $this->assertNotEquals( 'foo', $found ); 2220 2220 } 2221 2221 2222 /** 2223 * @ticket 38280 2224 */ 2225 public function test_wp_get_term_count_for_object_type_single_object_type() { 2226 $term_id = self::factory()->term->create( array( 'taxonomy' => 'category' ) ); 2227 $post_id = self::factory()->post->create( array( 'post_type' => 'post' ) ); 2228 2229 wp_set_object_terms( $post_id, array( $term_id ), 'category' ); 2230 2231 $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) ); 2232 2233 $term_object = get_term( $term_id, 'category' ); 2234 $this->assertEquals( 1, $term_object->count ); 2235 } 2236 2237 /** 2238 * @ticket 38280 2239 */ 2240 public function test_wp_get_term_count_for_object_type_multiple_object_types() { 2241 register_post_type( 'wptests_cpt' ); 2242 register_taxonomy_for_object_type( 'category', 'wptests_cpt' ); 2243 2244 $term_id = self::factory()->term->create( array( 'taxonomy' => 'category' ) ); 2245 $post_id = self::factory()->post->create( array( 'post_type' => 'post' ) ); 2246 $custom_post_id = self::factory()->post->create( array( 'post_type' => 'wptests_cpt' ) ); 2247 2248 $this->assertEmpty( wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) ); 2249 $this->assertEmpty( wp_get_term_count_for_object_type( $term_id, 'category', 'wptests_cpt' ) ); 2250 2251 wp_set_object_terms( $post_id, array( $term_id ), 'category' ); 2252 2253 $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) ); 2254 $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'wptests_cpt' ) ); 2255 2256 wp_set_object_terms( $custom_post_id, array( $term_id ), 'category' ); 2257 2258 $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) ); 2259 $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'wptests_cpt' ) ); 2260 2261 $this->assertEquals( array( 'post' => 1, 'wptests_cpt' => 1 ), get_term_meta( $term_id, '_wp_term_counts', true ) ); 2262 2263 $term_object = get_term( $term_id, 'category' ); 2264 $this->assertEquals( 2, $term_object->count ); 2265 } 2266 2222 2267 public static function maybe_filter_count() { 2223 2268 return 'foo'; 2224 2269 }