Plugin Directory

Changeset 2700848


Ignore:
Timestamp:
03/28/2022 10:09:35 PM (4 years ago)
Author:
abigegg
Message:

Commit 1.0.0 to trunk

Location:
hatch
Files:
2 edited
7 copied

Legend:

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

    r2634970 r2700848  
    33 * Plugin name: Hatch
    44 * Author: A Big Egg
    5  * Version: 0.6.2
     5 * Version: 1.0.0
    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 */
     
    99require_once('inc/image.php');
    1010
    11 register_activation_hook(__FILE__, [ 'Hatch', 'activate_plugin' ]);
    1211
    1312class Hatch
    1413{
    15     public static $context               = [];
    16 
    17     public static $post_transformers     = [];
     14    public static $context = [];
     15   
     16    public static $post_transformers = [];
    1817    public static $taxonomy_transformers = [];
    19     public static $main_transformer      = false;
    20 
     18    public static $main_transformer = false;
     19   
    2120    public function __construct()
    2221    {
     22        register_activation_hook(__FILE__, [ $this, 'activate_plugin' ]);
     23       
     24   
    2325        // ensure that post objects from ACF are passed through our transformation function
    2426        add_filter('acf/format_value/type=post_object', [ $this, 'construct_post' ], 50);
    2527        add_filter('acf/format_value/type=relationship', [ $this, 'construct_posts' ], 50);
    2628        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
    3441    /**
    3542     * Add an ACF field to the Timber array, passing an optional preparation function to prepare the value
     
    97104     *
    98105     * @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)
    103110    {
    104111        if (is_array($key)) {
     
    109116        }
    110117
    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        }
    150121    }
    151122   
     
    154125     *
    155126     * @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 ID
     127     * @param  mixed $callback A function which returns the form ID that should be used
    157128     * @param  mixed $args The args
    158129     * @return void
    159130     */
    160     public static function add_gform_context($key, $form_id, $args = [])
     131    public static function add_gform_context($key, $callback, $args = [])
    161132    {
    162133        $args = wp_parse_args($args, [
     
    169140        ]);
    170141       
    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, []);
    172153
    173154        $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  
    33Tags: timber, templating, developers
    44Requires at least: 4.7
    5 Tested up to: 5.6.1
     5Tested up to: 5.9.2
    66Stable tag: 0.4.4
    77Requires PHP: 7.0
     
    1414
    1515Hatch 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
     19Timber (latest version.)
    1620
    1721== A better way to use Timber ==
Note: See TracChangeset for help on using the changeset viewer.