forked from publishpress/PublishPress-Authors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWidget.php
179 lines (152 loc) · 5.31 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* @package MultipleAuthors
* @author PublishPress <[email protected]>
* @copyright Copyright (C) 2018 PublishPress. All rights reserved.
* @license GPLv2 or later
* @since 1.0.0
*/
namespace MultipleAuthors;
use MultipleAuthors\Traits\Author_box;
use WP_Widget;
class Widget extends WP_Widget {
use Author_box;
/**
* Widget Title
*
* @var string
*
* @since 3.4.0
*/
protected $title;
/**
* Widget Title for Plural
*
* @var string
*
* @since 3.4.0
*/
protected $titlePlural;
/**
* Sets up the widgets name etc
*/
public function __construct()
{
$this->title = esc_html__( 'Post Author', 'publishpress-authors' );
parent::__construct(
'multiple_authors_widget',
$this->title,
array(
'classname' => 'multiple_authors_widget',
'description' => esc_html__(
'Display a list of authors for the current post.',
'publishpress-authors'
),
)
);
}
/**
* Outputs the content of the widget
*
* @param array $args
* @param array $instance
*/
public function widget( $args, $instance )
{
$legacyPlugin = Factory::getLegacyPlugin();
$instance = wp_parse_args(
(array) $instance,
array(
'title' => $this->title,
'title_plural' => !empty($this->titlePlural) ? $this->titlePlural : $this->title,
)
);
$output = '';
if ( $this->should_display_author_box() ) {
$layout = isset( $instance['layout'] ) ? $instance['layout'] : $legacyPlugin->modules->multiple_authors->options->layout;
$output .= $this->get_author_box_markup( 'widget', false, $layout );
/** This filter is documented in core/src/wp-includes/default-widgets.php */
if ($this->authorsCount <= 1) {
$title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base);
} else {
$title = apply_filters('widget_title', $instance['title_plural'], $instance, $this->id_base);
}
if ( ! empty( $output ) ) {
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $args['before_title'] . apply_filters('widget_title', esc_html($title)) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
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' => $this->title,
'title_plural' => $this->titlePlural,
'layout' => $legacyPlugin->modules->multiple_authors->options->layout,
)
);
$titleSingle = strip_tags($instance['title']);
$titlePlural = strip_tags($instance['title_plural']);
$layout = strip_tags($instance['layout']);
$context = array(
'labels' => array(
'title' => esc_html__( 'Single Title', 'publishpress-authors' ),
'title_plural' => esc_html__( 'Plural Title', 'publishpress-authors' ),
'layout' => esc_html__( 'Layout', 'publishpress-authors' ),
),
'ids' => array(
'title' => esc_html($this->get_field_id( 'title' )),
'title_plural' => esc_html($this->get_field_id( 'title_plural' )),
'layout' => esc_html($this->get_field_id( 'layout' )),
'nonce' => esc_html($this->get_field_id( 'nonce' )),
),
'names' => array(
'title' => esc_html($this->get_field_name( 'title' )),
'title_plural' => esc_html($this->get_field_name( 'title_plural' )),
'layout' => esc_html($this->get_field_name( 'layout' )),
'nonce' => esc_html($this->get_field_name( 'nonce' )),
),
'values' => array(
'title' => esc_html($titleSingle),
'title_plural' => esc_html($titlePlural),
'layout' => esc_html($layout),
'nonce' => wp_create_nonce('pp_multiple_authors_widget_form'),
),
'layouts' => apply_filters( 'pp_multiple_authors_author_layouts', array() ),
);
$container = Factory::get_container();
echo $container['twig']->render( '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)
{
if (! isset($new_instance['nonce']) || ! wp_verify_nonce($new_instance['nonce'], 'pp_multiple_authors_widget_form')) {
return $old_instance;
}
$legacyPlugin = Factory::getLegacyPlugin();
$instance = [];
$instance['title'] = sanitize_text_field($new_instance['title']);
$instance['title_plural'] = isset($new_instance['title_plural']) ? sanitize_text_field($new_instance['title_plural']) : '';
$instance['layout'] = sanitize_text_field($new_instance['layout']);
$layouts = apply_filters('pp_multiple_authors_author_layouts', []);
if (! array_key_exists($instance['layout'], $layouts)) {
$instance['layout'] = $legacyPlugin->modules->multiple_authors->options->layout;
}
return $instance;
}
}