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

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