Plugin Directory

Changeset 2551418


Ignore:
Timestamp:
06/21/2021 12:42:03 PM (5 years ago)
Author:
abigegg
Message:

Commit 0.5.5 to trunk

File:
1 edited

Legend:

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

    r2477848 r2551418  
    33 * Plugin name: Hatch
    44 * Author: A Big Egg
    5  * Version: 0.4.4
     5 * Version: 0.5.5
    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 */
     
    1212
    1313class Hatch {
    14     static $context = [];
    15 
    16     static $post_transformers = [];
     14    static $context               = [];
     15
     16    static $post_transformers     = [];
    1717    static $taxonomy_transformers = [];
     18    static $main_transformer      = false;
    1819
    1920    function __construct() {
    2021        // ensure that post objects from ACF are passed through our transformation function
    21         add_filter( 'acf/format_value/type=post_object', [ $this, 'construct_post' ], 50 );
     22        add_filter( 'acf/format_value/type=post_object',  [ $this, 'construct_post' ], 50 );
    2223        add_filter( 'acf/format_value/type=relationship', [ $this, 'construct_posts' ], 50 );
    23         add_filter( 'acf/format_value/type=image', [ $this, 'construct_image' ], 50 );
     24        add_filter( 'acf/format_value/type=image',        [ $this, 'construct_image' ], 50 );
    2425    }
    2526
     
    188189        $base_context = Timber::get_context();
    189190        $base_context['post'] = self::get_post();
     191
     192        if ( self::has_transformer_for_main() ) {
     193            $base_context = self::transform_main( $base_context );
     194        }
    190195       
    191196        return array_merge( $base_context, static::$context, $context );
     
    348353    }
    349354   
     355    /**
     356     * has_transformer_for_main
     357     *
     358     * @return void
     359     */
     360    static function has_transformer_for_main() {
     361        return !! self::$main_transformer;
     362    }
     363   
    350364
    351365    /**
     
    379393
    380394        return call_user_func( $transformer, $term );
     395    }
     396   
     397    /**
     398     * transform_main
     399     *
     400     * @param  mixed $context
     401     * @return void
     402     */
     403    static function transform_main( $context ) {
     404        if ( ! self::has_transformer_for_main() ) {
     405            return $context;
     406        }
     407
     408        $transformer = self::$main_transformer;
     409
     410        return call_user_func( $transformer, $context );
    381411    }
    382412
     
    400430     *
    401431     * @param  mixed $post_id
    402      * @return void
     432     * @return array
    403433     */
    404434    static function construct_terms( $array_of_terms ) {
     
    434464        self::$taxonomy_transformers[$taxonomy] = $callback;
    435465    }
     466   
     467    /**
     468     * Register a transformer for the main context
     469     *
     470     * @param  mixed $callback
     471     * @return void
     472     */
     473    static function register_transformer_for_main( $callback ) {
     474        self::$main_transformer = $callback;
     475    }
     476
    436477    /**
    437478     * Debug function - call this to dump the current context to inspect it
Note: See TracChangeset for help on using the changeset viewer.