| | 2779 | /** |
| | 2780 | * @ticket 38280 |
| | 2781 | */ |
| | 2782 | public function test_wp_get_term_count_for_object_type_single_object_type() { |
| | 2783 | $term_id = self::factory()->term->create( array( 'taxonomy' => 'category' ) ); |
| | 2784 | $post_id = self::factory()->post->create( array( 'post_type' => 'post' ) ); |
| | 2785 | |
| | 2786 | wp_set_object_terms( $post_id, array( $term_id ), 'category' ); |
| | 2787 | |
| | 2788 | $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) ); |
| | 2789 | $this->assertFalse( (bool) get_term_meta( $term_id, '_wp_counted_object_types', true ) ); |
| | 2790 | |
| | 2791 | $term_object = get_term( $term_id, 'category' ); |
| | 2792 | $this->assertEquals( 1, $term_object->count ); |
| | 2793 | |
| | 2794 | wp_remove_object_terms( $post_id, array( $term_id ), 'category' ); |
| | 2795 | |
| | 2796 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) ); |
| | 2797 | } |
| | 2798 | |
| | 2799 | /** |
| | 2800 | * @ticket 38280 |
| | 2801 | */ |
| | 2802 | public function test_wp_get_term_count_for_object_type_multiple_object_types() { |
| | 2803 | register_post_type( 'wptests_cpt' ); |
| | 2804 | register_taxonomy_for_object_type( 'category', 'wptests_cpt' ); |
| | 2805 | |
| | 2806 | $term_id = self::factory()->term->create( array( 'taxonomy' => 'category' ) ); |
| | 2807 | $post_id = self::factory()->post->create( array( 'post_type' => 'post' ) ); |
| | 2808 | $custom_post_id = self::factory()->post->create( array( 'post_type' => 'wptests_cpt' ) ); |
| | 2809 | |
| | 2810 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) ); |
| | 2811 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'wptests_cpt' ) ); |
| | 2812 | $this->assertEmpty( get_term_meta( $term_id, '_wp_counted_object_types', true ) ); |
| | 2813 | |
| | 2814 | wp_set_object_terms( $post_id, array( $term_id ), 'category' ); |
| | 2815 | |
| | 2816 | $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) ); |
| | 2817 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'wptests_cpt' ) ); |
| | 2818 | $this->assertEquals( array( 'post', 'wptests_cpt' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) ); |
| | 2819 | |
| | 2820 | wp_set_object_terms( $custom_post_id, array( $term_id ), 'category' ); |
| | 2821 | |
| | 2822 | $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) ); |
| | 2823 | $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'wptests_cpt' ) ); |
| | 2824 | $this->assertEquals( array( 'post', 'wptests_cpt' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) ); |
| | 2825 | |
| | 2826 | $term_object = get_term( $term_id, 'category' ); |
| | 2827 | $this->assertEquals( 2, $term_object->count ); |
| | 2828 | |
| | 2829 | wp_remove_object_terms( $custom_post_id, array( $term_id ), 'category' ); |
| | 2830 | |
| | 2831 | $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) ); |
| | 2832 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'wptests_cpt' ) ); |
| | 2833 | $this->assertEquals( array( 'post', 'wptests_cpt' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) ); |
| | 2834 | |
| | 2835 | wp_remove_object_terms( $post_id, array( $term_id ), 'category' ); |
| | 2836 | |
| | 2837 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) ); |
| | 2838 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'wptests_cpt' ) ); |
| | 2839 | $this->assertEquals( array( 'post', 'wptests_cpt' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) ); |
| | 2840 | } |
| | 2841 | |
| | 2842 | /** |
| | 2843 | * @ticket 38280 |
| | 2844 | */ |
| | 2845 | public function test_wp_get_term_count_for_object_type_multiple_object_types_attachment() { |
| | 2846 | register_taxonomy_for_object_type( 'category', 'attachment' ); |
| | 2847 | |
| | 2848 | $term_id = self::factory()->term->create( array( 'taxonomy' => 'category' ) ); |
| | 2849 | $post_id = self::factory()->post->create( array( 'post_type' => 'post' ) ); |
| | 2850 | $attachment_id = self::factory()->attachment->create_upload_object( DIR_TESTDATA . '/images/canola.jpg', $post_id ); |
| | 2851 | |
| | 2852 | $this->assertEmpty( wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) ); |
| | 2853 | $this->assertEmpty( wp_get_term_count_for_object_type( $term_id, 'category', 'attachment' ) ); |
| | 2854 | $this->assertEmpty( get_term_meta( $term_id, '_wp_counted_object_types', true ) ); |
| | 2855 | |
| | 2856 | wp_set_object_terms( $post_id, array( $term_id ), 'category' ); |
| | 2857 | |
| | 2858 | $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) ); |
| | 2859 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'attachment' ) ); |
| | 2860 | $this->assertEquals( array( 'post', 'attachment' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) ); |
| | 2861 | |
| | 2862 | wp_set_object_terms( $attachment_id, array( $term_id ), 'category' ); |
| | 2863 | |
| | 2864 | $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) ); |
| | 2865 | $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'attachment' ) ); |
| | 2866 | $this->assertEquals( array( 'post', 'attachment' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) ); |
| | 2867 | |
| | 2868 | $term_object = get_term( $term_id, 'category' ); |
| | 2869 | $this->assertEquals( 2, $term_object->count ); |
| | 2870 | |
| | 2871 | wp_remove_object_terms( $attachment_id, array( $term_id ), 'category' ); |
| | 2872 | |
| | 2873 | $this->assertEquals( 1, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) ); |
| | 2874 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'attachment' ) ); |
| | 2875 | |
| | 2876 | wp_remove_object_terms( $post_id, array( $term_id ), 'category' ); |
| | 2877 | |
| | 2878 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'post' ) ); |
| | 2879 | $this->assertEquals( 0, wp_get_term_count_for_object_type( $term_id, 'category', 'attachment' ) ); |
| | 2880 | $this->assertEquals( array( 'post', 'attachment' ), get_term_meta( $term_id, '_wp_counted_object_types', true ) ); |
| | 2881 | } |
| | 2882 | |