| 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; |
| 15 |
|
| 16 |
use Buttonizer\Legacy\Frontend\Group\Button\Button; |
| 17 |
use Buttonizer\Legacy\Frontend\Group\Group; |
| 18 |
use Buttonizer\Legacy\Frontend\PageRules\PageRules; |
| 19 |
use Buttonizer\Legacy\Frontend\TimeSchedules\TimeSchedules; |
| 20 |
use Buttonizer\Core\Utils\PermissionCheck; |
| 21 |
class Buttonizer { |
| 22 |
private $buttonGroups; |
| 23 |
|
| 24 |
private $groupReturns = []; |
| 25 |
|
| 26 |
// Time schedule object |
| 27 |
private static $timeSchedules = null; |
| 28 |
|
| 29 |
private static $pageRules = null; |
| 30 |
|
| 31 |
private static $logs = []; |
| 32 |
|
| 33 |
private static $pageData = null; |
| 34 |
|
| 35 |
// Current page |
| 36 |
private static $currentPage = -1; |
| 37 |
|
| 38 |
private static $currentCategories = []; |
| 39 |
|
| 40 |
private static $currentUrl = ''; |
| 41 |
|
| 42 |
private static $currentPageTitle = ''; |
| 43 |
|
| 44 |
private static $currentPageIs404 = false; |
| 45 |
|
| 46 |
private static $currentPageIsFrontPage = false; |
| 47 |
|
| 48 |
private static $currentUserRoles = ''; |
| 49 |
|
| 50 |
private static $published = '_published'; |
| 51 |
|
| 52 |
/** |
| 53 |
* Buttons constructor. |
| 54 |
*/ |
| 55 |
public function __construct( $noAjax = false ) { |
| 56 |
if ( isset( $_GET['preview'] ) && $_GET['preview'] === '1' && PermissionCheck::hasPermission() ) { |
| 57 |
self::$published = ''; |
| 58 |
} |
| 59 |
// Get groups |
| 60 |
$this->buttonGroups = get_option( self::getSettingName( 'buttonizer_buttons' ) ); |
| 61 |
// No found groups |
| 62 |
if ( !is_array( $this->buttonGroups ) ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
if ( isset( $this->buttonGroups[0] ) ) { |
| 66 |
$this->createGroup( $this->buttonGroups[0] ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* @param $data |
| 72 |
*/ |
| 73 |
private function createGroup( $data ) { |
| 74 |
$group = new Group($data["data"]); |
| 75 |
// Add buttons to group |
| 76 |
foreach ( $data['buttons'] as $button ) { |
| 77 |
$group->add( new Button($group, $button) ); |
| 78 |
} |
| 79 |
if ( $group->show() ) { |
| 80 |
$groupData = $group->fix(); |
| 81 |
isset( $data["menu_button"] ) && ($groupData["menu_button"] = $data["menu_button"]); |
| 82 |
$this->groupReturns[] = $groupData; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* @param $id |
| 88 |
* @return bool |
| 89 |
*/ |
| 90 |
public static function isOpened( $id ) { |
| 91 |
return true; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Page rules |
| 96 |
* |
| 97 |
* @param $id |
| 98 |
* @return bool |
| 99 |
*/ |
| 100 |
public static function isActive( $id ) { |
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Returns array |
| 106 |
* |
| 107 |
* @return array |
| 108 |
*/ |
| 109 |
public function returnArray() { |
| 110 |
return $this->groupReturns; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* @param $message |
| 115 |
* @return bool |
| 116 |
*/ |
| 117 |
public static function addWarning( $message ) { |
| 118 |
self::$logs[] = [ |
| 119 |
'type' => 'warning', |
| 120 |
'message' => $message, |
| 121 |
]; |
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* @param array $message |
| 127 |
* @return bool |
| 128 |
*/ |
| 129 |
public static function addEvent( $messageData ) { |
| 130 |
self::$logs[] = $messageData; |
| 131 |
return true; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Get setting name |
| 136 |
* |
| 137 |
* @param string $string |
| 138 |
* @return string |
| 139 |
*/ |
| 140 |
public static function getSettingName( $string = '' ) { |
| 141 |
return $string . self::$published; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Is the user in preview mode? |
| 146 |
* @return boolean |
| 147 |
*/ |
| 148 |
public static function isPreview() { |
| 149 |
return self::$published === ""; |
| 150 |
} |
| 151 |
|
| 152 |
/********************************** |
| 153 |
* Current page data |
| 154 |
*/ |
| 155 |
/** |
| 156 |
* @return array |
| 157 |
*/ |
| 158 |
public static function getLogs() { |
| 159 |
// Not in preview |
| 160 |
if ( !self::isPreview() ) { |
| 161 |
return []; |
| 162 |
} |
| 163 |
return self::$logs; |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Get current page url |
| 168 |
* @param bool $baseUrl |
| 169 |
* @return string |
| 170 |
*/ |
| 171 |
public static function getUrl( $baseUrl = true ) { |
| 172 |
if ( $baseUrl === false ) { |
| 173 |
return str_replace( get_site_url(), "", self::$currentUrl ); |
| 174 |
} |
| 175 |
return self::$currentUrl; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Get current page |
| 180 |
*/ |
| 181 |
public static function getPage() { |
| 182 |
return self::$currentPage; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Get current post categories |
| 187 |
*/ |
| 188 |
public static function getCategories() { |
| 189 |
return self::$currentCategories; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Is the current page a front page? |
| 194 |
*/ |
| 195 |
public static function getTitle() { |
| 196 |
return strtolower( self::$currentPageTitle ); |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Is the current page a 404? |
| 201 |
*/ |
| 202 |
public static function is_404() { |
| 203 |
return self::$currentPageIs404; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Is the current page a front page? |
| 208 |
*/ |
| 209 |
public static function isFrontPage() { |
| 210 |
return self::$currentPageIsFrontPage; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Return the user role of the current user |
| 215 |
*/ |
| 216 |
public static function getUserRoles() { |
| 217 |
if ( self::$currentUserRoles === "" ) { |
| 218 |
$userRoles = get_userdata( get_current_user_id() )->roles; |
| 219 |
// If not logged in, add guest role in roles |
| 220 |
if ( !\is_user_logged_in() ) { |
| 221 |
$userRoles[] = "guest"; |
| 222 |
} |
| 223 |
self::$currentUserRoles = $userRoles; |
| 224 |
} |
| 225 |
return self::$currentUserRoles; |
| 226 |
} |
| 227 |
|
| 228 |
} |
| 229 |
|