forked from Log1x/acf-composer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWidget.php
155 lines (133 loc) · 3.74 KB
/
Widget.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
<?php
namespace Log1x\AcfComposer;
use WP_Widget;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Log1x\AcfComposer\Contracts\Widget as WidgetContract;
use Log1x\AcfComposer\Concerns\InteractsWithBlade;
abstract class Widget extends Composer implements WidgetContract
{
use InteractsWithBlade;
/**
* The widget instance.
*
* @var object
*/
public $widget;
/**
* The widget ID.
*
* @var string
*/
public $id;
/**
* The name of the widget.
*
* @var string
*/
public $name = '';
/**
* The widget slug.
*
* @var string
*/
public $slug = '';
/**
* The widget description.
*
* @var string
*/
public $description = '';
/**
* Compose and register the defined ACF field groups.
*
* @return void
*/
public function compose()
{
if (empty($this->fields) || empty($this->name)) {
return;
}
if (empty($this->slug)) {
$this->slug = Str::slug($this->name);
}
if (! Arr::has($this->fields, 'location.0.0')) {
Arr::set($this->fields, 'location.0.0', [
'param' => 'widget',
'operator' => '==',
'value' => $this->slug,
]);
}
$this->register(function () {
$this->widget = (object) collect(
Arr::get($GLOBALS, 'wp_registered_widgets')
)->filter(function ($value) {
return $value['name'] === $this->name;
})->pop();
});
add_filter('widgets_init', function () {
register_widget($this->widget());
}, 20);
return $this;
}
/**
* Returns an instance of WP_Widget used to register the widget.
*
* @return WP_Widget
*/
protected function widget()
{
return (new class ($this) extends WP_Widget {
/**
* Create a new WP_Widget instance.
*
* @param \Log1x\AcfComposer\Widget $composer
* @return void
*/
public function __construct($composer)
{
$this->composer = $composer;
parent::__construct(
$this->composer->slug,
$this->composer->name,
['description' => $this->composer->description]
);
}
/**
* Render the widget.
*
* @param array $args
* @param array $instance
* @return void
*/
public function widget($args, $instance)
{
$this->composer->id = $this->composer->widget->id = Str::start($args['widget_id'], 'widget_');
echo Arr::get($args, 'before_widget');
if (! empty($this->composer->title())) {
echo collect([
Arr::get($args, 'before_title'),
$this->composer->title(),
Arr::get($args, 'after_title')
])->implode(PHP_EOL);
}
echo $this->composer->view(
Str::finish('widgets.', $this->composer->slug),
['widget' => $this->composer]
);
echo Arr::get($args, 'after_widget');
}
/**
* Output the widget settings update form.
* This is intentionally blank due to it being set by ACF.
*
* @param array $instance
* @return void
*/
public function form($instance)
{
//
}
});
}
}