XPressME Integration Kit

Trac

source: trunk/xpressme_integration_kit/wp-content/themes/xpress_default/blocks/widget_block_theme.php @ 437

Last change on this file since 437 was 437, checked in by toemon, 14 years ago

ブロックオプション、 ウィジェットをカンマ区切りデータとして格納するようにした。 #243

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