| 1 |
<?php |
| 2 |
namespace MaxButtons; |
| 3 |
defined('ABSPATH') or die('No direct access permitted'); |
| 4 |
|
| 5 |
/** Blocks collection |
| 6 |
* |
| 7 |
* Class for general block functions - transitional |
| 8 |
*/ |
| 9 |
use \RecursiveDirectoryIterator as RecursiveDirectoryIterator; |
| 10 |
use \RecursiveIteratorIterator as RecursiveIteratorIterator; |
| 11 |
use \FilesystemIterator as FilesystemIterator; |
| 12 |
|
| 13 |
class maxBlocks |
| 14 |
{ |
| 15 |
protected static $blocks; // collection! |
| 16 |
protected static $block_classes; |
| 17 |
|
| 18 |
protected static $data; // full data array |
| 19 |
protected static $fields = array(); // all fields |
| 20 |
|
| 21 |
public static function init() |
| 22 |
{ |
| 23 |
|
| 24 |
} |
| 25 |
|
| 26 |
/** Find the block classes */ |
| 27 |
public static function initBlocks() |
| 28 |
{ |
| 29 |
|
| 30 |
$block_paths = apply_filters('mb-block-paths', array(MB()->get_plugin_path() . "blocks/") ); |
| 31 |
|
| 32 |
//global $blockClass; // load requires only onc |
| 33 |
|
| 34 |
$newBlocks = array(); |
| 35 |
$templates = array(); |
| 36 |
|
| 37 |
|
| 38 |
foreach($block_paths as $block_path) |
| 39 |
{ |
| 40 |
$dir_iterator = new RecursiveDirectoryIterator($block_path, FilesystemIterator::SKIP_DOTS); |
| 41 |
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST); |
| 42 |
|
| 43 |
foreach ($iterator as $fileinfo) |
| 44 |
{ |
| 45 |
|
| 46 |
$path = $fileinfo->getRealPath(); |
| 47 |
// THIS IS PHP > 5.3.6 |
| 48 |
//$extension = $fileinfo->getExtension(); |
| 49 |
$extension = pathinfo($path, PATHINFO_EXTENSION); |
| 50 |
|
| 51 |
if ($fileinfo->isFile() ) |
| 52 |
{ |
| 53 |
if ($extension == 'php') |
| 54 |
{ |
| 55 |
require_once($path); |
| 56 |
} |
| 57 |
elseif($extension == 'tpl') |
| 58 |
{ |
| 59 |
$filename = $fileinfo->getBasename('.tpl'); |
| 60 |
$templates[$filename] = array('path' => $path); |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
} |
| 66 |
ksort($blockOrder); |
| 67 |
foreach($blockOrder as $prio => $blockArray) |
| 68 |
{ |
| 69 |
foreach($blockArray as $block) |
| 70 |
{ |
| 71 |
if (isset($blockClass[$block])) |
| 72 |
$newBlocks[$block] = $blockClass[$block]; |
| 73 |
|
| 74 |
} |
| 75 |
} |
| 76 |
$blockClass = $newBlocks; |
| 77 |
if (is_admin()) |
| 78 |
{ |
| 79 |
// possible issue with some hosters faking is_admin flag. |
| 80 |
if (class_exists( maxUtils::namespaceit('maxBlocks') ) && class_exists( maxUtils::namespaceit('maxBlocks') ) ) |
| 81 |
{ |
| 82 |
maxField::setTemplates($templates); |
| 83 |
|
| 84 |
} |
| 85 |
else |
| 86 |
{ |
| 87 |
error_log('[MaxButtons] - MaxField class is not set within admin context. This can cause issues when using button editor'); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
//$this->loadBlockClasses($blockClass); |
| 92 |
|
| 93 |
static::$block_classes = array_values($blockClass); |
| 94 |
} |
| 95 |
|
| 96 |
public static function getBlockClasses() |
| 97 |
{ |
| 98 |
if ( is_null(static::$block_classes) ) |
| 99 |
self::initBlocks(); |
| 100 |
|
| 101 |
return static::$block_classes; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
public static function setData($data) |
| 106 |
{ |
| 107 |
|
| 108 |
$new_data = array(); //egalite |
| 109 |
if (! is_array($data) || count($data) == 0) // no data |
| 110 |
return false; |
| 111 |
|
| 112 |
foreach($data as $block => $fields) |
| 113 |
{ |
| 114 |
if (is_array($fields)) |
| 115 |
$new_data = array_merge($new_data, $fields); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
self::$data = $new_data; |
| 120 |
} |
| 121 |
|
| 122 |
public static function add($block) |
| 123 |
{ |
| 124 |
$name = $block->get_name(); |
| 125 |
|
| 126 |
static::$blocks[$name] = $block; |
| 127 |
static::$fields = array_merge(self::$fields, $block->get_fields()); |
| 128 |
} |
| 129 |
|
| 130 |
public static function getValue($fieldname) |
| 131 |
{ |
| 132 |
|
| 133 |
if (isset(self::$data[$fieldname])) |
| 134 |
return self::$data[$fieldname]; |
| 135 |
if (isset(self::$fields[$fieldname])) |
| 136 |
return self::$fields[$fieldname]['default']; |
| 137 |
|
| 138 |
return false; // dunno. |
| 139 |
} |
| 140 |
|
| 141 |
public static function getColorValue($fieldname) |
| 142 |
{ |
| 143 |
$value = self::getValue($fieldname); |
| 144 |
if (! $value ) |
| 145 |
return false; |
| 146 |
|
| 147 |
if (substr($value,0,1) !== '#') |
| 148 |
{ |
| 149 |
$value = '#' . $value; |
| 150 |
} |
| 151 |
|
| 152 |
return $value; |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
public static function getDefault($fieldname) |
| 157 |
{ |
| 158 |
if (isset(self::$fields[$fieldname]['default'])) |
| 159 |
return self::$fields[$fieldname]['default']; |
| 160 |
|
| 161 |
return false; // dunno |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
} |
| 167 |
|