PluginProbe
MaxButtons – Create buttons / 6.25
MaxButtons – Create buttons v6.25
6.23 6.24 6.25 6.26 6.26.1 6.27 6.28 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.1.1 7.1.2 7.1.3 7.10 7.11 7.13 7.13.1 7.13.2 7.13.3 All 100 releases
maxbuttons / classes / collections.php

collections.php in MaxButtons – Create buttons 6.25, at classes/collections.php

515 lines 13.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace MaxButtons;
3 defined('ABSPATH') or die('No direct access permitted');
4
5 use \RecursiveDirectoryIterator as RecursiveDirectoryIterator;
6 use \FilesystemIterator as FilesystemIterator;
7 use \RecursiveIteratorIterator as RecursiveIteratorIterator;
8
9
10 class maxCollections
11 {
12 static $init = false;
13 //static $collections = array();
14 static $collectionClass = array();
15 static $collectionBlock = array();
16
17 static $hooks = array();
18
19 static $cached_collections = array();
20 protected static $transientChecked = false;
21
22 static $collectionButtons = null; // all button ID's in a collection.
23
24 static function init()
25 {
26 $collection_paths = apply_filters("mbcollection_paths", array( MB()->get_plugin_path() . '/collections/') );
27 $collectionClass = array();
28 $collectionBlock = array();
29
30 foreach($collection_paths as $cpath)
31 {
32 $dir_iterator = new RecursiveDirectoryIterator($cpath, FilesystemIterator::SKIP_DOTS);
33 $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
34
35 foreach ($iterator as $fileinfo)
36 {
37 $collection = $fileinfo->getFilename();
38
39
40 if (file_exists($cpath . $collection))
41 {
42 require_once( $cpath . $collection);
43 }
44 }
45 }
46 self::$collectionClass = $collectionClass;
47 self::$collectionBlock = $collectionBlock;
48
49 self::$init = true;
50
51 }
52
53 /*
54 Check our custom transients for expiration. This should be done only once per run or less.
55 */
56 static function checkExpireTrans()
57 {
58 if (! self::$transientChecked)
59 {
60 maxUtils::removeExpiredTrans(); // instead of on each button to reduce load.
61 self::$transientChecked = true;
62 }
63 }
64 /*
65 Function to hook into WordPress for automatic display of collections.
66 */
67 static function setupHooks()
68 {
69 // check for admin side, we need not hooks nor queries for this.
70 if (is_admin())
71 return;
72
73
74 global $pagenow;
75 if ( in_array($pagenow, array('wp-login.php', 'wp-register.php')) )
76 return;
77
78 global $wpdb;
79 $table = maxUtils::get_collection_table_name();
80 $sql = "select collection_id, collection_key, collection_value from $table where
81 collection_key in ('show_homepage','show_page','show_post', 'show_archive')
82 and collection_id IN (select collection_id from $table where collection_key = 'collection_type'
83 and collection_value = 'social' ) ";
84 $results = $wpdb->get_results($sql, ARRAY_A);
85
86 $hook_array = array();
87
88 foreach($results as $result)
89 {
90 $id = $result["collection_id"];
91 $key = $result["collection_key"];
92 $placement = $result["collection_value"];
93
94 // override - if needed you can specify a different placement for each position
95 $placement = apply_filters("mb-collection-$id-$key-placement", $placement);
96
97 switch($placement)
98 {
99 case "after":
100 case "before":
101 case "after-before":
102 $show_type = "post"; // show on the post loop
103 break;
104 case "static-left":
105 case "static-right":
106 case "static-top":
107 case "static-bottom":
108 $show_type = "once"; // show once - otherwise for overviews they will be many static buttons.
109 break;
110 }
111
112 $hook_array[$show_type][$key][] = array("collection_id" => $id, "placement" => $placement);
113 }
114
115
116 self::$hooks = $hook_array;
117
118
119
120 if (isset($hook_array["post"]) && count($hook_array["post"]) > 0)
121 {
122 add_filter("the_content", array(maxUtils::namespaceit('maxCollections'), "doContentHooks"));
123 }
124 if (isset($hook_array["once"]) && count($hook_array["once"]) > 0)
125 {
126 //self::doFooterHooks(); // the stuff that goes once.
127 add_action('wp_head', array(maxUtils::namespaceit('maxCollections'), 'doFooterHooks'));
128 }
129
130 if (count($hook_array) > 0)
131 return true; // yes, bind action and check
132 else
133 return false; // no binds, don't check.
134 }
135
136 /* Try to find the current place we are at in the site ( front / blog / page etc ) .*/
137 protected static function getCurrentHook()
138 {
139 $hook = '';
140 if (is_front_page()) // check if home page ( home page > page )
141 {
142 $hook = "show_homepage";
143
144 }
145 elseif (is_page()) // check if page
146 {
147 $hook = "show_page";
148 }
149 elseif (is_single() || is_home()) // check if post
150 {
151 $hook = "show_post";
152 }
153 elseif (is_archive())
154 {
155 $hook = "show_archive";
156 }
157
158
159 return $hook;
160 }
161
162 static function doContentHooks($content)
163 {
164 $hook_array = self::$hooks["post"];
165 $hook = self::getCurrentHook();
166
167 if ($hook == '')
168 return $content; // nothing
169
170 if (! isset($hook_array[$hook])) // nothing as well
171 return $content;
172
173 $collections = $hook_array[$hook];
174
175 // do all collections on hook -- check for placement as well.
176 foreach($collections as $settings)
177 {
178 $collection_id = $settings["collection_id"];
179 $placement = $settings["placement"];
180
181 $col = self::getCollectionByID($collection_id);
182 $col->set($collection_id);
183 $output = $col->display(array("echo" => false)); // output default, no echo
184
185
186 switch($placement) // where to output, rather limited atm.
187 {
188 case "before":
189 $place = "before";
190 break;
191 case "after-before";
192 $place = "both";
193 break;
194 default:
195 $place = "after";
196 break;
197
198 }
199
200
201
202 if($place == 'before' || $place == 'both')
203 {
204 $content = $output . $content;
205 }
206 if($place == 'after' || $place == 'both')
207 {
208
209 $content = $content . $output;
210 }
211 }
212
213 return $content;
214
215 }
216
217 static function doFooterHooks()
218 {
219 $hook_array = self::$hooks["once"];
220 $hook = self::getCurrentHook();
221
222 if (! isset($hook_array[$hook])) // nothing
223 return;
224
225 $collections = $hook_array[$hook];
226 foreach($collections as $settings)
227 {
228 $collection_id = $settings["collection_id"];
229 $placement = $settings["placement"];
230
231 $col = self::getCollectionByID($collection_id);
232 $col->set($collection_id);
233
234 $output = $col->display(array("echo" => false)); // output default, no echo
235
236 do_action('mb-footer', 'collection-' . $collection_id, $output, 'collection_output');
237
238 }
239 }
240
241 static function checkCachedCollection($collection_id)
242 {
243 /* The cache is pointless since for every collection the shared URL's and fields can change. This means that the same collection
244 can server up multiple situations. Deactivated this when discovered in /blogs/ all have the same share URL on different posts.
245 */
246 return false;
247
248 // if (isset(self::$cached_collections[$collection_id]))
249 // return self::$cached_collections[$collection_id];
250
251 // return false;
252 }
253
254 static function addCachedCollection($collection_id, $data)
255 {
256 self::$cached_collections[$collection_id] = $data;
257 }
258
259 public static function isButtonInCollection($button_id)
260 {
261 if (is_null(self::$collectionButtons))
262 {
263 global $wpdb;
264 $table = maxUtils::get_collection_table_name();
265 $sql = 'SELECT collection_id, collection_value FROM ' . $table . ' WHERE
266 collection_key = "picker";';
267 $result = $wpdb->get_results($sql, ARRAY_A);
268
269 $buttonarray = array();
270 foreach ($result as $index => $row)
271 {
272 $collection_id = $row['collection_id'];
273 $buttons = json_decode($row['collection_value']);
274
275 if (isset($buttons->selection))
276 $buttons = $buttons->selection;
277 else
278 $buttons = array();
279
280 foreach($buttons as $index => $b_id)
281 {
282 if (isset(static::$collectionButtons[$b_id]))
283 self::$collectionButtons[$b_id][] = $collection_id;
284 else
285 self::$collectionButtons[$b_id] = array($collection_id);
286 }
287
288 }
289
290 }
291
292 if (isset(self::$collectionButtons[$button_id]))
293 return self::$collectionButtons[$button_id];
294 else
295 return false;
296 }
297
298 static function ajax_save()
299 {
300 $nonce = sanitize_text_field($_POST["nonce"]);
301 $action = sanitize_text_field($_POST["action"]);
302
303
304 $collection_id = intval($_POST["collection_id"]);
305 $collection_type = sanitize_text_field($_POST["collection_type"]);
306
307 $admin = MB()->getClass('admin');
308
309 $result_title = array("success" => __("Your collection was saved","maxbuttons"),
310 "error" => __("Error","maxbuttons")
311 );
312
313 $close_text = __('Close', 'maxbuttons');
314
315 $result = array(
316 "error" => false,
317 "body" => '',
318 "result" => true,
319 "data" => array(),
320 "close_text" => $close_text,
321 "new_nonce" => 0,
322 );
323
324
325
326 if (! wp_verify_nonce($nonce, $action . "-" . $collection_id))
327 {
328 $result["error"] = true;
329 $result["body"] = __("Nonce not verified","maxbuttons");
330 $result["result"] = false;
331 $result["title"] = $result_title["error"];
332 $result["data"] = array("id" => $collection_id);
333
334 $admin->endAjaxRequest($result);
335
336 }
337
338 if (! isset($collection_type))
339 {
340 MB()->add_notice("error", __("Collection type not found in save. Aborting","maxbuttons"));
341 $result["error"] = true;
342 $result["body"] = MB()->display_notices(false);
343 $result["result"] = false;
344 $result["title"] = $result_title["error"];
345
346 $admin->endAjaxRequest($result);
347 }
348
349 $collection = self::getCollection($collection_type);
350 $collection->set($collection_id);
351
352 $force_reload = false;
353 if ($collection_id == 0)
354 $force_reload = true;
355
356 // this can be a new id (!)
357 $collection_id = $collection->save($_POST);
358
359 $result["data"]["id"] = $collection_id;
360 $result["data"]["new_nonce"] = wp_create_nonce($action . "-" . $collection_id);
361 $result["data"]["reload"] = apply_filters("collections_ajax_force_reload",$force_reload);
362
363 $result["title"] = $result_title["success"];
364
365 $admin->endAjaxRequest($result);
366
367 //echo json_encode($result);
368 //exit();
369 }
370
371 static function ajax_action_front()
372 {
373 // only for trivial front page actions!
374 self::ajax_action(array("ajax_nopriv" => true));
375 }
376
377 static function ajax_action($args = array())
378 {
379 ob_start();
380 $defaults = array("ajax_nopriv" => false);
381 $args = wp_parse_args($args, $defaults);
382
383
384 $admin = MB()->getClass('admin');
385
386 $nonce = isset($_POST["nonce"]) ? $_POST["nonce"] : false;
387 $block_name = sanitize_text_field($_POST["block_name"]);
388 $block_action = sanitize_text_field($_POST["block_action"]);
389 $block_data = (isset($_POST["block_data"])) ? $_POST["block_data"] : '';
390 $action = sanitize_text_field($_POST["action"]);
391
392 $collection_id = intval($_POST["collection_id"]);
393 $collection_type = sanitize_text_field($_POST["collection_type"]);
394
395 if(! $args["ajax_nopriv"])
396 {
397 if (! wp_verify_nonce($nonce, $action . "-" . $collection_id))
398 {
399 $result["error"] = true;
400 $result["body"] = __("Nonce not verified","maxbuttons");
401 $result["result"] = false;
402 $result["title"] = __("Security error","maxbuttons");
403 $result["data"] = array("id" => $collection_id);
404
405 $admin->endAjaxRequest($result);
406
407 }
408 }
409
410 $result = array(
411 "error" => false,
412 "body" => '',
413 "result" => true,
414 "data" => array(),
415 "new_nonce" => 0,
416 );
417
418 $collection = self::getCollection($collection_type);
419 $collection->set($collection_id);
420
421 $result = $collection->doBlockAjax($result, $block_name, $block_action, $block_data);
422
423 //ob_end_clean(); // prevent PHP errors from breaking JSON response.
424
425 $admin->endAjaxRequest($result);
426 $results = $collection->get_meta($name, 'collection_name');
427 }
428
429 static function getCollections()
430 {
431 if (! self::$init) self::init();
432
433 global $wpdb;
434
435 $table = maxUtils::get_collection_table_name();
436
437 $sql = "SELECT distinct collection_id from $table";
438
439 $results = $wpdb->get_results($sql,ARRAY_A);
440 return $results;
441 }
442
443 /* This will invoke a collection class by name ( i.e. social-collection or basic-collection */
444 static function getCollection($name)
445 {
446 if (! self::$init) self::init();
447
448 if (isset(self::$collectionClass[$name]))
449 {
450 $class = maxUtils::namespaceit(self::$collectionClass[$name]);
451 return new $class;
452 }
453 }
454
455 /* This will find an user defined collection from the database by ID */
456 static function getCollectionByID($id)
457 {
458 $collection = new maxCollection();
459 $results = $collection->get_meta($id, 'collection_type');
460
461
462 if ( count($results) == 1)
463 {
464
465 $type = $results["collection_type"];
466
467 $usecol = self::getCollection($type);
468 if (! $usecol)
469 return false;
470
471 $usecol->set($id);
472 return $usecol;
473 }
474
475 return false;
476 }
477
478 /* Find a collection from the database by name */
479 static function getCollectionbyName($name)
480 {
481 //$collection = new maxCollection();
482 global $wpdb;
483 $sql = "select collection_id from " . maxUtils::get_collection_table_name() . " where collection_key = 'collection_name' and collection_value = %s ";
484 $sql = $wpdb->prepare($sql, $name);
485 $result = $wpdb->get_row($sql, ARRAY_A); // find first
486
487
488 if (count($result) > 0)
489 {
490 if (isset($result["collection_id"]))
491 {
492
493 $usecol = self::getCollectionByID($result["collection_id"]);
494 return $usecol;
495 }
496
497 }
498 return false;
499 }
500
501 static function getBlock($name)
502 {
503 if (! self::$init) self::init();
504
505 if (isset(self::$collectionBlock[$name]))
506 {
507 $class = maxUtils::namespaceit(self::$collectionBlock[$name]);
508 return new $class;
509 }
510 else return false;
511 }
512
513
514 }
515