| 1 | <?php |
|---|
| 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 | |
|---|
| 12 | if (!defined('DC_RC_PATH')) {return;} |
|---|
| 13 | |
|---|
| 14 | include dirname(__FILE__) . '/_default_widgets.php'; |
|---|
| 15 | require_once dirname(__FILE__) . '/_widgets_functions.php'; |
|---|
| 16 | |
|---|
| 17 | $core->tpl->addValue('Widgets', array('publicWidgets', 'tplWidgets')); |
|---|
| 18 | $core->tpl->addBlock('Widget', array('publicWidgets', 'tplWidget')); |
|---|
| 19 | $core->tpl->addBlock('IfWidgets', array('publicWidgets', 'tplIfWidgets')); |
|---|
| 20 | |
|---|
| 21 | class publicWidgets |
|---|
| 22 | { |
|---|
| 23 | public static function tplWidgets($attr) |
|---|
| 24 | { |
|---|
| 25 | $type = isset($attr['type']) ? $attr['type'] : ''; |
|---|
| 26 | |
|---|
| 27 | # widgets to disable |
|---|
| 28 | $disable = isset($attr['disable']) ? trim($attr['disable']) : ''; |
|---|
| 29 | |
|---|
| 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 | } |
|---|
| 42 | |
|---|
| 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}; |
|---|
| 48 | |
|---|
| 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 | } |
|---|
| 56 | |
|---|
| 57 | if ($widgets->isEmpty()) { |
|---|
| 58 | // Widgets are empty, don't show anything |
|---|
| 59 | return; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | $disable = preg_split('/\s*,\s*/', $disable, -1, PREG_SPLIT_NO_EMPTY); |
|---|
| 63 | $disable = array_flip($disable); |
|---|
| 64 | |
|---|
| 65 | foreach ($widgets->elements() as $k => $w) { |
|---|
| 66 | if (isset($disable[$w->id()])) { |
|---|
| 67 | continue; |
|---|
| 68 | } |
|---|
| 69 | echo $w->call($k); |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | public static function tplIfWidgets($attr, $content) |
|---|
| 74 | { |
|---|
| 75 | $type = isset($attr['type']) ? $attr['type'] : ''; |
|---|
| 76 | |
|---|
| 77 | # widgets to disable |
|---|
| 78 | $disable = isset($attr['disable']) ? trim($attr['disable']) : ''; |
|---|
| 79 | |
|---|
| 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 | } |
|---|
| 92 | |
|---|
| 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}; |
|---|
| 98 | |
|---|
| 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 | } |
|---|
| 106 | |
|---|
| 107 | return (!$widgets->isEmpty()); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | private static function defaultWidgets($type) |
|---|
| 111 | { |
|---|
| 112 | $widgets = new dcWidgets(); |
|---|
| 113 | $w = new dcWidgets(); |
|---|
| 114 | |
|---|
| 115 | if (isset($GLOBALS['__default_widgets'][$type])) { |
|---|
| 116 | $w = $GLOBALS['__default_widgets'][$type]; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | return $w; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | public static function tplWidget($attr, $content) |
|---|
| 123 | { |
|---|
| 124 | if (!isset($attr['id']) || !($GLOBALS['__widgets']->{$attr['id']} instanceof dcWidget)) { |
|---|
| 125 | return; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | # We change tpl:lang syntax, we need it |
|---|
| 129 | $content = preg_replace('/\{\{tpl:lang\s+(.*?)\}\}/msu', '{tpl:lang $1}', $content); |
|---|
| 130 | |
|---|
| 131 | # We remove every {{tpl: |
|---|
| 132 | $content = preg_replace('/\{\{tpl:.*?\}\}/msu', '', $content); |
|---|
| 133 | |
|---|
| 134 | return |
|---|
| 135 | "<?php publicWidgets::widgetHandler('" . addslashes($attr['id']) . "','" . str_replace("'", "\\'", $content) . "'); ?>"; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | public static function widgetHandler($id, $xml) |
|---|
| 139 | { |
|---|
| 140 | $widgets = &$GLOBALS['__widgets']; |
|---|
| 141 | |
|---|
| 142 | if (!($widgets->{$id} instanceof dcWidget)) { |
|---|
| 143 | return; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 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 | } |
|---|
| 152 | |
|---|
| 153 | $w = clone $widgets->{$id}; |
|---|
| 154 | |
|---|
| 155 | foreach ($xml->setting as $e) { |
|---|
| 156 | if (empty($e['name'])) { |
|---|
| 157 | continue; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 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 | } |
|---|
| 168 | |
|---|
| 169 | echo $w->call(0); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | private static function widgetL10nHandler($m) |
|---|
| 173 | { |
|---|
| 174 | return __($m[1]); |
|---|
| 175 | } |
|---|
| 176 | } |
|---|