| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 1.0.8 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2021 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\Helpers; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
use FPFramework\Libs\Registry; |
| 20 |
|
| 21 |
class BoxHelper |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Gets all Boxes. |
| 25 |
* We use get_posts instead of firebox()->tables->box->getResults |
| 26 |
* as we need it on Widgets initialization where we don't have the framework |
| 27 |
* available yet to query the database with our wrapper |
| 28 |
* |
| 29 |
* @return array |
| 30 |
*/ |
| 31 |
public static function getAllBoxes($status = 'publish') |
| 32 |
{ |
| 33 |
$payload = [ |
| 34 |
'post_status' => $status, |
| 35 |
'post_type' => 'firebox', |
| 36 |
'numberposts' => -1 |
| 37 |
]; |
| 38 |
|
| 39 |
return get_posts($payload); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Retrieves all boxes in a key => value array of ID => title |
| 44 |
* |
| 45 |
* @return array |
| 46 |
*/ |
| 47 |
public static function getAllBoxesParsedByKeyValue() |
| 48 |
{ |
| 49 |
if (!$boxes = self::getAllBoxes()) |
| 50 |
{ |
| 51 |
return []; |
| 52 |
} |
| 53 |
|
| 54 |
return self::produceKeyValueBoxes($boxes); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Produce a key,value pair of boxes containg their ID,title |
| 59 |
* |
| 60 |
* @return array |
| 61 |
*/ |
| 62 |
public static function produceKeyValueBoxes($boxes) |
| 63 |
{ |
| 64 |
if (!$boxes) |
| 65 |
{ |
| 66 |
return []; |
| 67 |
} |
| 68 |
|
| 69 |
$data = []; |
| 70 |
|
| 71 |
foreach ($boxes as $key => $box) |
| 72 |
{ |
| 73 |
$data[$box->ID] = $box->post_title; |
| 74 |
} |
| 75 |
|
| 76 |
return $data; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Gets all published Boxes except the given id. |
| 81 |
* The array structure is [ID, title] to properly appear in a Dropdown field. |
| 82 |
* |
| 83 |
* @param integer $id |
| 84 |
* |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
public static function getAllMirrorBoxesExceptID($id) |
| 88 |
{ |
| 89 |
if (!$id) |
| 90 |
{ |
| 91 |
return []; |
| 92 |
} |
| 93 |
|
| 94 |
$boxes = firebox()->tables->box->getResults([ |
| 95 |
'where' => [ |
| 96 |
'ID' => ' NOT IN (' . esc_sql($id) . ')', |
| 97 |
'post_status' => " = 'publish'", |
| 98 |
'post_type' => " = 'firebox'" |
| 99 |
] |
| 100 |
]); |
| 101 |
|
| 102 |
$boxes_parsed = []; |
| 103 |
|
| 104 |
foreach ($boxes as $key => $p) |
| 105 |
{ |
| 106 |
$boxes_parsed[$p->ID] = $p->post_title . ' (' . $p->ID . ')'; |
| 107 |
} |
| 108 |
|
| 109 |
return $boxes_parsed; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Get box data |
| 114 |
* |
| 115 |
* @param int $box |
| 116 |
* |
| 117 |
* @return array |
| 118 |
*/ |
| 119 |
public static function getBoxData($box) |
| 120 |
{ |
| 121 |
if (!$box) |
| 122 |
{ |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
$box = (int) $box; |
| 127 |
|
| 128 |
$box = firebox()->tables->box->getResults([ |
| 129 |
'where' => [ |
| 130 |
'ID' => " = '" . esc_sql($box) . "'" |
| 131 |
] |
| 132 |
]); |
| 133 |
|
| 134 |
return isset($box[0]) ? $box[0] : []; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Checks whether the box exist |
| 139 |
* |
| 140 |
* @param int $box |
| 141 |
* |
| 142 |
* @return boolean |
| 143 |
*/ |
| 144 |
public static function boxExist($box) |
| 145 |
{ |
| 146 |
if (!$box) |
| 147 |
{ |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
$box = (int) $box; |
| 152 |
|
| 153 |
$box = firebox()->tables->box->getResults([ |
| 154 |
'where' => [ |
| 155 |
'ID' => " = '" . esc_sql($box) . "'" |
| 156 |
] |
| 157 |
]); |
| 158 |
|
| 159 |
if (!$box) |
| 160 |
{ |
| 161 |
return false; |
| 162 |
} |
| 163 |
|
| 164 |
return true; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Gets boxes in a [id, title] pair from a list of Box IDs |
| 169 |
* |
| 170 |
* @param array $items |
| 171 |
* |
| 172 |
* @return array |
| 173 |
*/ |
| 174 |
public static function getSelectedSearchItems($items) |
| 175 |
{ |
| 176 |
$boxes = firebox()->tables->box->getResults([ |
| 177 |
'where' => [ |
| 178 |
'ID' => ' IN(' . implode(',', array_map('intval', $items)) . ')', |
| 179 |
'post_status' => " = 'publish'", |
| 180 |
'post_type' => " = 'firebox'" |
| 181 |
] |
| 182 |
]); |
| 183 |
|
| 184 |
$boxes_parsed = []; |
| 185 |
|
| 186 |
foreach ($boxes as $key => $p) |
| 187 |
{ |
| 188 |
$boxes_parsed[] = [ |
| 189 |
'id' => $p->ID, |
| 190 |
'title' => $p->post_title |
| 191 |
]; |
| 192 |
} |
| 193 |
|
| 194 |
return $boxes_parsed; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Gets Settings Data |
| 199 |
* |
| 200 |
* @return array |
| 201 |
*/ |
| 202 |
public static function getParams() |
| 203 |
{ |
| 204 |
// cache key |
| 205 |
$cache_key = md5('fboxSettings'); |
| 206 |
|
| 207 |
// check cache |
| 208 |
if ($params = wp_cache_get($cache_key)) |
| 209 |
{ |
| 210 |
return $params; |
| 211 |
} |
| 212 |
|
| 213 |
// get params |
| 214 |
$params = get_option('firebox_settings'); |
| 215 |
|
| 216 |
// set cache |
| 217 |
wp_cache_set($cache_key, $params); |
| 218 |
|
| 219 |
return $params; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Finds and returns the box from $_GET['template'] |
| 224 |
* |
| 225 |
* @return mixed |
| 226 |
*/ |
| 227 |
public static function getBoxFromTemplate() |
| 228 |
{ |
| 229 |
$template = isset($_GET['template']) ? sanitize_text_field($_GET['template']) : ''; |
| 230 |
|
| 231 |
if (empty($template)) |
| 232 |
{ |
| 233 |
return ''; |
| 234 |
} |
| 235 |
|
| 236 |
return firebox()->library->find($template); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Transforms an array of key,value to inline CSS |
| 241 |
* |
| 242 |
* @param array $array |
| 243 |
* |
| 244 |
* @return string |
| 245 |
*/ |
| 246 |
public static function arrayToCSSS($array) |
| 247 |
{ |
| 248 |
$array = array_filter($array); |
| 249 |
|
| 250 |
if (empty($array)) |
| 251 |
{ |
| 252 |
return ''; |
| 253 |
} |
| 254 |
|
| 255 |
$styles = ''; |
| 256 |
|
| 257 |
foreach ($array as $key => $value) |
| 258 |
{ |
| 259 |
$styles .= $key . ':' . $value . ';'; |
| 260 |
} |
| 261 |
|
| 262 |
return $styles; |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Duplicates a box |
| 267 |
* |
| 268 |
* @param integer $box_id |
| 269 |
* |
| 270 |
* @return bool |
| 271 |
*/ |
| 272 |
public static function duplicateBox($box_id) |
| 273 |
{ |
| 274 |
// get box |
| 275 |
$box = firebox()->tables->box->getResults([ |
| 276 |
'where' => [ |
| 277 |
'ID' => " = '" . esc_sql($box_id) . "'", |
| 278 |
'post_status' => " = '" . esc_sql(get_post_status($box_id)) . "'", |
| 279 |
'post_type' => " = 'firebox'" |
| 280 |
], |
| 281 |
'limit' => 1 |
| 282 |
]); |
| 283 |
|
| 284 |
if (empty($box)) |
| 285 |
{ |
| 286 |
return false; |
| 287 |
} |
| 288 |
|
| 289 |
// reset box ID and make it a draft |
| 290 |
$box = $box[0]; |
| 291 |
$box->ID = ''; |
| 292 |
$box->post_title = 'Copy of ' . $box->post_title; |
| 293 |
$box->post_status = 'draft'; |
| 294 |
|
| 295 |
// insert new box |
| 296 |
$new_box_id = firebox()->tables->box->insert($box); |
| 297 |
|
| 298 |
// get meta options |
| 299 |
$meta = get_post_meta($box_id, 'fpframework_meta_settings', true); |
| 300 |
|
| 301 |
// add meta options for new box |
| 302 |
update_post_meta($new_box_id, 'fpframework_meta_settings', $meta); |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Reset Box Stats |
| 307 |
* |
| 308 |
* @param array $box_ids |
| 309 |
* |
| 310 |
* @return void |
| 311 |
*/ |
| 312 |
public static function resetBoxStats($box_ids) |
| 313 |
{ |
| 314 |
$logs_table = firebox()->tables->boxlog->getFullTableName(); |
| 315 |
$logs_details_table = firebox()->tables->boxlogdetails->getFullTableName(); |
| 316 |
|
| 317 |
// delete box logs details |
| 318 |
firebox()->tables->boxlogdetails->executeRaw("DELETE FROM `$logs_details_table` WHERE log_id IN (SELECT id FROM `$logs_table` WHERE box IN (" . implode(",", $box_ids) . "))"); |
| 319 |
|
| 320 |
// delete box logs |
| 321 |
firebox()->tables->boxlog->deleteRaw('WHERE box IN (' . implode(',', $box_ids) . ')'); |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* Exports boxes |
| 326 |
* |
| 327 |
* @param array $box_ids |
| 328 |
* |
| 329 |
* @return string |
| 330 |
*/ |
| 331 |
public static function exportBoxes($box_ids) |
| 332 |
{ |
| 333 |
// get boxes |
| 334 |
$boxes = firebox()->tables->box->getResults([ |
| 335 |
'where' => [ |
| 336 |
'ID' => ' IN (' . implode(',', esc_sql($box_ids)) . ')', |
| 337 |
'post_type' => " = 'firebox'" |
| 338 |
] |
| 339 |
]); |
| 340 |
|
| 341 |
$boxes = (array) $boxes; |
| 342 |
|
| 343 |
if (!count($boxes)) |
| 344 |
{ |
| 345 |
return; |
| 346 |
} |
| 347 |
|
| 348 |
$exported = []; |
| 349 |
|
| 350 |
$filename = firebox()->_('FB_PLUGIN_NAME') . ' Items'; |
| 351 |
|
| 352 |
// name for 1 box |
| 353 |
if (count($boxes) == 1) |
| 354 |
{ |
| 355 |
$name = mb_strtolower(html_entity_decode($boxes['0']->post_title)); |
| 356 |
$name = preg_replace('#[^a-z0-9_-]#', '_', $name); |
| 357 |
$name = trim(preg_replace('#__+#', '_', $name), '_-'); |
| 358 |
|
| 359 |
$filename = firebox()->_('FB_PLUGIN_NAME') . ' Item (' . $name . ')'; |
| 360 |
} |
| 361 |
|
| 362 |
foreach ($boxes as $box) |
| 363 |
{ |
| 364 |
$meta = get_post_meta($box->ID, 'fpframework_meta_settings', true); |
| 365 |
|
| 366 |
$exported[] = [ |
| 367 |
'box' => $box, |
| 368 |
'meta' => $meta |
| 369 |
]; |
| 370 |
} |
| 371 |
|
| 372 |
$string = json_encode($exported); |
| 373 |
|
| 374 |
// SET DOCUMENT HEADER |
| 375 |
if (preg_match('#Opera(/| )([0-9].[0-9]{1,2})#', $_SERVER['HTTP_USER_AGENT'])) |
| 376 |
{ |
| 377 |
$UserBrowser = "Opera"; |
| 378 |
} |
| 379 |
elseif (preg_match('#MSIE ([0-9].[0-9]{1,2})#', $_SERVER['HTTP_USER_AGENT'])) |
| 380 |
{ |
| 381 |
$UserBrowser = "IE"; |
| 382 |
} |
| 383 |
else |
| 384 |
{ |
| 385 |
$UserBrowser = ''; |
| 386 |
} |
| 387 |
$mime_type = ($UserBrowser == 'IE' || $UserBrowser == 'Opera') ? 'application/octetstream' : 'application/octet-stream'; |
| 388 |
@ob_end_clean(); |
| 389 |
ob_start(); |
| 390 |
|
| 391 |
header('Content-Type: ' . $mime_type); |
| 392 |
header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT'); |
| 393 |
|
| 394 |
if ($UserBrowser == 'IE') |
| 395 |
{ |
| 396 |
header('Content-Disposition: inline; filename="' . $filename . '.fbox"'); |
| 397 |
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); |
| 398 |
header('Pragma: public'); |
| 399 |
} |
| 400 |
else |
| 401 |
{ |
| 402 |
header('Content-Disposition: attachment; filename="' . $filename . '.fbox"'); |
| 403 |
header('Pragma: no-cache'); |
| 404 |
} |
| 405 |
|
| 406 |
// PRINT STRING |
| 407 |
echo $string; |
| 408 |
die; |
| 409 |
} |
| 410 |
} |