-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-cms-widget-control.php
232 lines (170 loc) · 5.03 KB
/
wp-cms-widget-control.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
<?php
/*
Plugin Name: WP-CMS Widget Control
Plugin URI: http://wp-cms.com
Description: Remove any core, theme or plugin widget from the Widgets setting screen.
Author: Jonny Allbut
Version: 1.21
Author URI: http://jonnya.net
*/
/*
///////// VERSION HISTORY
1.21 - WordPress 4.4 full compatiblity check and text tidying
1.2 - WordPress 4.3.x fix compatibility (also Customizer support)
1.1 - Minor tweaks, fix translation text domain 'wpcms-widget-setting'
1.0 - First release
*/
/**
*
* Execute plugin
*
*/
function wpcms_wc_admin_do() {
$wpcms_wc_admin_do = ( is_admin() || is_customize_preview() ) ? new wpcms_widget_control : '';
}
add_action( 'after_setup_theme','wpcms_wc_admin_do', 11 );
/**
*
* All the widget control functionality
*
*/
class wpcms_widget_control {
var $plugin; /* Common plugin definition */
var $widget_data; /* Saved options */
var $widgets_available; /* Holds master array of all core WP widget data */
function __construct() {
// Setup data
$this->plugin = 'wpcms-widget-setting';
$this->widget_data = get_option( $this->plugin );
$this->widgets_available = ( isset($GLOBALS['wp_widget_factory']) ) ? $GLOBALS['wp_widget_factory'] : '';
// Do it
add_action( 'admin_init', array($this,'settings_init') );
add_action( 'admin_menu', array($this,'admin_menu') );
add_action( 'widgets_init', array($this,'unregister_widgets'), 11 );
}
/**
*
* Returns a cleaner array of widget data for use
*
*/
function get_widgets() {
$clean_array = '';
if ( is_array($this->widgets_available->widgets) && !empty($this->widgets_available->widgets) ){
foreach ( $this->widgets_available->widgets as $key => $value ) {
$clean_array[$key]['nice_name'] = $value->name;
$clean_array[$key]['description'] = $value->widget_options['description'];
}
}
return($clean_array);
}
/**
*
* Adds admin menu
*
*/
function admin_menu() {
add_options_page(
esc_html__( 'Widget Control', 'wpcms-widget-setting' ),
esc_html__( 'Widget Control', 'wpcms-widget-setting' ),
'manage_options',
$this->plugin,
array( $this,'admin_page' )
);
}
/**
*
* Renders admin menu
*
*/
function admin_page(){
global $pagenow;
echo '<div class="wrap">';
echo '<h2>' . esc_html__( 'WP-CMS Widget Control', 'wpcms-widget-setting' ) . '</h2>';
echo '<form action="options.php" method="POST">';
submit_button( esc_html__('Update widget settings', 'wpcms-widget-setting') );
settings_fields( $this->plugin . 'group1' );
do_settings_sections( $this->plugin . 'section1' );
submit_button( esc_html__('Update widget settings', 'wpcms-widget-setting' ) );
echo '</form>';
echo '</div>';
}
/**
*
* WP Settings API init
*
*/
function settings_init() {
register_setting( $this->plugin . 'group1', $this->plugin, array($this,'sanitize_data') );
add_settings_section( 'form-section-one', null, array($this,'section_one_callback'), $this->plugin . 'section1' );
$widgets = $this->get_widgets();
if ( !empty($widgets) ){
foreach ($widgets as $key => $value) {
add_settings_field(
$this->plugin . '[' . $key . ']',
$value['nice_name'],
array($this,'field_builder'),
$this->plugin . 'section1',
'form-section-one',
array(
'name' => $value['nice_name'],
'desc' => ( !empty($value['description']) ) ? $value['description'] : '',
'def' => $key
)
);
}
}
}
/**
*
* Settings section intro
*
*/
function section_one_callback() {
echo '<p>';
esc_html_e( 'Tick the relevant checkbox below to REMOVE the selected widget from the widget admin area and theme customizer.', 'wpcms-widget-setting' );
echo '</p>';
echo '<p><strong>';
esc_html_e( 'WARNING - DO NOT REMOVE WIDGETS YOU ARE CURRENTLY USING - you risk deleting them (and their settings) from your site - so PLEASE use this with caution!', 'wpcms-widget-setting' );
echo '</strong></p>';
}
/**
*
* Common settings field builder
*
*/
function field_builder($args) {
$data = ( !empty($this->widget_data) && array_key_exists($args['def'], $this->widget_data) ) ? $this->widget_data[$args['def']] : false;
echo '<input name="' . $this->plugin . '[' . $args['def'] . ']" ';
echo 'id="' . esc_attr($args['desc'] ) . '[' . $args['def'] . '" ';
echo 'type="checkbox" value="1" ' . checked( 1, $data, false ) . ' /> ';
echo '<span class="description">' . esc_html($args['desc'] ) . '</span>';
}
/**
*
* Sanitize input before saving like any good plugin should!
*
*/
function sanitize_data($input) {
$clean_data = array();
if ( !empty($input) && is_array($input) ){
foreach ( $input as $key => $value ) {
$clean_data[$key] = ( $value == 1 ) ? $value : 0;
}
}
return $clean_data;
}
/**
*
* Remove those pesky widgets
*
*/
function unregister_widgets() {
global $pagenow;
if ( (is_customize_preview() || $pagenow == 'widgets.php') && is_array($this->widget_data) ){
foreach ( $this->widget_data as $widget => $value ) {
unregister_widget($widget);
}
}
}
}
?>