PluginProbe
Social Share Buttons / 1.10
Social Share Buttons v1.10
trunk 0.9 1.0 1.1 1.1.1 1.1.2 1.10 1.11 1.12 1.15 1.16 1.17 1.18 1.19 1.2 1.20 1.3 1.30 1.4 1.5 1.6 1.7 1.8 1.9
share-button / classes / class-collections.php

class-collections.php in Social Share Buttons 1.10, at classes/class-collections.php

676 lines 15.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace MBSocial;
3 use MaxButtons\maxUtils as maxUtils;
4 use MaxButtons\maxBlocks as maxBlocks;
5
6 defined('ABSPATH') or die('No direct access permitted');
7
8
9
10 class collections
11 {
12 static $init = false;
13 //static $collectionClass = array();
14 static $collectionBlocks = array();
15
16 static $hooks = array();
17
18 protected static $transientChecked = false;
19
20 static $collectionButtons = null; // all button ID's in a collection.
21
22 static function init()
23 {
24
25 self::$init = true;
26
27 }
28
29 public static function shortcode($atts, $content = null)
30 {
31 $display_args = shortcode_atts(array(
32 'id' => 0,
33 "echo" => false,
34 "mode" => "normal",
35 "nocache" => false,
36 "style" => 'footer',
37 ),
38
39 $atts);
40
41 $collection_id = $display_args['id'];
42
43 if($collection_id == 0)
44 return ''; // nothing.
45
46 $collection = new collection($collection_id);
47 $collection->setHook('shortcode');
48
49
50 //$this->set($collection_id);
51 $display_args["compile"] = $display_args["nocache"];
52 $display_args['load_type'] = $display_args['style'];
53 unset($display_args['style']);
54 unset($display_args["nocache"]);
55
56 $output = $collection->display($display_args);
57
58 return $output;
59 }
60
61 /*
62 Check our custom transients for expiration. This should be done only once per run or less.
63 */
64 static function checkExpireTrans()
65 {
66 if (! self::$transientChecked)
67 {
68 maxUtils::removeExpiredTrans(); // instead of on each button to reduce load.
69 self::$transientChecked = true;
70 }
71 }
72 /*
73 Function to hook into WordPress for automatic display of collections.
74 */
75 static function setupHooks()
76 {
77 // check for admin side, we need not hooks nor queries for this.
78 if (is_admin())
79 return;
80
81 global $pagenow;
82 if ( in_array($pagenow, array('wp-login.php', 'wp-register.php')) )
83 return;
84
85 global $wpdb;
86 $table = maxUtils::get_collection_table_name();
87 $sql = "select collection_id, collection_key, collection_value from $table where
88 collection_key like 'display_%' and collection_id IN (select collection_id from $table where collection_key = 'collection_type'
89 and collection_value = 'social_share' ) ";
90 $results = $wpdb->get_results($sql, ARRAY_A);
91
92 $hook_array = array();
93
94 foreach($results as $result)
95 {
96 $id = $result["collection_id"];
97 $key = $result["collection_key"];
98 $placement = $result["collection_value"];
99
100 // override - if needed you can specify a different placement for each position
101 $placement = apply_filters("mb-collection-$id-$key-placement", $placement);
102
103 switch($placement)
104 {
105 case "after":
106 case "before":
107 case "after-before":
108 $show_type = "post"; // show on the post loop
109 break;
110 case "static";
111 $show_type = "once"; // show once - otherwise for overviews they will be many static buttons.
112 break;
113 case "hidden":
114 default:
115 $show_type = 'none';
116 break;
117 }
118
119 if ($show_type != 'none')
120 $hook_array[$show_type][$key][] = array("collection_id" => $id, "placement" => $placement);
121 }
122
123 self::$hooks = $hook_array;
124
125 if (isset($hook_array["post"]) && count($hook_array["post"]) > 0)
126 {
127 add_filter("the_content", array(MBSocial()->admin()->namespaceit('collections'), "doContentHooks"));
128 }
129 if (isset($hook_array["once"]) && count($hook_array["once"]) > 0)
130 {
131 //self::doFooterHooks(); // the stuff that goes once.
132 add_action('wp_head', array(MBSocial()->admin()->namespaceit('collections'), 'doFooterHooks'));
133 }
134
135 if (count($hook_array) > 0)
136 return true; // yes, bind action and check
137 else
138 return false; // no binds, don't check.
139 }
140
141 /* Try to find the current place we are at in the site ( front / blog / page etc ) .*/
142 protected static function getCurrentHook()
143 {
144 $hook = '';
145 if (is_front_page() ) // check if home page ( home page > page )
146 {
147 $hook = "display_homepage";
148 }
149
150 elseif (is_page()) // check if page
151 {
152 $hook = "display_page";
153 }
154 elseif (is_single()) // check if post
155 {
156 $hook = "display_post";
157 }
158 elseif (is_archive() || is_home() && ! is_front_page()) // second stat, is if post main page, in static mode.
159 {
160 $hook = "display_archive";
161 }
162
163 $post_types = \get_post_types(
164 array('public' => true,
165 '_builtin' => false,
166 )
167 );
168
169 $this_post_type = get_post_type( get_the_ID() );
170
171 foreach($post_types as $post_type) // custom post type
172 {
173 if ($post_type == $this_post_type)
174 {
175 $hook = 'display_' . $this_post_type;
176 }
177
178 }
179
180
181 return $hook;
182 }
183
184 static function doContentHooks($content)
185 {
186 $hook_array = self::$hooks["post"];
187 $hook = self::getCurrentHook();
188
189 if ($hook == '')
190 return $content; // nothing
191
192 if (! isset($hook_array[$hook])) // nothing as well
193 return $content;
194
195 $collections = $hook_array[$hook];
196
197 // do all collections on hook -- check for placement as well.
198 foreach($collections as $settings)
199 {
200 $collection_id = $settings["collection_id"];
201 $placement = $settings["placement"];
202
203 //$col = self::getCollectionByID($collection_id);
204 $collection = new collection($collection_id);
205 $collection->set($collection_id);
206 $collection->setHook($hook);
207 $output = $collection->display(array("echo" => false)); // output default, no echo
208
209 switch($placement) // where to output, rather limited atm.
210 {
211 case "before":
212 $place = "before";
213 break;
214 case "after-before";
215 $place = "both";
216 break;
217 default:
218 $place = "after";
219 break;
220 }
221
222 if($place == 'before' || $place == 'both')
223 {
224 $content = $output . $content;
225 }
226 if($place == 'after' || $place == 'both')
227 {
228
229 $content = $content . $output;
230 }
231 }
232
233 return $content;
234 }
235
236 static function doFooterHooks()
237 {
238 $hook_array = self::$hooks["once"];
239 $hook = self::getCurrentHook();
240
241 if (! isset($hook_array[$hook])) // nothing
242 return;
243
244 $collections = $hook_array[$hook];
245
246 foreach($collections as $settings)
247 {
248 $collection_id = $settings["collection_id"];
249 $placement = $settings["placement"];
250
251 $collection = new collection($collection_id);
252 $collection->setHook($hook);
253
254 $output = $collection->display(array("echo" => false)); // output default, no echo
255
256 do_action('mb-footer', 'collection-' . $collection_id, $output, 'collection_output');
257
258 }
259 }
260
261 static function ajax_save()
262 {
263 $form = maybe_unserialize($_POST['form']);
264
265 $post_form = array();
266 parse_str($form, $post_form);
267
268 $collection_id = intval($post_form["collection_id"]);
269
270 $admin = MB()->getClass('admin');
271
272
273 if (! Install::isPro() && $collection_id == 0)
274 {
275 $collections = self::getCollections();
276 if (count($collections) > 0)
277 {
278 $result = array(
279 "error" => true,
280 );
281 $admin->endAjaxRequest($result);
282 }
283 }
284
285
286
287 $close_text = __('Close', 'maxbuttons');
288
289 $result = array(
290 "error" => false,
291 "body" => __("Saved", 'mbsocial'),
292 "result" => true,
293 "data" => array(),
294 "close_text" => $close_text,
295 "new_nonce" => 0,
296 "title" => __("Saved!","mbsocial"),
297 );
298
299 $collection = new collection();
300 $collection->set($collection_id);
301
302 // this can be a new id (!)
303 $collection_id = $collection->save($post_form);
304
305 $result["data"]["id"] = $collection_id;
306
307 // $result["data"]["new_nonce"] = wp_create_nonce($action . "-" . $collection_id);
308 // $result["data"]["reload"] = apply_filters("collections_ajax_force_reload",$force_reload);
309
310 // $result["title"] = $result_title["success"];
311
312 $admin->endAjaxRequest($result);
313 }
314
315 static function ajax_remove_collection()
316 {
317 $admin = MB()->getClass('admin');
318
319 $collection_id = intval($_POST['param']);
320
321 $url = admin_url('?page=maxbuttons-social');
322
323 $result = array(
324 'error' => false,
325 'redirect' => $url,
326 );
327
328 global $wpdb;
329
330 $table = maxUtils::get_collection_table_name();
331 $where = array("collection_id" => $collection_id);
332 $where_format = array("%d");
333 $wpdb->delete($table, $where, $where_format);
334
335
336 $admin->endAjaxRequest($result);
337
338 }
339
340 static function ajax_action_front()
341 {
342
343 // only for trivial front page actions!
344 self::ajax_action(array("ajax_nopriv" => true));
345 }
346
347 static function ajax_action($args = array())
348 {
349 ob_start();
350 $defaults = array("ajax_nopriv" => false);
351 $args = wp_parse_args($args, $defaults);
352
353
354 $admin = MB()->getClass('admin');
355
356 $nonce = isset($_POST["nonce"]) ? $_POST["nonce"] : false;
357 $block_name = sanitize_text_field($_POST["block_name"]);
358 $block_action = sanitize_text_field($_POST["block_action"]);
359 $block_data = (isset($_POST["block_data"])) ? $_POST["block_data"] : '';
360 $action = sanitize_text_field($_POST["action"]);
361
362 $collection_id = intval($_POST["collection_id"]);
363 // $collection_type = sanitize_text_field($_POST["collection_type"]);
364
365 if(! $args["ajax_nopriv"])
366 {
367 if (! wp_verify_nonce($nonce, $action . "-" . $collection_id))
368 {
369 $result["error"] = true;
370 $result["body"] = __("Nonce not verified","maxbuttons");
371 $result["result"] = false;
372 $result["title"] = __("Security error","maxbuttons");
373 $result["data"] = array("id" => $collection_id);
374
375 $admin->endAjaxRequest($result);
376
377 }
378 }
379
380 $result = array(
381 "error" => false,
382 "body" => '',
383 "result" => true,
384 "data" => array(),
385 "new_nonce" => 0,
386 );
387
388 $collection = new collection($collection_id);
389
390 $result = $collection->doBlockAjax($result, $block_name, $block_action, $block_data);
391
392 //ob_end_clean(); // prevent PHP errors from breaking JSON response.
393
394 $admin->endAjaxRequest($result);
395 $results = $collection->get_meta($name, 'collection_name');
396 }
397
398
399 public static function ajax_refreshblock()
400 {
401 $post = $_POST;
402 $collection_id = intval($_POST['collection_id']);
403 $target_class = sanitize_text_field($_POST['block_name']);
404 //$source_block = sanitize_text_field($_POST['source_block']);
405
406 $postdata = array();
407 parse_str( $_POST['form'], $postdata );
408
409 // invoke correct collection
410 $collection = new collection($collection_id);
411
412
413 $blocks = self::getBlocks();
414 $data = array();
415 foreach($blocks as $block_class)
416 {
417 $block = $collection->getBlock($block_class);
418 $data = $block->save_fields($data, $postdata);
419 }
420 foreach($blocks as $block_class)
421 {
422 $block = $collection->getBlock($block_class);
423 $block->set($data);
424 }
425
426 foreach($data as $blockname => $blockdata)
427 {
428
429 $collection->setData($blockname, $blockdata);
430 }
431 // get the requested block
432 $target_block = $collection->getBlock($target_class);
433 $target_block_name = $target_block->get_name();
434
435 //foreach($blockdata as $source_block => $data)
436 //{
437 // something wrong here ( you're welcome )
438 /* $s_block = $collection->getBlock($source_block);
439 $source_name = $s_block->get_name();
440
441 $s_block->set(array($source_name => $data));
442 maxBlocks::setData(array($source_name => $data ) );
443 } */
444
445 if ($target_block)
446 {
447 ob_start();
448 $target_block->admin();
449 $content = ob_get_contents();
450 ob_clean();
451 }
452
453 $result = array(
454 'status' => true,
455 'content' => $content,
456 'block' => $target_class,
457 'block_name' => $target_block_name,
458 );
459
460 echo json_encode($result);
461
462 exit();
463
464 }
465
466 public static function ajax_getpresets()
467 {
468 $post = $_POST;
469 $admin = mbSocial()->admin();
470
471 $collection_id = isset($post['collection_id']) ? intval($post['collection_id']) : false;
472 //$current_style = isset($post['current_style']) ? sanitize_text_field($post['current_style']): false;
473
474 //$collection = new collection($collection_id);
475
476 $presets = new presets();
477 $sets = $presets->get();
478 $labels = $presets->getLabels();
479
480 $json = array();
481
482 $args = array('preview' => true,
483 'load_type' => 'inline',
484 'echo' => false,
485 'compile' => true,
486 );
487
488 collection::setCount(10);
489
490 foreach($sets as $name => $set)
491 {
492 $set = json_decode($set, ARRAY_A);
493 //$styleBlock = $collection->getBlock('styleBlock');
494 //$styleBlock->add_preview_style = $style_class->class;
495 //$collection->setStyle($style_class);
496
497 $stylename = $set['style']['mbs-style'];
498 $style = styles::getStyle($stylename);
499
500 $collection = new collection($collection_id);
501
502 $collection->setStyle($style);
503
504 foreach($set as $blockname => $blockdata)
505 {
506 $collection->setData($blockname, $blockdata);
507 }
508
509 $networks = $admin->get_networks();
510 $networks = $networks['unselected'];
511
512 $set_active = array();
513 foreach($networks as $nwObj)
514 {
515 $set_active[] = $nwObj->get('network');
516 }
517
518 $collection->setData('network', array('network_active' => $set_active) );
519
520 $label = $labels[$name];
521
522 // $collection->display($args);
523 // $countBlock = $collection->getBlock('countBlock');
524 // $countBlock->resetTotal();
525
526 $json[] = array('name' => $name,
527 'collection' => $collection->display($args),
528 'label' => $label,
529 );
530 }
531
532
533 echo json_encode($json);
534 exit();
535 }
536
537 public static function ajax_setPreset()
538 {
539 $collection_id = intval($_POST['collection_id']);
540 $preset = sanitize_text_field($_POST['preset']);
541
542 $collection = new collection($collection_id);
543
544 $presets = new presets();
545 $sets = $presets->get();
546 $labels = $presets->getLabels();
547
548 $do_set = json_decode($sets[$preset], ARRAY_A);
549
550 foreach($do_set as $blockname => $blockdata)
551 {
552 $collection->setData($blockname, $blockdata);
553 }
554
555 $collection->saveData();
556
557 $collection_id = $collection->getID();
558 $url = admin_url('?page=maxbuttons-social');
559 $url = add_query_arg('social_id', $collection_id, $url);
560
561 echo json_encode( array(
562 'done' => true,
563 'redirect' => $url,
564 ) );
565 exit();
566
567 }
568
569 static function getCollections()
570 {
571 if (! self::$init) self::init();
572
573 global $wpdb;
574
575 $table = maxUtils::get_collection_table_name();
576
577 $sql = "SELECT distinct collection_id from $table WHERE collection_key = 'collection_type' and collection_value = 'social_share' order by collection_id ";
578
579 $results = $wpdb->get_col($sql);
580 return $results;
581 }
582
583
584 /* This will find an user defined collection from the database by ID - */
585 // This function is outdated and should be removed in time
586 /*static function getCollectionByID($id)
587 {
588 $collection = new collection($id);
589
590 return $collection;
591 /* $results = $collection->get_meta($id, 'collection_type');
592
593
594 if ( count($results) == 1)
595 {
596
597 $type = $results["collection_type"];
598
599 $usecol = self::getCollection($type);
600 $usecol->set($id);
601 return $usecol;
602 }
603
604 return false;
605 } */
606
607 /* Find a collection from the database by name */
608 public static function getCollectionbyName($name)
609 {
610 //$collection = new maxCollection();
611 global $wpdb;
612 $sql = "select collection_id from " . maxUtils::get_collection_table_name() . " where collection_key = 'collection_name' and collection_value = %s ";
613 $sql = $wpdb->prepare($sql, $name);
614 $result = $wpdb->get_row($sql, ARRAY_A); // find first
615
616
617 if (count($result) > 0)
618 {
619 if (isset($result["collection_id"]))
620 {
621
622 //$usecol = self::getCollectionByID($result["collection_id"]);
623 $collection = new collection($result['collection_id']);
624 //return $usecol;
625 }
626
627 }
628 return false;
629 }
630
631
632 public static function getBlock($name)
633 {
634 if (! self::$init) self::init();
635
636 if ($index = array_search($name, self::$collectionBlocks))
637 {
638 $classname = __NAMESPACE__ . "\\" . self::$collectionBlocks[$index];
639 return new $classname;
640 }
641 else return false;
642 }
643
644 public static function setBlocks($blocks)
645 {
646 $new_array = array();
647
648 foreach($blocks as $block)
649 {
650 $order = $block['order'];
651 if (isset($new_array[$order]))
652 $new_array[$order][] = $block['class'];
653 else
654 $new_array[$order] = $block['class'];
655
656 }
657 ksort($new_array);
658 self::$collectionBlocks = $new_array;
659
660 }
661
662 public static function getBlocks()
663 {
664 $blocks = array();
665 foreach(self::$collectionBlocks as $index => $block)
666 {
667 $blocks[] = $block;
668 }
669
670 return $blocks;
671 }
672
673
674
675 } // class
676