PluginProbe
URL Params / 2.5
URL Params v2.5
trunk 1.6 1.7 1.8 2.0 2.1 2.2 2.3 2.4 2.5 2.5-review
url-params / urlparams.php

urlparams.php in URL Params 2.5, at urlparams.php

261 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: URL Params
4 Plugin URI: http://asandia.com/wordpress-plugins/urlparams/
5 Description: Short Code to grab any URL Parameter
6 Version: 2.5
7 Author: Jeremy B. Shapiro
8 Author URI: http://www.asandia.com/
9 */
10
11 /*
12 URL Params (Wordpress Plugin)
13 Copyright (C) 2011-2026 Jeremy Shapiro
14
15 */
16
17 // error_reporting(E_ALL); // Helpful for checking for warnings that are TYPICALLY hidden but may be present on some installs
18
19 // Tell WordPress to register the shortcodes
20 add_shortcode("urlparam", "urlparam");
21 add_shortcode("ifurlparam", "ifurlparam");
22
23 register_uninstall_hook(__FILE__, 'uninstall_urlparams');
24
25 // If we're an admin, add a URL Params options page under Settings
26 if (is_admin()) {
27 add_action('admin_menu', 'urlparams_admin_menu');
28 add_action('admin_init', 'admin_init_urlparams');
29 }
30 function urlparams_admin_menu()
31 {
32 add_options_page('URL Params', 'URL Params', 'manage_options', 'urlparams', 'options_page_urlparams');
33 }
34
35 // Add a link to the Options page from the Plugins page
36 add_filter( 'plugin_action_links', 'urlparams_plugin_action_links', 10, 2 );
37 function urlparams_plugin_action_links( $links, $file )
38 {
39 if ( $file == plugin_basename( __DIR__ .'/urlparams.php' ) ) {
40 $links[] = '<a href="options-general.php?page=urlparams"><b>'.__('Settings').'</b></a>';
41 }
42 return $links;
43 }
44
45 function options_page_urlparams()
46 {
47 include __DIR__ .'/options.php';
48 }
49
50 function urlparam($attributes, $content) {
51 $defaults = array(
52 'param' => '',
53 'default' => '',
54 'dateformat' => '',
55 'attr' => '',
56 'htmltag' => false,
57 );
58
59 // If $attributes is an array, we merge it with our defaults
60 if(is_array($attributes)) {
61 // We used to use shortcode_atts(), but that would nuke an extra attributes that we don't know about but want. array_merge() keeps them all.
62 $attributes = array_merge($defaults, $attributes);
63 } else {
64 $attributes = $defaults;
65 }
66
67 $params = preg_split('/,\s*/',$attributes['param']);
68
69 $return = false;
70
71 foreach($params as $param)
72 {
73 if(!$return
74 && array_key_exists($param, $_REQUEST)
75 && ($rawText = $_REQUEST[$param])
76 ) {
77 if(($attributes['dateformat'] != '')
78 && strtotime($rawText)
79 ) {
80 $return = date($attributes['dateformat'], strtotime($rawText));
81 } else {
82 $return = esc_html($rawText);
83 }
84 }
85 }
86
87 if(!$return) {
88 $return = $attributes['default'];
89 }
90
91 if($attr = $attributes['attr']) {
92 $return = ' ' . sanitize_key($attr) . '="' . esc_attr($return) . '" ';
93
94 if($attributes['htmltag']) {
95 $tagName = $attributes['htmltag'];
96
97 foreach(array_keys($defaults) as $key) {
98 unset($attributes[$key]);
99 }
100 $otherAttributes = "";
101 foreach($attributes as $key => $val) {
102 $otherAttributes .= ' '.sanitize_key($key).'="'.esc_attr($val).'" ';
103 }
104
105 $return = "<$tagName $otherAttributes $return".($content ? ">$content</$tagName>" : "/>");
106 }
107 }
108
109 $allowedHtml = wp_kses_allowed_html('post');
110
111 // Do we have any custom tags set? Add those
112 if($customTags = urlparams_get_customtags()) {
113 foreach($customTags as $customTag => $attributes) {
114 // Is this already an allowed tag? Merge our approved attributes in, too
115 if(array_key_exists($customTag, $allowedHtml)) {
116 $allowedHtml[$customTag] = array_merge($allowedHtml[$customTag], $attributes);
117 } else {
118 $allowedHtml[$customTag] = $attributes;
119 }
120 }
121 }
122
123 return wp_kses($return, $allowedHtml);
124 }
125
126 /*
127 * If 'param' is found and 'is' is set, compare the two and display the contact if they match
128 * If 'param' is found and 'is' isn't set, display the content between the tags
129 * If 'param' is not found and 'empty' is set, display the content between the tags
130 *
131 */
132 function ifurlparam($attributes, $content) {
133 $attributes = shortcode_atts(array(
134 'param' => '',
135 'empty' => false,
136 'is' => false,
137 ), $attributes);
138
139 $params = preg_split('/,\s*/',$attributes['param']);
140
141 foreach($params as $param)
142 {
143 if(isset($_REQUEST[$param])
144 && ($value = $_REQUEST[$param]))
145 {
146 if($attributes['empty'])
147 {
148 return '';
149 } elseif(!($isAttribute = $attributes['is'])
150 || ($value == $isAttribute)
151 ) {
152 return do_shortcode($content);
153 }
154 }
155 }
156
157 if ($attributes['empty'])
158 {
159 return do_shortcode($content);
160 }
161
162 return '';
163 }
164
165 function uninstall_urlparams() {
166 delete_option('urlparams_customtags');
167
168 }
169
170 function admin_init_urlparams()
171 {
172 register_setting('urlparams', 'urlparams_customtags', [
173 'sanitize_callback' => 'urlparams_sanitize_customtags',
174 ]);
175 }
176
177 /**
178 *
179 * Sanitize our custom tag options
180 *
181 * @param string $newCustomTags
182 * @return string
183 */
184 function urlparams_sanitize_customtags($newCustomTags) {
185 $isValid = true;
186
187 $lines = explode("\n", $newCustomTags);
188 $validLines = [];
189
190 foreach($lines as $line) {
191 $lineParts = explode(':', $line);
192 if(count($lineParts) !== 2) {
193 $isValid = false;
194 continue;
195 }
196
197 if(!($tagName = trim(strtolower($lineParts[0])))) {
198 $isValid = false;
199 continue;
200 }
201
202 if(!($attributes = explode(',', $lineParts[1]))) {
203 $isValid = false;
204 continue;
205 }
206
207 $validAttributes = [];
208 foreach($attributes as $attribute) {
209 if($attribute = strtolower(trim($attribute))) {
210 $validAttributes[] = $attribute;
211 }
212 }
213
214 $validAttributes = array_unique($validAttributes);
215
216 if(!$validAttributes) {
217 $isValid = false;
218 continue;
219 }
220
221 $validLines[] = $tagName.': '.implode(', ', $validAttributes);
222 }
223
224 if(!$isValid) {
225 add_settings_error('urlparams_customtags', 'urlparams_customtags', __('Invalid Custom Tags were removed', 'urlparams'), 'warning');
226 }
227
228 return join("\n", $validLines);
229 }
230
231 /**
232 *
233 * Return our array of custom tag options
234 *
235 * @return array
236 */
237 function urlparams_get_customtags() {
238 $lines = explode("\n", get_option('urlparams_customtags'));
239 $customTags = [];
240
241 foreach($lines as $line) {
242 $lineParts = explode(':', $line);
243 if(count($lineParts) !== 2) {
244 continue;
245 }
246
247 $tagName = trim($lineParts[0]);
248 $attributes = explode(',', $lineParts[1]);
249
250 $customTags[$tagName] = [];
251
252 foreach($attributes as $attribute) {
253 $customTags[$tagName][trim($attribute)] = true;
254 }
255 }
256
257 return $customTags;
258 }
259
260 ?>
261