Changeset 2551418
- Timestamp:
- 06/21/2021 12:42:03 PM (5 years ago)
- File:
-
- 1 edited
-
hatch/trunk/abe-hatch.php (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
hatch/trunk/abe-hatch.php
r2477848 r2551418 3 3 * Plugin name: Hatch 4 4 * Author: A Big Egg 5 * Version: 0. 4.45 * Version: 0.5.5 6 6 * 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. 7 7 */ … … 12 12 13 13 class Hatch { 14 static $context = [];15 16 static $post_transformers = [];14 static $context = []; 15 16 static $post_transformers = []; 17 17 static $taxonomy_transformers = []; 18 static $main_transformer = false; 18 19 19 20 function __construct() { 20 21 // 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 ); 22 23 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 ); 24 25 } 25 26 … … 188 189 $base_context = Timber::get_context(); 189 190 $base_context['post'] = self::get_post(); 191 192 if ( self::has_transformer_for_main() ) { 193 $base_context = self::transform_main( $base_context ); 194 } 190 195 191 196 return array_merge( $base_context, static::$context, $context ); … … 348 353 } 349 354 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 350 364 351 365 /** … … 379 393 380 394 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 ); 381 411 } 382 412 … … 400 430 * 401 431 * @param mixed $post_id 402 * @return void432 * @return array 403 433 */ 404 434 static function construct_terms( $array_of_terms ) { … … 434 464 self::$taxonomy_transformers[$taxonomy] = $callback; 435 465 } 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 436 477 /** 437 478 * Debug function - call this to dump the current context to inspect it
Note: See TracChangeset
for help on using the changeset viewer.