Plugin Directory

Changeset 2477848


Ignore:
Timestamp:
02/19/2021 04:44:16 PM (5 years ago)
Author:
abigegg
Message:

Add 0.4.4

Location:
hatch/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • hatch/trunk/abe-hatch.php

    r2474064 r2477848  
    33 * Plugin name: Hatch
    44 * Author: A Big Egg
    5  * Version: 0.4.3
     5 * Version: 0.4.4
    66 * Description: Provides helper functions for working with Timber and ACF. Requires Timber (timber-library) and works well with Advanced Custom Fields Pro and Gravity Forms.
    77 */
     
    1515
    1616    static $post_transformers = [];
     17    static $taxonomy_transformers = [];
    1718
    1819    function __construct() {
     
    250251     */
    251252    static function get_terms( $args ) {
    252         return Timber::get_terms( $args );
     253        return self::construct_terms( Timber::get_terms( $args ) );
    253254    }
    254255   
     
    291292    }
    292293   
     294       
     295    /**
     296     * construct term
     297     *
     298     * @param  mixed $term_id
     299     * @return void
     300     */
     301    static function construct_term( $term_id = 0 ) {
     302        if ( ! $term_id ) {
     303            return $term_id;
     304        }
     305
     306        $term = new Timber\Term( $term_id );
     307
     308        if ( self::has_transformer_for_taxonomy( $term->taxonomy ) ) {
     309            $term = self::transform_term( $term );
     310        }
     311   
     312        return $term;
     313    }
     314   
     315   
    293316    /**
    294317     * construct_image
     
    316339   
    317340    /**
     341     * has_transformer_for_term_type
     342     *
     343     * @param  mixed $taxonomy
     344     * @return void
     345     */
     346    static function has_transformer_for_taxonomy( $taxonomy ) {
     347        return isset( self::$taxonomy_transformers[$taxonomy] );
     348    }
     349   
     350
     351    /**
    318352     * transform_post
    319353     *
     
    329363
    330364        return call_user_func( $transformer, $post );
     365    }
     366   
     367    /**
     368     * transform_term
     369     *
     370     * @param  mixed $post
     371     * @return void
     372     */
     373    static function transform_term( $term ) {
     374        $transformer = self::$taxonomy_transformers[$term->taxonomy];
     375
     376        if ( ! self::has_transformer_for_taxonomy( $term->taxonomy ) ) {
     377            return $term;
     378        }
     379
     380        return call_user_func( $transformer, $term );
    331381    }
    332382
     
    346396
    347397   
     398    /**
     399     * construct_terms
     400     *
     401     * @param  mixed $post_id
     402     * @return void
     403     */
     404    static function construct_terms( $array_of_terms ) {
     405        if ( ! is_array( $array_of_terms ) ) {
     406            return [];
     407        }
     408
     409        return array_map( 'self::construct_term', $array_of_terms );
     410    }
     411
     412   
    348413   
    349414    /**
     
    359424    }
    360425   
     426    /**
     427     * Register a transformer for a taxonomy
     428     *
     429     * @param  mixed $taxonomy
     430     * @param  mixed $callback
     431     * @return void
     432     */
     433    static function register_transformer_for_taxonomy( $taxonomy, $callback ) {
     434        self::$taxonomy_transformers[$taxonomy] = $callback;
     435    }
    361436    /**
    362437     * Debug function - call this to dump the current context to inspect it
  • hatch/trunk/readme.txt

    r2474064 r2477848  
    44Requires at least: 4.7
    55Tested up to: 5.6.1
    6 Stable tag: 0.4.3
     6Stable tag: 0.4.4
    77Requires PHP: 7.0
    88License: MIT
Note: See TracChangeset for help on using the changeset viewer.