author | ymh <ymh.work@gmail.com> |
Tue, 22 Oct 2019 16:11:46 +0200 | |
changeset 15 | 3d4e9c994f10 |
parent 9 | 177826044cd9 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
* Core Widgets API |
0 | 4 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5 |
* This API is used for creating dynamic sidebar without hardcoding functionality into |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6 |
* themes |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
8 |
* Includes both internal WordPress routines and theme-use routines. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
9 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
10 |
* This functionality was found in a plugin before the WordPress 2.2 release, which |
0 | 11 |
* included it in the core from that point on. |
12 |
* |
|
5 | 13 |
* @link https://codex.wordpress.org/Plugins/WordPress_Widgets WordPress Widgets |
14 |
* @link https://codex.wordpress.org/Plugins/WordPress_Widgets_Api Widgets API |
|
0 | 15 |
* |
16 |
* @package WordPress |
|
17 |
* @subpackage Widgets |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
18 |
* @since 2.2.0 |
0 | 19 |
*/ |
20 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
21 |
// |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
22 |
// Global Variables |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
23 |
// |
0 | 24 |
|
25 |
/** @ignore */ |
|
26 |
global $wp_registered_sidebars, $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates; |
|
27 |
||
28 |
/** |
|
29 |
* Stores the sidebars, since many themes can have more than one. |
|
30 |
* |
|
9 | 31 |
* @global array $wp_registered_sidebars Registered sidebars. |
0 | 32 |
* @since 2.2.0 |
33 |
*/ |
|
34 |
$wp_registered_sidebars = array(); |
|
35 |
||
36 |
/** |
|
37 |
* Stores the registered widgets. |
|
38 |
* |
|
39 |
* @global array $wp_registered_widgets |
|
40 |
* @since 2.2.0 |
|
41 |
*/ |
|
42 |
$wp_registered_widgets = array(); |
|
43 |
||
44 |
/** |
|
45 |
* Stores the registered widget control (options). |
|
46 |
* |
|
47 |
* @global array $wp_registered_widget_controls |
|
48 |
* @since 2.2.0 |
|
49 |
*/ |
|
50 |
$wp_registered_widget_controls = array(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
51 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
52 |
* @global array $wp_registered_widget_updates |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
53 |
*/ |
0 | 54 |
$wp_registered_widget_updates = array(); |
55 |
||
56 |
/** |
|
57 |
* Private |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
58 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
59 |
* @global array $_wp_sidebars_widgets |
0 | 60 |
*/ |
61 |
$_wp_sidebars_widgets = array(); |
|
62 |
||
63 |
/** |
|
64 |
* Private |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
65 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
66 |
* @global array $_wp_deprecated_widgets_callbacks |
0 | 67 |
*/ |
5 | 68 |
$GLOBALS['_wp_deprecated_widgets_callbacks'] = array( |
69 |
'wp_widget_pages', |
|
0 | 70 |
'wp_widget_pages_control', |
71 |
'wp_widget_calendar', |
|
72 |
'wp_widget_calendar_control', |
|
73 |
'wp_widget_archives', |
|
74 |
'wp_widget_archives_control', |
|
75 |
'wp_widget_links', |
|
76 |
'wp_widget_meta', |
|
77 |
'wp_widget_meta_control', |
|
78 |
'wp_widget_search', |
|
79 |
'wp_widget_recent_entries', |
|
80 |
'wp_widget_recent_entries_control', |
|
81 |
'wp_widget_tag_cloud', |
|
82 |
'wp_widget_tag_cloud_control', |
|
83 |
'wp_widget_categories', |
|
84 |
'wp_widget_categories_control', |
|
85 |
'wp_widget_text', |
|
86 |
'wp_widget_text_control', |
|
87 |
'wp_widget_rss', |
|
88 |
'wp_widget_rss_control', |
|
89 |
'wp_widget_recent_comments', |
|
9 | 90 |
'wp_widget_recent_comments_control', |
5 | 91 |
); |
0 | 92 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
93 |
// |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
94 |
// Template tags & API functions |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
95 |
// |
0 | 96 |
|
97 |
/** |
|
98 |
* Register a widget |
|
99 |
* |
|
100 |
* Registers a WP_Widget widget |
|
101 |
* |
|
102 |
* @since 2.8.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
103 |
* @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
104 |
* instead of simply a `WP_Widget` subclass name. |
0 | 105 |
* |
106 |
* @see WP_Widget |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
107 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
108 |
* @global WP_Widget_Factory $wp_widget_factory |
0 | 109 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
110 |
* @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass. |
0 | 111 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
112 |
function register_widget( $widget ) { |
0 | 113 |
global $wp_widget_factory; |
114 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
115 |
$wp_widget_factory->register( $widget ); |
0 | 116 |
} |
117 |
||
118 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
119 |
* Unregisters a widget. |
0 | 120 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
121 |
* Unregisters a WP_Widget widget. Useful for un-registering default widgets. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
122 |
* Run within a function hooked to the {@see 'widgets_init'} action. |
0 | 123 |
* |
124 |
* @since 2.8.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
125 |
* @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
126 |
* instead of simply a `WP_Widget` subclass name. |
0 | 127 |
* |
128 |
* @see WP_Widget |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
129 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
130 |
* @global WP_Widget_Factory $wp_widget_factory |
0 | 131 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
132 |
* @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass. |
0 | 133 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
134 |
function unregister_widget( $widget ) { |
0 | 135 |
global $wp_widget_factory; |
136 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
137 |
$wp_widget_factory->unregister( $widget ); |
0 | 138 |
} |
139 |
||
140 |
/** |
|
141 |
* Creates multiple sidebars. |
|
142 |
* |
|
143 |
* If you wanted to quickly create multiple sidebars for a theme or internally. |
|
144 |
* This function will allow you to do so. If you don't pass the 'name' and/or |
|
5 | 145 |
* 'id' in `$args`, then they will be built for you. |
0 | 146 |
* |
147 |
* @since 2.2.0 |
|
148 |
* |
|
149 |
* @see register_sidebar() The second parameter is documented by register_sidebar() and is the same here. |
|
5 | 150 |
* |
9 | 151 |
* @global array $wp_registered_sidebars The new sidebars are stored in this array by sidebar ID. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
152 |
* |
5 | 153 |
* @param int $number Optional. Number of sidebars to create. Default 1. |
154 |
* @param array|string $args { |
|
155 |
* Optional. Array or string of arguments for building a sidebar. |
|
0 | 156 |
* |
5 | 157 |
* @type string $id The base string of the unique identifier for each sidebar. If provided, and multiple |
158 |
* sidebars are being defined, the id will have "-2" appended, and so on. |
|
159 |
* Default 'sidebar-' followed by the number the sidebar creation is currently at. |
|
160 |
* @type string $name The name or title for the sidebars displayed in the admin dashboard. If registering |
|
161 |
* more than one sidebar, include '%d' in the string as a placeholder for the uniquely |
|
162 |
* assigned number for each sidebar. |
|
163 |
* Default 'Sidebar' for the first sidebar, otherwise 'Sidebar %d'. |
|
164 |
* } |
|
0 | 165 |
*/ |
5 | 166 |
function register_sidebars( $number = 1, $args = array() ) { |
0 | 167 |
global $wp_registered_sidebars; |
168 |
$number = (int) $number; |
|
169 |
||
9 | 170 |
if ( is_string( $args ) ) { |
171 |
parse_str( $args, $args ); |
|
172 |
} |
|
0 | 173 |
|
174 |
for ( $i = 1; $i <= $number; $i++ ) { |
|
175 |
$_args = $args; |
|
176 |
||
9 | 177 |
if ( $number > 1 ) { |
178 |
$_args['name'] = isset( $args['name'] ) ? sprintf( $args['name'], $i ) : sprintf( __( 'Sidebar %d' ), $i ); |
|
179 |
} else { |
|
180 |
$_args['name'] = isset( $args['name'] ) ? $args['name'] : __( 'Sidebar' ); |
|
181 |
} |
|
0 | 182 |
|
183 |
// Custom specified ID's are suffixed if they exist already. |
|
184 |
// Automatically generated sidebar names need to be suffixed regardless starting at -0 |
|
9 | 185 |
if ( isset( $args['id'] ) ) { |
0 | 186 |
$_args['id'] = $args['id']; |
9 | 187 |
$n = 2; // Start at -2 for conflicting custom ID's |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
188 |
while ( is_registered_sidebar( $_args['id'] ) ) { |
0 | 189 |
$_args['id'] = $args['id'] . '-' . $n++; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
190 |
} |
0 | 191 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
192 |
$n = count( $wp_registered_sidebars ); |
0 | 193 |
do { |
194 |
$_args['id'] = 'sidebar-' . ++$n; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
195 |
} while ( is_registered_sidebar( $_args['id'] ) ); |
0 | 196 |
} |
9 | 197 |
register_sidebar( $_args ); |
0 | 198 |
} |
199 |
} |
|
200 |
||
201 |
/** |
|
202 |
* Builds the definition for a single sidebar and returns the ID. |
|
203 |
* |
|
204 |
* Accepts either a string or an array and then parses that against a set |
|
205 |
* of default arguments for the new sidebar. WordPress will automatically |
|
206 |
* generate a sidebar ID and name based on the current number of registered |
|
207 |
* sidebars if those arguments are not included. |
|
208 |
* |
|
209 |
* When allowing for automatic generation of the name and ID parameters, keep |
|
210 |
* in mind that the incrementor for your sidebar can change over time depending |
|
211 |
* on what other plugins and themes are installed. |
|
212 |
* |
|
213 |
* If theme support for 'widgets' has not yet been added when this function is |
|
214 |
* called, it will be automatically enabled through the use of add_theme_support() |
|
215 |
* |
|
5 | 216 |
* @since 2.2.0 |
0 | 217 |
* |
5 | 218 |
* @global array $wp_registered_sidebars Stores the new sidebar in this array by sidebar ID. |
0 | 219 |
* |
5 | 220 |
* @param array|string $args { |
221 |
* Optional. Array or string of arguments for the sidebar being registered. |
|
0 | 222 |
* |
5 | 223 |
* @type string $name The name or title of the sidebar displayed in the Widgets |
224 |
* interface. Default 'Sidebar $instance'. |
|
225 |
* @type string $id The unique identifier by which the sidebar will be called. |
|
226 |
* Default 'sidebar-$instance'. |
|
227 |
* @type string $description Description of the sidebar, displayed in the Widgets interface. |
|
228 |
* Default empty string. |
|
229 |
* @type string $class Extra CSS class to assign to the sidebar in the Widgets interface. |
|
230 |
* Default empty. |
|
231 |
* @type string $before_widget HTML content to prepend to each widget's HTML output when |
|
232 |
* assigned to this sidebar. Default is an opening list item element. |
|
233 |
* @type string $after_widget HTML content to append to each widget's HTML output when |
|
234 |
* assigned to this sidebar. Default is a closing list item element. |
|
235 |
* @type string $before_title HTML content to prepend to the sidebar title when displayed. |
|
236 |
* Default is an opening h2 element. |
|
237 |
* @type string $after_title HTML content to append to the sidebar title when displayed. |
|
238 |
* Default is a closing h2 element. |
|
239 |
* } |
|
0 | 240 |
* @return string Sidebar ID added to $wp_registered_sidebars global. |
241 |
*/ |
|
9 | 242 |
function register_sidebar( $args = array() ) { |
0 | 243 |
global $wp_registered_sidebars; |
244 |
||
9 | 245 |
$i = count( $wp_registered_sidebars ) + 1; |
0 | 246 |
|
5 | 247 |
$id_is_empty = empty( $args['id'] ); |
248 |
||
0 | 249 |
$defaults = array( |
9 | 250 |
'name' => sprintf( __( 'Sidebar %d' ), $i ), |
251 |
'id' => "sidebar-$i", |
|
252 |
'description' => '', |
|
253 |
'class' => '', |
|
0 | 254 |
'before_widget' => '<li id="%1$s" class="widget %2$s">', |
9 | 255 |
'after_widget' => "</li>\n", |
256 |
'before_title' => '<h2 class="widgettitle">', |
|
257 |
'after_title' => "</h2>\n", |
|
0 | 258 |
); |
259 |
||
260 |
$sidebar = wp_parse_args( $args, $defaults ); |
|
261 |
||
5 | 262 |
if ( $id_is_empty ) { |
263 |
/* translators: 1: the id argument, 2: sidebar name, 3: recommended id value */ |
|
264 |
_doing_it_wrong( __FUNCTION__, sprintf( __( 'No %1$s was set in the arguments array for the "%2$s" sidebar. Defaulting to "%3$s". Manually set the %1$s to "%3$s" to silence this notice and keep existing sidebar content.' ), '<code>id</code>', $sidebar['name'], $sidebar['id'] ), '4.2.0' ); |
|
265 |
} |
|
266 |
||
9 | 267 |
$wp_registered_sidebars[ $sidebar['id'] ] = $sidebar; |
0 | 268 |
|
9 | 269 |
add_theme_support( 'widgets' ); |
0 | 270 |
|
5 | 271 |
/** |
272 |
* Fires once a sidebar has been registered. |
|
273 |
* |
|
274 |
* @since 3.0.0 |
|
275 |
* |
|
276 |
* @param array $sidebar Parsed arguments for the registered sidebar. |
|
277 |
*/ |
|
0 | 278 |
do_action( 'register_sidebar', $sidebar ); |
279 |
||
280 |
return $sidebar['id']; |
|
281 |
} |
|
282 |
||
283 |
/** |
|
284 |
* Removes a sidebar from the list. |
|
285 |
* |
|
286 |
* @since 2.2.0 |
|
287 |
* |
|
9 | 288 |
* @global array $wp_registered_sidebars Removes the sidebar from this array by sidebar ID. |
0 | 289 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
290 |
* @param string|int $sidebar_id The ID of the sidebar when it was registered. |
0 | 291 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
292 |
function unregister_sidebar( $sidebar_id ) { |
0 | 293 |
global $wp_registered_sidebars; |
294 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
295 |
unset( $wp_registered_sidebars[ $sidebar_id ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
296 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
297 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
298 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
299 |
* Checks if a sidebar is registered. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
300 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
301 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
302 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
303 |
* @global array $wp_registered_sidebars Registered sidebars. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
304 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
305 |
* @param string|int $sidebar_id The ID of the sidebar when it was registered. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
306 |
* @return bool True if the sidebar is registered, false otherwise. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
307 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
308 |
function is_registered_sidebar( $sidebar_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
309 |
global $wp_registered_sidebars; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
310 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
311 |
return isset( $wp_registered_sidebars[ $sidebar_id ] ); |
0 | 312 |
} |
313 |
||
314 |
/** |
|
5 | 315 |
* Register an instance of a widget. |
0 | 316 |
* |
5 | 317 |
* The default widget option is 'classname' that can be overridden. |
0 | 318 |
* |
5 | 319 |
* The function can also be used to un-register widgets when `$output_callback` |
0 | 320 |
* parameter is an empty string. |
321 |
* |
|
322 |
* @since 2.2.0 |
|
323 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
324 |
* @global array $wp_registered_widgets Uses stored registered widgets. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
325 |
* @global array $wp_registered_widget_controls Stores the registered widget controls (options). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
326 |
* @global array $wp_registered_widget_updates |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
327 |
* @global array $_wp_deprecated_widgets_callbacks |
5 | 328 |
* |
329 |
* @param int|string $id Widget ID. |
|
330 |
* @param string $name Widget display title. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
331 |
* @param callable $output_callback Run when widget is called. |
5 | 332 |
* @param array $options { |
333 |
* Optional. An array of supplementary widget options for the instance. |
|
0 | 334 |
* |
5 | 335 |
* @type string $classname Class name for the widget's HTML container. Default is a shortened |
336 |
* version of the output callback name. |
|
337 |
* @type string $description Widget description for display in the widget administration |
|
338 |
* panel and/or theme. |
|
339 |
* } |
|
0 | 340 |
*/ |
5 | 341 |
function wp_register_sidebar_widget( $id, $name, $output_callback, $options = array() ) { |
0 | 342 |
global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_widget_updates, $_wp_deprecated_widgets_callbacks; |
343 |
||
9 | 344 |
$id = strtolower( $id ); |
0 | 345 |
|
9 | 346 |
if ( empty( $output_callback ) ) { |
347 |
unset( $wp_registered_widgets[ $id ] ); |
|
0 | 348 |
return; |
349 |
} |
|
350 |
||
9 | 351 |
$id_base = _get_widget_id_base( $id ); |
352 |
if ( in_array( $output_callback, $_wp_deprecated_widgets_callbacks, true ) && ! is_callable( $output_callback ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
353 |
unset( $wp_registered_widget_controls[ $id ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
354 |
unset( $wp_registered_widget_updates[ $id_base ] ); |
0 | 355 |
return; |
356 |
} |
|
357 |
||
9 | 358 |
$defaults = array( 'classname' => $output_callback ); |
359 |
$options = wp_parse_args( $options, $defaults ); |
|
360 |
$widget = array( |
|
361 |
'name' => $name, |
|
362 |
'id' => $id, |
|
0 | 363 |
'callback' => $output_callback, |
9 | 364 |
'params' => array_slice( func_get_args(), 4 ), |
0 | 365 |
); |
9 | 366 |
$widget = array_merge( $widget, $options ); |
0 | 367 |
|
9 | 368 |
if ( is_callable( $output_callback ) && ( ! isset( $wp_registered_widgets[ $id ] ) || did_action( 'widgets_init' ) ) ) { |
5 | 369 |
|
370 |
/** |
|
371 |
* Fires once for each registered widget. |
|
372 |
* |
|
373 |
* @since 3.0.0 |
|
374 |
* |
|
375 |
* @param array $widget An array of default widget arguments. |
|
376 |
*/ |
|
0 | 377 |
do_action( 'wp_register_sidebar_widget', $widget ); |
9 | 378 |
$wp_registered_widgets[ $id ] = $widget; |
0 | 379 |
} |
380 |
} |
|
381 |
||
382 |
/** |
|
383 |
* Retrieve description for widget. |
|
384 |
* |
|
385 |
* When registering widgets, the options can also include 'description' that |
|
386 |
* describes the widget for display on the widget administration panel or |
|
387 |
* in the theme. |
|
388 |
* |
|
389 |
* @since 2.5.0 |
|
390 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
391 |
* @global array $wp_registered_widgets |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
392 |
* |
0 | 393 |
* @param int|string $id Widget ID. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
394 |
* @return string|void Widget description, if available. |
0 | 395 |
*/ |
396 |
function wp_widget_description( $id ) { |
|
9 | 397 |
if ( ! is_scalar( $id ) ) { |
0 | 398 |
return; |
9 | 399 |
} |
0 | 400 |
|
401 |
global $wp_registered_widgets; |
|
402 |
||
9 | 403 |
if ( isset( $wp_registered_widgets[ $id ]['description'] ) ) { |
404 |
return esc_html( $wp_registered_widgets[ $id ]['description'] ); |
|
405 |
} |
|
0 | 406 |
} |
407 |
||
408 |
/** |
|
409 |
* Retrieve description for a sidebar. |
|
410 |
* |
|
411 |
* When registering sidebars a 'description' parameter can be included that |
|
412 |
* describes the sidebar for display on the widget administration panel. |
|
413 |
* |
|
414 |
* @since 2.9.0 |
|
415 |
* |
|
9 | 416 |
* @global array $wp_registered_sidebars Registered sidebars. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
417 |
* |
5 | 418 |
* @param string $id sidebar ID. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
419 |
* @return string|void Sidebar description, if available. |
0 | 420 |
*/ |
421 |
function wp_sidebar_description( $id ) { |
|
9 | 422 |
if ( ! is_scalar( $id ) ) { |
0 | 423 |
return; |
9 | 424 |
} |
0 | 425 |
|
426 |
global $wp_registered_sidebars; |
|
427 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
428 |
if ( isset( $wp_registered_sidebars[ $id ]['description'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
429 |
return wp_kses( $wp_registered_sidebars[ $id ]['description'], 'sidebar_description' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
430 |
} |
0 | 431 |
} |
432 |
||
433 |
/** |
|
434 |
* Remove widget from sidebar. |
|
435 |
* |
|
436 |
* @since 2.2.0 |
|
437 |
* |
|
438 |
* @param int|string $id Widget ID. |
|
439 |
*/ |
|
9 | 440 |
function wp_unregister_sidebar_widget( $id ) { |
5 | 441 |
|
442 |
/** |
|
443 |
* Fires just before a widget is removed from a sidebar. |
|
444 |
* |
|
445 |
* @since 3.0.0 |
|
446 |
* |
|
447 |
* @param int $id The widget ID. |
|
448 |
*/ |
|
0 | 449 |
do_action( 'wp_unregister_sidebar_widget', $id ); |
450 |
||
9 | 451 |
wp_register_sidebar_widget( $id, '', '' ); |
452 |
wp_unregister_widget_control( $id ); |
|
0 | 453 |
} |
454 |
||
455 |
/** |
|
456 |
* Registers widget control callback for customizing options. |
|
457 |
* |
|
458 |
* @since 2.2.0 |
|
459 |
* |
|
5 | 460 |
* @todo `$params` parameter? |
461 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
462 |
* @global array $wp_registered_widget_controls |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
463 |
* @global array $wp_registered_widget_updates |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
464 |
* @global array $wp_registered_widgets |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
465 |
* @global array $_wp_deprecated_widgets_callbacks |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
466 |
* |
5 | 467 |
* @param int|string $id Sidebar ID. |
468 |
* @param string $name Sidebar display name. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
469 |
* @param callable $control_callback Run when sidebar is displayed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
470 |
* @param array $options { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
471 |
* Optional. Array or string of control options. Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
472 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
473 |
* @type int $height Never used. Default 200. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
474 |
* @type int $width Width of the fully expanded control form (but try hard to use the default width). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
475 |
* Default 250. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
476 |
* @type int|string $id_base Required for multi-widgets, i.e widgets that allow multiple instances such as the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
477 |
* text widget. The widget id will end up looking like `{$id_base}-{$unique_number}`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
478 |
* } |
0 | 479 |
*/ |
5 | 480 |
function wp_register_widget_control( $id, $name, $control_callback, $options = array() ) { |
0 | 481 |
global $wp_registered_widget_controls, $wp_registered_widget_updates, $wp_registered_widgets, $_wp_deprecated_widgets_callbacks; |
482 |
||
9 | 483 |
$id = strtolower( $id ); |
484 |
$id_base = _get_widget_id_base( $id ); |
|
0 | 485 |
|
9 | 486 |
if ( empty( $control_callback ) ) { |
487 |
unset( $wp_registered_widget_controls[ $id ] ); |
|
488 |
unset( $wp_registered_widget_updates[ $id_base ] ); |
|
0 | 489 |
return; |
490 |
} |
|
491 |
||
9 | 492 |
if ( in_array( $control_callback, $_wp_deprecated_widgets_callbacks, true ) && ! is_callable( $control_callback ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
493 |
unset( $wp_registered_widgets[ $id ] ); |
0 | 494 |
return; |
495 |
} |
|
496 |
||
9 | 497 |
if ( isset( $wp_registered_widget_controls[ $id ] ) && ! did_action( 'widgets_init' ) ) { |
0 | 498 |
return; |
9 | 499 |
} |
0 | 500 |
|
9 | 501 |
$defaults = array( |
502 |
'width' => 250, |
|
503 |
'height' => 200, |
|
504 |
); // height is never used |
|
505 |
$options = wp_parse_args( $options, $defaults ); |
|
506 |
$options['width'] = (int) $options['width']; |
|
0 | 507 |
$options['height'] = (int) $options['height']; |
508 |
||
509 |
$widget = array( |
|
9 | 510 |
'name' => $name, |
511 |
'id' => $id, |
|
0 | 512 |
'callback' => $control_callback, |
9 | 513 |
'params' => array_slice( func_get_args(), 4 ), |
0 | 514 |
); |
9 | 515 |
$widget = array_merge( $widget, $options ); |
0 | 516 |
|
9 | 517 |
$wp_registered_widget_controls[ $id ] = $widget; |
0 | 518 |
|
9 | 519 |
if ( isset( $wp_registered_widget_updates[ $id_base ] ) ) { |
0 | 520 |
return; |
9 | 521 |
} |
0 | 522 |
|
9 | 523 |
if ( isset( $widget['params'][0]['number'] ) ) { |
0 | 524 |
$widget['params'][0]['number'] = -1; |
9 | 525 |
} |
0 | 526 |
|
9 | 527 |
unset( $widget['width'], $widget['height'], $widget['name'], $widget['id'] ); |
528 |
$wp_registered_widget_updates[ $id_base ] = $widget; |
|
0 | 529 |
} |
530 |
||
5 | 531 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
532 |
* Registers the update callback for a widget. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
533 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
534 |
* @since 2.8.0 |
5 | 535 |
* |
536 |
* @global array $wp_registered_widget_updates |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
537 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
538 |
* @param string $id_base The base ID of a widget created by extending WP_Widget. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
539 |
* @param callable $update_callback Update callback method for the widget. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
540 |
* @param array $options Optional. Widget control options. See wp_register_widget_control(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
541 |
* Default empty array. |
5 | 542 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
543 |
function _register_widget_update_callback( $id_base, $update_callback, $options = array() ) { |
0 | 544 |
global $wp_registered_widget_updates; |
545 |
||
9 | 546 |
if ( isset( $wp_registered_widget_updates[ $id_base ] ) ) { |
547 |
if ( empty( $update_callback ) ) { |
|
548 |
unset( $wp_registered_widget_updates[ $id_base ] ); |
|
549 |
} |
|
0 | 550 |
return; |
551 |
} |
|
552 |
||
553 |
$widget = array( |
|
554 |
'callback' => $update_callback, |
|
9 | 555 |
'params' => array_slice( func_get_args(), 3 ), |
0 | 556 |
); |
557 |
||
9 | 558 |
$widget = array_merge( $widget, $options ); |
559 |
$wp_registered_widget_updates[ $id_base ] = $widget; |
|
0 | 560 |
} |
561 |
||
5 | 562 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
563 |
* Registers the form callback for a widget. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
564 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
565 |
* @since 2.8.0 |
5 | 566 |
* |
567 |
* @global array $wp_registered_widget_controls |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
568 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
569 |
* @param int|string $id Widget ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
570 |
* @param string $name Name attribute for the widget. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
571 |
* @param callable $form_callback Form callback. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
572 |
* @param array $options Optional. Widget control options. See wp_register_widget_control(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
573 |
* Default empty array. |
5 | 574 |
*/ |
9 | 575 |
function _register_widget_form_callback( $id, $name, $form_callback, $options = array() ) { |
0 | 576 |
global $wp_registered_widget_controls; |
577 |
||
9 | 578 |
$id = strtolower( $id ); |
0 | 579 |
|
9 | 580 |
if ( empty( $form_callback ) ) { |
581 |
unset( $wp_registered_widget_controls[ $id ] ); |
|
582 |
return; |
|
583 |
} |
|
584 |
||
585 |
if ( isset( $wp_registered_widget_controls[ $id ] ) && ! did_action( 'widgets_init' ) ) { |
|
0 | 586 |
return; |
587 |
} |
|
588 |
||
9 | 589 |
$defaults = array( |
590 |
'width' => 250, |
|
591 |
'height' => 200, |
|
592 |
); |
|
593 |
$options = wp_parse_args( $options, $defaults ); |
|
594 |
$options['width'] = (int) $options['width']; |
|
0 | 595 |
$options['height'] = (int) $options['height']; |
596 |
||
597 |
$widget = array( |
|
9 | 598 |
'name' => $name, |
599 |
'id' => $id, |
|
0 | 600 |
'callback' => $form_callback, |
9 | 601 |
'params' => array_slice( func_get_args(), 4 ), |
0 | 602 |
); |
9 | 603 |
$widget = array_merge( $widget, $options ); |
0 | 604 |
|
9 | 605 |
$wp_registered_widget_controls[ $id ] = $widget; |
0 | 606 |
} |
607 |
||
608 |
/** |
|
609 |
* Remove control callback for widget. |
|
610 |
* |
|
611 |
* @since 2.2.0 |
|
612 |
* |
|
613 |
* @param int|string $id Widget ID. |
|
614 |
*/ |
|
9 | 615 |
function wp_unregister_widget_control( $id ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
616 |
wp_register_widget_control( $id, '', '' ); |
0 | 617 |
} |
618 |
||
619 |
/** |
|
620 |
* Display dynamic sidebar. |
|
621 |
* |
|
622 |
* By default this displays the default sidebar or 'sidebar-1'. If your theme specifies the 'id' or |
|
623 |
* 'name' parameter for its registered sidebars you can pass an id or name as the $index parameter. |
|
624 |
* Otherwise, you can pass in a numerical index to display the sidebar at that index. |
|
625 |
* |
|
626 |
* @since 2.2.0 |
|
627 |
* |
|
9 | 628 |
* @global array $wp_registered_sidebars Registered sidebars. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
629 |
* @global array $wp_registered_widgets |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
630 |
* |
0 | 631 |
* @param int|string $index Optional, default is 1. Index, name or ID of dynamic sidebar. |
632 |
* @return bool True, if widget sidebar was found and called. False if not found or not called. |
|
633 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
634 |
function dynamic_sidebar( $index = 1 ) { |
0 | 635 |
global $wp_registered_sidebars, $wp_registered_widgets; |
636 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
637 |
if ( is_int( $index ) ) { |
0 | 638 |
$index = "sidebar-$index"; |
639 |
} else { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
640 |
$index = sanitize_title( $index ); |
0 | 641 |
foreach ( (array) $wp_registered_sidebars as $key => $value ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
642 |
if ( sanitize_title( $value['name'] ) == $index ) { |
0 | 643 |
$index = $key; |
644 |
break; |
|
645 |
} |
|
646 |
} |
|
647 |
} |
|
648 |
||
649 |
$sidebars_widgets = wp_get_sidebars_widgets(); |
|
650 |
if ( empty( $wp_registered_sidebars[ $index ] ) || empty( $sidebars_widgets[ $index ] ) || ! is_array( $sidebars_widgets[ $index ] ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
651 |
/** This action is documented in wp-includes/widget.php */ |
5 | 652 |
do_action( 'dynamic_sidebar_before', $index, false ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
653 |
/** This action is documented in wp-includes/widget.php */ |
9 | 654 |
do_action( 'dynamic_sidebar_after', $index, false ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
655 |
/** This filter is documented in wp-includes/widget.php */ |
5 | 656 |
return apply_filters( 'dynamic_sidebar_has_widgets', false, $index ); |
0 | 657 |
} |
658 |
||
5 | 659 |
/** |
660 |
* Fires before widgets are rendered in a dynamic sidebar. |
|
661 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
662 |
* Note: The action also fires for empty sidebars, and on both the front end |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
663 |
* and back end, including the Inactive Widgets sidebar on the Widgets screen. |
5 | 664 |
* |
665 |
* @since 3.9.0 |
|
666 |
* |
|
667 |
* @param int|string $index Index, name, or ID of the dynamic sidebar. |
|
668 |
* @param bool $has_widgets Whether the sidebar is populated with widgets. |
|
669 |
* Default true. |
|
670 |
*/ |
|
671 |
do_action( 'dynamic_sidebar_before', $index, true ); |
|
9 | 672 |
$sidebar = $wp_registered_sidebars[ $index ]; |
0 | 673 |
|
674 |
$did_one = false; |
|
9 | 675 |
foreach ( (array) $sidebars_widgets[ $index ] as $id ) { |
0 | 676 |
|
9 | 677 |
if ( ! isset( $wp_registered_widgets[ $id ] ) ) { |
678 |
continue; |
|
679 |
} |
|
0 | 680 |
|
681 |
$params = array_merge( |
|
9 | 682 |
array( |
683 |
array_merge( |
|
684 |
$sidebar, |
|
685 |
array( |
|
686 |
'widget_id' => $id, |
|
687 |
'widget_name' => $wp_registered_widgets[ $id ]['name'], |
|
688 |
) |
|
689 |
), |
|
690 |
), |
|
691 |
(array) $wp_registered_widgets[ $id ]['params'] |
|
0 | 692 |
); |
693 |
||
694 |
// Substitute HTML id and class attributes into before_widget |
|
695 |
$classname_ = ''; |
|
9 | 696 |
foreach ( (array) $wp_registered_widgets[ $id ]['classname'] as $cn ) { |
697 |
if ( is_string( $cn ) ) { |
|
0 | 698 |
$classname_ .= '_' . $cn; |
9 | 699 |
} elseif ( is_object( $cn ) ) { |
700 |
$classname_ .= '_' . get_class( $cn ); |
|
701 |
} |
|
0 | 702 |
} |
9 | 703 |
$classname_ = ltrim( $classname_, '_' ); |
704 |
$params[0]['before_widget'] = sprintf( $params[0]['before_widget'], $id, $classname_ ); |
|
0 | 705 |
|
5 | 706 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
707 |
* Filters the parameters passed to a widget's display callback. |
5 | 708 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
709 |
* Note: The filter is evaluated on both the front end and back end, |
5 | 710 |
* including for the Inactive Widgets sidebar on the Widgets screen. |
711 |
* |
|
712 |
* @since 2.5.0 |
|
713 |
* |
|
714 |
* @see register_sidebar() |
|
715 |
* |
|
716 |
* @param array $params { |
|
717 |
* @type array $args { |
|
718 |
* An array of widget display arguments. |
|
719 |
* |
|
720 |
* @type string $name Name of the sidebar the widget is assigned to. |
|
721 |
* @type string $id ID of the sidebar the widget is assigned to. |
|
722 |
* @type string $description The sidebar description. |
|
723 |
* @type string $class CSS class applied to the sidebar container. |
|
724 |
* @type string $before_widget HTML markup to prepend to each widget in the sidebar. |
|
725 |
* @type string $after_widget HTML markup to append to each widget in the sidebar. |
|
726 |
* @type string $before_title HTML markup to prepend to the widget title when displayed. |
|
727 |
* @type string $after_title HTML markup to append to the widget title when displayed. |
|
728 |
* @type string $widget_id ID of the widget. |
|
729 |
* @type string $widget_name Name of the widget. |
|
730 |
* } |
|
731 |
* @type array $widget_args { |
|
732 |
* An array of multi-widget arguments. |
|
733 |
* |
|
734 |
* @type int $number Number increment used for multiples of the same widget. |
|
735 |
* } |
|
736 |
* } |
|
737 |
*/ |
|
0 | 738 |
$params = apply_filters( 'dynamic_sidebar_params', $params ); |
739 |
||
9 | 740 |
$callback = $wp_registered_widgets[ $id ]['callback']; |
0 | 741 |
|
5 | 742 |
/** |
743 |
* Fires before a widget's display callback is called. |
|
744 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
745 |
* Note: The action fires on both the front end and back end, including |
5 | 746 |
* for widgets in the Inactive Widgets sidebar on the Widgets screen. |
747 |
* |
|
748 |
* The action is not fired for empty sidebars. |
|
749 |
* |
|
750 |
* @since 3.0.0 |
|
751 |
* |
|
752 |
* @param array $widget_id { |
|
753 |
* An associative array of widget arguments. |
|
754 |
* |
|
755 |
* @type string $name Name of the widget. |
|
756 |
* @type string $id Widget ID. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
757 |
* @type array|callable $callback When the hook is fired on the front end, $callback is an array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
758 |
* containing the widget object. Fired on the back end, $callback |
5 | 759 |
* is 'wp_widget_control', see $_callback. |
760 |
* @type array $params An associative array of multi-widget arguments. |
|
761 |
* @type string $classname CSS class applied to the widget container. |
|
762 |
* @type string $description The widget description. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
763 |
* @type array $_callback When the hook is fired on the back end, $_callback is populated |
5 | 764 |
* with an array containing the widget object, see $callback. |
765 |
* } |
|
766 |
*/ |
|
767 |
do_action( 'dynamic_sidebar', $wp_registered_widgets[ $id ] ); |
|
0 | 768 |
|
9 | 769 |
if ( is_callable( $callback ) ) { |
770 |
call_user_func_array( $callback, $params ); |
|
0 | 771 |
$did_one = true; |
772 |
} |
|
773 |
} |
|
774 |
||
5 | 775 |
/** |
776 |
* Fires after widgets are rendered in a dynamic sidebar. |
|
777 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
778 |
* Note: The action also fires for empty sidebars, and on both the front end |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
779 |
* and back end, including the Inactive Widgets sidebar on the Widgets screen. |
5 | 780 |
* |
781 |
* @since 3.9.0 |
|
782 |
* |
|
783 |
* @param int|string $index Index, name, or ID of the dynamic sidebar. |
|
784 |
* @param bool $has_widgets Whether the sidebar is populated with widgets. |
|
785 |
* Default true. |
|
786 |
*/ |
|
787 |
do_action( 'dynamic_sidebar_after', $index, true ); |
|
788 |
||
789 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
790 |
* Filters whether a sidebar has widgets. |
5 | 791 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
792 |
* Note: The filter is also evaluated for empty sidebars, and on both the front end |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
793 |
* and back end, including the Inactive Widgets sidebar on the Widgets screen. |
5 | 794 |
* |
795 |
* @since 3.9.0 |
|
796 |
* |
|
797 |
* @param bool $did_one Whether at least one widget was rendered in the sidebar. |
|
798 |
* Default false. |
|
799 |
* @param int|string $index Index, name, or ID of the dynamic sidebar. |
|
800 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
801 |
return apply_filters( 'dynamic_sidebar_has_widgets', $did_one, $index ); |
0 | 802 |
} |
803 |
||
804 |
/** |
|
9 | 805 |
* Determines whether a given widget is displayed on the front end. |
0 | 806 |
* |
807 |
* Either $callback or $id_base can be used |
|
808 |
* $id_base is the first argument when extending WP_Widget class |
|
809 |
* Without the optional $widget_id parameter, returns the ID of the first sidebar |
|
810 |
* in which the first instance of the widget with the given callback or $id_base is found. |
|
811 |
* With the $widget_id parameter, returns the ID of the sidebar where |
|
812 |
* the widget with that callback/$id_base AND that ID is found. |
|
813 |
* |
|
814 |
* NOTE: $widget_id and $id_base are the same for single widgets. To be effective |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
815 |
* this function has to run after widgets have initialized, at action {@see 'init'} or later. |
0 | 816 |
* |
9 | 817 |
* For more information on this and similar theme functions, check out |
818 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
819 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
820 |
* |
|
0 | 821 |
* @since 2.2.0 |
822 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
823 |
* @global array $wp_registered_widgets |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
824 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
825 |
* @param string|false $callback Optional, Widget callback to check. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
826 |
* @param int|false $widget_id Optional. Widget ID. Optional, but needed for checking. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
827 |
* @param string|false $id_base Optional. The base ID of a widget created by extending WP_Widget. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
828 |
* @param bool $skip_inactive Optional. Whether to check in 'wp_inactive_widgets'. Default true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
829 |
* @return string|false False if widget is not active or id of sidebar in which the widget is active. |
0 | 830 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
831 |
function is_active_widget( $callback = false, $widget_id = false, $id_base = false, $skip_inactive = true ) { |
0 | 832 |
global $wp_registered_widgets; |
833 |
||
834 |
$sidebars_widgets = wp_get_sidebars_widgets(); |
|
835 |
||
9 | 836 |
if ( is_array( $sidebars_widgets ) ) { |
0 | 837 |
foreach ( $sidebars_widgets as $sidebar => $widgets ) { |
5 | 838 |
if ( $skip_inactive && ( 'wp_inactive_widgets' === $sidebar || 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) ) { |
0 | 839 |
continue; |
5 | 840 |
} |
0 | 841 |
|
9 | 842 |
if ( is_array( $widgets ) ) { |
0 | 843 |
foreach ( $widgets as $widget ) { |
9 | 844 |
if ( ( $callback && isset( $wp_registered_widgets[ $widget ]['callback'] ) && $wp_registered_widgets[ $widget ]['callback'] == $callback ) || ( $id_base && _get_widget_id_base( $widget ) == $id_base ) ) { |
845 |
if ( ! $widget_id || $widget_id == $wp_registered_widgets[ $widget ]['id'] ) { |
|
0 | 846 |
return $sidebar; |
9 | 847 |
} |
0 | 848 |
} |
849 |
} |
|
850 |
} |
|
851 |
} |
|
852 |
} |
|
853 |
return false; |
|
854 |
} |
|
855 |
||
856 |
/** |
|
9 | 857 |
* Determines whether the dynamic sidebar is enabled and used by the theme. |
858 |
* |
|
859 |
* For more information on this and similar theme functions, check out |
|
860 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
861 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
0 | 862 |
* |
863 |
* @since 2.2.0 |
|
864 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
865 |
* @global array $wp_registered_widgets |
9 | 866 |
* @global array $wp_registered_sidebars Registered sidebars. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
867 |
* |
0 | 868 |
* @return bool True, if using widgets. False, if not using widgets. |
869 |
*/ |
|
870 |
function is_dynamic_sidebar() { |
|
871 |
global $wp_registered_widgets, $wp_registered_sidebars; |
|
9 | 872 |
$sidebars_widgets = get_option( 'sidebars_widgets' ); |
0 | 873 |
foreach ( (array) $wp_registered_sidebars as $index => $sidebar ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
874 |
if ( ! empty( $sidebars_widgets[ $index ] ) ) { |
9 | 875 |
foreach ( (array) $sidebars_widgets[ $index ] as $widget ) { |
876 |
if ( array_key_exists( $widget, $wp_registered_widgets ) ) { |
|
0 | 877 |
return true; |
9 | 878 |
} |
879 |
} |
|
0 | 880 |
} |
881 |
} |
|
882 |
return false; |
|
883 |
} |
|
884 |
||
885 |
/** |
|
9 | 886 |
* Determines whether a sidebar is in use. |
887 |
* |
|
888 |
* For more information on this and similar theme functions, check out |
|
889 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
890 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
0 | 891 |
* |
5 | 892 |
* @since 2.8.0 |
0 | 893 |
* |
5 | 894 |
* @param string|int $index Sidebar name, id or number to check. |
0 | 895 |
* @return bool true if the sidebar is in use, false otherwise. |
896 |
*/ |
|
897 |
function is_active_sidebar( $index ) { |
|
9 | 898 |
$index = ( is_int( $index ) ) ? "sidebar-$index" : sanitize_title( $index ); |
899 |
$sidebars_widgets = wp_get_sidebars_widgets(); |
|
900 |
$is_active_sidebar = ! empty( $sidebars_widgets[ $index ] ); |
|
5 | 901 |
|
902 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
903 |
* Filters whether a dynamic sidebar is considered "active". |
5 | 904 |
* |
905 |
* @since 3.9.0 |
|
906 |
* |
|
907 |
* @param bool $is_active_sidebar Whether or not the sidebar should be considered "active". |
|
908 |
* In other words, whether the sidebar contains any widgets. |
|
909 |
* @param int|string $index Index, name, or ID of the dynamic sidebar. |
|
910 |
*/ |
|
911 |
return apply_filters( 'is_active_sidebar', $is_active_sidebar, $index ); |
|
0 | 912 |
} |
913 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
914 |
// |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
915 |
// Internal Functions |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
916 |
// |
0 | 917 |
|
918 |
/** |
|
5 | 919 |
* Retrieve full list of sidebars and their widget instance IDs. |
0 | 920 |
* |
921 |
* Will upgrade sidebar widget list, if needed. Will also save updated list, if |
|
922 |
* needed. |
|
923 |
* |
|
924 |
* @since 2.2.0 |
|
925 |
* @access private |
|
926 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
927 |
* @global array $_wp_sidebars_widgets |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
928 |
* @global array $sidebars_widgets |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
929 |
* |
5 | 930 |
* @param bool $deprecated Not used (argument deprecated). |
0 | 931 |
* @return array Upgraded list of widgets to version 3 array format when called from the admin. |
932 |
*/ |
|
5 | 933 |
function wp_get_sidebars_widgets( $deprecated = true ) { |
9 | 934 |
if ( $deprecated !== true ) { |
0 | 935 |
_deprecated_argument( __FUNCTION__, '2.8.1' ); |
9 | 936 |
} |
0 | 937 |
|
938 |
global $_wp_sidebars_widgets, $sidebars_widgets; |
|
939 |
||
940 |
// If loading from front page, consult $_wp_sidebars_widgets rather than options |
|
941 |
// to see if wp_convert_widget_settings() has made manipulations in memory. |
|
9 | 942 |
if ( ! is_admin() ) { |
943 |
if ( empty( $_wp_sidebars_widgets ) ) { |
|
944 |
$_wp_sidebars_widgets = get_option( 'sidebars_widgets', array() ); |
|
945 |
} |
|
0 | 946 |
|
947 |
$sidebars_widgets = $_wp_sidebars_widgets; |
|
948 |
} else { |
|
9 | 949 |
$sidebars_widgets = get_option( 'sidebars_widgets', array() ); |
0 | 950 |
} |
951 |
||
9 | 952 |
if ( is_array( $sidebars_widgets ) && isset( $sidebars_widgets['array_version'] ) ) { |
953 |
unset( $sidebars_widgets['array_version'] ); |
|
954 |
} |
|
0 | 955 |
|
5 | 956 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
957 |
* Filters the list of sidebars and their widgets. |
5 | 958 |
* |
959 |
* @since 2.7.0 |
|
960 |
* |
|
961 |
* @param array $sidebars_widgets An associative array of sidebars and their widgets. |
|
962 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
963 |
return apply_filters( 'sidebars_widgets', $sidebars_widgets ); |
0 | 964 |
} |
965 |
||
966 |
/** |
|
967 |
* Set the sidebar widget option to update sidebars. |
|
968 |
* |
|
969 |
* @since 2.2.0 |
|
970 |
* @access private |
|
971 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
972 |
* @global array $_wp_sidebars_widgets |
0 | 973 |
* @param array $sidebars_widgets Sidebar widgets and their settings. |
974 |
*/ |
|
975 |
function wp_set_sidebars_widgets( $sidebars_widgets ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
976 |
global $_wp_sidebars_widgets; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
977 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
978 |
// Clear cached value used in wp_get_sidebars_widgets(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
979 |
$_wp_sidebars_widgets = null; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
980 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
981 |
if ( ! isset( $sidebars_widgets['array_version'] ) ) { |
0 | 982 |
$sidebars_widgets['array_version'] = 3; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
983 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
984 |
|
0 | 985 |
update_option( 'sidebars_widgets', $sidebars_widgets ); |
986 |
} |
|
987 |
||
988 |
/** |
|
989 |
* Retrieve default registered sidebars list. |
|
990 |
* |
|
991 |
* @since 2.2.0 |
|
992 |
* @access private |
|
993 |
* |
|
9 | 994 |
* @global array $wp_registered_sidebars Registered sidebars. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
995 |
* |
0 | 996 |
* @return array |
997 |
*/ |
|
998 |
function wp_get_widget_defaults() { |
|
999 |
global $wp_registered_sidebars; |
|
1000 |
||
1001 |
$defaults = array(); |
|
1002 |
||
9 | 1003 |
foreach ( (array) $wp_registered_sidebars as $index => $sidebar ) { |
1004 |
$defaults[ $index ] = array(); |
|
1005 |
} |
|
0 | 1006 |
|
1007 |
return $defaults; |
|
1008 |
} |
|
1009 |
||
1010 |
/** |
|
1011 |
* Convert the widget settings from single to multi-widget format. |
|
1012 |
* |
|
1013 |
* @since 2.8.0 |
|
1014 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1015 |
* @global array $_wp_sidebars_widgets |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1016 |
* |
5 | 1017 |
* @param string $base_name |
1018 |
* @param string $option_name |
|
1019 |
* @param array $settings |
|
0 | 1020 |
* @return array |
1021 |
*/ |
|
9 | 1022 |
function wp_convert_widget_settings( $base_name, $option_name, $settings ) { |
0 | 1023 |
// This test may need expanding. |
1024 |
$single = $changed = false; |
|
9 | 1025 |
if ( empty( $settings ) ) { |
0 | 1026 |
$single = true; |
1027 |
} else { |
|
9 | 1028 |
foreach ( array_keys( $settings ) as $number ) { |
1029 |
if ( 'number' == $number ) { |
|
0 | 1030 |
continue; |
9 | 1031 |
} |
1032 |
if ( ! is_numeric( $number ) ) { |
|
0 | 1033 |
$single = true; |
1034 |
break; |
|
1035 |
} |
|
1036 |
} |
|
1037 |
} |
|
1038 |
||
1039 |
if ( $single ) { |
|
1040 |
$settings = array( 2 => $settings ); |
|
1041 |
||
1042 |
// If loading from the front page, update sidebar in memory but don't save to options |
|
1043 |
if ( is_admin() ) { |
|
9 | 1044 |
$sidebars_widgets = get_option( 'sidebars_widgets' ); |
0 | 1045 |
} else { |
9 | 1046 |
if ( empty( $GLOBALS['_wp_sidebars_widgets'] ) ) { |
1047 |
$GLOBALS['_wp_sidebars_widgets'] = get_option( 'sidebars_widgets', array() ); |
|
1048 |
} |
|
0 | 1049 |
$sidebars_widgets = &$GLOBALS['_wp_sidebars_widgets']; |
1050 |
} |
|
1051 |
||
1052 |
foreach ( (array) $sidebars_widgets as $index => $sidebar ) { |
|
9 | 1053 |
if ( is_array( $sidebar ) ) { |
0 | 1054 |
foreach ( $sidebar as $i => $name ) { |
1055 |
if ( $base_name == $name ) { |
|
9 | 1056 |
$sidebars_widgets[ $index ][ $i ] = "$name-2"; |
1057 |
$changed = true; |
|
0 | 1058 |
break 2; |
1059 |
} |
|
1060 |
} |
|
1061 |
} |
|
1062 |
} |
|
1063 |
||
9 | 1064 |
if ( is_admin() && $changed ) { |
1065 |
update_option( 'sidebars_widgets', $sidebars_widgets ); |
|
1066 |
} |
|
0 | 1067 |
} |
1068 |
||
1069 |
$settings['_multiwidget'] = 1; |
|
9 | 1070 |
if ( is_admin() ) { |
0 | 1071 |
update_option( $option_name, $settings ); |
9 | 1072 |
} |
0 | 1073 |
|
1074 |
return $settings; |
|
1075 |
} |
|
1076 |
||
1077 |
/** |
|
5 | 1078 |
* Output an arbitrary widget as a template tag. |
1079 |
* |
|
1080 |
* @since 2.8.0 |
|
0 | 1081 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1082 |
* @global WP_Widget_Factory $wp_widget_factory |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1083 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1084 |
* @param string $widget The widget's PHP class name (see class-wp-widget.php). |
5 | 1085 |
* @param array $instance Optional. The widget's instance settings. Default empty array. |
1086 |
* @param array $args { |
|
1087 |
* Optional. Array of arguments to configure the display of the widget. |
|
0 | 1088 |
* |
5 | 1089 |
* @type string $before_widget HTML content that will be prepended to the widget's HTML output. |
1090 |
* Default `<div class="widget %s">`, where `%s` is the widget's class name. |
|
1091 |
* @type string $after_widget HTML content that will be appended to the widget's HTML output. |
|
1092 |
* Default `</div>`. |
|
1093 |
* @type string $before_title HTML content that will be prepended to the widget's title when displayed. |
|
1094 |
* Default `<h2 class="widgettitle">`. |
|
1095 |
* @type string $after_title HTML content that will be appended to the widget's title when displayed. |
|
1096 |
* Default `</h2>`. |
|
1097 |
* } |
|
1098 |
*/ |
|
1099 |
function the_widget( $widget, $instance = array(), $args = array() ) { |
|
0 | 1100 |
global $wp_widget_factory; |
1101 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1102 |
if ( ! isset( $wp_widget_factory->widgets[ $widget ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1103 |
/* translators: %s: register_widget() */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1104 |
_doing_it_wrong( __FUNCTION__, sprintf( __( 'Widgets need to be registered using %s, before they can be displayed.' ), '<code>register_widget()</code>' ), '4.9.0' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1105 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1106 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1107 |
|
9 | 1108 |
$widget_obj = $wp_widget_factory->widgets[ $widget ]; |
5 | 1109 |
if ( ! ( $widget_obj instanceof WP_Widget ) ) { |
0 | 1110 |
return; |
5 | 1111 |
} |
0 | 1112 |
|
9 | 1113 |
$default_args = array( |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1114 |
'before_widget' => '<div class="widget %s">', |
9 | 1115 |
'after_widget' => '</div>', |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1116 |
'before_title' => '<h2 class="widgettitle">', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1117 |
'after_title' => '</h2>', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1118 |
); |
9 | 1119 |
$args = wp_parse_args( $args, $default_args ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1120 |
$args['before_widget'] = sprintf( $args['before_widget'], $widget_obj->widget_options['classname'] ); |
0 | 1121 |
|
9 | 1122 |
$instance = wp_parse_args( $instance ); |
0 | 1123 |
|
5 | 1124 |
/** |
1125 |
* Fires before rendering the requested widget. |
|
1126 |
* |
|
1127 |
* @since 3.0.0 |
|
1128 |
* |
|
1129 |
* @param string $widget The widget's class name. |
|
1130 |
* @param array $instance The current widget instance's settings. |
|
1131 |
* @param array $args An array of the widget's sidebar arguments. |
|
1132 |
*/ |
|
0 | 1133 |
do_action( 'the_widget', $widget, $instance, $args ); |
1134 |
||
9 | 1135 |
$widget_obj->_set( -1 ); |
1136 |
$widget_obj->widget( $args, $instance ); |
|
0 | 1137 |
} |
1138 |
||
1139 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1140 |
* Retrieves the widget ID base value. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1141 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1142 |
* @since 2.8.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1143 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1144 |
* @param string $id Widget ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1145 |
* @return string Widget ID base. |
0 | 1146 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1147 |
function _get_widget_id_base( $id ) { |
0 | 1148 |
return preg_replace( '/-[0-9]+$/', '', $id ); |
1149 |
} |
|
1150 |
||
1151 |
/** |
|
1152 |
* Handle sidebars config after theme change |
|
1153 |
* |
|
1154 |
* @access private |
|
1155 |
* @since 3.3.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1156 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1157 |
* @global array $sidebars_widgets |
0 | 1158 |
*/ |
1159 |
function _wp_sidebars_changed() { |
|
1160 |
global $sidebars_widgets; |
|
1161 |
||
9 | 1162 |
if ( ! is_array( $sidebars_widgets ) ) { |
0 | 1163 |
$sidebars_widgets = wp_get_sidebars_widgets(); |
9 | 1164 |
} |
0 | 1165 |
|
9 | 1166 |
retrieve_widgets( true ); |
0 | 1167 |
} |
1168 |
||
5 | 1169 |
/** |
1170 |
* Look for "lost" widgets, this has to run at least on each theme change. |
|
1171 |
* |
|
1172 |
* @since 2.8.0 |
|
1173 |
* |
|
9 | 1174 |
* @global array $wp_registered_sidebars Registered sidebars. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1175 |
* @global array $sidebars_widgets |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1176 |
* @global array $wp_registered_widgets |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1177 |
* |
5 | 1178 |
* @param string|bool $theme_changed Whether the theme was changed as a boolean. A value |
1179 |
* of 'customize' defers updates for the Customizer. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1180 |
* @return array Updated sidebars widgets. |
5 | 1181 |
*/ |
1182 |
function retrieve_widgets( $theme_changed = false ) { |
|
0 | 1183 |
global $wp_registered_sidebars, $sidebars_widgets, $wp_registered_widgets; |
1184 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1185 |
$registered_sidebars_keys = array_keys( $wp_registered_sidebars ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1186 |
$registered_widgets_ids = array_keys( $wp_registered_widgets ); |
0 | 1187 |
|
9 | 1188 |
if ( ! is_array( get_theme_mod( 'sidebars_widgets' ) ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1189 |
if ( empty( $sidebars_widgets ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1190 |
return array(); |
0 | 1191 |
} |
1192 |
||
1193 |
unset( $sidebars_widgets['array_version'] ); |
|
1194 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1195 |
$sidebars_widgets_keys = array_keys( $sidebars_widgets ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1196 |
sort( $sidebars_widgets_keys ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1197 |
sort( $registered_sidebars_keys ); |
0 | 1198 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1199 |
if ( $sidebars_widgets_keys === $registered_sidebars_keys ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1200 |
$sidebars_widgets = _wp_remove_unregistered_widgets( $sidebars_widgets, $registered_widgets_ids ); |
0 | 1201 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1202 |
return $sidebars_widgets; |
0 | 1203 |
} |
1204 |
} |
|
1205 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1206 |
// Discard invalid, theme-specific widgets from sidebars. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1207 |
$sidebars_widgets = _wp_remove_unregistered_widgets( $sidebars_widgets, $registered_widgets_ids ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1208 |
$sidebars_widgets = wp_map_sidebars_widgets( $sidebars_widgets ); |
0 | 1209 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1210 |
// Find hidden/lost multi-widget instances. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1211 |
$shown_widgets = call_user_func_array( 'array_merge', $sidebars_widgets ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1212 |
$lost_widgets = array_diff( $registered_widgets_ids, $shown_widgets ); |
0 | 1213 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1214 |
foreach ( $lost_widgets as $key => $widget_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1215 |
$number = preg_replace( '/.+?-([0-9]+)$/', '$1', $widget_id ); |
0 | 1216 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1217 |
// Only keep active and default widgets. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1218 |
if ( is_numeric( $number ) && (int) $number < 2 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1219 |
unset( $lost_widgets[ $key ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1220 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1221 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1222 |
$sidebars_widgets['wp_inactive_widgets'] = array_merge( $lost_widgets, (array) $sidebars_widgets['wp_inactive_widgets'] ); |
0 | 1223 |
|
5 | 1224 |
if ( 'customize' !== $theme_changed ) { |
1225 |
wp_set_sidebars_widgets( $sidebars_widgets ); |
|
1226 |
} |
|
0 | 1227 |
|
1228 |
return $sidebars_widgets; |
|
1229 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1230 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1231 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1232 |
* Compares a list of sidebars with their widgets against a whitelist. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1233 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1234 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1235 |
* @since 4.9.2 Always tries to restore widget assignments from previous data, not just if sidebars needed mapping. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1236 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1237 |
* @param array $existing_sidebars_widgets List of sidebars and their widget instance IDs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1238 |
* @return array Mapped sidebars widgets. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1239 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1240 |
function wp_map_sidebars_widgets( $existing_sidebars_widgets ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1241 |
global $wp_registered_sidebars; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1242 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1243 |
$new_sidebars_widgets = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1244 |
'wp_inactive_widgets' => array(), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1245 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1246 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1247 |
// Short-circuit if there are no sidebars to map. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1248 |
if ( ! is_array( $existing_sidebars_widgets ) || empty( $existing_sidebars_widgets ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1249 |
return $new_sidebars_widgets; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1250 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1251 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1252 |
foreach ( $existing_sidebars_widgets as $sidebar => $widgets ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1253 |
if ( 'wp_inactive_widgets' === $sidebar || 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1254 |
$new_sidebars_widgets['wp_inactive_widgets'] = array_merge( $new_sidebars_widgets['wp_inactive_widgets'], (array) $widgets ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1255 |
unset( $existing_sidebars_widgets[ $sidebar ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1256 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1257 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1258 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1259 |
// If old and new theme have just one sidebar, map it and we're done. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1260 |
if ( 1 === count( $existing_sidebars_widgets ) && 1 === count( $wp_registered_sidebars ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1261 |
$new_sidebars_widgets[ key( $wp_registered_sidebars ) ] = array_pop( $existing_sidebars_widgets ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1262 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1263 |
return $new_sidebars_widgets; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1264 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1265 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1266 |
// Map locations with the same slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1267 |
$existing_sidebars = array_keys( $existing_sidebars_widgets ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1268 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1269 |
foreach ( $wp_registered_sidebars as $sidebar => $name ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1270 |
if ( in_array( $sidebar, $existing_sidebars, true ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1271 |
$new_sidebars_widgets[ $sidebar ] = $existing_sidebars_widgets[ $sidebar ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1272 |
unset( $existing_sidebars_widgets[ $sidebar ] ); |
9 | 1273 |
} elseif ( ! array_key_exists( $sidebar, $new_sidebars_widgets ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1274 |
$new_sidebars_widgets[ $sidebar ] = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1275 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1276 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1277 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1278 |
// If there are more sidebars, try to map them. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1279 |
if ( ! empty( $existing_sidebars_widgets ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1280 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1281 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1282 |
* If old and new theme both have sidebars that contain phrases |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1283 |
* from within the same group, make an educated guess and map it. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1284 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1285 |
$common_slug_groups = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1286 |
array( 'sidebar', 'primary', 'main', 'right' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1287 |
array( 'second', 'left' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1288 |
array( 'sidebar-2', 'footer', 'bottom' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1289 |
array( 'header', 'top' ), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1290 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1291 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1292 |
// Go through each group... |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1293 |
foreach ( $common_slug_groups as $slug_group ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1294 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1295 |
// ...and see if any of these slugs... |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1296 |
foreach ( $slug_group as $slug ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1297 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1298 |
// ...and any of the new sidebars... |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1299 |
foreach ( $wp_registered_sidebars as $new_sidebar => $args ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1300 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1301 |
// ...actually match! |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1302 |
if ( false === stripos( $new_sidebar, $slug ) && false === stripos( $slug, $new_sidebar ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1303 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1304 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1305 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1306 |
// Then see if any of the existing sidebars... |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1307 |
foreach ( $existing_sidebars_widgets as $sidebar => $widgets ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1308 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1309 |
// ...and any slug in the same group... |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1310 |
foreach ( $slug_group as $slug ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1311 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1312 |
// ... have a match as well. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1313 |
if ( false === stripos( $sidebar, $slug ) && false === stripos( $slug, $sidebar ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1314 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1315 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1316 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1317 |
// Make sure this sidebar wasn't mapped and removed previously. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1318 |
if ( ! empty( $existing_sidebars_widgets[ $sidebar ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1319 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1320 |
// We have a match that can be mapped! |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1321 |
$new_sidebars_widgets[ $new_sidebar ] = array_merge( $new_sidebars_widgets[ $new_sidebar ], $existing_sidebars_widgets[ $sidebar ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1322 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1323 |
// Remove the mapped sidebar so it can't be mapped again. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1324 |
unset( $existing_sidebars_widgets[ $sidebar ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1325 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1326 |
// Go back and check the next new sidebar. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1327 |
continue 3; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1328 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1329 |
} // endforeach ( $slug_group as $slug ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1330 |
} // endforeach ( $existing_sidebars_widgets as $sidebar => $widgets ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1331 |
} // endforeach foreach ( $wp_registered_sidebars as $new_sidebar => $args ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1332 |
} // endforeach ( $slug_group as $slug ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1333 |
} // endforeach ( $common_slug_groups as $slug_group ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1334 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1335 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1336 |
// Move any left over widgets to inactive sidebar. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1337 |
foreach ( $existing_sidebars_widgets as $widgets ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1338 |
if ( is_array( $widgets ) && ! empty( $widgets ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1339 |
$new_sidebars_widgets['wp_inactive_widgets'] = array_merge( $new_sidebars_widgets['wp_inactive_widgets'], $widgets ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1340 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1341 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1342 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1343 |
// Sidebars_widgets settings from when this theme was previously active. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1344 |
$old_sidebars_widgets = get_theme_mod( 'sidebars_widgets' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1345 |
$old_sidebars_widgets = isset( $old_sidebars_widgets['data'] ) ? $old_sidebars_widgets['data'] : false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1346 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1347 |
if ( is_array( $old_sidebars_widgets ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1348 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1349 |
// Remove empty sidebars, no need to map those. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1350 |
$old_sidebars_widgets = array_filter( $old_sidebars_widgets ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1351 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1352 |
// Only check sidebars that are empty or have not been mapped to yet. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1353 |
foreach ( $new_sidebars_widgets as $new_sidebar => $new_widgets ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1354 |
if ( array_key_exists( $new_sidebar, $old_sidebars_widgets ) && ! empty( $new_widgets ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1355 |
unset( $old_sidebars_widgets[ $new_sidebar ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1356 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1357 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1358 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1359 |
// Remove orphaned widgets, we're only interested in previously active sidebars. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1360 |
foreach ( $old_sidebars_widgets as $sidebar => $widgets ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1361 |
if ( 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1362 |
unset( $old_sidebars_widgets[ $sidebar ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1363 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1364 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1365 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1366 |
$old_sidebars_widgets = _wp_remove_unregistered_widgets( $old_sidebars_widgets ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1367 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1368 |
if ( ! empty( $old_sidebars_widgets ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1369 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1370 |
// Go through each remaining sidebar... |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1371 |
foreach ( $old_sidebars_widgets as $old_sidebar => $old_widgets ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1372 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1373 |
// ...and check every new sidebar... |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1374 |
foreach ( $new_sidebars_widgets as $new_sidebar => $new_widgets ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1375 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1376 |
// ...for every widget we're trying to revive. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1377 |
foreach ( $old_widgets as $key => $widget_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1378 |
$active_key = array_search( $widget_id, $new_widgets, true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1379 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1380 |
// If the widget is used elsewhere... |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1381 |
if ( false !== $active_key ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1382 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1383 |
// ...and that elsewhere is inactive widgets... |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1384 |
if ( 'wp_inactive_widgets' === $new_sidebar ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1385 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1386 |
// ...remove it from there and keep the active version... |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1387 |
unset( $new_sidebars_widgets['wp_inactive_widgets'][ $active_key ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1388 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1389 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1390 |
// ...otherwise remove it from the old sidebar and keep it in the new one. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1391 |
unset( $old_sidebars_widgets[ $old_sidebar ][ $key ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1392 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1393 |
} // endif ( $active_key ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1394 |
} // endforeach ( $old_widgets as $key => $widget_id ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1395 |
} // endforeach ( $new_sidebars_widgets as $new_sidebar => $new_widgets ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1396 |
} // endforeach ( $old_sidebars_widgets as $old_sidebar => $old_widgets ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1397 |
} // endif ( ! empty( $old_sidebars_widgets ) ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1398 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1399 |
// Restore widget settings from when theme was previously active. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1400 |
$new_sidebars_widgets = array_merge( $new_sidebars_widgets, $old_sidebars_widgets ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1401 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1402 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1403 |
return $new_sidebars_widgets; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1404 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1405 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1406 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1407 |
* Compares a list of sidebars with their widgets against a whitelist. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1408 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1409 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1410 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1411 |
* @param array $sidebars_widgets List of sidebars and their widget instance IDs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1412 |
* @param array $whitelist Optional. List of widget IDs to compare against. Default: Registered widgets. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1413 |
* @return array Sidebars with whitelisted widgets. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1414 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1415 |
function _wp_remove_unregistered_widgets( $sidebars_widgets, $whitelist = array() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1416 |
if ( empty( $whitelist ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1417 |
$whitelist = array_keys( $GLOBALS['wp_registered_widgets'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1418 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1419 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1420 |
foreach ( $sidebars_widgets as $sidebar => $widgets ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1421 |
if ( is_array( $widgets ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1422 |
$sidebars_widgets[ $sidebar ] = array_intersect( $widgets, $whitelist ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1423 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1424 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1425 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1426 |
return $sidebars_widgets; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1427 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1428 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1429 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1430 |
* Display the RSS entries in a list. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1431 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1432 |
* @since 2.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1433 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1434 |
* @param string|array|object $rss RSS url. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1435 |
* @param array $args Widget arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1436 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1437 |
function wp_widget_rss_output( $rss, $args = array() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1438 |
if ( is_string( $rss ) ) { |
9 | 1439 |
$rss = fetch_feed( $rss ); |
1440 |
} elseif ( is_array( $rss ) && isset( $rss['url'] ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1441 |
$args = $rss; |
9 | 1442 |
$rss = fetch_feed( $rss['url'] ); |
1443 |
} elseif ( ! is_object( $rss ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1444 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1445 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1446 |
|
9 | 1447 |
if ( is_wp_error( $rss ) ) { |
1448 |
if ( is_admin() || current_user_can( 'manage_options' ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1449 |
echo '<p><strong>' . __( 'RSS Error:' ) . '</strong> ' . $rss->get_error_message() . '</p>'; |
9 | 1450 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1451 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1452 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1453 |
|
9 | 1454 |
$default_args = array( |
1455 |
'show_author' => 0, |
|
1456 |
'show_date' => 0, |
|
1457 |
'show_summary' => 0, |
|
1458 |
'items' => 0, |
|
1459 |
); |
|
1460 |
$args = wp_parse_args( $args, $default_args ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1461 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1462 |
$items = (int) $args['items']; |
9 | 1463 |
if ( $items < 1 || 20 < $items ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1464 |
$items = 10; |
9 | 1465 |
} |
1466 |
$show_summary = (int) $args['show_summary']; |
|
1467 |
$show_author = (int) $args['show_author']; |
|
1468 |
$show_date = (int) $args['show_date']; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1469 |
|
9 | 1470 |
if ( ! $rss->get_item_quantity() ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1471 |
echo '<ul><li>' . __( 'An error has occurred, which probably means the feed is down. Try again later.' ) . '</li></ul>'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1472 |
$rss->__destruct(); |
9 | 1473 |
unset( $rss ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1474 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1475 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1476 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1477 |
echo '<ul>'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1478 |
foreach ( $rss->get_items( 0, $items ) as $item ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1479 |
$link = $item->get_link(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1480 |
while ( stristr( $link, 'http' ) != $link ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1481 |
$link = substr( $link, 1 ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1482 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1483 |
$link = esc_url( strip_tags( $link ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1484 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1485 |
$title = esc_html( trim( strip_tags( $item->get_title() ) ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1486 |
if ( empty( $title ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1487 |
$title = __( 'Untitled' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1488 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1489 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1490 |
$desc = @html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1491 |
$desc = esc_attr( wp_trim_words( $desc, 55, ' […]' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1492 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1493 |
$summary = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1494 |
if ( $show_summary ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1495 |
$summary = $desc; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1496 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1497 |
// Change existing [...] to […]. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1498 |
if ( '[...]' == substr( $summary, -5 ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1499 |
$summary = substr( $summary, 0, -5 ) . '[…]'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1500 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1501 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1502 |
$summary = '<div class="rssSummary">' . esc_html( $summary ) . '</div>'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1503 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1504 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1505 |
$date = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1506 |
if ( $show_date ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1507 |
$date = $item->get_date( 'U' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1508 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1509 |
if ( $date ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1510 |
$date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date ) . '</span>'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1511 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1512 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1513 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1514 |
$author = ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1515 |
if ( $show_author ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1516 |
$author = $item->get_author(); |
9 | 1517 |
if ( is_object( $author ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1518 |
$author = $author->get_name(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1519 |
$author = ' <cite>' . esc_html( strip_tags( $author ) ) . '</cite>'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1520 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1521 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1522 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1523 |
if ( $link == '' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1524 |
echo "<li>$title{$date}{$summary}{$author}</li>"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1525 |
} elseif ( $show_summary ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1526 |
echo "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$summary}{$author}</li>"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1527 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1528 |
echo "<li><a class='rsswidget' href='$link'>$title</a>{$date}{$author}</li>"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1529 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1530 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1531 |
echo '</ul>'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1532 |
$rss->__destruct(); |
9 | 1533 |
unset( $rss ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1534 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1535 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1536 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1537 |
* Display RSS widget options form. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1538 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1539 |
* The options for what fields are displayed for the RSS form are all booleans |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1540 |
* and are as follows: 'url', 'title', 'items', 'show_summary', 'show_author', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1541 |
* 'show_date'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1542 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1543 |
* @since 2.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1544 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1545 |
* @param array|string $args Values for input fields. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1546 |
* @param array $inputs Override default display options. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1547 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1548 |
function wp_widget_rss_form( $args, $inputs = null ) { |
9 | 1549 |
$default_inputs = array( |
1550 |
'url' => true, |
|
1551 |
'title' => true, |
|
1552 |
'items' => true, |
|
1553 |
'show_summary' => true, |
|
1554 |
'show_author' => true, |
|
1555 |
'show_date' => true, |
|
1556 |
); |
|
1557 |
$inputs = wp_parse_args( $inputs, $default_inputs ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1558 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1559 |
$args['title'] = isset( $args['title'] ) ? $args['title'] : ''; |
9 | 1560 |
$args['url'] = isset( $args['url'] ) ? $args['url'] : ''; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1561 |
$args['items'] = isset( $args['items'] ) ? (int) $args['items'] : 0; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1562 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1563 |
if ( $args['items'] < 1 || 20 < $args['items'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1564 |
$args['items'] = 10; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1565 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1566 |
|
9 | 1567 |
$args['show_summary'] = isset( $args['show_summary'] ) ? (int) $args['show_summary'] : (int) $inputs['show_summary']; |
1568 |
$args['show_author'] = isset( $args['show_author'] ) ? (int) $args['show_author'] : (int) $inputs['show_author']; |
|
1569 |
$args['show_date'] = isset( $args['show_date'] ) ? (int) $args['show_date'] : (int) $inputs['show_date']; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1570 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1571 |
if ( ! empty( $args['error'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1572 |
echo '<p class="widget-error"><strong>' . __( 'RSS Error:' ) . '</strong> ' . $args['error'] . '</p>'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1573 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1574 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1575 |
$esc_number = esc_attr( $args['number'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1576 |
if ( $inputs['url'] ) : |
9 | 1577 |
?> |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1578 |
<p><label for="rss-url-<?php echo $esc_number; ?>"><?php _e( 'Enter the RSS feed URL here:' ); ?></label> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1579 |
<input class="widefat" id="rss-url-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][url]" type="text" value="<?php echo esc_url( $args['url'] ); ?>" /></p> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1580 |
<?php endif; if ( $inputs['title'] ) : ?> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1581 |
<p><label for="rss-title-<?php echo $esc_number; ?>"><?php _e( 'Give the feed a title (optional):' ); ?></label> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1582 |
<input class="widefat" id="rss-title-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][title]" type="text" value="<?php echo esc_attr( $args['title'] ); ?>" /></p> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1583 |
<?php endif; if ( $inputs['items'] ) : ?> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1584 |
<p><label for="rss-items-<?php echo $esc_number; ?>"><?php _e( 'How many items would you like to display?' ); ?></label> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1585 |
<select id="rss-items-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][items]"> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1586 |
<?php |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1587 |
for ( $i = 1; $i <= 20; ++$i ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1588 |
echo "<option value='$i' " . selected( $args['items'], $i, false ) . ">$i</option>"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1589 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1590 |
?> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1591 |
</select></p> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1592 |
<?php endif; if ( $inputs['show_summary'] ) : ?> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1593 |
<p><input id="rss-show-summary-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_summary]" type="checkbox" value="1" <?php checked( $args['show_summary'] ); ?> /> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1594 |
<label for="rss-show-summary-<?php echo $esc_number; ?>"><?php _e( 'Display item content?' ); ?></label></p> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1595 |
<?php endif; if ( $inputs['show_author'] ) : ?> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1596 |
<p><input id="rss-show-author-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_author]" type="checkbox" value="1" <?php checked( $args['show_author'] ); ?> /> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1597 |
<label for="rss-show-author-<?php echo $esc_number; ?>"><?php _e( 'Display item author if available?' ); ?></label></p> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1598 |
<?php endif; if ( $inputs['show_date'] ) : ?> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1599 |
<p><input id="rss-show-date-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][show_date]" type="checkbox" value="1" <?php checked( $args['show_date'] ); ?>/> |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1600 |
<label for="rss-show-date-<?php echo $esc_number; ?>"><?php _e( 'Display item date?' ); ?></label></p> |
9 | 1601 |
<?php |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1602 |
endif; |
9 | 1603 |
foreach ( array_keys( $default_inputs ) as $input ) : |
1604 |
if ( 'hidden' === $inputs[ $input ] ) : |
|
1605 |
$id = str_replace( '_', '-', $input ); |
|
1606 |
?> |
|
1607 |
<input type="hidden" id="rss-<?php echo esc_attr( $id ); ?>-<?php echo $esc_number; ?>" name="widget-rss[<?php echo $esc_number; ?>][<?php echo esc_attr( $input ); ?>]" value="<?php echo esc_attr( $args[ $input ] ); ?>" /> |
|
1608 |
<?php |
|
1609 |
endif; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1610 |
endforeach; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1611 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1612 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1613 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1614 |
* Process RSS feed widget data and optionally retrieve feed items. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1615 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1616 |
* The feed widget can not have more than 20 items or it will reset back to the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1617 |
* default, which is 10. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1618 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1619 |
* The resulting array has the feed title, feed url, feed link (from channel), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1620 |
* feed items, error (if any), and whether to show summary, author, and date. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1621 |
* All respectively in the order of the array elements. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1622 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1623 |
* @since 2.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1624 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1625 |
* @param array $widget_rss RSS widget feed data. Expects unescaped data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1626 |
* @param bool $check_feed Optional, default is true. Whether to check feed for errors. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1627 |
* @return array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1628 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1629 |
function wp_widget_rss_process( $widget_rss, $check_feed = true ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1630 |
$items = (int) $widget_rss['items']; |
9 | 1631 |
if ( $items < 1 || 20 < $items ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1632 |
$items = 10; |
9 | 1633 |
} |
1634 |
$url = esc_url_raw( strip_tags( $widget_rss['url'] ) ); |
|
1635 |
$title = isset( $widget_rss['title'] ) ? trim( strip_tags( $widget_rss['title'] ) ) : ''; |
|
1636 |
$show_summary = isset( $widget_rss['show_summary'] ) ? (int) $widget_rss['show_summary'] : 0; |
|
1637 |
$show_author = isset( $widget_rss['show_author'] ) ? (int) $widget_rss['show_author'] : 0; |
|
1638 |
$show_date = isset( $widget_rss['show_date'] ) ? (int) $widget_rss['show_date'] : 0; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1639 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1640 |
if ( $check_feed ) { |
9 | 1641 |
$rss = fetch_feed( $url ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1642 |
$error = false; |
9 | 1643 |
$link = ''; |
1644 |
if ( is_wp_error( $rss ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1645 |
$error = $rss->get_error_message(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1646 |
} else { |
9 | 1647 |
$link = esc_url( strip_tags( $rss->get_permalink() ) ); |
1648 |
while ( stristr( $link, 'http' ) != $link ) { |
|
1649 |
$link = substr( $link, 1 ); |
|
1650 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1651 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1652 |
$rss->__destruct(); |
9 | 1653 |
unset( $rss ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1654 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1655 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1656 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1657 |
return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1658 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1659 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1660 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1661 |
* Registers all of the default WordPress widgets on startup. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1662 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1663 |
* Calls {@see 'widgets_init'} action after all of the WordPress widgets have been registered. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1664 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1665 |
* @since 2.2.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1666 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1667 |
function wp_widgets_init() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1668 |
if ( ! is_blog_installed() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1669 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1670 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1671 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1672 |
register_widget( 'WP_Widget_Pages' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1673 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1674 |
register_widget( 'WP_Widget_Calendar' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1675 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1676 |
register_widget( 'WP_Widget_Archives' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1677 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1678 |
if ( get_option( 'link_manager_enabled' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1679 |
register_widget( 'WP_Widget_Links' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1680 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1681 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1682 |
register_widget( 'WP_Widget_Media_Audio' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1683 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1684 |
register_widget( 'WP_Widget_Media_Image' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1685 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1686 |
register_widget( 'WP_Widget_Media_Gallery' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1687 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1688 |
register_widget( 'WP_Widget_Media_Video' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1689 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1690 |
register_widget( 'WP_Widget_Meta' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1691 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1692 |
register_widget( 'WP_Widget_Search' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1693 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1694 |
register_widget( 'WP_Widget_Text' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1695 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1696 |
register_widget( 'WP_Widget_Categories' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1697 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1698 |
register_widget( 'WP_Widget_Recent_Posts' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1699 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1700 |
register_widget( 'WP_Widget_Recent_Comments' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1701 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1702 |
register_widget( 'WP_Widget_RSS' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1703 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1704 |
register_widget( 'WP_Widget_Tag_Cloud' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1705 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1706 |
register_widget( 'WP_Nav_Menu_Widget' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1707 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1708 |
register_widget( 'WP_Widget_Custom_HTML' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1709 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1710 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1711 |
* Fires after all default WordPress widgets have been registered. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1712 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1713 |
* @since 2.2.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1714 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1715 |
do_action( 'widgets_init' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1716 |
} |