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

708 lines 16.6 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 $collection = new collection();
290
291
292 if (! Install::isPro() && $collection_id == 0)
293 {
294 $collections = self::getCollections();
295 if (count($collections) > 0)
296 {
297 $result = array(
298 "error" => true,
299 );
300 $collection->endAjaxRequest($result);
301 }
302 }
303
304
305
306 $close_text = __('Close', 'maxbuttons');
307
308 $result = array(
309 "error" => false,
310 "body" => __("Saved", 'mbsocial'),
311 "result" => true,
312 "data" => array(),
313 "close_text" => $close_text,
314 "new_nonce" => 0,
315 "title" => __("Saved!","mbsocial"),
316 );
317
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 $collection->endAjaxRequest($result);
331 }
332
333 static function ajax_remove_collection()
334 {
335 $admin = MB()->getClass('admin');
336
337 $collection_id = intval($_POST['param']);
338 $collection = new collection();
339
340
341 $url = admin_url('?page=maxbuttons-social');
342
343 $result = array(
344 'error' => false,
345 'redirect' => $url,
346 );
347
348 global $wpdb;
349
350 $table = maxUtils::get_collection_table_name();
351 $where = array("collection_id" => $collection_id);
352 $where_format = array("%d");
353 $wpdb->delete($table, $where, $where_format);
354
355
356 $collection->endAjaxRequest($result);
357
358 }
359
360 static function ajax_action_front()
361 {
362 // only for trivial front page actions!
363 self::ajax_action(array("ajax_nopriv" => true));
364 }
365
366 static function ajax_action($args = array())
367 {
368 ob_start();
369 $defaults = array("ajax_nopriv" => false);
370 $args = wp_parse_args($args, $defaults);
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 // Weirdly only one, but security, lets checks this without changing other code too much
381 $possible_actions =['mbsocial_get_count'];
382
383 $collection_id = intval($_POST["collection_id"]);
384 $collection = new collection($collection_id);
385
386 if (false === in_array($action, $possible_actions))
387 {
388 $result["error"] = true;
389 $result["body"] = __("Bad Action","maxbuttons");
390 $result["result"] = false;
391 $result["title"] = __("Security error","maxbuttons");
392 $result["data"] = array("id" => $collection_id);
393
394 $collection->endAjaxRequest($result);
395 }
396
397
398 // $collection_type = sanitize_text_field($_POST["collection_type"]);
399
400 //if(! $args["ajax_nopriv"])
401 //{
402 if (! wp_verify_nonce($nonce, "maxsocial_ajax_action-" . $collection_id))
403 {
404 $result["error"] = true;
405 $result["body"] = __("Nonce not verified","maxbuttons");
406 $result["result"] = false;
407 $result["title"] = __("Security error","maxbuttons");
408 $result["data"] = array("id" => $collection_id);
409
410 $collection->endAjaxRequest($result);
411
412 }
413 //}
414
415 $result = array(
416 "error" => false,
417 "body" => '',
418 "result" => true,
419 "data" => array(),
420 "new_nonce" => 0,
421 );
422
423 $result = $collection->doBlockAjax($result, $block_name, $block_action, $block_data);
424
425 //ob_end_clean(); // prevent PHP errors from breaking JSON response.
426
427 $collection->endAjaxRequest($result);
428 }
429
430
431 public static function ajax_refreshblock()
432 {
433 $post = $_POST;
434 $collection_id = intval($_POST['collection_id']);
435 $target_class = sanitize_text_field($_POST['block_name']);
436 //$source_block = sanitize_text_field($_POST['source_block']);
437
438 $postdata = array();
439 parse_str( $_POST['form'], $postdata );
440
441 // invoke correct collection
442 $collection = new collection($collection_id);
443
444
445 $blocks = self::getBlocks();
446 $data = array();
447 foreach($blocks as $block_class)
448 {
449 $block = $collection->getBlock($block_class);
450 $data = $block->save_fields($data, $postdata);
451 }
452 foreach($blocks as $block_class)
453 {
454 $block = $collection->getBlock($block_class);
455 $block->set($data);
456 }
457
458 foreach($data as $blockname => $blockdata)
459 {
460
461 $collection->setData($blockname, $blockdata);
462 }
463 // get the requested block
464 $target_block = $collection->getBlock($target_class);
465 $target_block_name = $target_block->get_name();
466
467 //foreach($blockdata as $source_block => $data)
468 //{
469 // something wrong here ( you're welcome )
470 /* $s_block = $collection->getBlock($source_block);
471 $source_name = $s_block->get_name();
472
473 $s_block->set(array($source_name => $data));
474 maxBlocks::setData(array($source_name => $data ) );
475 } */
476
477 if ($target_block)
478 {
479 ob_start();
480 $target_block->admin();
481 $content = ob_get_contents();
482 ob_clean();
483 }
484
485 $result = array(
486 'status' => true,
487 'content' => $content,
488 'block' => $target_class,
489 'block_name' => $target_block_name,
490 );
491
492 echo json_encode($result);
493
494 exit();
495
496 }
497
498 public static function ajax_getpresets()
499 {
500 $post = $_POST;
501 $admin = mbSocial()->admin();
502
503 $collection_id = isset($post['collection_id']) ? intval($post['collection_id']) : false;
504 //$current_style = isset($post['current_style']) ? sanitize_text_field($post['current_style']): false;
505
506 //$collection = new collection($collection_id);
507
508 $presets = new presets();
509 $sets = $presets->get();
510 $labels = $presets->getLabels();
511
512 $json = array();
513
514 $args = array('preview' => true,
515 'load_type' => 'inline',
516 'echo' => false,
517 'compile' => true,
518 );
519
520 collection::setCount(10);
521
522 foreach($sets as $name => $set)
523 {
524 $set = json_decode($set, ARRAY_A);
525 //$styleBlock = $collection->getBlock('styleBlock');
526 //$styleBlock->add_preview_style = $style_class->class;
527 //$collection->setStyle($style_class);
528
529 $stylename = $set['style']['mbs-style'];
530 $style = styles::getStyle($stylename);
531
532 $collection = new collection($collection_id);
533
534 $collection->setStyle($style);
535
536 foreach($set as $blockname => $blockdata)
537 {
538 $collection->setData($blockname, $blockdata);
539 }
540
541 $networks = $admin->get_networks();
542 $networks = $networks['unselected'];
543
544 $set_active = array();
545 foreach($networks as $nwObj)
546 {
547 $set_active[] = $nwObj->get('network');
548 }
549
550 $collection->setData('network', array('network_active' => $set_active) );
551
552 $label = $labels[$name];
553
554 // $collection->display($args);
555 // $countBlock = $collection->getBlock('countBlock');
556 // $countBlock->resetTotal();
557
558 $json[] = array('name' => $name,
559 'collection' => $collection->display($args),
560 'label' => $label,
561 );
562 }
563
564
565 echo json_encode($json);
566 exit();
567 }
568
569 public static function ajax_setPreset()
570 {
571 $collection_id = intval($_POST['collection_id']);
572 $preset = sanitize_text_field($_POST['preset']);
573
574 $collection = new collection($collection_id);
575
576 $presets = new presets();
577 $sets = $presets->get();
578 $labels = $presets->getLabels();
579
580 $do_set = json_decode($sets[$preset], ARRAY_A);
581
582 foreach($do_set as $blockname => $blockdata)
583 {
584 $collection->setData($blockname, $blockdata);
585 }
586
587 $collection->saveData();
588
589 $collection_id = $collection->getID();
590 $url = admin_url('?page=maxbuttons-social');
591 $url = add_query_arg('social_id', $collection_id, $url);
592
593 echo json_encode( array(
594 'done' => true,
595 'redirect' => $url,
596 ) );
597 exit();
598
599 }
600
601 static function getCollections()
602 {
603 if (! self::$init) self::init();
604
605 global $wpdb;
606
607 $table = maxUtils::get_collection_table_name();
608
609 $sql = "SELECT distinct collection_id from $table WHERE collection_key = 'collection_type' and collection_value = 'social_share' order by collection_id ";
610
611 $results = $wpdb->get_col($sql);
612 return $results;
613 }
614
615
616 /* This will find an user defined collection from the database by ID - */
617 // This function is outdated and should be removed in time
618 /*static function getCollectionByID($id)
619 {
620 $collection = new collection($id);
621
622 return $collection;
623 /* $results = $collection->get_meta($id, 'collection_type');
624
625
626 if ( count($results) == 1)
627 {
628
629 $type = $results["collection_type"];
630
631 $usecol = self::getCollection($type);
632 $usecol->set($id);
633 return $usecol;
634 }
635
636 return false;
637 } */
638
639 /* Find a collection from the database by name */
640 public static function getCollectionbyName($name)
641 {
642 //$collection = new maxCollection();
643 global $wpdb;
644 $sql = "select collection_id from " . maxUtils::get_collection_table_name() . " where collection_key = 'collection_name' and collection_value = %s ";
645 $sql = $wpdb->prepare($sql, $name);
646 $result = $wpdb->get_row($sql, ARRAY_A); // find first
647
648
649 if (count($result) > 0)
650 {
651 if (isset($result["collection_id"]))
652 {
653
654 //$usecol = self::getCollectionByID($result["collection_id"]);
655 $collection = new collection($result['collection_id']);
656 //return $usecol;
657 }
658
659 }
660 return false;
661 }
662
663
664 public static function getBlock($name)
665 {
666 if (! self::$init) self::init();
667
668 if ($index = array_search($name, self::$collectionBlocks))
669 {
670 $classname = __NAMESPACE__ . "\\" . self::$collectionBlocks[$index];
671 return new $classname;
672 }
673 else return false;
674 }
675
676 public static function setBlocks($blocks)
677 {
678 $new_array = array();
679
680 foreach($blocks as $block)
681 {
682 $order = $block['order'];
683 if (isset($new_array[$order]))
684 $new_array[$order][] = $block['class'];
685 else
686 $new_array[$order] = $block['class'];
687
688 }
689 ksort($new_array);
690 self::$collectionBlocks = $new_array;
691
692 }
693
694 public static function getBlocks()
695 {
696 $blocks = array();
697 foreach(self::$collectionBlocks as $index => $block)
698 {
699 $blocks[] = $block;
700 }
701
702 return $blocks;
703 }
704
705
706
707 } // class
708