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
|
<?php /** * Additional sidebars */
class Penci_Custom_Sidebar{
protected static $initialized = false;
public static function initialize() { if ( self::$initialized ) { return; }
add_action( 'wp_ajax_pennews_add_sidebar' , array( __CLASS__, 'add_sidebar' ) ); add_action( 'wp_ajax_pennews_remove_sidebar', array( __CLASS__, 'remove_sidebar' ) );
add_action( 'init' , array( __CLASS__, 'register_sidebars' ) ); add_action( 'sidebar_admin_page', array( __CLASS__, 'admin_page' ) );
self::$initialized = true; }
/** * Register sidebars */ public static function register_sidebars() {
if( is_page_template( 'page-templates/full-width.php' ) ) { return; }
$sidebars = get_option( 'pennews_custom_sidebars' );
if( empty( $sidebars ) ) { return; }
foreach ( ( array )$sidebars as $id => $sidebar ) { if ( ! isset( $sidebar['id'] ) ) { $sidebar['id'] = $id; }
$sidebar['before_widget'] = self::get_before_widget();
register_sidebar( $sidebar ); } }
public static function get_before_widget(){ $class_before_widget = ' penci-block-vc penci-widget-sidebar'; $class_before_widget .= ' ' . penci_get_setting( 'penci_style_block_title' ); $class_before_widget .= ' ' . penci_get_setting( 'penci_align_blocktitle' ); $before_widget = '<div id="%1$s" class="widget ' . $class_before_widget . ' %2$s">';
return $before_widget; }
/** * Add sidebar */ public static function add_sidebar() {
$name = isset( $_POST['_nameval'] ) ? $_POST['_nameval'] : ''; $nonce = isset( $_POST['_wpnonce'] ) ? $_POST['_wpnonce'] : '';
if ( empty( $nonce ) ) { wp_send_json_error( esc_html__( 'Invalid request.', 'pennews' ) ); } elseif ( empty( $name ) ) { wp_send_json_error( esc_html__( 'Missing sidebar name.', 'pennews' ) ); }
if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) ) { wp_send_json_error( esc_html__( 'Nonce verification fails.', 'pennews' ) ); }
// Get custom sidebars. $sidebars = get_option( 'pennews_custom_sidebars', array() ); $sidebar_num = get_option( 'pennews_custom_sidebars_lastid', - 1 );
if ( $sidebar_num < 0 ) { $sidebar_num = 0; if ( is_array( $sidebars ) ) { $key_sidebars = explode( '-', end( array_keys( $sidebars ) ) ); $sidebar_num = ( int ) end( $key_sidebars ); } }
update_option( 'pennews_custom_sidebars_lastid', ++ $sidebar_num );
$before_title = '<div class="penci-block-heading"><h4 class="widget-title penci-block__title"><span>'; $after_title = '</span></h4></div>';
$sidebars[ 'pennews-custom-sidebar-' . $sidebar_num ] = array( 'class' => 'pennews-custom-sidebar', 'id' => 'pennews-custom-sidebar-' . $sidebar_num, 'name' => stripcslashes( $name ), 'description' => '', 'before_widget' => self::get_before_widget(), 'after_widget' => '</div>', 'before_title' => $before_title, 'after_title' => $after_title );
update_option( 'pennews_custom_sidebars', $sidebars );
if ( ! function_exists( 'wp_list_widget_controls' ) ) { include_once ABSPATH . 'wp-admin/includes/widgets.php'; }
ob_start(); ?> <div class="widgets-holder-wrap pennews-custom-sidebar closed"> <?php wp_list_widget_controls( 'pennews-custom-sidebar-' . $sidebar_num, stripcslashes( $name ) ); ?> </div> <?php $output = ob_get_clean();
wp_send_json_success( $output ); }
/** * Remove sidebar */ public static function remove_sidebar(){
$idSidebar = isset( $_POST['idSidebar'] ) ? $_POST['idSidebar'] : ''; $nonce = isset( $_POST['_wpnonce'] ) ? $_POST['_wpnonce'] : '';
if ( empty( $nonce ) ) { wp_send_json_error( esc_html__( 'Invalid request.', 'pennews' ) ); } elseif ( empty( $idSidebar ) ) { wp_send_json_error( esc_html__( 'Missing sidebar ID', 'pennews' ) ); }
if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) ) { wp_send_json_error( esc_html__( 'Nonce verification fails.', 'pennews' ) ); }
$custom_sidebars = get_option( 'pennews_custom_sidebars', array() );
unset( $custom_sidebars[ $idSidebar ] );
update_option( 'pennews_custom_sidebars', $custom_sidebars );
wp_send_json_success(); }
/** * Print HTML code to manage custom sidebar */ public static function admin_page() { global $wp_registered_sidebars; ?> <div class="widgets-holder-wrap"> <div id="penci-add-custom-sidebar" class="widgets-sortables"> <div class="sidebar-name"> <div class="sidebar-name-arrow"><br></div> <h2> <?php esc_html_e( 'Add New Sidebar', 'pennews' ); ?> <span class="spinner"></span> </h2> </div> <div class="sidebar-description"> <form class="description" method="POST" action=""> <?php wp_nonce_field( 'pennews_add_sidebar' ); ?> <table class="form-table"> <tr valign="top"> <td> <input id="penci-add-custom-sidebar-name" style="width: 100%;" type="text" class="text" name="name" value="" placeholder="<?php esc_attr_e( 'Enter sidebar name', 'pennews' ) ?>"> </td> <td> <input type="submit" class="button-primary" value="<?php esc_attr_e( 'Add', 'pennews' ) ?>"> </td> </tr> </table> </form> </div> </div> </div> <style type="text/css" media="screen"> .pennews-remove-custom-sidebar .notice-dismiss { right: 30px; top: 3px; } </style> <?php }
public static function get_list_sidebar( $pos = 'left' ) { $custom_sidebars = get_option( 'pennews_custom_sidebars' );
$list_sidebar = array( 'sidebar-1' => esc_html__( 'Sidebar Right', 'pennews' ), 'sidebar-2' => esc_html__( 'Sidebar Left', 'pennews' ), 'footer-1' => esc_html__( 'Footer Column #1', 'pennews' ), 'footer-2' => esc_html__( 'Footer Column #2', 'pennews' ), 'footer-3' => esc_html__( 'Footer Column #3', 'pennews' ), 'footer-4' => esc_html__( 'Footer Column #4', 'pennews' ), );
if ( class_exists( 'WooCommerce' ) ) { $list_sidebar['penci-shop-sidebar'] = esc_html__( 'Sidebar For Shop', 'pennews' ); }
if ( class_exists( 'Penci_Portfolio' ) ) { $list_sidebar['penci-portfolio-sidebar-left'] = esc_html__( 'Portfolio Sidebar Left', 'pennews' ); $list_sidebar['penci-portfolio-sidebar-right'] = esc_html__( 'Portfolio Sidebar Right', 'pennews' ); }
if ( class_exists( 'bbPress' ) ) { $list_sidebar['penci-bbpress'] = esc_html__( 'Sidebar bbPress', 'pennews' ); }
if ( class_exists( 'BuddyPress' ) ) { $list_sidebar['penci-buddypress'] = esc_html__( 'Sidebar BuddyPress', 'pennews' ); }
if ( class_exists( 'Tribe__Events__Main' ) ) { $list_sidebar['penci-event'] = esc_html__( 'Sidebar Events', 'pennews' ); }
if ( class_exists( 'Penci_Instagram' ) ) { $list_sidebar['footer-instagram'] = esc_html__( 'Footer Instagram', 'pennews' ); }
if ( empty( $custom_sidebars ) || ! is_array( $custom_sidebars ) ) { return $list_sidebar; }
foreach ( $custom_sidebars as $sidebar_id => $custom_sidebar ) {
if ( empty( $custom_sidebar['name'] ) ) { continue; } $list_sidebar[ $sidebar_id ] = $custom_sidebar['name']; }
return $list_sidebar; } }
|