Changeset 2700848
- Timestamp:
- 03/28/2022 10:09:35 PM (4 years ago)
- Location:
- hatch
- Files:
-
- 2 edited
- 7 copied
-
tags/0.6.2 (copied) (copied from hatch/trunk)
-
tags/0.6.2/LICENSE.md (copied) (copied from hatch/trunk/LICENSE.md)
-
tags/0.6.2/abe-hatch.php (copied) (copied from hatch/trunk/abe-hatch.php)
-
tags/0.6.2/inc (copied) (copied from hatch/trunk/inc)
-
tags/0.6.2/inc/image.php (copied) (copied from hatch/trunk/inc/image.php)
-
tags/0.6.2/readme.md (copied) (copied from hatch/trunk/readme.md)
-
tags/0.6.2/readme.txt (copied) (copied from hatch/trunk/readme.txt)
-
trunk/abe-hatch.php (modified) (6 diffs)
-
trunk/readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
hatch/trunk/abe-hatch.php
r2634970 r2700848 3 3 * Plugin name: Hatch 4 4 * Author: A Big Egg 5 * Version: 0.6.25 * Version: 1.0.0 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 */ … … 9 9 require_once('inc/image.php'); 10 10 11 register_activation_hook(__FILE__, [ 'Hatch', 'activate_plugin' ]);12 11 13 12 class Hatch 14 13 { 15 public static $context = [];16 17 public static $post_transformers = [];14 public static $context = []; 15 16 public static $post_transformers = []; 18 17 public static $taxonomy_transformers = []; 19 public static $main_transformer = false;20 18 public static $main_transformer = false; 19 21 20 public function __construct() 22 21 { 22 register_activation_hook(__FILE__, [ $this, 'activate_plugin' ]); 23 24 23 25 // ensure that post objects from ACF are passed through our transformation function 24 26 add_filter('acf/format_value/type=post_object', [ $this, 'construct_post' ], 50); 25 27 add_filter('acf/format_value/type=relationship', [ $this, 'construct_posts' ], 50); 26 28 add_filter('acf/format_value/type=image', [ $this, 'construct_image' ], 50); 27 } 28 29 public function activate_plugin() 30 { 31 update_option('_abe_hatch_use_timber', true); 32 } 33 29 30 // check environment is suitable 31 add_filter('plugins_loaded', [ $this, 'check_prerequisites']); 32 } 33 34 public function check_prerequisites() 35 { 36 if (! class_exists('Timber') && ! defined('WP_CLI')) { 37 throw new Exception('The Timber plugin must be installed for Hatch to work.'); 38 } 39 } 40 34 41 /** 35 42 * Add an ACF field to the Timber array, passing an optional preparation function to prepare the value … … 97 104 * 98 105 * @param mixed $key The key to add to the context array OR an array of keys 99 * @param mixed $value The value to set (or a function which will return the value)100 * @return void 101 */ 102 public static function add_context($key, $ value= false)106 * @param mixed $value The function which, when called, will return the context to add 107 * @return void 108 */ 109 public static function add_context($key, $callback = false) 103 110 { 104 111 if (is_array($key)) { … … 109 116 } 110 117 111 $current_post = self::get_post(); 112 113 self::$context[$key] = self::get_value_from_thing($value, [ $current_post ]); 114 } 115 116 117 /** 118 * Sometimes it's nice to be able to pass a function OR a different type of variable, for more fluent coding 119 * 120 * For instance in add_context() we can take either a string or a function as the second value 121 * 122 * So we could do 123 * 124 * ```php 125 * add_context( 'key', 'my value' ) 126 * ``` 127 * 128 * OR 129 * 130 * ```php 131 * add_context( 'key', function() { 132 * return 'my value'; 133 * } ); 134 * ``` 135 * 136 * It's nicer this way! 137 * 138 * @param mixed $thing 139 * @param mixed Post for context (passed into the callback ) 140 * @return void 141 */ 142 private static function get_value_from_thing($thing, $context = []) 143 { 144 if (is_callable($thing) || function_exists($thing)) { 145 // we can optionally pass in context to the function 146 return call_user_func_array($thing, $context); 147 } 148 149 return $thing; 118 if (is_callable($callback)) { 119 self::$context[$key] = call_user_func_array($callback, []); 120 } 150 121 } 151 122 … … 154 125 * 155 126 * @param mixed $key The key to add to the context array 156 * @param mixed $ form_id The ID of the form, or a function which returns the ID127 * @param mixed $callback A function which returns the form ID that should be used 157 128 * @param mixed $args The args 158 129 * @return void 159 130 */ 160 public static function add_gform_context($key, $ form_id, $args = [])131 public static function add_gform_context($key, $callback, $args = []) 161 132 { 162 133 $args = wp_parse_args($args, [ … … 169 140 ]); 170 141 171 $form_id = self::get_value_from_thing($form_id); 142 if (! is_callable($callback)) { 143 throw new Exception('Callback must be of type callable'); 144 return; 145 } 146 147 if (! function_exists('gravity_form')) { 148 throw new Exception('Gravity Forms is not installed/active, or gravity_form function unavailable'); 149 return; 150 } 151 152 $form_id = call_user_func_array($callback, []); 172 153 173 154 $rendered_form = gravity_form($form_id, $args['display_title'], $args['display_description'], $args['display_inactive'], $args['field_values'], $args['ajax'], $args['tabindex'], false); -
hatch/trunk/readme.txt
r2477848 r2700848 3 3 Tags: timber, templating, developers 4 4 Requires at least: 4.7 5 Tested up to: 5. 6.15 Tested up to: 5.9.2 6 6 Stable tag: 0.4.4 7 7 Requires PHP: 7.0 … … 14 14 15 15 Hatch is a helper library for developers which makes it more pleasant to build websites using Timber, Advanced Custom Fields Pro and other common plugins. 16 17 == Requirements == 18 19 Timber (latest version.) 16 20 17 21 == A better way to use Timber ==
Note: See TracChangeset
for help on using the changeset viewer.