XPressME Integration Kit

Trac

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

Last change on this file since 339 was 339, checked in by toemon, 15 years ago

ウィジェットブロックでウィジェットを表示したとき、ウィジェットのタイトルを非表示にするオプション追加 fixed #182
ウィジェットのタイトルの非表示はブロックで1つだけのウィジェットを選択した場合のみ有効

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    $selected = array_slice($options,3); // get allowed cats
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        }
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;     
33        return $block ;
34}
35
36function 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        }
50        if (!function_exists('wp_get_sidebars_widgets')) {
51                echo 'Not support sidebar widget';
52                return;
53        }
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?>
Note: See TracBrowser for help on using the repository browser.