Make WordPress Core

Changeset 26510


Ignore:
Timestamp:
12/01/2013 11:59:13 PM (13 years ago)
Author:
wonderboymusic
Message:

Fix lingering reference problem in wp_get_object_terms() by not setting the foreach'd vars to a reference. Adds unit test.

Props stephenharris.
Fixes #26339.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r26028 r26510  
    20472047        if ( 'all' == $fields || 'all_with_object_id' == $fields ) {
    20482048                $_terms = $wpdb->get_results( $query );
    2049                 foreach ( $_terms as &$term )
    2050                         $term = sanitize_term( $term, $taxonomy, 'raw' );
     2049                foreach ( $_terms as $key => $term ) {
     2050                        $_terms[$key] = sanitize_term( $term, $taxonomy, 'raw' );
     2051                }
    20512052                $terms = array_merge( $terms, $_terms );
    20522053                update_term_cache( $terms );
    … …  
    20542055                $_terms = $wpdb->get_col( $query );
    20552056                $_field = ( 'ids' == $fields ) ? 'term_id' : 'name';
    2056                 foreach ( $_terms as &$term )
    2057                         $term = sanitize_term_field( $_field, $term, $term, $taxonomy, 'raw' );
     2057                foreach ( $_terms as $key => $term ) {
     2058                        $_terms[$key] = sanitize_term_field( $_field, $term, $term, $taxonomy, 'raw' );
     2059                }
    20582060                $terms = array_merge( $terms, $_terms );
    20592061        } else if ( 'tt_ids' == $fields ) {
    20602062                $terms = $wpdb->get_col("SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies) $orderby $order");
    2061                 foreach ( $terms as &$tt_id )
    2062                         $tt_id = sanitize_term_field( 'term_taxonomy_id', $tt_id, 0, $taxonomy, 'raw' ); // 0 should be the term id, however is not needed when using raw context.
     2063                foreach ( $terms as $key => $tt_id ) {
     2064                        $terms[$key] = sanitize_term_field( 'term_taxonomy_id', $tt_id, 0, $taxonomy, 'raw' ); // 0 should be the term id, however is not needed when using raw context.
     2065                }
    20632066        }
    20642067
  • trunk/tests/phpunit/tests/term.php

    r26187 r26510  
    99        function setUp() {
    1010                parent::setUp();
    11                
     11
    1212                _clean_term_filters();
    1313                // insert one term into every post taxonomy
    … …  
    8686                // clean up
    8787                $this->assertTrue( wp_delete_term($t['term_id'], $this->taxonomy) );
     88        }
     89
     90        function test_get_object_terms() {
     91                $post_id = $this->factory->post->create();
     92                $terms_1 = array('foo', 'bar', 'baz');
     93
     94                wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
     95                add_filter( 'wp_get_object_terms', array( $this, 'filter_get_object_terms' ) );
     96                $terms = wp_get_object_terms( $post_id, $this->taxonomy );
     97                remove_filter( 'wp_get_object_terms', array( $this, 'filter_get_object_terms' ) );
     98                foreach ( $terms as $term ) {
     99                        $this->assertInternalType( 'object', $term );
     100                }
     101        }
     102
     103        function filter_get_object_terms( $terms ) {
     104                // var_dump reveals an array of objects
     105                $term_ids = wp_list_pluck( $terms, 'term_id' );
     106                // last term is now an integer
     107                return $terms;
    88108        }
    89109
Note: See TracChangeset for help on using the changeset viewer.