-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-expandable-list.php
167 lines (148 loc) · 4.97 KB
/
class-expandable-list.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* Expandable List module class
*
* @package Hogan
*/
declare( strict_types = 1 );
namespace Dekode\Hogan;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
if ( ! class_exists( '\\Dekode\\Hogan\\Expandable_List' ) && class_exists( '\\Dekode\\Hogan\\Module' ) ) {
/**
* Expandable List module class.
*
* @extends Modules base class.
*/
class Expandable_List extends Module {
/**
* List of expandable items.
*
* @var array $items
*/
public $list_items;
/**
* Module index in flexible layout
*
* @var int $counter
*/
public $counter;
/**
* Module constructor.
*/
public function __construct() {
$this->label = __( 'Expandable List', 'hogan-expandable-list' );
$this->template = __DIR__ . '/assets/template.php';
parent::__construct();
}
/**
* Field definitions for module.
*
* @return array $fields Fields for this module
*/
public function get_fields() : array {
$fields = [
[
'type' => 'repeater',
'key' => $this->field_key . '_list_items',
'label' => __( 'List Items', 'hogan-expandable-list' ),
'name' => 'list_items',
'instructions' => __( 'Create a list of expandable items', 'hogan-expandable-list' ),
'min' => 1,
'max' => 0,
'layout' => 'block',
'button_label' => __( 'Add item', 'hogan-expandable-list' ),
'sub_fields' => [
[
'type' => 'image',
'key' => $this->field_key . '_item_thumbnail_id',
'name' => 'item_thumbnail_id',
'label' => __( 'Thumbnail', 'hogan-expandable-list' ),
'instructions' => __( 'Add thumbnail next to list item title', 'hogan-expandable-list' ),
'required' => false,
'return_format' => 'id',
'preview_size' => 'thumbnail',
'library' => 'all',
'wrapper' => [
'width' => '30',
],
],
[
'type' => 'text',
'key' => $this->field_key . '_item_title',
'label' => __( 'Title', 'hogan-expandable-list' ),
'name' => 'item_title',
'required' => true,
'instructions' => __( 'Add list item title', 'hogan-expandable-list' ),
'wrapper' => [
'width' => '70',
],
],
[
'key' => $this->field_key . '_item_accordion',
'label' => '',
'type' => 'accordion',
'instructions' => __( 'Click to open/close item', 'hogan-expandable-list' ),
'open' => 1,
'multi_expand' => 0,
'endpoint' => 0,
],
[
'type' => 'wysiwyg',
'key' => $this->field_key . '_item_content',
'name' => 'item_content',
'label' => __( 'Expandable content', 'hogan-expandable-list' ),
'instructions' => __( 'Add list item expandable content', 'hogan-expandable-list' ),
'delay' => false,
'required' => true,
'tabs' => apply_filters( 'hogan/module/expandable_list/content/tabs', 'all' ),
'media_upload' => apply_filters( 'hogan/module/expandable_list/content/media', 0 ),
'toolbar' => apply_filters( 'hogan/module/expandable_list/content/toolbar', 'hogan_caption' ),
],
[
'type' => 'text',
'key' => $this->field_key . '_item_id',
'label' => __( 'Optional item name', 'hogan-expandable-list' ),
'name' => 'item_id',
'instructions' => __( 'Set a fixed item name so you can link directly to a open item.', 'hogan-expandable-list' ),
],
],
],
];
return $fields;
}
/**
* Enqueue module assets
*/
public function enqueue_assets() {
$_version = defined( 'SCRIPT_DEBUG' ) && true === SCRIPT_DEBUG ? time() : HOGAN_EXPANDABLE_LIST_VERSION;
if ( true === apply_filters( 'hogan/module/expandable_list/load_styles', true ) ) {
wp_enqueue_style( 'hogan-expandable-list-styles', plugins_url( '/assets/styles.css', __FILE__ ), [], $_version );
}
if ( true === apply_filters( 'hogan/module/expandable_list/load_scripts', true ) ) {
wp_enqueue_script( 'hogan-expandable-list-scripts', plugins_url( '/assets/scripts.js', __FILE__ ), [], $_version, true );
}
}
/**
* Map raw fields from acf to object variable.
*
* @param array $raw_content Content values.
* @param int $counter Module location in page layout.
* @return void
*/
public function load_args_from_layout_content( array $raw_content, int $counter = 0 ) {
$this->list_items = $raw_content['list_items'];
$this->counter = $counter;
parent::load_args_from_layout_content( $raw_content, $counter );
}
/**
* Validate module content before template is loaded.
*
* @return bool Whether validation of the module is successful / filled with content.
*/
public function validate_args() : bool {
return ! empty( $this->list_items );
}
}
}