[136] | 1 | <?php
|
---|
| 2 | function widget_block($options)
|
---|
| 3 | {
|
---|
| 4 | $mydirname = empty( $options[0] ) ? 'xpress' : $options[0] ;
|
---|
| 5 | $this_template = empty( $options[1] ) ? 'db:'.$mydirname.'_block_widget.html' : trim( $options[1] );
|
---|
[339] | 6 | $title_show = empty( $options[2] ) ? false : true ;
|
---|
| 7 | $selected = array_slice($options,3); // get allowed cats
|
---|
[136] | 8 |
|
---|
| 9 | $output = '';
|
---|
| 10 | foreach($selected as $select){
|
---|
| 11 | $ex = explode('::',$select);
|
---|
| 12 | $sidebar_id = $ex[0];
|
---|
| 13 | $widget_id = $ex[1] ;
|
---|
| 14 |
|
---|
| 15 | ob_start();
|
---|
| 16 | render_widget($sidebar_id,$widget_id);
|
---|
| 17 | $output .= ob_get_contents();
|
---|
| 18 | ob_end_clean();
|
---|
| 19 | }
|
---|
[339] | 20 | if (count($selected) > 1){
|
---|
| 21 | $output = "<ul>\n" . $output . "\n</ul>\n";
|
---|
| 22 | } else {
|
---|
| 23 | if(!$title_show){
|
---|
| 24 | $del_pattern = '<[^>]*class\s*=\s*[\'|"]widgettitle[\'|"]\s*>[^<]*<\/[^>]*>';
|
---|
| 25 | $output = preg_replace('/' . $del_pattern . '/', '', $output);
|
---|
| 26 | }
|
---|
| 27 | if (preg_match('/^<li[^>]*>.*<\/li>$/s',$output)){
|
---|
| 28 | $output = preg_replace('/^<li[^>]*>/', '', $output);
|
---|
| 29 | $output = preg_replace('/<\/li>$/', '', $output);
|
---|
| 30 | }
|
---|
| 31 | }
|
---|
| 32 | $block['widget'] = $output;
|
---|
[136] | 33 | return $block ;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | function render_widget($index = 1, $widget_id) {
|
---|
| 37 | global $wp_registered_sidebars, $wp_registered_widgets;
|
---|
| 38 |
|
---|
| 39 | if ( is_int($index) ) {
|
---|
| 40 | $index = "sidebar-$index";
|
---|
| 41 | } else {
|
---|
| 42 | $index = sanitize_title($index);
|
---|
| 43 | foreach ( (array) $wp_registered_sidebars as $key => $value ) {
|
---|
| 44 | if ( sanitize_title($value['name']) == $index ) {
|
---|
| 45 | $index = $key;
|
---|
| 46 | break;
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
[252] | 50 | if (!function_exists('wp_get_sidebars_widgets')) {
|
---|
| 51 | echo 'Not support sidebar widget';
|
---|
| 52 | return;
|
---|
| 53 | }
|
---|
[136] | 54 | $sidebars_widgets = wp_get_sidebars_widgets();
|
---|
| 55 | $registered_sidebars = $wp_registered_sidebars[$index];
|
---|
| 56 | $key_exists = array_key_exists($index, $sidebars_widgets);
|
---|
| 57 | $is_array = is_array($sidebars_widgets[$index]);
|
---|
| 58 |
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | if ( empty($registered_sidebars) || !$key_exists || !$is_array || empty($sidebars_widgets[$index]) )
|
---|
| 62 | return false;
|
---|
| 63 |
|
---|
| 64 | $sidebar = $wp_registered_sidebars[$index];
|
---|
| 65 |
|
---|
| 66 | $did_one = false;
|
---|
| 67 | foreach ( (array) $sidebars_widgets[$index] as $id ) {
|
---|
| 68 | if ($id != $widget_id) continue;
|
---|
| 69 | $params = array_merge(
|
---|
| 70 | array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ) ),
|
---|
| 71 | (array) $wp_registered_widgets[$id]['params']
|
---|
| 72 | );
|
---|
| 73 |
|
---|
| 74 | // Substitute HTML id and class attributes into before_widget
|
---|
| 75 | $classname_ = '';
|
---|
| 76 | foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) {
|
---|
| 77 | if ( is_string($cn) )
|
---|
| 78 | $classname_ .= '_' . $cn;
|
---|
| 79 | elseif ( is_object($cn) )
|
---|
| 80 | $classname_ .= '_' . get_class($cn);
|
---|
| 81 | }
|
---|
| 82 | $classname_ = ltrim($classname_, '_');
|
---|
| 83 | $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);
|
---|
| 84 |
|
---|
| 85 | $params = apply_filters( 'dynamic_sidebar_params', $params );
|
---|
| 86 |
|
---|
| 87 | $callback = $wp_registered_widgets[$id]['callback'];
|
---|
| 88 |
|
---|
| 89 | if ( is_callable($callback) ) {
|
---|
| 90 | call_user_func_array($callback, $params);
|
---|
| 91 | $did_one = true;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | return $did_one;
|
---|
| 96 | }
|
---|
| 97 | ?> |
---|