PluginProbe
Social Share Buttons / 1.19
Social Share Buttons v1.19
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.19, at classes/class-collections.php

694 lines 16.2 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 // When MB PRO is configured to output CSS via file.
142 static function outputFileCSS()
143 {
144 $collection_id = intval($_GET['id']);
145 $doc_id = intval($_GET['doc']);
146 $collection = new collection($collection_id);
147 $css = $collection->getDisplayCSS($doc_id);
148 header( "Content-type: text/css; charset: UTF-8" );
149
150 if ($css)
151 {
152 header( "Content-type: text/css; charset: UTF-8" );
153 echo $css;
154 }
155 exit();
156
157 }
158
159 /* Try to find the current place we are at in the site ( front / blog / page etc ) .*/
160 protected static function getCurrentHook()
161 {
162 $hook = '';
163 if (is_front_page() ) // check if home page ( home page > page )
164 {
165 $hook = "display_homepage";
166 }
167
168 elseif (is_page()) // check if page
169 {
170 $hook = "display_page";
171 }
172 elseif (is_single()) // check if post
173 {
174 $hook = "display_post";
175 }
176 elseif (is_archive() || is_home() && ! is_front_page()) // second stat, is if post main page, in static mode.
177 {
178 $hook = "display_archive";
179 }
180
181 $post_types = \get_post_types(
182 array('public' => true,
183 '_builtin' => false,
184 )
185 );
186
187 $this_post_type = get_post_type( get_the_ID() );
188
189 foreach($post_types as $post_type) // custom post type
190 {
191 if ($post_type == $this_post_type)
192 {
193 $hook = 'display_' . $this_post_type;
194 }
195
196 }
197
198
199 return $hook;
200 }
201
202 static function doContentHooks($content)
203 {
204 $hook_array = self::$hooks["post"];
205 $hook = self::getCurrentHook();
206
207 if ($hook == '')
208 return $content; // nothing
209
210 if (! isset($hook_array[$hook])) // nothing as well
211 return $content;
212
213 $collections = $hook_array[$hook];
214
215 // do all collections on hook -- check for placement as well.
216 foreach($collections as $settings)
217 {
218 $collection_id = $settings["collection_id"];
219 $placement = $settings["placement"];
220
221 //$col = self::getCollectionByID($collection_id);
222 $collection = new collection($collection_id);
223 $collection->set($collection_id);
224 $collection->setHook($hook);
225 $output = $collection->display(array("echo" => false)); // output default, no echo
226
227 switch($placement) // where to output, rather limited atm.
228 {
229 case "before":
230 $place = "before";
231 break;
232 case "after-before";
233 $place = "both";
234 break;
235 default:
236 $place = "after";
237 break;
238 }
239
240 if($place == 'before' || $place == 'both')
241 {
242 $content = $output . $content;
243 }
244 if($place == 'after' || $place == 'both')
245 {
246
247 $content = $content . $output;
248 }
249 }
250
251 return $content;
252 }
253
254 static function doFooterHooks()
255 {
256 $hook_array = self::$hooks["once"];
257 $hook = self::getCurrentHook();
258
259 if (! isset($hook_array[$hook])) // nothing
260 return;
261
262 $collections = $hook_array[$hook];
263
264 foreach($collections as $settings)
265 {
266 $collection_id = $settings["collection_id"];
267 $placement = $settings["placement"];
268
269 $collection = new collection($collection_id);
270 $collection->setHook($hook);
271
272 $output = $collection->display(array("echo" => false)); // output default, no echo
273
274 do_action('mb-footer', 'collection-' . $collection_id, $output, 'collection_output');
275
276 }
277 }
278
279 static function ajax_save()
280 {
281 $form = maybe_unserialize($_POST['form']);
282
283 $post_form = array();
284 parse_str($form, $post_form);
285
286 $collection_id = intval($post_form["collection_id"]);
287
288 $admin = MB()->getClass('admin');
289
290
291 if (! Install::isPro() && $collection_id == 0)
292 {
293 $collections = self::getCollections();
294 if (count($collections) > 0)
295 {
296 $result = array(
297 "error" => true,
298 );
299 $admin->endAjaxRequest($result);
300 }
301 }
302
303
304
305 $close_text = __('Close', 'maxbuttons');
306
307 $result = array(
308 "error" => false,
309 "body" => __("Saved", 'mbsocial'),
310 "result" => true,
311 "data" => array(),
312 "close_text" => $close_text,
313 "new_nonce" => 0,
314 "title" => __("Saved!","mbsocial"),
315 );
316
317 $collection = new collection();
318 $collection->set($collection_id);
319
320 // this can be a new id (!)
321 $collection_id = $collection->save($post_form);
322
323 $result["data"]["id"] = $collection_id;
324
325 // $result["data"]["new_nonce"] = wp_create_nonce($action . "-" . $collection_id);
326 // $result["data"]["reload"] = apply_filters("collections_ajax_force_reload",$force_reload);
327
328 // $result["title"] = $result_title["success"];
329
330 $admin->endAjaxRequest($result);
331 }
332
333 static function ajax_remove_collection()
334 {
335 $admin = MB()->getClass('admin');
336
337 $collection_id = intval($_POST['param']);
338
339 $url = admin_url('?page=maxbuttons-social');
340
341 $result = array(
342 'error' => false,
343 'redirect' => $url,
344 );
345
346 global $wpdb;
347
348 $table = maxUtils::get_collection_table_name();
349 $where = array("collection_id" => $collection_id);
350 $where_format = array("%d");
351 $wpdb->delete($table, $where, $where_format);
352
353
354 $admin->endAjaxRequest($result);
355
356 }
357
358 static function ajax_action_front()
359 {
360
361 // only for trivial front page actions!
362 self::ajax_action(array("ajax_nopriv" => true));
363 }
364
365 static function ajax_action($args = array())
366 {
367 ob_start();
368 $defaults = array("ajax_nopriv" => false);
369 $args = wp_parse_args($args, $defaults);
370
371
372 $admin = MB()->getClass('admin');
373
374 $nonce = isset($_POST["nonce"]) ? $_POST["nonce"] : false;
375 $block_name = sanitize_text_field($_POST["block_name"]);
376 $block_action = sanitize_text_field($_POST["block_action"]);
377 $block_data = (isset($_POST["block_data"])) ? $_POST["block_data"] : '';
378 $action = sanitize_text_field($_POST["action"]);
379
380 $collection_id = intval($_POST["collection_id"]);
381 // $collection_type = sanitize_text_field($_POST["collection_type"]);
382
383 if(! $args["ajax_nopriv"])
384 {
385 if (! wp_verify_nonce($nonce, $action . "-" . $collection_id))
386 {
387 $result["error"] = true;
388 $result["body"] = __("Nonce not verified","maxbuttons");
389 $result["result"] = false;
390 $result["title"] = __("Security error","maxbuttons");
391 $result["data"] = array("id" => $collection_id);
392
393 $admin->endAjaxRequest($result);
394
395 }
396 }
397
398 $result = array(
399 "error" => false,
400 "body" => '',
401 "result" => true,
402 "data" => array(),
403 "new_nonce" => 0,
404 );
405
406 $collection = new collection($collection_id);
407
408 $result = $collection->doBlockAjax($result, $block_name, $block_action, $block_data);
409
410 //ob_end_clean(); // prevent PHP errors from breaking JSON response.
411
412 $admin->endAjaxRequest($result);
413 $results = $collection->get_meta($name, 'collection_name');
414 }
415
416
417 public static function ajax_refreshblock()
418 {
419 $post = $_POST;
420 $collection_id = intval($_POST['collection_id']);
421 $target_class = sanitize_text_field($_POST['block_name']);
422 //$source_block = sanitize_text_field($_POST['source_block']);
423
424 $postdata = array();
425 parse_str( $_POST['form'], $postdata );
426
427 // invoke correct collection
428 $collection = new collection($collection_id);
429
430
431 $blocks = self::getBlocks();
432 $data = array();
433 foreach($blocks as $block_class)
434 {
435 $block = $collection->getBlock($block_class);
436 $data = $block->save_fields($data, $postdata);
437 }
438 foreach($blocks as $block_class)
439 {
440 $block = $collection->getBlock($block_class);
441 $block->set($data);
442 }
443
444 foreach($data as $blockname => $blockdata)
445 {
446
447 $collection->setData($blockname, $blockdata);
448 }
449 // get the requested block
450 $target_block = $collection->getBlock($target_class);
451 $target_block_name = $target_block->get_name();
452
453 //foreach($blockdata as $source_block => $data)
454 //{
455 // something wrong here ( you're welcome )
456 /* $s_block = $collection->getBlock($source_block);
457 $source_name = $s_block->get_name();
458
459 $s_block->set(array($source_name => $data));
460 maxBlocks::setData(array($source_name => $data ) );
461 } */
462
463 if ($target_block)
464 {
465 ob_start();
466 $target_block->admin();
467 $content = ob_get_contents();
468 ob_clean();
469 }
470
471 $result = array(
472 'status' => true,
473 'content' => $content,
474 'block' => $target_class,
475 'block_name' => $target_block_name,
476 );
477
478 echo json_encode($result);
479
480 exit();
481
482 }
483
484 public static function ajax_getpresets()
485 {
486 $post = $_POST;
487 $admin = mbSocial()->admin();
488
489 $collection_id = isset($post['collection_id']) ? intval($post['collection_id']) : false;
490 //$current_style = isset($post['current_style']) ? sanitize_text_field($post['current_style']): false;
491
492 //$collection = new collection($collection_id);
493
494 $presets = new presets();
495 $sets = $presets->get();
496 $labels = $presets->getLabels();
497
498 $json = array();
499
500 $args = array('preview' => true,
501 'load_type' => 'inline',
502 'echo' => false,
503 'compile' => true,
504 );
505
506 collection::setCount(10);
507
508 foreach($sets as $name => $set)
509 {
510 $set = json_decode($set, ARRAY_A);
511 //$styleBlock = $collection->getBlock('styleBlock');
512 //$styleBlock->add_preview_style = $style_class->class;
513 //$collection->setStyle($style_class);
514
515 $stylename = $set['style']['mbs-style'];
516 $style = styles::getStyle($stylename);
517
518 $collection = new collection($collection_id);
519
520 $collection->setStyle($style);
521
522 foreach($set as $blockname => $blockdata)
523 {
524 $collection->setData($blockname, $blockdata);
525 }
526
527 $networks = $admin->get_networks();
528 $networks = $networks['unselected'];
529
530 $set_active = array();
531 foreach($networks as $nwObj)
532 {
533 $set_active[] = $nwObj->get('network');
534 }
535
536 $collection->setData('network', array('network_active' => $set_active) );
537
538 $label = $labels[$name];
539
540 // $collection->display($args);
541 // $countBlock = $collection->getBlock('countBlock');
542 // $countBlock->resetTotal();
543
544 $json[] = array('name' => $name,
545 'collection' => $collection->display($args),
546 'label' => $label,
547 );
548 }
549
550
551 echo json_encode($json);
552 exit();
553 }
554
555 public static function ajax_setPreset()
556 {
557 $collection_id = intval($_POST['collection_id']);
558 $preset = sanitize_text_field($_POST['preset']);
559
560 $collection = new collection($collection_id);
561
562 $presets = new presets();
563 $sets = $presets->get();
564 $labels = $presets->getLabels();
565
566 $do_set = json_decode($sets[$preset], ARRAY_A);
567
568 foreach($do_set as $blockname => $blockdata)
569 {
570 $collection->setData($blockname, $blockdata);
571 }
572
573 $collection->saveData();
574
575 $collection_id = $collection->getID();
576 $url = admin_url('?page=maxbuttons-social');
577 $url = add_query_arg('social_id', $collection_id, $url);
578
579 echo json_encode( array(
580 'done' => true,
581 'redirect' => $url,
582 ) );
583 exit();
584
585 }
586
587 static function getCollections()
588 {
589 if (! self::$init) self::init();
590
591 global $wpdb;
592
593 $table = maxUtils::get_collection_table_name();
594
595 $sql = "SELECT distinct collection_id from $table WHERE collection_key = 'collection_type' and collection_value = 'social_share' order by collection_id ";
596
597 $results = $wpdb->get_col($sql);
598 return $results;
599 }
600
601
602 /* This will find an user defined collection from the database by ID - */
603 // This function is outdated and should be removed in time
604 /*static function getCollectionByID($id)
605 {
606 $collection = new collection($id);
607
608 return $collection;
609 /* $results = $collection->get_meta($id, 'collection_type');
610
611
612 if ( count($results) == 1)
613 {
614
615 $type = $results["collection_type"];
616
617 $usecol = self::getCollection($type);
618 $usecol->set($id);
619 return $usecol;
620 }
621
622 return false;
623 } */
624
625 /* Find a collection from the database by name */
626 public static function getCollectionbyName($name)
627 {
628 //$collection = new maxCollection();
629 global $wpdb;
630 $sql = "select collection_id from " . maxUtils::get_collection_table_name() . " where collection_key = 'collection_name' and collection_value = %s ";
631 $sql = $wpdb->prepare($sql, $name);
632 $result = $wpdb->get_row($sql, ARRAY_A); // find first
633
634
635 if (count($result) > 0)
636 {
637 if (isset($result["collection_id"]))
638 {
639
640 //$usecol = self::getCollectionByID($result["collection_id"]);
641 $collection = new collection($result['collection_id']);
642 //return $usecol;
643 }
644
645 }
646 return false;
647 }
648
649
650 public static function getBlock($name)
651 {
652 if (! self::$init) self::init();
653
654 if ($index = array_search($name, self::$collectionBlocks))
655 {
656 $classname = __NAMESPACE__ . "\\" . self::$collectionBlocks[$index];
657 return new $classname;
658 }
659 else return false;
660 }
661
662 public static function setBlocks($blocks)
663 {
664 $new_array = array();
665
666 foreach($blocks as $block)
667 {
668 $order = $block['order'];
669 if (isset($new_array[$order]))
670 $new_array[$order][] = $block['class'];
671 else
672 $new_array[$order] = $block['class'];
673
674 }
675 ksort($new_array);
676 self::$collectionBlocks = $new_array;
677
678 }
679
680 public static function getBlocks()
681 {
682 $blocks = array();
683 foreach(self::$collectionBlocks as $index => $block)
684 {
685 $blocks[] = $block;
686 }
687
688 return $blocks;
689 }
690
691
692
693 } // class
694