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