XPressME Integration Kit

Trac

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

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

ブロックのバージョン管理実装 Fixes #246

File size: 3.0 KB
Line 
1<?php
2// Block Version: 1.0
3function 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        return $block ;
37}
38
39function render_widget($index = 1, $widget_id) {
40        global $wp_registered_sidebars, $wp_registered_widgets;
41
42        if ( is_int($index) ) {
43                $index = "sidebar-$index";
44        } else {
45                $index = sanitize_title($index);
46                foreach ( (array) $wp_registered_sidebars as $key => $value ) {
47                        if ( sanitize_title($value['name']) == $index ) {
48                                $index = $key;
49                                break;
50                        }
51                }
52        }
53        if (!function_exists('wp_get_sidebars_widgets')) {
54                echo 'Not support sidebar widget';
55                return;
56        }
57        $sidebars_widgets = wp_get_sidebars_widgets();
58        $registered_sidebars = $wp_registered_sidebars[$index];
59        $key_exists = array_key_exists($index, $sidebars_widgets);
60        $is_array = is_array($sidebars_widgets[$index]);
61       
62
63       
64        if ( empty($registered_sidebars) || !$key_exists || !$is_array || empty($sidebars_widgets[$index]) )
65                return false;
66
67        $sidebar = $wp_registered_sidebars[$index];
68
69        $did_one = false;
70        foreach ( (array) $sidebars_widgets[$index] as $id ) {
71                if ($id != $widget_id) continue;
72                $params = array_merge(
73                        array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ) ),
74                        (array) $wp_registered_widgets[$id]['params']
75                );
76
77                // Substitute HTML id and class attributes into before_widget
78                $classname_ = '';
79                foreach ( (array) $wp_registered_widgets[$id]['classname'] as $cn ) {
80                        if ( is_string($cn) )
81                                $classname_ .= '_' . $cn;
82                        elseif ( is_object($cn) )
83                                $classname_ .= '_' . get_class($cn);
84                }
85                $classname_ = ltrim($classname_, '_');
86                $params[0]['before_widget'] = sprintf($params[0]['before_widget'], $id, $classname_);
87
88                $params = apply_filters( 'dynamic_sidebar_params', $params );
89
90                $callback = $wp_registered_widgets[$id]['callback'];
91
92                if ( is_callable($callback) ) {
93                        call_user_func_array($callback, $params);
94                        $did_one = true;
95                }
96        }
97
98        return $did_one;
99}
100?>
Note: See TracBrowser for help on using the repository browser.