| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* SOFTWARE LICENSE INFORMATION |
| 5 |
* |
| 6 |
* Copyright (c) 2017 Buttonizer, all rights reserved. |
| 7 |
* |
| 8 |
* This file is part of Buttonizer |
| 9 |
* |
| 10 |
* For detailed information regarding to the licensing of |
| 11 |
* this software, please review the license.txt or visit: |
| 12 |
* https://buttonizer.pro/license/ |
| 13 |
*/ |
| 14 |
namespace Buttonizer\Legacy\Frontend\Group; |
| 15 |
|
| 16 |
use Buttonizer\Legacy\Frontend\Group\Button\Button; |
| 17 |
use Buttonizer\Legacy\Frontend\Buttonizer; |
| 18 |
use Buttonizer\Core\Utils\Settings; |
| 19 |
class Group { |
| 20 |
private $buttons = []; |
| 21 |
|
| 22 |
private $data; |
| 23 |
|
| 24 |
private $noLimit; |
| 25 |
|
| 26 |
private $totalButtons = 0; |
| 27 |
|
| 28 |
private $countMobile = 0; |
| 29 |
|
| 30 |
private $countDesktop = 0; |
| 31 |
|
| 32 |
/** |
| 33 |
* Buttons constructor. |
| 34 |
* @param $data |
| 35 |
*/ |
| 36 |
public function __construct( $data ) { |
| 37 |
$this->data = $data; |
| 38 |
$this->noLimit = Settings::getSetting( "no_limit" ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Return option data |
| 43 |
* |
| 44 |
* @param $key |
| 45 |
* @param $default null |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
private function getOption( $key, $default = '' ) { |
| 49 |
return ( isset( $this->data[$key] ) ? $this->data[$key] : $default ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Return option data as boolean |
| 54 |
* |
| 55 |
* @param $key |
| 56 |
* @param $default false |
| 57 |
* @return boolean |
| 58 |
*/ |
| 59 |
public function getBoolean( $key, $default = false ) { |
| 60 |
return ( isset( $this->data[$key] ) ? filter_var( $this->data[$key], FILTER_VALIDATE_BOOLEAN, [ |
| 61 |
'options' => [ |
| 62 |
'default' => false, |
| 63 |
], |
| 64 |
] ) === true : $default ); |
| 65 |
} |
| 66 |
|
| 67 |
public function getId() { |
| 68 |
return $this->getOption( "id", null ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Add button |
| 73 |
* |
| 74 |
* @param Button $button |
| 75 |
*/ |
| 76 |
public function add( Button $button ) { |
| 77 |
$this->totalButtons++; |
| 78 |
// Show button (on page OR only when opened |
| 79 |
if ( $button->showButton() ) { |
| 80 |
if ( !$this->noLimit ) { |
| 81 |
if ( ($button->getBoolean( 'show_desktop' ) || $button->getBoolean( 'show_mobile' )) && ($this->countDesktop >= 7 || $this->countMobile >= 7) ) { |
| 82 |
// Is desktop, but no place on desktop? Force hide on desktop |
| 83 |
if ( $button->getBoolean( 'show_desktop' ) && $this->countDesktop >= 7 && $this->countMobile < 7 && $button->getBoolean( 'show_mobile' ) ) { |
| 84 |
$button->setOption( "show_desktop", false ); |
| 85 |
} else { |
| 86 |
if ( $button->getBoolean( 'show_mobile' ) && $this->countMobile >= 7 && $this->countDesktop < 7 && $button->getBoolean( 'show_desktop' ) ) { |
| 87 |
$button->setOption( "show_mobile", false ); |
| 88 |
} else { |
| 89 |
return; |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
// Add mobile |
| 95 |
if ( $button->getBoolean( 'show_mobile' ) ) { |
| 96 |
$this->countMobile++; |
| 97 |
} |
| 98 |
// Add desktop |
| 99 |
if ( $button->getBoolean( 'show_desktop' ) ) { |
| 100 |
$this->countDesktop++; |
| 101 |
} |
| 102 |
$this->buttons[] = $button->generate(); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Show group? |
| 108 |
* |
| 109 |
* @return bool |
| 110 |
*/ |
| 111 |
public function show() { |
| 112 |
// Hide on all devices |
| 113 |
if ( !$this->getBoolean( 'show_desktop' ) && !$this->getBoolean( 'show_mobile' ) && !$this->getBoolean( 'single_button_mode' ) ) { |
| 114 |
Buttonizer::addEvent( [ |
| 115 |
"id" => $this->getOption( 'id', null ), |
| 116 |
"name" => $this->getOption( 'name', "Unnamed" ), |
| 117 |
"button_type" => "group", |
| 118 |
"message" => __( 'The group is hidden on all devices', 'buttonizer-multifunctional-button' ), |
| 119 |
"type" => "all_devices_hidden", |
| 120 |
] ); |
| 121 |
return; |
| 122 |
} |
| 123 |
return ( count( $this->buttons ) > 0 ? true : false ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Output |
| 128 |
*/ |
| 129 |
public function fix() { |
| 130 |
return [ |
| 131 |
"data" => $this->data, |
| 132 |
"buttons" => $this->buttons, |
| 133 |
]; |
| 134 |
} |
| 135 |
|
| 136 |
} |
| 137 |
|