forked from publishpress/PublishPress-Authors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthors_Widget.php
270 lines (231 loc) · 9.54 KB
/
Authors_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
/**
* @package MultipleAuthors
* @author PublishPress <[email protected]>
* @copyright Copyright (C) 2018 PublishPress. All rights reserved.
* @license GPLv2 or later
* @since 3.4.0
*/
namespace MultipleAuthors;
use MultipleAuthors\Classes\Utils;
use WP_Widget;
class Authors_Widget extends WP_Widget
{
/**
* Sets up the widgets name etc
*/
public function __construct()
{
$this->title = esc_html__('Authors List', 'publishpress-authors');
parent::__construct(
'multiple_authors_list_widget',
$this->title,
array(
'classname' => 'multiple_authors_authors-list_widget',
'description' => esc_html__(
'Display authors list.',
'publishpress-authors'
),
)
);
}
/**
* Outputs the content of the widget
*
* @param array $args
* @param array $instance
*/
public function widget($args, $instance)
{
$instance = wp_parse_args(
(array)$instance,
array(
'title' => esc_html($this->title),
)
);
/** This filter is documented in core/src/wp-includes/default-widgets.php */
$title = apply_filters(
'widget_title',
isset($instance['title']) ? esc_html($instance['title']) : '',
$instance,
$this->id_base
);
$output = '';
$output .= $this->get_author_box_markup($args, $instance);
if (!empty($output)) {
if (isset($args['before_widget'])) {
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
if (!isset($instance['show_title']) || true === $instance['show_title']) {
echo sprintf(
'%s<h2 class="widget-title">%s</h2>%s',
isset($args['before_title']) ? $args['before_title'] : '', // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
esc_html(apply_filters('widget_title', $title)), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
isset($args['after_title']) ? $args['after_title'] : '' // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
);
}
echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
if (isset($args['after_widget'])) {
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
}
/**
* Outputs the options form on admin
*
* @param array $instance The widget options
*/
public function form($instance)
{
$legacyPlugin = Factory::getLegacyPlugin();
$instance = wp_parse_args(
(array)$instance,
array(
'title' => esc_html($this->title),
'layout' => esc_html($legacyPlugin->modules->multiple_authors->options->layout),
'show_empty' => true
)
);
$title = esc_html($instance['title']);
$layout = esc_html($instance['layout']);
$showEmpty = isset($instance['show_empty']) && (bool)$instance['show_empty'];
$context = array(
'labels' => array(
'title' => esc_html__('Title', 'publishpress-authors'),
'layout' => esc_html__('Layout', 'publishpress-authors'),
'show_empty' => esc_html__(
'Display All Authors (including those who have not written any posts)',
'publishpress-authors'
)
),
'ids' => array(
'title' => esc_html($this->get_field_id('title')),
'layout' => esc_html($this->get_field_id('layout')),
'show_empty' => esc_html($this->get_field_id('show_empty'))
),
'names' => array(
'title' => esc_html($this->get_field_name('title')),
'layout' => esc_html($this->get_field_name('layout')),
'show_empty' => esc_html($this->get_field_name('show_empty'))
),
'values' => array(
'title' => esc_html($title),
'layout' => esc_html($layout),
'show_empty' => $showEmpty
),
'layouts' => apply_filters('pp_multiple_authors_author_layouts', array()),
);
$container = Factory::get_container();
echo $container['twig']->render('authors-list-widget-form.twig', $context); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Processing widget options on save
*
* @param array $new_instance The new options
* @param array $old_instance The previous options
*/
public function update($new_instance, $old_instance)
{
$instance = array();
$instance['title'] = sanitize_text_field($new_instance['title']);
$instance['layout'] = sanitize_text_field($new_instance['layout']);
$instance['show_empty'] = isset($new_instance['show_empty']) ? (bool)$new_instance['show_empty'] : false;
$layouts = apply_filters('pp_multiple_authors_author_layouts', array());
if (!array_key_exists($instance['layout'], $layouts)) {
$instance['layout'] = Utils::getDefaultLayout();
}
return $instance;
}
/**
* Get HTML markdown
*
* @param array $args The args.
* @param array $instance The object instance.
*
* @return string $html The html.
*/
private function get_author_box_markup(
$args,
$instance,
$target = 'widget'
) {
$html = '';
$legacyPlugin = Factory::getLegacyPlugin();
if (apply_filters('publishpress_authors_load_style_in_frontend', PUBLISHPRESS_AUTHORS_LOAD_STYLE_IN_FRONTEND)) {
wp_enqueue_style('dashicons');
wp_enqueue_style(
'multiple-authors-widget-css',
PP_AUTHORS_ASSETS_URL . 'css/multiple-authors-widget.css',
false,
PP_AUTHORS_VERSION,
'all'
);
//load font awesome assets if enable
$load_font_awesome = isset($legacyPlugin->modules->multiple_authors->options->load_font_awesome)
? 'yes' === $legacyPlugin->modules->multiple_authors->options->load_font_awesome : true;
if ($load_font_awesome) {
wp_enqueue_style(
'multiple-authors-fontawesome',
PP_AUTHORS_ASSETS_URL . 'lib/fontawesome/css/fontawesome.min.css',
false,
PP_AUTHORS_VERSION,
'all'
);
wp_enqueue_script(
'multiple-authors-fontawesome',
PP_AUTHORS_ASSETS_URL . 'lib/fontawesome/js/fontawesome.min.js',
['jquery'],
PP_AUTHORS_VERSION
);
}
}
if (!function_exists('multiple_authors')) {
require_once PP_AUTHORS_BASE_PATH . 'functions/template-tags.php';
}
$css_class = '';
if (!empty($target)) {
$css_class = 'multiple-authors-target-' . str_replace('_', '-', $target);
}
$title = isset($instance['title']) ? esc_html($instance['title']) : '';
$layout = isset($instance['layout']) ? $instance['layout'] : null;
if (empty($layout)) {
$layout = isset($legacyPlugin->modules->multiple_authors->options->layout)
? $legacyPlugin->modules->multiple_authors->options->layout : Utils::getDefaultLayout();
}
if (empty($color_scheme)) {
$color_scheme = isset($legacyPlugin->modules->multiple_authors->options->color_scheme)
? $legacyPlugin->modules->multiple_authors->options->color_scheme : '#655997';
}
$show_email = isset($legacyPlugin->modules->multiple_authors->options->show_email_link)
? 'yes' === $legacyPlugin->modules->multiple_authors->options->show_email_link : true;
$show_site = isset($legacyPlugin->modules->multiple_authors->options->show_site_link)
? 'yes' === $legacyPlugin->modules->multiple_authors->options->show_site_link : true;
$showEmpty = isset($instance['show_empty']) ? $instance['show_empty'] : false;
$args = [
'show_title' => false,
'css_class' => esc_attr($css_class),
'title' => $title,
'authors' => multiple_authors_get_all_authors(array('hide_empty' => !$showEmpty)),
'target' => $target,
'item_class' => 'author url fn',
'layout' => $layout,
'color_scheme' => $color_scheme,
'show_email' => $show_email,
'show_site' => $show_site
];
/**
* Filter the author box arguments before sending to the renderer.
*
* @param array $args
*/
$args = apply_filters('pp_multiple_authors_authors_list_box_args', $args);
/**
* Filter the author box HTML code, allowing to use custom rendered layouts.
*
* @param string $html
* @param array $args
*/
$html = apply_filters('pp_multiple_authors_authors_list_box_html', null, $args);
return $html;
}
}