1: <?php
2:
3: /**
4: *
5: * Macros are "delcared" by filters registered to the <var>content_macro</var> filter. The filter should add its macros to the
6: * array passed and return the result. A macro is defined by an array element. The index of the array element is the macro identifier.
7: *
8: * Note: the plugin should be active both on THEMES to provide the function and on the ADMIN pages to provide
9: * the macro documentation.
10: *
11: * The content of the array is as follows:
12: * <ol>
13: * <li>"class" => <var>macro class</var>, see below.</li>
14: * <li>"params" => An array of parameter types. Append an <b>*</b> if the parameter may be omitted. The types allowed are:
15: * <ul>
16: * <li>"string": may be enclosed in quotation marks when the macro is invoked. The quotes are stripped before the macro is processed. </li>
17: * <li>"int": a number</li>
18: * <li>"bool": <var>true</var> or <var>false</var></li>
19: * <li>"array": will process assignment type parameter (<var>x = y</var>) lists. If the assignment is left out, the value will be inserted with its position in the list as the array index. Since an array parameter will consume all remaining elements it must be the last item in the parameter list.</li>
20: * </ul>
21: * </li>
22: * <li>"value" => This is a function, procedure, expression or content as defined by the macro class.</li>
23: * <li>"owner" => This should be your plugin name.</li>
24: * <li>"desc" => Text that describes the macro usage.</li>
25: * </ol>
26: *
27: * Macro classes:
28: * <ol>
29: * <li>
30: * <var>procedure</var> calls a script function that produces output. The output is captured and inserted in place of the macro instance.
31: * </li>
32: * <li>
33: * <var>function</var> calls a script function that returns a result. The result is inserted in place of the macro instance.
34: * </li>
35: * <li>
36: * <var>constant</var> replaces the macro instances with the constant provided.
37: * </li>
38: * <li>
39: * <var>expression</var> evaluates the expression provided and replaces the instance with the result of the evaluation. If a regex is supplied for an expression.
40: * The values provided will replace placeholders in the expression. The first parameter replaces $1, the second $2, etc.
41: * </li>
42: * </ol>
43: *
44: * Useage examples:
45: * <ol>
46: * <li>
47: * [CODEBLOCK 3] (Places codeblock number 3 in your content)
48: * </li>
49: * <li>
50: * [PAGE] (Prints the current page number)
51: * </li>
52: * <li>
53: * [ZENPHOTO_VERSION] (Prints the version of the Zenphoto installation)
54: * </li>
55: * <li>
56: * [PAGELINK mylinktext customscriptpage] (Provides text for a link to a "custom" script page)
57: * </li>
58: * </ol>
59: *
60: * <b>Note</b> the <var>spaces<var> after the <var>[<var> and before the <var>]<var> square brackets. They are used in these examples to prevent the code from actually executing. They should not be present in your real code.
61: *
62: * @author Stephen Billard (sbillard)
63: * @package plugins
64: * @subpackage development
65: */
66: $plugin_is_filter = 5 | ADMIN_PLUGIN;
67: $plugin_description = gettext('View available <code>content macros</code>.');
68: $plugin_author = "Stephen Billard (sbillard)";
69:
70: if (OFFSET_PATH != 2 && zp_loggedin(ZENPAGE_PAGES_RIGHTS | ZENPAGE_NEWS_RIGHTS | ALBUM_RIGHTS)) {
71: foreach (getEnabledPlugins() as $ext => $pn) {
72: $loadtype = $pn['priority'];
73: if ($loadtype & (FEATURE_PLUGIN | THEME_PLUGIN)) {
74: require_once($pn['path']);
75: }
76: }
77: unset($ext);
78: unset($pn);
79: $macros = getMacros();
80: if (!empty($macros)) {
81: zp_register_filter('admin_tabs', 'macro_admin_tabs');
82: }
83: }
84:
85: function macro_admin_tabs($tabs) {
86: if (!isset($tabs['development'])) {
87: $tabs['development'] = array('text' => gettext("development"),
88: 'subtabs' => NULL);
89: }
90: $tabs['development']['subtabs'][gettext("macros")] = PLUGIN_FOLDER . '/macroList/macroList_tab.php?page=macros&tab=' . gettext('macros');
91: $named = array_flip($tabs['development']['subtabs']);
92: natcasesort($named);
93: $tabs['development']['subtabs'] = $named = array_flip($named);
94: $tabs['development']['link'] = array_shift($named);
95: return $tabs;
96: }
97:
98: function macroList_show($macro, $detail) {
99: $warned = array();
100: echo '<dl>';
101: $warn = array();
102: if (preg_match('/[^\w]/', $macro)) {
103: $warn['identifier'] = gettext('Macro identifiers may not contain special characters.');
104: echo '<dt class="top"><code>[<span class="error">' . $macro . '</span>';
105: } else {
106: echo '<dt class="top"><code>[' . $macro;
107: }
108: $required = $array = false;
109: if ($detail['class'] == 'expression') {
110: preg_match_all('/\$\d+/', $detail['value'], $replacements);
111: foreach ($replacements as $rkey => $v) {
112: if (empty($v))
113: unset($replacements[$rkey]);
114: }
115: if (count($detail['params']) != count($replacements)) {
116: $warn['paremeters'] = gettext('The number of macro parameters must match the number of replacement tokens in the expression.');
117: }
118: } else if ($detail['class'] == 'function' || $detail['class'] == 'procedure') {
119: if (!is_callable($detail['value'])) {
120: $warn['method'] = sprintf(gettext('<code>%s</code> is not callable'), $detail['value']);
121: }
122: }
123: if (!empty($detail['params'])) {
124: $params = '';
125: $brace = '{';
126: for ($i = 1; $i <= count($detail['params']); $i++) {
127: $type = rtrim($rawtype = $detail['params'][$i - 1], '*');
128: if ($array) {
129: $params .= ' <em><span class="error">' . $type . ' %' . $i . '</span></em>';
130: $warn['array'] = gettext('An array parameter must be the last parameter.');
131: } else if ($type == $rawtype) {
132: if ($required) {
133: $params .= ' <em><span class="error">' . $type . ' %' . $i . '</span></em>';
134: $warn['required'] = gettext('Required parameters should not follow optional ones.');
135: } else {
136: $params = $params . ' <em>' . $type . " %$i</em>";
137: }
138: } else {
139: if ($detail['class'] == 'expression') {
140: $params = $params . " <em>$brace" . '<span class="error">' . $type . " %$i</span></em>";
141: $warn['expression'] = gettext('Expressions may not have optional parameters.');
142: } else {
143: $params = $params . " <em>$brace" . $type . " %$i</em>";
144: }
145: $required = true;
146: $brace = '';
147: }
148: $array = $array || $type == 'array';
149: }
150: if ($required)
151: $params .= "<em>}</em>";
152: echo $params;
153: }
154: echo ']</code> <em>(' . @$detail['owner'] . ')</em></dt><dd class="top">' . $detail['desc'] . '</dd>';
155: if (count($warn)) {
156: echo '<div class="notebox"><strong>Warning:</strong>';
157: foreach ($warn as $warning) {
158: echo '<p>' . $warning . '</p>';
159: }
160: echo'</div>';
161: }
162: echo '</dl>';
163: }
164:
165: ?>