[wp-trac] [WordPress Trac] #32789: Abstract get_category_by_path into get_term_by_path

WordPress Trac noreply at wordpress.org
Thu Jun 25 16:18:40 UTC 2015


#32789: Abstract get_category_by_path into get_term_by_path
-------------------------+------------------------------
 Reporter:  sc0ttkclark  |       Owner:
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  Taxonomy     |     Version:
 Severity:  normal       |  Resolution:
 Keywords:               |     Focuses:
-------------------------+------------------------------

Comment (by sc0ttkclark):

 Based on `get_category_by_path`:

 {{{
 /**
  * Retrieve term based on URL containing the term slug.
  *
  * Breaks the $term_path parameter up to get the term slug.
  *
  * Tries to find the child path and will return it. If it doesn't find a
  * match, then it will return the first term matching slug, if
 $full_match,
  * is set to false. If it does not, then it will return null.
  *
  * It is also possible that it will return a WP_Error object on failure.
 Check
  * for it when using this function.
  *
  * @param string $term_path URL containing term slugs.
  * @param string $taxonomy Taxonomy to match path for.
  * @param bool $full_match Optional. Whether full path should be matched.
  * @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
  *
  * @return null|object|array|\WP_Error Null on failure. Type is based on
 $output value.
  */
 function get_term_by_path( $term_path, $taxonomy, $full_match = true,
 $output = OBJECT ) {

         $term_path = rawurlencode( urldecode( $term_path ) );
         $term_path = str_replace( '%2F', '/', $term_path );
         $term_path = str_replace( '%20', ' ', $term_path );

         $term_paths = '/' . trim( $term_path, '/' );
         $term_paths = explode( '/', $term_paths );

         $leaf_path = sanitize_title( basename( $term_paths ) );

         $full_path = '';

         foreach ( (array) $term_paths as $pathdir ) {
                 if ( '' != $pathdir ) {
                         $full_path .= '/';
                 }

                 $full_path .= sanitize_title( $pathdir );
         }

         $terms = get_terms( $taxonomy, array( 'get' => 'all', 'slug' =>
 $leaf_path ) );

         if ( empty( $terms ) ) {
                 return null;
         } elseif ( is_wp_error( $terms ) ) {
                 return $terms;
         }

         foreach ( $terms as $term ) {
                 $current_term = $term;

                 $path = '/' . $leaf_path;

                 while ( ( $current_term->parent != 0 ) && (
 $current_term->parent != $current_term->term_id ) ) {
                         $current_term = get_term( $current_term->parent,
 $taxonomy );

                         if ( is_wp_error( $current_term ) ) {
                                 return $current_term;
                         }

                         $path = '/' . $current_term->slug . $path;
                 }

                 if ( $path === $full_path ) {
                         return get_term( $term->term_id, $taxonomy,
 $output );
                 }
         }

         // If full matching is not required, return the first cat that
 matches the leaf.
         if ( ! $full_match ) {
                 $term = reset( $terms );

                 return get_term( $term->term_id, $taxonomy, $output );
         }

         return null;

 }
 }}}

 I added $taxonomy as a required second parameter.

 I also fixed a few things, one was `@return` was not properly reporting
 WP_Error as a possible return type, others were formatting or variable
 names. The get_terms empty check did not have an elseif check for
 `is_wp_error( $terms )` so that's been put in place as well.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/32789#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list