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

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