-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwidget-alias.php
More file actions
344 lines (272 loc) · 11 KB
/
Copy pathwidget-alias.php
File metadata and controls
344 lines (272 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/**
* Plugin Name: Widget Alias
* Plugin URI: http://mightyminnow.com
* Description: Creates an alias widget so you only have to edit once.
* Version: 1.7.2
* Author: Mickey Kay, MIGHTYminnow
* Author URI: http://mightyminnow.com
* License: GPLv2+
*/
/**
* To-dos:
*
* Add <option> hover state to show aliased widget with border
* i18n
*/
// Make sure we don't expose any info if called directly
if ( !function_exists( 'add_action' ) ) {
_e('Hi there! I\'m just a plugin, not much I can do when called directly.', 'widget-alias');
exit;
}
/**
* Enqueue jQuery
*/
function wa_enqueue_admin_scripts( $hook ) {
// Don't enqueue anything unless we're on the widgets page or customizer.
if ( 'widgets.php' != $hook && 'customize.php' != $hook ) {
return;
}
wp_enqueue_style( 'widget-alias', plugin_dir_url( __FILE__ ) . 'lib/css/widget-alias.css' );
wp_enqueue_script( 'widget-alias', plugin_dir_url( __FILE__ ) . 'lib/js/widget-alias.js', array('jquery'), null, true );
// Load plugin text domain
load_plugin_textdomain( 'widget-alias', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
// jQuery strings for translation
$translation_array = array(
'aliasCountText' => _x( 'This widget is aliased', 'Text preceeding alias count', 'widget-alias' ),
'timeSing' => _x( 'time', 'singular of "time"', 'widget-alias' ),
'timePlur' => _x( 'times', 'plural of "time"', 'widget-alias' ),
);
wp_localize_script( 'widget-alias-jquery', 'translations', $translation_array );
}
add_action( 'admin_enqueue_scripts','wa_enqueue_admin_scripts' );
/**
* Registers widget and loads plugin text domain
*
* @package Widget Alias
* @since 1.0
*/
function wa_alias_init() {
// Register widget
register_widget( 'WidgetAlias');
}
add_action( 'widgets_init', 'wa_alias_init' );
/**
* The main Super Simple Related Posts Widget class
*
* @package Widget Alias
* @since 1.0
*/
class WidgetAlias extends WP_Widget {
// Object variables
public $override_title, $alias_id;
/**
* PHP5 constructor - initializes widget and sets defaults
*
* @package Widget Alias
* @since 1.0
*/
public function __construct() {
// Set widget options
$widget_options = array(
'classname' => 'widget-alias',
'description' => __('An alias widget to reproduce the output of an existing widget.', 'widget-alias') );
parent::__construct('widget-alias', __('Widget Alias', 'widget-alias'), $widget_options
);
// Add shortcode to output specific widget [wa title="title"]
add_shortcode( 'wa', array( $this, 'wa_shortcode' ) );
add_shortcode( 'widget_alias', array( $this, 'wa_shortcode' ) );
}
/**
* Output the widget settings form
*
* @package Widget Alias
* @since 1.0
*
* @param array $instance the current widget settings
*/
public function form( $instance ) {
global $wp_registered_sidebars;
// Map all undefined $instance properties to defaults
if ( !empty( $this->defaults ) )
$instance = wp_parse_args( (array) $instance, $this->defaults );
// Get all existing widgets
$sidebar_widgets = get_option( 'sidebars_widgets' );
// Remove 'wp_inactive_widgets' and 'array_version' key/value pairs
unset( $sidebar_widgets['wp_inactive_widgets'] );
unset( $sidebar_widgets['array_version'] );
// Begin <select> element
$select = '<select id="' . $this->get_field_id( 'alias-widget-id' ) . '" name="' . $this->get_field_name( 'alias-widget-id' ) . '" class="widefat" >' . "\n";
// Add default option
$select .= "\t" . '<option value="none">None</option>' . "\n";
// Loop through each sidebar
foreach ( $sidebar_widgets as $sidebar => $widgets ) {
if ( empty( $widgets ) )
continue;
// Reset $sidebars_output and $widgets_output string variables
$widgets_output = '';
// Output <option> for each widget in sidebar (excluding this widget to avoid recursion)
foreach( $widgets as $widget ) {
if ( $this->id != $widget )
$widgets_output .= "\t" . '<option value="'. $widget . '"' . selected( !empty( $instance['alias-widget-id'] ) ? $instance['alias-widget-id'] : '', $widget, __return_false() ) . '>' . $widget . '</option>' . "\n";
}
// Add <optgroup> for each sidebar that has widgets
if ( ! empty( $widgets_output ) && isset( $wp_registered_sidebars[ $sidebar ]['name'] ) )
$select .= "\t" . '<optgroup label="' . $wp_registered_sidebars[ $sidebar ]['name'] . '">' . "\n" . $widgets_output;
}
// Close <select> element
$select .= "</select>\n";
// Output widget form
?>
<!-- Title -->
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Override title:', 'widget-alias'); ?></label>
<input type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo !empty( $instance['title'] ) ? $instance['title'] : ''; ?>" class="widefat" />
</p>
<?php if ( !empty( $select ) ) : ?>
<!-- Widget drop-down select -->
<p>
<label for="<?php echo $this->get_field_id( 'alias-widget-id' ); ?>"><?php _e('Widget to alias:', 'widget-alias'); ?></label>
<?php echo $select; ?>
</p>
<?php
endif;
}
/**
* Sanitize and update widget form values
*
* @package Widget Alias
* @since 1.0
*
* @param array $new_instance the old widget settings
* @param array $old_instance the new widget settings
*/
public function update( $new_instance, $old_instance ) {
// Sanitize title
$new_instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $new_instance;
}
/**
* Output the widget contents
*
* @package Widget Alias
* @since 1.0
*
* @param array $args widget display arguments including before_title, after_title, before_widget, and after_widget
* @param array $instance the widget settings
*
* @return true if successful, false if not
*/
public function widget( $args, $instance ) {
global $wp_registered_sidebars, $wp_registered_widgets;
// Get ID of this widget
if ( isset( $args['widget_id'] ) ) {
$widget_id = $args['widget_id'];
}
// Get ID of widget to alias
$alias_widget_id = isset( $instance['alias-widget-id'] ) ? $instance['alias-widget-id'] : 'none';
// Do nothing if set to 'none'
if ( 'none' == $alias_widget_id )
return false;
// Do nothing if the aliased widget is no longer active
if ( empty( $wp_registered_widgets[ $alias_widget_id ] ) )
return false;
// Get alias widget object
$alias_widget = $wp_registered_widgets[ $alias_widget_id ];
// Get sidebar in which the alias widget lives
$alias_widget_sidebar = '';
$sidebar_widgets = get_option( 'sidebars_widgets' );
// Remove 'wp_inactive_widgets' and 'array_version' key/value pairs
unset( $sidebar_widgets['wp_inactive_widgets'] );
unset( $sidebar_widgets['array_version'] );
foreach ( $sidebar_widgets as $sidebar => $widgets ) {
foreach( $widgets as $widget ) {
if ( $alias_widget_id == $widget) {
$alias_widget_sidebar = $sidebar;
}
}
}
// Get alias widget's callback
$callback = $alias_widget[ 'callback' ];
// Don't ouput anything if the widget isn't assigned to a sidebar (e.g. Inactive Widgets sidebar)
if ( empty( $alias_widget_sidebar ) )
return false;
// Get sidebar params
$sidebar = $wp_registered_sidebars[ $alias_widget_sidebar ];
$params = array_merge(
array( array_merge( $sidebar, array('widget_id' => $alias_widget_id, 'widget_name' => $wp_registered_widgets[ $alias_widget_id ]['name'] ) ) ),
(array) $wp_registered_widgets[ $alias_widget_id ]['params']
);
// Add appropriate class name
$classname_ = '';
foreach ( (array) $wp_registered_widgets[ $alias_widget_id ]['classname'] as $cn ) {
if ( is_string($cn) )
$classname_ .= '_' . $cn;
elseif ( is_object($cn) )
$classname_ .= '_' . get_class($cn);
}
$classname_ = ltrim($classname_, '_') . ' widget-alias';
$params[0]['before_widget'] = sprintf( $params[0]['before_widget'], $alias_widget_id, $classname_ );
// Add widget alias identifier for use in later title filter
$params[0]['class'] = 'widget-alias ' . $params[0]['class'];
// Apply filters
$params = apply_filters( 'dynamic_sidebar_params', $params );
// Run actions
do_action( 'dynamic_sidebar', $alias_widget );
// Modify title if override title is set
if ( !empty( $instance['title'] ) ) {
$this->override_title = $instance['title'];
$this->alias_id = $alias_widget_id;
add_filter( 'widget_display_callback', array( $this, 'output_override_title'), 99, 3 );
}
// Do actual widget
if ( is_callable( $callback ) )
call_user_func_array( $callback, $params );
return true;
}
/**
* Override the aliased widgets
*
* @package Widget Alias
* @since 1.0
*
* @param array $instance settings for this particular instance of the widget
* @param array $widget the widget object
* @param array $args widget container args
*
* @return $instance
*/
function output_override_title( $instance, $widget, $args ) {
// Test if widget has specail 'widget-alias' class $arg and if the widget's id matches the alias id
if( false !== strpos( $args['class'], 'widget') && $this->alias_id == $widget->id )
$instance['title'] = $this->override_title;
return $instance;
}
/**
* Output widget based on shortcode
*
* Example usage: [wa id="widget-id" title="Override Title"]
*
* @package Widget Alias
* @since 1.0
*
* @param array $atts shortcode attributes
*/
function wa_shortcode( $atts ) {
extract( shortcode_atts( array(
'id' => '',
'title' => '',
), $atts ) );
// Output widget with shortcode atts
if ( !empty( $id ) ) {
$instance = array(
'alias-widget-id' => $id,
'title' => $title,
);
// Capture and return widget output
ob_start();
$this->widget( '', $instance );
return ob_get_clean();
}
}
}