PluginProbe
Snippet Shortcodes / 1.5
Snippet Shortcodes v1.5
5.2.1 5.2 5.1.8 5.1.6 5.1.7 5.1.5 trunk 1.0 1.1 1.2 1.3 1.3.1 1.4 1.5 1.5.1 1.6 1.6.1 1.7 1.7.1 1.7.2 1.7.3 1.7.4 1.8 2.0 2.0.1 All 74 releases
shortcode-variables / includes / functions.php

functions.php in Snippet Shortcodes 1.5, at includes/functions.php

317 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 defined('ABSPATH') or die('Jog on!');
4
5 function sh_cd_get_all_shortcodes()
6 {
7 global $wpdb;
8
9 $sql = 'SELECT * FROM ' . $wpdb->prefix . SH_CD_TABLE . ' order by slug asc';
10
11 $rows = $wpdb->get_results( $sql );
12
13 if (!is_null($rows))
14 return $rows;
15
16 return false;
17
18 }
19
20 function sh_cd_get_shortcode($id)
21 {
22 if (!is_admin())
23 return false;
24
25 global $wpdb;
26
27 $sql = $wpdb->prepare('SELECT slug, data FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where id = %d', $id);
28
29 $row = $wpdb->get_row( $sql );
30
31 if (!is_null($row))
32 return $row;
33
34 return false;
35
36 }
37 function sh_cd_get_shortcode_by_slug($slug)
38 {
39 global $wpdb;
40
41 $sql = $wpdb->prepare('SELECT data FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where slug = %s', $slug);
42
43 $row = $wpdb->get_var( $sql );
44
45 if (!is_null($row))
46 return stripslashes($row);
47
48 return false;
49
50 }
51 function sh_cd_get_slug_by_id($id)
52 {
53 global $wpdb;
54
55 $sql = $wpdb->prepare('SELECT slug FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where id = %d', $id);
56
57 $row = $wpdb->get_var( $sql );
58
59 if (!is_null($row))
60 return $row;
61
62 return false;
63
64 }
65
66 function sh_cd_save_shortcode($slug, $data, $id = false)
67 {
68 if (!is_admin())
69 return false;
70
71 if (false == $id && empty($slug))
72 return false;
73
74 global $wpdb;
75
76 $slug = sh_cd_get_slug($slug);
77
78 $result = false;
79
80 if ($id)
81 {
82 //Update data (Slug can never be updated!)
83 $result = $wpdb->update(
84 $wpdb->prefix . SH_CD_TABLE,
85 array(
86 'data' => $data
87 ),
88 array( 'id' => $id),
89 array(
90 '%s'
91 ),
92 array( '%d' )
93 );
94
95 sh_cd_delete_cache(sh_cd_get_slug_by_id($id));
96 }
97 else
98 {
99 $result = $wpdb->insert(
100 $wpdb->prefix . SH_CD_TABLE,
101 array(
102 'slug' => $slug,
103 'data' => $data
104 ),
105 array(
106 '%s',
107 '%s'
108 )
109 );
110
111 sh_cd_delete_cache($slug);
112 }
113
114 if (false === $result) {
115 return false;
116 }
117 else
118 {
119 return true;
120 }
121
122 }
123
124 function sh_cd_delete_shortcode($id)
125 {
126 if (!is_admin() || !is_numeric($id))
127 return false;
128
129 global $wpdb;
130
131 // Clear cached version
132 sh_cd_delete_cache(sh_cd_get_slug_by_id($id));
133
134 if (false === $wpdb->delete( $wpdb->prefix . SH_CD_TABLE, array( 'id' => $id ) )) {
135 return false;
136 }
137 else {
138 return true;
139 }
140
141 }
142
143 function sh_cd_get_slug($text)
144 {
145 if(!empty($text))
146 {
147 $text = sanitize_title($text);
148
149 $original_slug = $text;
150
151 $try = 1;
152 var_dump(in_array($text, sh_cd_shortcode_presets()));
153
154 // If slug exists, then fetch a unique one!
155 // v1.1: and ensure slug isn't a preset
156 while (!sh_cd_is_slug_unique($text))
157 {
158 $text = $original_slug . '_' . $try;
159
160 $try++;
161 }
162 }
163
164 return $text;
165 }
166
167 function sh_cd_is_slug_unique($slug)
168 {
169 if (!is_admin() || empty($slug))
170 return false;
171
172 // 1.1 Ensure slug is not a prefix
173 if (sh_cd_is_shortcode_preset($slug))
174 return false;
175
176 global $wpdb;
177
178 $sql = $wpdb->prepare('SELECT count(slug) FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where slug = %s', $slug);
179
180 $row = $wpdb->get_var( $sql );
181
182 if(0 == $row)
183 {
184 return true;
185 }
186 else
187 {
188 return false;
189 }
190
191
192 }
193
194 function sh_cd_create_database_table()
195 {
196 global $wpdb;
197
198 $table_name = $wpdb->prefix . SH_CD_TABLE;
199
200 $charset_collate = $wpdb->get_charset_collate();
201
202 $sql = "CREATE TABLE $table_name (
203 id mediumint(9) NOT NULL AUTO_INCREMENT,
204 slug varchar(100) NOT NULL,
205 data text,
206 UNIQUE KEY id (id)
207 ) $charset_collate;";
208
209 $wpdb->query($sql);
210
211 update_option('sh-cd-version', SH_CD_PLUGIN_VERSION);
212
213 }
214
215 function sh_cd_display_message($text, $error = false)
216 {
217 if (!empty($text))
218 {
219 $class = (($error) ? 'error' : 'updated');
220
221 echo '<div class="' . $class . '">
222 <p>' . $text . '</p>
223 </div>';
224 }
225 }
226
227 function sh_cd_create_dialog_jquery_code($title, $message, $class_used_to_prompt_confirmation, $js_call = false)
228 {
229 global $wp_scripts;
230 $queryui = $wp_scripts->query('jquery-ui-core');
231 $url = "//ajax.googleapis.com/ajax/libs/jqueryui/".$queryui->ver."/themes/smoothness/jquery-ui.css";
232 wp_enqueue_script( 'jquery-ui-dialog' );
233 wp_enqueue_style('jquery-ui-smoothness', $url, false, null);
234
235 $id_hash = md5($title . $message . $class_used_to_prompt_confirmation);
236
237 ?>
238 <div id='<?php echo $id_hash; ?>' title='<?php echo $title; ?>'>
239 <p><?php echo $message; ?></p>
240 </div>
241 <script>
242 jQuery(function($) {
243
244 var $info = $('#<?php echo $id_hash; ?>');
245
246 $info.dialog({
247 'dialogClass' : 'wp-dialog',
248 'modal' : true,
249 'autoOpen' : false
250 });
251
252 $('.<?php echo $class_used_to_prompt_confirmation; ?>').click(function(event) {
253
254 event.preventDefault();
255
256 target_url = $(this).attr('href');
257
258 var $info = $('#<?php echo $id_hash; ?>');
259
260 $info.dialog({
261 'dialogClass' : 'wp-dialog',
262 'modal' : true,
263 'autoOpen' : false,
264 'closeOnEscape' : true,
265 'buttons' : {
266 'Yes': function() {
267
268 <?php if ($js_call != false): ?>
269 <?php echo $js_call; ?>
270
271 $(this).dialog('close');
272 <?php else: ?>
273 window.location.href = target_url;
274 <?php endif; ?>
275 },
276 'No': function() {
277 $(this).dialog('close');
278 }
279 }
280 });
281
282 $info.dialog('open');
283 });
284
285 });
286
287
288
289
290 </script>
291
292 <?php
293 }
294
295 function sh_cd_get_cache($key)
296 {
297 $key = sh_cd_generate_cache_key($key);
298
299 return get_transient($key);
300 }
301 function sh_cd_set_cache($key, $data)
302 {
303 $key = sh_cd_generate_cache_key($key);
304
305 set_transient($key, $data, SH_CD_CACHING_TIME);
306 }
307 function sh_cd_delete_cache($key)
308 {
309 $key = sh_cd_generate_cache_key($key);
310
311 return delete_transient($key);
312 }
313 function sh_cd_generate_cache_key($key)
314 {
315 return SH_CD_SHORTCODE . $key;
316 }
317