[0] | 1 | <?php |
---|
[3731] | 2 | /** |
---|
| 3 | * @brief widgets, a plugin for Dotclear 2 |
---|
| 4 | * |
---|
| 5 | * @package Dotclear |
---|
| 6 | * @subpackage Plugins |
---|
| 7 | * |
---|
| 8 | * @copyright Olivier Meunier & Association Dotclear |
---|
| 9 | * @copyright GPL-2.0-only |
---|
| 10 | */ |
---|
| 11 | |
---|
[3730] | 12 | if (!defined('DC_RC_PATH')) {return;} |
---|
[0] | 13 | |
---|
[3730] | 14 | include dirname(__FILE__) . '/_default_widgets.php'; |
---|
| 15 | require_once dirname(__FILE__) . '/_widgets_functions.php'; |
---|
[0] | 16 | |
---|
[3730] | 17 | $core->tpl->addValue('Widgets', array('publicWidgets', 'tplWidgets')); |
---|
| 18 | $core->tpl->addBlock('Widget', array('publicWidgets', 'tplWidget')); |
---|
| 19 | $core->tpl->addBlock('IfWidgets', array('publicWidgets', 'tplIfWidgets')); |
---|
[0] | 20 | |
---|
| 21 | class publicWidgets |
---|
| 22 | { |
---|
[3730] | 23 | public static function tplWidgets($attr) |
---|
| 24 | { |
---|
| 25 | $type = isset($attr['type']) ? $attr['type'] : ''; |
---|
[936] | 26 | |
---|
[3730] | 27 | # widgets to disable |
---|
| 28 | $disable = isset($attr['disable']) ? trim($attr['disable']) : ''; |
---|
[2566] | 29 | |
---|
[3730] | 30 | if ($type == '') { |
---|
| 31 | $res = "publicWidgets::widgetsHandler('nav','" . addslashes($disable) . "');" . "\n" . |
---|
| 32 | " publicWidgets::widgetsHandler('extra','" . addslashes($disable) . "');" . "\n" . |
---|
| 33 | " publicWidgets::widgetsHandler('custom','" . addslashes($disable) . "');" . "\n"; |
---|
| 34 | } else { |
---|
| 35 | if (!in_array($type, array('nav', 'extra', 'custom'))) { |
---|
| 36 | $type = 'nav'; |
---|
| 37 | } |
---|
| 38 | $res = "publicWidgets::widgetsHandler('" . addslashes($type) . "','" . addslashes($disable) . "');"; |
---|
| 39 | } |
---|
| 40 | return '<?php ' . $res . ' ?>'; |
---|
| 41 | } |
---|
[2566] | 42 | |
---|
[3730] | 43 | public static function widgetsHandler($type, $disable = '') |
---|
| 44 | { |
---|
| 45 | $wtype = 'widgets_' . $type; |
---|
| 46 | $GLOBALS['core']->blog->settings->addNameSpace('widgets'); |
---|
| 47 | $widgets = $GLOBALS['core']->blog->settings->widgets->{$wtype}; |
---|
[2566] | 48 | |
---|
[3730] | 49 | if (!$widgets) { |
---|
| 50 | // If widgets value is empty, get defaults |
---|
| 51 | $widgets = self::defaultWidgets($type); |
---|
| 52 | } else { |
---|
| 53 | // Otherwise, load widgets |
---|
| 54 | $widgets = dcWidgets::load($widgets); |
---|
| 55 | } |
---|
[2566] | 56 | |
---|
[3730] | 57 | if ($widgets->isEmpty()) { |
---|
| 58 | // Widgets are empty, don't show anything |
---|
| 59 | return; |
---|
| 60 | } |
---|
[2566] | 61 | |
---|
[3730] | 62 | $disable = preg_split('/\s*,\s*/', $disable, -1, PREG_SPLIT_NO_EMPTY); |
---|
| 63 | $disable = array_flip($disable); |
---|
[2566] | 64 | |
---|
[3730] | 65 | foreach ($widgets->elements() as $k => $w) { |
---|
| 66 | if (isset($disable[$w->id()])) { |
---|
| 67 | continue; |
---|
| 68 | } |
---|
| 69 | echo $w->call($k); |
---|
| 70 | } |
---|
| 71 | } |
---|
[2566] | 72 | |
---|
[3730] | 73 | public static function tplIfWidgets($attr, $content) |
---|
| 74 | { |
---|
| 75 | $type = isset($attr['type']) ? $attr['type'] : ''; |
---|
[2772] | 76 | |
---|
[3730] | 77 | # widgets to disable |
---|
| 78 | $disable = isset($attr['disable']) ? trim($attr['disable']) : ''; |
---|
[2772] | 79 | |
---|
[3730] | 80 | if ($type == '') { |
---|
| 81 | $res = "publicWidgets::ifWidgetsHandler('nav','" . addslashes($disable) . "') &&" . "\n" . |
---|
| 82 | " publicWidgets::ifWidgetsHandler('extra','" . addslashes($disable) . "') &&" . "\n" . |
---|
| 83 | " publicWidgets::ifWidgetsHandler('custom','" . addslashes($disable) . "')" . "\n"; |
---|
| 84 | } else { |
---|
| 85 | if (!in_array($type, array('nav', 'extra', 'custom'))) { |
---|
| 86 | $type = 'nav'; |
---|
| 87 | } |
---|
| 88 | $res = "publicWidgets::ifWidgetsHandler('" . addslashes($type) . "','" . addslashes($disable) . "')"; |
---|
| 89 | } |
---|
| 90 | return '<?php if(' . $res . ') : ?>' . $content . '<?php endif; ?>'; |
---|
| 91 | } |
---|
[2772] | 92 | |
---|
[3730] | 93 | public static function ifWidgetsHandler($type, $disable = '') |
---|
| 94 | { |
---|
| 95 | $wtype = 'widgets_' . $type; |
---|
| 96 | $GLOBALS['core']->blog->settings->addNameSpace('widgets'); |
---|
| 97 | $widgets = $GLOBALS['core']->blog->settings->widgets->{$wtype}; |
---|
[2772] | 98 | |
---|
[3730] | 99 | if (!$widgets) { |
---|
| 100 | // If widgets value is empty, get defaults |
---|
| 101 | $widgets = self::defaultWidgets($type); |
---|
| 102 | } else { |
---|
| 103 | // Otherwise, load widgets |
---|
| 104 | $widgets = dcWidgets::load($widgets); |
---|
| 105 | } |
---|
[2772] | 106 | |
---|
[3730] | 107 | return (!$widgets->isEmpty()); |
---|
| 108 | } |
---|
[2772] | 109 | |
---|
[3730] | 110 | private static function defaultWidgets($type) |
---|
| 111 | { |
---|
| 112 | $widgets = new dcWidgets(); |
---|
| 113 | $w = new dcWidgets(); |
---|
[2566] | 114 | |
---|
[3730] | 115 | if (isset($GLOBALS['__default_widgets'][$type])) { |
---|
| 116 | $w = $GLOBALS['__default_widgets'][$type]; |
---|
| 117 | } |
---|
[2566] | 118 | |
---|
[3730] | 119 | return $w; |
---|
| 120 | } |
---|
[2566] | 121 | |
---|
[3730] | 122 | public static function tplWidget($attr, $content) |
---|
| 123 | { |
---|
| 124 | if (!isset($attr['id']) || !($GLOBALS['__widgets']->{$attr['id']} instanceof dcWidget)) { |
---|
| 125 | return; |
---|
| 126 | } |
---|
[2566] | 127 | |
---|
[3730] | 128 | # We change tpl:lang syntax, we need it |
---|
| 129 | $content = preg_replace('/\{\{tpl:lang\s+(.*?)\}\}/msu', '{tpl:lang $1}', $content); |
---|
[2566] | 130 | |
---|
[3730] | 131 | # We remove every {{tpl: |
---|
| 132 | $content = preg_replace('/\{\{tpl:.*?\}\}/msu', '', $content); |
---|
[2566] | 133 | |
---|
[3730] | 134 | return |
---|
| 135 | "<?php publicWidgets::widgetHandler('" . addslashes($attr['id']) . "','" . str_replace("'", "\\'", $content) . "'); ?>"; |
---|
| 136 | } |
---|
[2566] | 137 | |
---|
[3730] | 138 | public static function widgetHandler($id, $xml) |
---|
| 139 | { |
---|
| 140 | $widgets = &$GLOBALS['__widgets']; |
---|
[2566] | 141 | |
---|
[3730] | 142 | if (!($widgets->{$id} instanceof dcWidget)) { |
---|
| 143 | return; |
---|
| 144 | } |
---|
[2566] | 145 | |
---|
[3730] | 146 | $xml = '<?xml version="1.0" encoding="utf-8" ?><widget>' . $xml . '</widget>'; |
---|
| 147 | $xml = @simplexml_load_string($xml); |
---|
| 148 | if (!($xml instanceof SimpleXMLElement)) { |
---|
| 149 | echo "Invalid widget XML fragment"; |
---|
| 150 | return; |
---|
| 151 | } |
---|
[2566] | 152 | |
---|
[3730] | 153 | $w = clone $widgets->{$id}; |
---|
[2566] | 154 | |
---|
[3730] | 155 | foreach ($xml->setting as $e) { |
---|
| 156 | if (empty($e['name'])) { |
---|
| 157 | continue; |
---|
| 158 | } |
---|
[2566] | 159 | |
---|
[3730] | 160 | $setting = (string) $e['name']; |
---|
| 161 | if (count($e->children()) > 0) { |
---|
| 162 | $text = preg_replace('#^<setting[^>]*>(.*)</setting>$#msu', '\1', (string) $e->asXML()); |
---|
| 163 | } else { |
---|
| 164 | $text = $e; |
---|
| 165 | } |
---|
| 166 | $w->{$setting} = preg_replace_callback('/\{tpl:lang (.*?)\}/msu', array('self', 'widgetL10nHandler'), $text); |
---|
| 167 | } |
---|
[2566] | 168 | |
---|
[3730] | 169 | echo $w->call(0); |
---|
| 170 | } |
---|
[2566] | 171 | |
---|
[3730] | 172 | private static function widgetL10nHandler($m) |
---|
| 173 | { |
---|
| 174 | return __($m[1]); |
---|
| 175 | } |
---|
[0] | 176 | } |
---|