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

578 lines 13.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
30 public static function shortcode($atts, $content = null)
31 {
32 $display_args = shortcode_atts(array(
33 'id' => 0,
34 "echo" => false,
35 "mode" => "normal",
36 "nocache" => false,
37 "style" => 'footer',
38 ),
39
40 $atts);
41
42 $collection_id = $display_args['id'];
43
44 if($collection_id == 0)
45 return ''; // nothing.
46
47 $collection = new Collection($collection_id);
48
49 //$this->set($collection_id);
50 $display_args["compile"] = $display_args["nocache"];
51 $display_args['load_type'] = $display_args['style'];
52 unset($display_args['style']);
53 unset($display_args["nocache"]);
54
55 $output = $collection->display($display_args);
56
57 return $output;
58 }
59
60 /*
61 Check our custom transients for expiration. This should be done only once per run or less.
62 */
63 static function checkExpireTrans()
64 {
65 if (! self::$transientChecked)
66 {
67 maxUtils::removeExpiredTrans(); // instead of on each button to reduce load.
68 self::$transientChecked = true;
69 }
70 }
71 /*
72 Function to hook into WordPress for automatic display of collections.
73 */
74 static function setupHooks()
75 {
76 // check for admin side, we need not hooks nor queries for this.
77 if (is_admin())
78 return;
79
80 global $pagenow;
81 if ( in_array($pagenow, array('wp-login.php', 'wp-register.php')) )
82 return;
83
84 global $wpdb;
85 $table = maxUtils::get_collection_table_name();
86 $sql = "select collection_id, collection_key, collection_value from $table where
87 collection_key like 'display_%' and collection_id IN (select collection_id from $table where collection_key = 'collection_type'
88 and collection_value = 'social_share' ) ";
89 $results = $wpdb->get_results($sql, ARRAY_A);
90
91 $hook_array = array();
92
93 foreach($results as $result)
94 {
95 $id = $result["collection_id"];
96 $key = $result["collection_key"];
97 $placement = $result["collection_value"];
98
99 // override - if needed you can specify a different placement for each position
100 $placement = apply_filters("mb-collection-$id-$key-placement", $placement);
101
102 switch($placement)
103 {
104 case "after":
105 case "before":
106 case "after-before":
107 $show_type = "post"; // show on the post loop
108 break;
109 case "static";
110 $show_type = "once"; // show once - otherwise for overviews they will be many static buttons.
111 break;
112 case "hidden":
113 default:
114 $show_type = 'none';
115 break;
116 }
117
118 if ($show_type != 'none')
119 $hook_array[$show_type][$key][] = array("collection_id" => $id, "placement" => $placement);
120 }
121
122 self::$hooks = $hook_array;
123
124 if (isset($hook_array["post"]) && count($hook_array["post"]) > 0)
125 {
126 add_filter("the_content", array(MBSocial()->admin()->namespaceit('collections'), "doContentHooks"));
127 }
128 if (isset($hook_array["once"]) && count($hook_array["once"]) > 0)
129 {
130 //self::doFooterHooks(); // the stuff that goes once.
131 add_action('wp_head', array(MBSocial()->admin()->namespaceit('collections'), 'doFooterHooks'));
132 }
133
134 if (count($hook_array) > 0)
135 return true; // yes, bind action and check
136 else
137 return false; // no binds, don't check.
138 }
139
140 /* Try to find the current place we are at in the site ( front / blog / page etc ) .*/
141 protected static function getCurrentHook()
142 {
143 $hook = '';
144 if (is_front_page() || is_home() ) // check if home page ( home page > page )
145 {
146 $hook = "display_homepage";
147
148 }
149 elseif (is_page()) // check if page
150 {
151 $hook = "display_page";
152 }
153 elseif (is_single()) // check if post
154 {
155 $hook = "display_post";
156 }
157 elseif (is_archive())
158 {
159 $hook = "display_archive";
160 }
161
162 $post_types = \get_post_types(
163 array('public' => true,
164 '_builtin' => false,
165 )
166 );
167
168 $this_post_type = get_post_type( get_the_ID() );
169
170 foreach($post_types as $post_type) // custom post type
171 {
172 if ($post_type == $this_post_type)
173 {
174 $hook = 'display_' . $this_post_type;
175 }
176
177 }
178
179 return $hook;
180 }
181
182 static function doContentHooks($content)
183 {
184 $hook_array = self::$hooks["post"];
185 $hook = self::getCurrentHook();
186
187 if ($hook == '')
188 return $content; // nothing
189
190 if (! isset($hook_array[$hook])) // nothing as well
191 return $content;
192
193 $collections = $hook_array[$hook];
194
195 // do all collections on hook -- check for placement as well.
196 foreach($collections as $settings)
197 {
198 $collection_id = $settings["collection_id"];
199 $placement = $settings["placement"];
200
201 //$col = self::getCollectionByID($collection_id);
202 $collection = new collection($collection_id);
203 $collection->set($collection_id);
204 $collection->setHook($hook);
205 $output = $collection->display(array("echo" => false)); // output default, no echo
206
207 switch($placement) // where to output, rather limited atm.
208 {
209 case "before":
210 $place = "before";
211 break;
212 case "after-before";
213 $place = "both";
214 break;
215 default:
216 $place = "after";
217 break;
218 }
219
220 if($place == 'before' || $place == 'both')
221 {
222 $content = $output . $content;
223 }
224 if($place == 'after' || $place == 'both')
225 {
226
227 $content = $content . $output;
228 }
229 }
230
231 return $content;
232 }
233
234 static function doFooterHooks()
235 {
236 $hook_array = self::$hooks["once"];
237 $hook = self::getCurrentHook();
238
239 if (! isset($hook_array[$hook])) // nothing
240 return;
241
242 $collections = $hook_array[$hook];
243
244 foreach($collections as $settings)
245 {
246 $collection_id = $settings["collection_id"];
247 $placement = $settings["placement"];
248 // echo " SHOW $collection_id on $placement ";
249 //$col = self::getCollectionByID($collection_id);
250 //$col->set($collection_id);
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 $close_text = __('Close', 'maxbuttons');
274
275 $result = array(
276 "error" => false,
277 "body" => __("Saved", 'mbsocial'),
278 "result" => true,
279 "data" => array(),
280 "close_text" => $close_text,
281 "new_nonce" => 0,
282 "title" => __("Saved!","mbsocial"),
283 );
284
285
286
287
288 $collection = new collection();
289 $collection->set($collection_id);
290
291
292 // this can be a new id (!)
293 $collection_id = $collection->save($post_form);
294
295 $result["data"]["id"] = $collection_id;
296
297 // $result["data"]["new_nonce"] = wp_create_nonce($action . "-" . $collection_id);
298 // $result["data"]["reload"] = apply_filters("collections_ajax_force_reload",$force_reload);
299
300 // $result["title"] = $result_title["success"];
301
302 $admin->endAjaxRequest($result);
303
304 //echo json_encode($result);
305 //exit();
306 }
307
308 static function ajax_action_front()
309 {
310
311 // only for trivial front page actions!
312 self::ajax_action(array("ajax_nopriv" => true));
313 }
314
315 static function ajax_action($args = array())
316 {
317 ob_start();
318 $defaults = array("ajax_nopriv" => false);
319 $args = wp_parse_args($args, $defaults);
320
321
322 $admin = MB()->getClass('admin');
323
324 $nonce = isset($_POST["nonce"]) ? $_POST["nonce"] : false;
325 $block_name = sanitize_text_field($_POST["block_name"]);
326 $block_action = sanitize_text_field($_POST["block_action"]);
327 $block_data = (isset($_POST["block_data"])) ? $_POST["block_data"] : '';
328 $action = sanitize_text_field($_POST["action"]);
329
330 $collection_id = intval($_POST["collection_id"]);
331 // $collection_type = sanitize_text_field($_POST["collection_type"]);
332
333 if(! $args["ajax_nopriv"])
334 {
335 if (! wp_verify_nonce($nonce, $action . "-" . $collection_id))
336 {
337 $result["error"] = true;
338 $result["body"] = __("Nonce not verified","maxbuttons");
339 $result["result"] = false;
340 $result["title"] = __("Security error","maxbuttons");
341 $result["data"] = array("id" => $collection_id);
342
343 $admin->endAjaxRequest($result);
344
345 }
346 }
347
348 $result = array(
349 "error" => false,
350 "body" => '',
351 "result" => true,
352 "data" => array(),
353 "new_nonce" => 0,
354 );
355
356 $collection = new collection($collection_id);
357
358 $result = $collection->doBlockAjax($result, $block_name, $block_action, $block_data);
359
360 //ob_end_clean(); // prevent PHP errors from breaking JSON response.
361
362 $admin->endAjaxRequest($result);
363 $results = $collection->get_meta($name, 'collection_name');
364 }
365
366
367 public static function ajax_refreshblock()
368 {
369 $post = $_POST;
370 $collection_id = intval($_POST['collection_id']);
371 $target_class = sanitize_text_field($_POST['block_name']);
372 //$source_block = sanitize_text_field($_POST['source_block']);
373
374 $postdata = array();
375 parse_str( $_POST['form'], $postdata );
376
377 // invoke correct collection
378 $collection = new collection($collection_id);
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 $collection->setData($data);
394
395 // get the requested block
396 $target_block = $collection->getBlock($target_class);
397 $target_block_name = $target_block->get_name();
398
399 //foreach($blockdata as $source_block => $data)
400 //{
401 // something wrong here ( you're welcome )
402 /* $s_block = $collection->getBlock($source_block);
403 $source_name = $s_block->get_name();
404
405 $s_block->set(array($source_name => $data));
406 maxBlocks::setData(array($source_name => $data ) );
407 } */
408
409 if ($target_block)
410 {
411 ob_start();
412 $target_block->admin();
413 $content = ob_get_contents();
414 ob_clean();
415 }
416
417 $result = array(
418 'status' => true,
419 'content' => $content,
420 'block' => $target_class,
421 'block_name' => $target_block_name,
422 );
423
424 echo json_encode($result);
425
426 exit();
427
428 }
429
430 public static function ajax_getstyles()
431 {
432 $post = $_POST;
433
434 $collection_id = isset($post['collection_id']) ? intval($post['collection_id']) : false;
435 $current_style = isset($post['current_style']) ? sanitize_text_field($post['current_style']): false;
436
437 $collection = new collection($collection_id);
438
439 $styles = styles::getStyles();
440
441 $json = array();
442
443 $args = array('preview' => true,
444 'load_type' => 'inline',
445 'echo' => false,
446 'compile' => true,
447 );
448
449 foreach($styles as $order => $style_classes)
450 {
451 foreach($style_classes as $style_class)
452 {
453 $styleBlock = $collection->getBlock('styleBlock');
454 $styleBlock->add_preview_style = $style_class->class;
455 $collection->setStyle($style_class);
456
457 $countBlock = $collection->getBlock('countBlock');
458 $countBlock->resetTotal();
459
460 $json[$style_class->name] = array('class' => $style_class->class,
461 'collection' => $collection->display($args),
462 'description' => $style_class->description,
463 );
464 }
465 }
466
467 echo json_encode($json);
468 exit();
469 }
470
471 static function getCollections()
472 {
473 if (! self::$init) self::init();
474
475 global $wpdb;
476
477 $table = maxUtils::get_collection_table_name();
478
479 $sql = "SELECT distinct collection_id from $table WHERE collection_key = 'collection_type' and collection_value = 'social_share' ";
480
481 $results = $wpdb->get_col($sql);
482 return $results;
483 }
484
485
486 /* This will find an user defined collection from the database by ID - */
487 // This function is outdated and should be removed in time
488 /*static function getCollectionByID($id)
489 {
490 $collection = new collection($id);
491
492 return $collection;
493 /* $results = $collection->get_meta($id, 'collection_type');
494
495
496 if ( count($results) == 1)
497 {
498
499 $type = $results["collection_type"];
500
501 $usecol = self::getCollection($type);
502 $usecol->set($id);
503 return $usecol;
504 }
505
506 return false;
507 } */
508
509 /* Find a collection from the database by name */
510 public static function getCollectionbyName($name)
511 {
512 //$collection = new maxCollection();
513 global $wpdb;
514 $sql = "select collection_id from " . maxUtils::get_collection_table_name() . " where collection_key = 'collection_name' and collection_value = %s ";
515 $sql = $wpdb->prepare($sql, $name);
516 $result = $wpdb->get_row($sql, ARRAY_A); // find first
517
518
519 if (count($result) > 0)
520 {
521 if (isset($result["collection_id"]))
522 {
523
524 //$usecol = self::getCollectionByID($result["collection_id"]);
525 $collection = new collection($result['collection_id']);
526 //return $usecol;
527 }
528
529 }
530 return false;
531 }
532
533
534 public static function getBlock($name)
535 {
536 if (! self::$init) self::init();
537
538 if ($index = array_search($name, self::$collectionBlocks))
539 {
540 $classname = __NAMESPACE__ . "\\" . self::$collectionBlocks[$index];
541 return new $classname;
542 }
543 else return false;
544 }
545
546 public static function setBlocks($blocks)
547 {
548 $new_array = array();
549
550 foreach($blocks as $block)
551 {
552 $order = $block['order'];
553 if (isset($new_array[$order]))
554 $new_array[$order][] = $block['class'];
555 else
556 $new_array[$order] = $block['class'];
557
558 }
559 ksort($new_array);
560 self::$collectionBlocks = $new_array;
561
562 }
563
564 public static function getBlocks()
565 {
566 $blocks = array();
567 foreach(self::$collectionBlocks as $index => $block)
568 {
569 $blocks[] = $block;
570 }
571
572 return $blocks;
573 }
574
575
576
577 } // class
578