-
Notifications
You must be signed in to change notification settings - Fork 1
/
class-simple-sidebars.php
executable file
·365 lines (303 loc) · 9.43 KB
/
class-simple-sidebars.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
/**
* Plugin Name.
*
* @package Simple_Sidebars
* @author Dovy Paukstys <[email protected]>
* @license GPL-3.0+
* @link http://simplerain.com
* @copyright 2013 SimpleRain
*/
/**
* Plugin class.
*
* TODO: Rename this class to a proper name for your plugin.
*
* @package Simple_Sidebars
* @author Dovy Paukstys <[email protected]>
*/
class Simple_Sidebars {
/**
* Plugin version, used for cache-busting of style and script file references.
*
* @since 1.0.0
*
* @var string
*/
protected $version = '1.0.0';
/**
* Unique identifier for your plugin.
*
* Use this value (not the variable name) as the text domain when internationalizing strings of text. It should
* match the Text Domain file header in the main plugin file.
*
* @since 1.0.0
*
* @var string
*/
protected $plugin_slug = 'simple-sidebars';
/**
* Instance of this class.
*
* @since 1.0.0
*
* @var object
*/
protected static $instance = null;
/**
* Slug of the plugin screen.
*
* @since 1.0.0
*
* @var string
*/
protected $plugin_screen_hook_suffix = null;
/**
* Array of enabled sidebars
*
* @since 1.0.0
*
* @var array
*/
protected $sidebars = array();
/**
* Initialize the plugin by setting localization, filters, and administration functions.
*
* @since 1.0.0
*/
private function __construct() {
// Load plugin text domain
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
// Additional hooks and items.
add_action('load-widgets.php', array(&$this, 'load_assets') , 5 );
add_action('widgets_init', array(&$this, 'register_custom_sidebars') , 1000 );
add_action('wp_ajax_delete_simple_sidebar', array(&$this, 'delete_sidebar_area') , 1000 );
$this->title = __('Create Custom Widget', $this->plugin_slug);
}
/**
* Function to create the HTML used to create sidebars.
*
* @since 1.0.0
*
*/
public function add_new_widget_area_box() {
$nonce = wp_create_nonce ('delete-simple-sidebar-nonce');
?>
<script type="text/html" id="simple-add-widget-template">
<input type="hidden" name="simple-nonce" value="<?php echo $nonce ?>" />
<div id="simple-add-widget" class="widgets-holder-wrap">
<div class="sidebar-name" style="cursor: inherit;">
<h3><?php echo $this->title; ?> <span class="spinner"></span></h3>
</div>
<div id="jumbotron" class="widgets-sortables ui-sortable" style="min-height: 50px;">
<form action="" method="post">
<div class="widget-content">
<p style="font-weight: bold;"><label for="simple-add-widget-input"><?php echo __('New Widget Name', $this->plugin_slug); ?>:</label>
<input id="simple-add-widget-input" name="simple-add-widget-input" type="text" class="regular-text" title="<?php echo __('New Widget Name', $this->plugin_slug); ?>" />
</div>
<div class="widget-control-actions">
<div class="aligncenter">
<input class="button-primary" type="submit" value="<?php echo __('Create New Widget', $this->plugin_slug); ?>" />
</div>
<br class="clear">
</div>
</form>
</div>
</div>
</script>
<?php
}
/**
* Function to add all needed assets when used.
*
* @since 1.0.0
*
*/
function load_assets() {
add_action('admin_print_scripts', array(&$this, 'add_new_widget_area_box') );
add_action('load-widgets.php', array(&$this, 'add_sidebar_area'), 100);
add_action('load-widgets.php', array(&$this, 'enqueue_admin_styles'), 100);
add_action('load-widgets.php', array(&$this, 'enqueue_admin_scripts'), 100);
}
/**
* Function to create a new sidebar
*
* @since 1.0.0
*
* @param string Name of the sidebar to be deleted.
*
* @return string 'sidebar-deleted' if successful.
*
*/
function add_sidebar_area() {
if(!empty($_POST['simple-add-widget-input'])) {
$this->sidebars = get_theme_mod($this->plugin_slug);
$name = $this->check_sidebar_name($_POST['simple-add-widget-input']);
if(empty($this->sidebars)) {
$this->sidebars = array($name);
} else {
$this->sidebars = array_merge($this->sidebars, array($name));
}
set_theme_mod($this->plugin_slug, $this->sidebars);
wp_redirect( admin_url('widgets.php') );
die();
}
}
/**
* Before we create a new sidebar, verify it doesn't already exist. If it does, append a number to the name.
*
* @since 1.0.0
*
* @param string Name of the sidebar to be deleted.
*
* @return string 'sidebar-deleted' if successful.
*
*/
function delete_sidebar_area() {
check_ajax_referer('delete-simple-sidebar-nonce');
if(!empty($_POST['name'])) {
$name = stripslashes($_POST['name']);
$this->sidebars = get_theme_mod($this->plugin_slug);
print_r($this->sidebars);
if(($key = array_search($name, $this->sidebars)) !== false) {
unset($this->sidebars[$key]);
set_theme_mod($this->plugin_slug, $this->sidebars);
echo "sidebar-deleted";
}
}
die();
}
/**
* Before we create a new sidebar, verify it doesn't already exist. If it does, append a number to the name.
*
* @since 1.0.0
*
* @param string $name Name of the sidebar to be created.
*
* @return name $name Name of the new sidebar just created.
*
*/
function check_sidebar_name($name) {
if(empty($GLOBALS['wp_registered_sidebars']))
return $name;
$taken = array();
foreach ( $GLOBALS['wp_registered_sidebars'] as $sidebar ) {
$taken[] = $sidebar['name'];
}
if(empty($this->sidebars))
$this->sidebars = array();
$taken = array_merge($taken, $this->sidebars);
if(in_array($name, $taken)) {
$counter = substr($name, -1);
$new_name = "";
if(!is_numeric($counter)) {
$new_name = $name . " 1";
} else {
$new_name = substr($name, 0, -1) . ((int) $counter + 1);
}
$name = $this->check_sidebar_name($new_name);
}
return $name;
}
/**
* Register and display the custom sidebar areas we have set.
*
* @since 1.0.0
*
*/
function register_custom_sidebars() {
if(empty($this->sidebars)) $this->sidebars = get_theme_mod($this->plugin_slug);
$options = array(
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>',
'before_widget' => '<div id="%1$s" class="widget clearfix %2$s">',
'after_widget' => '</div>'
);
$options = apply_filters('simple_custom_widget_args', $options);
if(is_array($this->sidebars)) {
foreach ($this->sidebars as $sidebar) {
$options['class'] = 'simple-custom';
$options['name'] = $sidebar;
register_sidebar($options);
}
}
}
/**
* Return the sidebars array.
*
* @since 1.0.0
*
* @return array If not empty, active simple sidebars are returned.
*/
public static function get_simple_sidebars() {
// If the single instance hasn't been set, set it now.
if ( !empty($this->sidebars) ) {
return $this->sidebars;
}
}
/**
* Return an instance of this class.
*
* @since 1.0.0
*
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Fired when the plugin is activated.
*
* @since 1.0.0
*
* @param boolean $network_wide True if WPMU superadmin uses "Network Activate" action, false if WPMU is disabled or plugin is activated on an individual blog.
*/
public static function activate( $network_wide ) {
// TODO: Define activation functionality here
}
/**
* Fired when the plugin is deactivated.
*
* @since 1.0.0
*
* @param boolean $network_wide True if WPMU superadmin uses "Network Deactivate" action, false if WPMU is disabled or plugin is deactivated on an individual blog.
*/
public static function deactivate( $network_wide ) {
// TODO: Define deactivation functionality here
}
/**
* Load the plugin text domain for translation.
*
* @since 1.0.0
*/
public function load_plugin_textdomain() {
$domain = $this->plugin_slug;
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
load_textdomain( $domain, WP_LANG_DIR . '/' . $domain . '/' . $domain . '-' . $locale . '.mo' );
load_plugin_textdomain( $domain, FALSE, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
}
/**
* Register and enqueue admin-specific style sheet.
*
* @since 1.0.0
*
* @return null Return early if no settings page is registered.
*/
public function enqueue_admin_styles() {
wp_enqueue_style( 'Simple_Sidebars', plugins_url( 'css/admin.css', __FILE__ ), array(), $this->version );
}
/**
* Register and enqueue admin-specific JavaScript.
*
* @since 1.0.0
*
* @return null Return early if no settings page is registered.
*/
public function enqueue_admin_scripts() {
wp_enqueue_script( 'Simple_Sidebars', plugins_url( 'js/admin.js', __FILE__ ), array( 'jquery' ), $this->version );
}
}