|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Plugin Name: shortcodely |
|
4
|
|
|
* Plugin URI: htps://github.com/patilswapnilv/shortcodely |
|
5
|
|
|
* Description: Include any widget in a page/post for any theme. |
|
6
|
|
|
* Author: patilswapnilv |
|
7
|
|
|
* Version: 0.1 |
|
8
|
|
|
* Author URI: http://swapnilpatil.in |
|
9
|
|
|
* Domain Path: /languages/ |
|
10
|
|
|
* |
|
11
|
|
|
* Include any widget in page/post for any theme. [do_widget widgetname ] or |
|
12
|
|
|
* [do_widget "widget name" ] [do_widget id=widgetnamedashed-n] or |
|
13
|
|
|
* include a whole widget area [do_widget_area]. |
|
14
|
|
|
* |
|
15
|
|
|
* Please read: <a href="https://github.com/patilswapnilv/shortcodely/installation/">Installation</a> |
|
16
|
|
|
* and <a href="https://github.com/patilswapnilv/shortcodely/faq/">FAQ</a>. |
|
17
|
|
|
* |
|
18
|
|
|
* PHP version 5 |
|
19
|
|
|
* |
|
20
|
|
|
* LICENCE: GNU GENERAL PUBLIC LICENSE |
|
21
|
|
|
* Version 3, 29 June 2007 |
|
22
|
|
|
* Copyright © 2007 Free Software Foundation, Inc. <http://fsf.org/> |
|
23
|
|
|
* Everyone is permitted to copy and distribute verbatim copies |
|
24
|
|
|
* of this license document, but changing it is not allowed. |
|
25
|
|
|
* |
|
26
|
|
|
* Main file, contains the plugin metadata and activation processes |
|
27
|
|
|
* |
|
28
|
|
|
* @category Core |
|
29
|
|
|
* @package Shortcodely |
|
30
|
|
|
* @author Swapnil V. Patil <[email protected]> |
|
31
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html |
|
32
|
|
|
* @version 0.1 |
|
33
|
|
|
* @link https://github.com/patilswapnilv/shortcodely |
|
34
|
|
|
*/ |
|
35
|
|
|
|
|
36
|
|
|
add_action('in_widget_form', 'shortcodely_spice_get_widget_id'); |
|
37
|
|
View Code Duplication |
function shortcodely_spice_get_widget_id($widget_instance) |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
/* |
|
40
|
|
|
* Main function to get widget id |
|
41
|
|
|
* |
|
42
|
|
|
*/ |
|
43
|
|
|
echo '<p><strong>To use as shortcode with id:</strong> '; |
|
44
|
|
|
if ('__i__' == $widget_instance->number) { |
|
|
|
|
|
|
45
|
|
|
echo 'Save the widget first!</p>'; |
|
46
|
|
|
} else { |
|
47
|
|
|
echo '[do_widget id=' . $widget_instance->id . ']</p>'; |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @return callable |
|
53
|
|
|
*/ |
|
54
|
|
View Code Duplication |
function shortcodely_remove_widget_class($params) |
|
|
|
|
|
|
55
|
|
|
{ |
|
56
|
|
|
/* |
|
57
|
|
|
* Remove the widget classes |
|
58
|
|
|
*/ |
|
59
|
|
|
if (! empty($params[0]['before_widget'])) { |
|
|
|
|
|
|
60
|
|
|
$params[0]['before_widget'] = |
|
61
|
|
|
str_replace('"widget ', '"', $params[0]['before_widget']); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (! empty($params[0]['before_title'])) { |
|
|
|
|
|
|
65
|
|
|
$params[0]['before_title'] |
|
66
|
|
|
= $params[0]['before_title'] = str_replace('widget-title', '', $params[0]['before_title']); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $params; |
|
70
|
|
|
} |
|
71
|
|
|
/*-----------------------------------*/ |
|
72
|
|
View Code Duplication |
function shortcodely_do_widget_area($atts) |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
global $wp_registered_widgets, $_wp_sidebars_widgets, $wp_registered_sidebars; |
|
75
|
|
|
|
|
76
|
|
|
extract( |
|
|
|
|
|
|
77
|
|
|
shortcode_atts( |
|
|
|
|
|
|
78
|
|
|
array( |
|
79
|
|
|
'widget_area' => 'widgets_for_shortcodes', |
|
80
|
|
|
'class' => 'shortcodely-widget-area', /* the widget class is picked up automatically. If we want to add an additional class at the wrap level to try to match a theme, use this */ |
|
81
|
|
|
'widget_area_class' => '', /* option to disassociate from themes widget styling use =none*/ |
|
82
|
|
|
'widget_classes' => '', /* option to disassociate from themes widget styling */ |
|
83
|
|
|
|
|
84
|
|
|
), $atts |
|
85
|
|
|
) |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
if (! empty($atts)) { |
|
|
|
|
|
|
89
|
|
|
if (('widgets_for_shortcodes' == $widget_area) and ! empty($atts[0])) { |
|
|
|
|
|
|
90
|
|
|
$widget_area = $atts[0]; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if (empty($wp_registered_sidebars[$widget_area])) { |
|
|
|
|
|
|
95
|
|
|
echo '<br/>Widget area "' . $widget_area . '" not found. Registered widget areas (sidebars) are: <br/>'; |
|
|
|
|
|
|
96
|
|
|
foreach ($wp_registered_sidebars as $area => $sidebar) { |
|
|
|
|
|
|
97
|
|
|
echo $area . '<br />'; |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
//if (isset($_REQUEST['do_widget_debug']) and current_user_can('administrator')) var_dump( $wp_registered_sidebars); /**/ |
|
101
|
|
|
|
|
102
|
|
|
if ('none' == $widget_area_class) { |
|
|
|
|
|
|
103
|
|
|
$class = ''; |
|
104
|
|
|
} else { |
|
105
|
|
|
if (! empty($widget_area_class)) { //2014 08 |
|
|
|
|
|
|
106
|
|
|
$class .= 'class="' . $class . ' ' . $widget_area_class . '"'; |
|
107
|
|
|
} else { |
|
108
|
|
|
$class = 'class="' . $class . '"'; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (! empty($widget_classes) and ('none' == $widget_classes)) { |
|
|
|
|
|
|
113
|
|
|
add_filter('dynamic_sidebar_params', 'shortcodely_remove_widget_class'); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
ob_start(); /* catch the echo output, so we can control where it appears in the text */ |
|
117
|
|
|
dynamic_sidebar($widget_area); |
|
118
|
|
|
$output = ob_get_clean(); |
|
119
|
|
|
remove_filter('dynamic_sidebar_params', 'shortcodely_remove_widget_class'); |
|
120
|
|
|
|
|
121
|
|
|
$output = PHP_EOL . '<div id="' . $widget_area . '" ' . $class . '>' |
|
122
|
|
|
. $output |
|
123
|
|
|
. '</div>' . PHP_EOL; |
|
124
|
|
|
|
|
125
|
|
|
return $output; |
|
126
|
|
|
} |
|
127
|
|
|
/*-----------------------------------*/ |
|
128
|
|
View Code Duplication |
function shortcodely_do_widget($atts) |
|
|
|
|
|
|
129
|
|
|
{ |
|
130
|
|
|
global $wp_registered_widgets, $_wp_sidebars_widgets, $wp_registered_sidebars; |
|
131
|
|
|
|
|
132
|
|
|
/* check if the widget is in the shortcode x sidebar if not , just use generic, |
|
133
|
|
|
if it is in, then get the instance data and use that */ |
|
134
|
|
|
|
|
135
|
|
|
if (is_admin()) { |
|
|
|
|
|
|
136
|
|
|
return ''; |
|
137
|
|
|
} // eg in case someone decides to apply content filters when apost is saved, and not all widget stuff is there. |
|
138
|
|
|
|
|
139
|
|
|
extract( |
|
|
|
|
|
|
140
|
|
|
shortcode_atts( |
|
|
|
|
|
|
141
|
|
|
array( |
|
142
|
|
|
'sidebar' => 'Widgets for Shortcodely', //default |
|
143
|
|
|
'id' => '', |
|
144
|
|
|
'name' => '', |
|
145
|
|
|
'title' => '', /* do the default title unless they ask us not to - use string here not boolean */ |
|
146
|
|
|
'class' => 'shortcodely_widget', /* the widget class is picked up automatically. If we want to add an additional class at the wrap level to try to match a theme, use this */ |
|
147
|
|
|
'wrap' => '', /* wrap the whole thing - title plus widget in a div - maybe the themes use a div, maybe not, maybe we want that styling, maybe not */ |
|
148
|
|
|
'widget_classes' => '', /* option to disassociate from themes widget styling */ |
|
149
|
|
|
), $atts |
|
150
|
|
|
) |
|
151
|
|
|
); |
|
152
|
|
|
|
|
153
|
|
|
if (isset($_wp_sidebars_widgets)) { |
|
|
|
|
|
|
154
|
|
|
shortcodely_show_widget_debug('which one', $name, $id, $sidebar); //check for debug prompt and show widgets in shortcode sidebar if requested and logged in etc |
|
155
|
|
|
} else { |
|
156
|
|
|
$output = '<br />No widgets defined at all in any sidebar!'; |
|
157
|
|
|
|
|
158
|
|
|
return $output; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/* compatibility check - if the name is not entered, then the first parameter is the name */ |
|
162
|
|
|
if (empty($name) and ! empty($atts[0])) { |
|
|
|
|
|
|
163
|
|
|
$name = $atts[0]; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/* the widget need not be specified, [do_widget widgetname] is adequate */ |
|
167
|
|
|
if (! empty($name)) { // we have a name |
|
|
|
|
|
|
168
|
|
|
$widget = $name; |
|
169
|
|
|
|
|
170
|
|
|
foreach ($wp_registered_widgets as $i => $w) {/* get the official internal name or id that the widget was registered with */ |
|
|
|
|
|
|
171
|
|
|
if (strtolower($widget) == (strtolower($w ['name']))) { |
|
|
|
|
|
|
172
|
|
|
$widget_ids[] = $i; |
|
173
|
|
|
} |
|
174
|
|
|
//if ($debug) {echo '<br /> Check: '.$w['name'];} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
if (! ($sidebarid = shortcodely_get_sidebar_id($sidebar))) { |
|
|
|
|
|
|
178
|
|
|
$sidebarid = $sidebar; /* get the official sidebar id for this widget area - will take the first one */ |
|
179
|
|
|
} |
|
180
|
|
|
} else { /* check for id if we do not have a name */ |
|
181
|
|
|
|
|
182
|
|
|
if (! empty($id)) { /* if a specific id has been specified */ |
|
|
|
|
|
|
183
|
|
|
foreach ($wp_registered_widgets as $i => $w) { /* get the official internal name or id that the widget was registered with */ |
|
|
|
|
|
|
184
|
|
|
if ($id == $w['id']) { |
|
|
|
|
|
|
185
|
|
|
$widget_ids[] = $id; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
//echo '<h2>We have an id: '.$id.'</h2>'; if (!empty($widget_ids)) var_dump($widget_ids); |
|
189
|
|
|
} else { |
|
190
|
|
|
$output = '<br />No valid widget name or id given in shortcode parameters'; |
|
191
|
|
|
|
|
192
|
|
|
return $output; |
|
193
|
|
|
} |
|
194
|
|
|
// if we have id, get the sidebar for it |
|
195
|
|
|
$sidebarid = shortcodely_get_widgets_sidebar($id); |
|
196
|
|
|
if (! $sidebarid) { |
|
|
|
|
|
|
197
|
|
|
$output = '<br />Widget not in any sidebars<br />'; |
|
198
|
|
|
|
|
199
|
|
|
return $output; |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
if (empty($widget)) { |
|
|
|
|
|
|
204
|
|
|
$widget = ''; |
|
205
|
|
|
} |
|
206
|
|
|
if (empty($id)) { |
|
|
|
|
|
|
207
|
|
|
$id = ''; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
if (empty($widget_ids)) { |
|
|
|
|
|
|
211
|
|
|
$output = '<br />Error: Your Requested widget "' . $widget . ' ' . $id . '" is not in the widget list.<br />'; |
|
212
|
|
|
$output .= shortcodely_show_widget_debug('empty', $name, $id, $sidebar); |
|
213
|
|
|
|
|
214
|
|
|
return $output; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
if (empty($widget)) { |
|
|
|
|
|
|
218
|
|
|
$widget = ''; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
//$content = ''; |
|
222
|
|
|
/* if the widget is in our chosen sidebar, then use the options stored for that */ |
|
223
|
|
|
|
|
224
|
|
|
if ((! isset($_wp_sidebars_widgets[$sidebarid])) or (empty($_wp_sidebars_widgets[$sidebarid]))) { // try upgrade |
|
|
|
|
|
|
225
|
|
|
shortcodely_upgrade_sidebar(); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
//if we have a specific sidebar selected, use that |
|
229
|
|
|
if ((isset($_wp_sidebars_widgets[$sidebarid])) and (! empty($_wp_sidebars_widgets[$sidebarid]))) { |
|
|
|
|
|
|
230
|
|
|
/* get the intersect of the 2 widget setups so we just get the widget we want */ |
|
231
|
|
|
|
|
232
|
|
|
$wid = array_intersect($_wp_sidebars_widgets[$sidebarid], $widget_ids); |
|
|
|
|
|
|
233
|
|
|
} else { /* the sidebar is not defined or selected - should not happen */ |
|
234
|
|
|
if (isset($debug)) { // only do this in debug mode |
|
|
|
|
|
|
235
|
|
|
if (! isset($_wp_sidebars_widgets[$sidebarid])) { |
|
|
|
|
|
|
236
|
|
|
$output = '<p>Error: Sidebar "' . $sidebar . '" with sidebarid "' . $sidebarid . '" is not defined.</p>'; |
|
237
|
|
|
} // shouldnt happen - maybe someone running content filters on save |
|
238
|
|
|
else { |
|
239
|
|
|
$output = '<p>Error: Sidebar "' . $sidebar . '" with sidebarid "' . $sidebarid . '" is empty (no widgets)</p>'; |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
$output = ''; |
|
245
|
|
|
if (empty($wid) or (! is_array($wid)) or (count($wid) < 1)) { |
|
|
|
|
|
|
246
|
|
|
$output = '<p>Error: Your requested Widget "' . $widget . '" is not in the "' . $sidebar . '" sidebar</p>'; |
|
247
|
|
|
$output .= shortcodely_show_widget_debug('empty', $name, $id, $sidebar); |
|
248
|
|
|
|
|
249
|
|
|
unset($sidebar); |
|
250
|
|
|
unset($sidebarid); |
|
251
|
|
|
} else { |
|
252
|
|
|
/* There may only be one but if we have two in our chosen widget then it will do both */ |
|
253
|
|
|
$output = ''; |
|
254
|
|
|
foreach ($wid as $i => $widget_instance) { |
|
|
|
|
|
|
255
|
|
|
ob_start(); /* catch the echo output, so we can control where it appears in the text */ |
|
256
|
|
|
shortcodely_shortcode_sidebar($widget_instance, $sidebar, $title, $class, $wrap, $widget_classes); |
|
257
|
|
|
$output .= ob_get_clean(); |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
return $output; |
|
262
|
|
|
} |
|
263
|
|
|
/* -------------------------------------------------------------------------*/ |
|
264
|
|
View Code Duplication |
function shortcodely_shortcode_sidebar($widget_id, |
|
|
|
|
|
|
265
|
|
|
$name = 'widgets_for_shortcode', |
|
266
|
|
|
$title = true, |
|
267
|
|
|
$class = '', |
|
268
|
|
|
$wrap = '', |
|
269
|
|
|
$widget_classes = '' |
|
270
|
|
|
) { |
|
271
|
|
|
/* This is basically the wordpress code, slightly modified */ |
|
272
|
|
|
global $wp_registered_sidebars, $wp_registered_widgets; |
|
273
|
|
|
|
|
274
|
|
|
$debug = shortcodely_check_if_widget_debug(); |
|
275
|
|
|
|
|
276
|
|
|
$sidebarid = shortcodely_get_sidebar_id($name); |
|
277
|
|
|
|
|
278
|
|
|
$sidebars_widgets = wp_get_sidebars_widgets(); |
|
279
|
|
|
|
|
280
|
|
|
$sidebar = $wp_registered_sidebars[$sidebarid]; // has the params etc |
|
|
|
|
|
|
281
|
|
|
|
|
282
|
|
|
$did_one = false; |
|
283
|
|
|
|
|
284
|
|
|
/* lifted from wordpress code, keep as similar as possible for now */ |
|
285
|
|
|
|
|
286
|
|
|
if (! isset($wp_registered_widgets[$widget_id])) { |
|
|
|
|
|
|
287
|
|
|
return; |
|
288
|
|
|
} // wp had c o n t i n u e |
|
289
|
|
|
|
|
290
|
|
|
$params = array_merge( |
|
291
|
|
|
array( |
|
292
|
|
|
array_merge( |
|
293
|
|
|
$sidebar, |
|
294
|
|
|
array( |
|
295
|
|
|
'widget_id' => $widget_id, |
|
296
|
|
|
'widget_name' => $wp_registered_widgets[$widget_id]['name'], |
|
|
|
|
|
|
297
|
|
|
) |
|
298
|
|
|
), |
|
299
|
|
|
), |
|
300
|
|
|
(array) $wp_registered_widgets[$widget_id]['params'] |
|
|
|
|
|
|
301
|
|
|
); |
|
302
|
|
|
|
|
303
|
|
|
$validtitletags = array('h1', 'h2', 'h3', 'h4', 'h5', 'header', 'strong', 'em'); |
|
304
|
|
|
$validwraptags = array('div', 'p', 'main', 'aside', 'section'); |
|
305
|
|
|
|
|
306
|
|
|
if (! empty($wrap)) { /* then folks want to 'wrap' with their own html tag, or wrap = yes */ |
|
|
|
|
|
|
307
|
|
|
if ((! in_array($wrap, $validwraptags))) { |
|
|
|
|
|
|
308
|
|
|
$wrap = ''; |
|
309
|
|
|
} |
|
310
|
|
|
/* To match a variety of themes, allow for a variety of html tags. */ |
|
311
|
|
|
/* May not need if our sidebar match attempt has worked */ |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
if (! empty($wrap)) { |
|
|
|
|
|
|
315
|
|
|
$params[0]['before_widget'] = '<' . $wrap . ' id="%1$s" class="%2$s">'; |
|
316
|
|
|
$params[0]['after_widget'] = '</' . $wrap . '>'; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
// wp code to get classname |
|
320
|
|
|
$classname_ = ''; |
|
321
|
|
|
//foreach ( (array) $wp_registered_widgets[$widget_id]['classname'] as $cn ) { |
|
322
|
|
|
$cn = $wp_registered_widgets[$widget_id]['classname']; |
|
|
|
|
|
|
323
|
|
|
if (is_string($cn)) { |
|
|
|
|
|
|
324
|
|
|
$classname_ .= '_' . $cn; |
|
325
|
|
|
} elseif (is_object($cn)) { |
|
|
|
|
|
|
326
|
|
|
$classname_ .= '_' . get_class($cn); |
|
327
|
|
|
} |
|
328
|
|
|
//} |
|
329
|
|
|
$classname_ = ltrim($classname_, '_'); |
|
330
|
|
|
|
|
331
|
|
|
// add MKM and others requested class in to the wp classname string |
|
332
|
|
|
// if no class specfied, then class will = shortcodelywidget. These classes are so can reverse out unwanted widget styling. |
|
333
|
|
|
|
|
334
|
|
|
// $classname_ .= ' widget '; // wordpress seems to almost always adds the widget class |
|
335
|
|
|
|
|
336
|
|
|
$classname_ .= ' ' . $class; |
|
337
|
|
|
|
|
338
|
|
|
// we are picking up the defaults from the thems sidebar ad they have registered heir sidebar to issue widget classes? |
|
339
|
|
|
|
|
340
|
|
|
// Substitute HTML id and class attributes into before_widget |
|
341
|
|
|
if (! empty($params[0]['before_widget'])) { |
|
|
|
|
|
|
342
|
|
|
$params[0]['before_widget'] = sprintf($params[0]['before_widget'], $widget_id, $classname_); |
|
343
|
|
|
} else { |
|
344
|
|
|
$params[0]['before_widget'] = ''; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
if (empty($params[0]['before_widget'])) { |
|
|
|
|
|
|
348
|
|
|
$params[0]['after_widget'] = ''; |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
$params = apply_filters('dynamic_sidebar_params', $params); |
|
352
|
|
|
// allow, any pne usingmust ensure they apply to the correct sidebars |
|
353
|
|
|
|
|
354
|
|
|
if (! empty($title)) { |
|
|
|
|
|
|
355
|
|
|
if ('false' == $title) { /* shortcodely switch off the title html, still need to get rid of title separately */ |
|
|
|
|
|
|
356
|
|
|
$params[0]['before_title'] = '<span style="display: none">'; |
|
357
|
|
|
$params[0]['after_title'] = '</span>'; |
|
358
|
|
|
} else { |
|
359
|
|
|
if (in_array($title, $validtitletags)) { |
|
|
|
|
|
|
360
|
|
|
$class = ' class="widget-title" '; |
|
361
|
|
|
|
|
362
|
|
|
$params[0]['before_title'] = '<' . $title . ' ' . $class . ' >'; |
|
363
|
|
|
$params[0]['after_title'] = '</' . $title . '>'; |
|
364
|
|
|
} |
|
365
|
|
|
} |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
if (! empty($widget_classes) and ('none' == $widget_classes)) { |
|
|
|
|
|
|
369
|
|
|
$params = shortcodely_remove_widget_class($params); // also called in widget area shortcode |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
$callback = $wp_registered_widgets[$widget_id]['callback']; |
|
|
|
|
|
|
373
|
|
|
if (is_callable($callback)) { |
|
|
|
|
|
|
374
|
|
|
call_user_func_array($callback, $params); |
|
375
|
|
|
$did_one = true; |
|
376
|
|
|
} |
|
377
|
|
|
// } |
|
378
|
|
|
return $did_one; |
|
379
|
|
|
} |
|
380
|
|
|
/* ---------------------------------------------------------------------------*/ |
|
381
|
|
View Code Duplication |
function shortcodely_reg_sidebar() |
|
|
|
|
|
|
382
|
|
|
{ |
|
383
|
|
|
// this is fired late, so hopefully any theme sidebars will have been registered already. |
|
384
|
|
|
|
|
385
|
|
|
global $wp_registered_widgets, $_wp_sidebars_widgets, $wp_registered_sidebars; |
|
386
|
|
|
|
|
387
|
|
|
if (function_exists('register_sidebar')) { // maybe later, get the first main sidebar and copy it's before/after etc |
|
|
|
|
|
|
388
|
|
|
$args = array( |
|
389
|
|
|
'name' => 'Widgets for Shortcodely', |
|
390
|
|
|
'id' => 'widgets_for_shortcodes', // hope to avoid losing widgets |
|
391
|
|
|
'description' => __('Sidebar to hold widgets and their settings. These widgets will be used in a shortcode. This sidebars widgets should be saved with your theme settings now.', 'shortcodely-shortcode-any-widget'), |
|
392
|
|
|
'before_widget' => '<aside' . ' id="%1$s" class="%2$s ">', // 201402 to match twentyfourteen theme |
|
393
|
|
|
'after_widget' => '</aside>', |
|
394
|
|
|
'before_title' => '<h1 class="widget-title" >', // 201402 maybe dont use widget class - we are in content here not in a widget area but others want the widget styling. ? |
|
395
|
|
|
'after_title' => '</h1>', |
|
396
|
|
|
); |
|
397
|
|
|
|
|
398
|
|
|
if (! empty($wp_registered_sidebars)) { // we got some sidebars already. |
|
|
|
|
|
|
399
|
|
|
$main_sidebar = reset($wp_registered_sidebars); // Grab the first sidebar and use that as defaults for the widgets |
|
400
|
|
|
$args['before_widget'] = $main_sidebar['before_widget']; |
|
401
|
|
|
$args['after_widget'] = $main_sidebar['after_widget']; |
|
402
|
|
|
$args['before_title'] = $main_sidebar['before_title']; |
|
403
|
|
|
$args['after_title'] = $main_sidebar['after_title']; |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
|
|
register_sidebar($args); |
|
407
|
|
|
} |
|
408
|
|
|
|
|
409
|
|
|
//else { echo '<h1>CANNOT REGISTER widgets_for_shortcodes SIDEBAR</h1>';} |
|
410
|
|
|
} |
|
411
|
|
|
/*-----------------------------------*/ |
|
412
|
|
|
require 'shortcodely-admin-form-html.php'; |
|
413
|
|
|
require 'shortcodely-utilities.php'; |
|
414
|
|
|
|
|
415
|
|
|
if (is_admin()) { |
|
|
|
|
|
|
416
|
|
|
$shortcodely_saw_plugin_admin = new shortcodely_saw_plugin_admin(); |
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
|
|
add_action('widgets_init', 'shortcodely_reg_sidebar', 98); // register late so it appears last |
|
420
|
|
|
|
|
421
|
|
|
add_action('switch_theme', 'shortcodely_save_shortcodes_sidebar'); |
|
422
|
|
|
add_action('after_switch_theme', 'shortcodely_restore_shortcodes_sidebar'); |
|
423
|
|
|
|
|
424
|
|
|
add_shortcode('do_widget', 'shortcodely_do_widget'); |
|
425
|
|
|
add_shortcode('do_widget_area', 'shortcodely_do_widget_area'); // just dump the whole widget area - to get same styling |
|
426
|
|
|
|
|
427
|
|
|
//require_once(ABSPATH . 'wp-includes/widgets.php'); // *** do we really need this here? |
|
428
|
|
|
function shortcodely_saw_load_text() |
|
429
|
|
|
{ |
|
430
|
|
|
// wp (see l10n.php) will check wp-content/languages/plugins if nothing found in plugin dir |
|
431
|
|
|
$result = load_plugin_textdomain( |
|
432
|
|
|
'shortcodely-shortcode-any-widget', false, |
|
433
|
|
|
dirname(plugin_basename(__FILE__)) . '/languages/' |
|
434
|
|
|
); |
|
435
|
|
|
} |
|
436
|
|
|
|
|
437
|
|
|
add_action('plugins_loaded', 'shortcodely_saw_load_text'); |
|
438
|
|
|
|
|
439
|
|
|
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'shortcodely_add_action_links'); |
|
440
|
|
|
|
|
441
|
|
View Code Duplication |
function shortcodely_add_action_links($links) |
|
|
|
|
|
|
442
|
|
|
{ |
|
443
|
|
|
$mylinks[] = |
|
|
|
|
|
|
444
|
|
|
'<a title="Haven\'t read the instructions? Need your hand held?" href="' . admin_url('options-general.php?page=shortcodely_saw') . '">Settings</a>'; |
|
445
|
|
|
$mylinks[] = |
|
446
|
|
|
'<a title="Yes I know it is the same link, but some people ...." href="' . admin_url('options-general.php?page=shortcodely_saw') . '">HELP</a>'; |
|
447
|
|
|
|
|
448
|
|
|
return array_merge($links, $mylinks); |
|
449
|
|
|
} |
|
450
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.