/
voce-post-meta-widgets.php
370 lines (347 loc) · 10.9 KB
/
voce-post-meta-widgets.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
366
367
368
369
370
<?php
/*
Plugin Name: Voce Post Meta Widgets
Plugin URI: http://vocecommunications.com
Description: Extend Voce Post Meta with widget fields
Version: 1.0.1
Author: matstars, markparolisi, banderon, voceplatforms
Author URI: http://vocecommunications.com
License: GPL2
*/
/**
*
* @class Voce_Post_Meta_Widgets
* Class notes:
* Filters:
* voce_post_meta_widgets_widget_choices_location - location of metabox for widget choices (defaults to side)
* voce_post_meta_widgets_widget_choices_priority - priority of metabox for widget choices (defaults to low)
*
*/
class Voce_Post_Meta_Widgets {
/**
* Prefix for the sidebar name
* e.g. WIDGET_ID_PREFIX . meta_name . _post_id_ . $post_id
*/
const WIDGET_ID_PREFIX = "voce_post_meta_widgets_";
/**
* Holds all of the custom sidebar data
*/
const SIDEBAR_OPTION_NAME = "voce_post_meta_sidebars";
/**
* @method initialize
*/
public static function initialize() {
self::check_voce_meta_api();
}
/**
* Check if Voce Post Meta is loaded
* If it is, bootstrap the plugin
* @method check_voce_meta_api
* @return void
*/
public static function check_voce_meta_api() {
if ( class_exists( 'Voce_Meta_API' ) ) {
self::bootstrap();
} else {
add_action( 'admin_notices', function () {
printf( '<div class="error"><p>%s</p></div>', __( 'Voce Post Meta Widgets Plugin cannot be utilized without the <a href="https://github.com/voceconnect/voce-post-meta" target="_BLANK">Voce Post Meta</a> plugin.' ) );
} );
}
}
/**
* Register all of our action and filter callbacks
*
* @method bootstrap
*/
protected static function bootstrap() {
require_once( __DIR__ . '/voce-post-meta-widgets-field.php' );
require_once( ABSPATH . '/wp-admin/includes/widgets.php' );
add_action( 'widgets_init', array( __CLASS__, 'register_sidebars' ) );
add_action( 'load-widgets.php', array( __CLASS__, 'hide_sidebars' ) );
add_filter( 'meta_type_mapping', array( __CLASS__, 'meta_type_mapping' ) );
add_action( 'add_meta_boxes', array( __CLASS__, 'action_add_meta_boxes' ), 10, 2 );
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'action_admin_enqueue_scripts' ) );
}
/**
* Register all of our custom sidebars
* @method register_sidebars
*/
public static function register_sidebars() {
$sidebars = get_option( Voce_Post_Meta_Widgets::SIDEBAR_OPTION_NAME );
if ( is_array($sidebars) )
$sidebars = array_filter($sidebars);
if ( is_array( $sidebars ) && !empty( $sidebars ) ) {
foreach ( $sidebars as $sidebar ) {
$args = array(
'name' => $sidebar,
'id' => $sidebar,
'description' => 'Drag & Drop Widgets from the Right Sidebar to Below',
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>'
);
register_sidebar( $args );
}
}
}
/**
*
* Set the callbacks for the display and save methods on Voce Post Meta API
* @method meta_type_mapping
*
* @param type $mapping
*
* @return array
*/
public static function meta_type_mapping( $mapping ) {
$mapping[ 'widgets' ] = array(
'class' => 'Voce_Post_Meta_Widgets_Field',
'args' => array(
'display_callbacks' => array( 'voce_widgets_field_display' ),
'sanitize_callbacks' => array( 'voce_widgets_field_submit' )
)
);
return $mapping;
}
/**
* Create the metaboxes for the available widgets
*
* @param $post_type
* @param $post
*/
public static function action_add_meta_boxes( $post_type, $post ) {
global $_wp_post_type_features;
$add_metas = false;
if ( $custom_meta_groups = array_keys( array_intersect_key( $_wp_post_type_features[ $post_type ], Voce_Meta_API::GetInstance()->groups ) ) ) {
foreach ( $custom_meta_groups as $group_id ) {
if ( count( Voce_Meta_API::GetInstance()->groups[ $group_id ]->fields ) ) {
foreach ( Voce_Meta_API::GetInstance()->groups[ $group_id ]->fields as $field ) {
if ( ! empty( $field->field_type ) && 'widget' == $field->field_type ) {
$add_metas = true;
break 2;
}
}
}
}
}
if ( ! $add_metas ) {
return;
}
$location = apply_filters( 'voce_post_meta_widgets_widget_choices_location', 'side', $post_type, $post );
$priority = apply_filters( 'voce_post_meta_widgets_widget_choices_priority', 'low', $post_type, $post );
$callback = array(
__CLASS__,
'sidebar_admin_metabox'
);
add_meta_box( 'sidebar_admin', 'Sidebar Admin', $callback, $post_type, $location, $priority );
$callback = array(
__CLASS__,
'hidden_widgets_metabox'
);
add_meta_box( 'voce_widgets_hidden', 'Widgets Hidden', $callback, $post_type, $location, $priority, $post );
}
/**
* Generate HTML for meta box
*
* @method sidebar_admin_metabox
* @global Object $post
* @return void
*/
public static function sidebar_admin_metabox() {
?>
<div id="widget-list" class="voce-post-meta-widget column-1">
<strong><?php _e( 'Available Widgets' ); ?></strong>
<p class="description"><?php _e( 'Drag widgets from here to widget areas to activate them.' ); ?></p>
<?php wp_list_widgets(); ?>
</div>
<?php wp_nonce_field( 'save-sidebar-widgets', '_wpnonce_widgets', false ); ?>
<div class="clear"></div>
<?php
}
/**
* Display all of the widgets in a hidden metabox
*
* @method hidden_widgets_metabox
*/
public static function hidden_widgets_metabox( $post ) {
$widgets = wp_get_sidebars_widgets();
$sidebar_ids = array_keys( $widgets );
?>
<div class="hidden-field">
<?php
foreach ( $sidebar_ids as $sidebar ) :
if ( strpos( $sidebar, "post_id_" . $post->ID ) === false ) :
?>
<div class="sidebar widget-droppable widget-list" id="<?php echo $post->post_name; ?>_0">
<?php Voce_Post_Meta_Widgets::get_active_widgets( $sidebar ); ?>
</div>
<?php
endif;
endforeach;
?>
</div>
<?php
add_filter( 'postbox_classes_post_voce_widgets_hidden', function ( $classes ) {
return array_merge( $classes, array( 'hidden-field' ) );
} );
}
/**
* Hide Custom Sidebars on Widgets.php
*
* @method hide_sidebars
* @return void
*/
public static function hide_sidebars() {
global $pagenow, $wp_registered_sidebars;
if ( is_admin() && 'widgets.php' === $pagenow ) {
foreach ( $wp_registered_sidebars as $sidebar ) {
if ( false !== strpos( $sidebar[ 'id' ], self::WIDGET_ID_PREFIX ) ) {
unregister_sidebar( $sidebar[ 'id' ] );
}
}
}
}
/**
*
* Enqueue admin JavaScripts and CSS
*
* Filters:
* voce_post_meta_widgets_scripts - filter what pages the scripts/styles needs (defaults to 'post-new.php' and 'post.php')
*
* @method action_admin_enqueue_scripts
* @param type $hook
*
* @return void
*/
public static function action_admin_enqueue_scripts( $hook ) {
$pages = apply_filters( 'voce_post_meta_widgets_scripts', array( 'post-new.php', 'post.php', 'edit.php' ) );
if ( in_array( $hook, $pages ) ) {
wp_enqueue_script( 'voce-post-meta-widgets', self::plugins_url( '/js/voce-post-meta-widgets.min.js', __FILE__ ), array(
'jquery-ui-sortable',
'jquery-ui-draggable',
'jquery-ui-droppable'
) );
wp_enqueue_style( 'voce-post-meta-widgets', self::plugins_url( '/css/voce-post-meta-widgets.min.css', __FILE__ ) );
}
}
/**
*
* Allow this plugin to live either in the plugins directory or inside
* the themes directory.
*
* @method plugins_url
* @param type $relative_path
* @param type $plugin_path
*
* @return string
*/
public static function plugins_url( $relative_path, $plugin_path ) {
$template_dir = get_template_directory();
foreach ( array( 'template_dir', 'plugin_path' ) as $var ) {
$$var = str_replace( '\\', '/', $$var ); // sanitize for Win32 installs
$$var = preg_replace( '|/+|', '/', $$var );
}
if ( 0 === strpos( $plugin_path, $template_dir ) ) {
$url = get_template_directory_uri();
$folder = str_replace( $template_dir, '', dirname( $plugin_path ) );
if ( '.' != $folder ) {
$url .= '/' . ltrim( $folder, '/' );
}
if ( !empty( $relative_path ) && is_string( $relative_path ) && strpos( $relative_path, '..' ) === false ) {
$url .= '/' . ltrim( $relative_path, '/' );
}
return $url;
} else {
return plugins_url( $relative_path, $plugin_path );
}
}
/**
*
* Clean sidebars, removes any sidebars that are empty
*
* @method clean_sidebars
* @param type $plugin_sidebars
*
* @return array
*
*/
public static function clean_sidebars( $plugin_sidebars ) {
if ( !empty( $plugin_sidebars ) ) {
$all_widgets = wp_get_sidebars_widgets();
foreach ( $all_widgets as $sidebar_name => $widgets ) {
if ( in_array( $sidebar_name, $plugin_sidebars ) && empty( $widgets ) ) {
unset( $plugin_sidebars[ $sidebar_name ] );
}
}
}
return $plugin_sidebars;
}
/**
*
* Get Active Widgets, renders widget list
*
* @method get_active_widgets
* @param type $sidebar
*
* @return void
*
*/
public static function get_active_widgets( $sidebar ) {
wp_list_widget_controls( $sidebar );
}
}
/**
*
* @method voce_widgets_field_display
* @param type $field
* @param type $value
* @param type $post_id
*
* @return void
*/
if ( !function_exists( 'voce_widgets_field_display' ) ) {
function voce_widgets_field_display( $field, $value, $post_id ) {
$value_post = get_post( $value );
$sidebar_id = Voce_Post_Meta_Widgets::WIDGET_ID_PREFIX . $field->get_input_id() . '_post_id_' . $post_id;
?>
<input type="hidden" id="voce-post-widgets-exist" value="true"/>
<div class="column-2 voce-post-meta-widget-drop">
<?php voce_field_label_display( $field ); ?>
<div class="sidebar voce-post-meta-widget-wrap">
<div class="column-2-widgets">
<div class="sidebar widget-droppable widget-list" id="<?php echo $value_post->post_name; ?>_0">
<?php Voce_Post_Meta_Widgets::get_active_widgets( $sidebar_id ); ?>
</div>
</div>
</div>
<?php echo !empty( $field->description ) ? ('<span class="description">' . wp_kses( $field->description, Voce_Meta_API::GetInstance()->description_allowed_html ) . '</span>') : ''; ?>
</div>
<?php
}
}
/**
* Callback before post is submitted
*
* @method voce_widgets_field_submit
* @param type $field
* @param type $value
* @param type $post_id
*
* @return void
*
*/
if ( !function_exists( 'voce_widgets_field_submit' ) ) {
function voce_widgets_field_submit( $field, $value, $post_id ) {
if ( empty( $post_id ) ) {
global $post;
$post_id = $post->ID;
}
$sidebar_id = Voce_Post_Meta_Widgets::WIDGET_ID_PREFIX . $field->get_input_id() . '_post_id_' . $post_id;
$sidebars = get_option( Voce_Post_Meta_Widgets::SIDEBAR_OPTION_NAME );
$sidebars = array_unique(array_merge( (array) $sidebars, array( $sidebar_id ) ));
$sidebars = Voce_Post_Meta_Widgets::clean_sidebars( $sidebars );
update_option( Voce_Post_Meta_Widgets::SIDEBAR_OPTION_NAME, $sidebars );
}
}
Voce_Post_Meta_Widgets::initialize();