|
| 1 | +<?php |
| 2 | +namespace TMS\Theme\Tredu\ACF; |
| 3 | + |
| 4 | +use Geniem\ACF\Exception; |
| 5 | +use Geniem\ACF\Group; |
| 6 | +use Geniem\ACF\RuleGroup; |
| 7 | +use Geniem\ACF\Field; |
| 8 | +use TMS\Theme\Tredu\Logger; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class BlogCategoryGroup |
| 12 | + * |
| 13 | + * @package TMS\Theme\Tredu\ACF |
| 14 | + */ |
| 15 | +class BlogCategoryGroup { |
| 16 | + |
| 17 | + /** |
| 18 | + * BlogCategoryGroup constructor. |
| 19 | + */ |
| 20 | + public function __construct() { |
| 21 | + \add_action( |
| 22 | + 'init', |
| 23 | + \Closure::fromCallable( [ $this, 'register_fields' ] ) |
| 24 | + ); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Register fields for blog-category taxonomy. |
| 29 | + */ |
| 30 | + protected function register_fields() : void { |
| 31 | + try { |
| 32 | + $field_group = ( new Group( 'Blogikategoria' ) ) |
| 33 | + ->set_key( 'fg_blog_category_fields' ); |
| 34 | + |
| 35 | + $rule_group = ( new RuleGroup() ) |
| 36 | + ->add_rule( 'taxonomy', '==', 'blog-category' ); |
| 37 | + |
| 38 | + $field_group |
| 39 | + ->add_rule_group( $rule_group ) |
| 40 | + ->set_position( 'normal' ); |
| 41 | + |
| 42 | + $field_group->add_fields( |
| 43 | + \apply_filters( |
| 44 | + 'tms/acf/group/' . $field_group->get_key() . '/fields', |
| 45 | + [ |
| 46 | + $this->get_image_field( $field_group->get_key() ), |
| 47 | + ] |
| 48 | + ) |
| 49 | + ); |
| 50 | + |
| 51 | + $field_group = \apply_filters( |
| 52 | + 'tms/acf/group/' . $field_group->get_key(), |
| 53 | + $field_group |
| 54 | + ); |
| 55 | + |
| 56 | + $field_group->register(); |
| 57 | + } |
| 58 | + catch ( Exception $e ) { |
| 59 | + ( new Logger() )->error( $e->getMessage(), $e->getTraceAsString() ); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Get image field. |
| 65 | + * |
| 66 | + * @param string $key Field group key. |
| 67 | + * |
| 68 | + * @return Field\Image |
| 69 | + * @throws Exception In case of invalid option. |
| 70 | + */ |
| 71 | + protected function get_image_field( string $key ) : Field\Image { |
| 72 | + $strings = [ |
| 73 | + 'image' => [ |
| 74 | + 'title' => \_x( 'Kuva', 'theme ACF', 'tms-theme-tredu' ), |
| 75 | + 'instructions' => 'Kuvaa käytetään kategorian listaus-sivulla hero-kuvana', |
| 76 | + ], |
| 77 | + ]; |
| 78 | + |
| 79 | + return ( new Field\Image( $strings['image']['title'] ) ) |
| 80 | + ->set_key( "{$key}_image" ) |
| 81 | + ->set_name( 'image' ) |
| 82 | + ->set_instructions( $strings['image']['instructions'] ); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +( new BlogCategoryGroup() ); |
0 commit comments