| @@ -1,5 +1,5 @@ | ||
| 1 | -<?php | |
| 1 | +<?php | |
| 2 | 2 | namespace MaxButtons; |
| 3 | 3 | defined('ABSPATH') or die('No direct access permitted'); |
| 4 | 4 | |
| 5 | 5 | /** Blocks collection |
| @@ -5,73 +5,162 @@ | ||
| 5 | 5 | /** Blocks collection |
| 6 | 6 | * |
| 7 | 7 | * Class for general block functions - transitional |
| 8 | 8 | */ |
| 9 | -use MaxButtons\maxBlocks as maxBlocks; | |
| 9 | +use \RecursiveDirectoryIterator as RecursiveDirectoryIterator; | |
| 10 | +use \RecursiveIteratorIterator as RecursiveIteratorIterator; | |
| 11 | +use \FilesystemIterator as FilesystemIterator; | |
| 10 | 12 | |
| 11 | 13 | class maxBlocks |
| 12 | 14 | { |
| 13 | 15 | protected static $blocks; // collection! |
| 14 | - | |
| 16 | + protected static $block_classes; | |
| 17 | + | |
| 15 | 18 | protected static $data; // full data array |
| 16 | - protected static $fields = array(); // all fields | |
| 17 | - | |
| 18 | - public static function init() | |
| 19 | + protected static $fields = array(); // all fields | |
| 20 | + | |
| 21 | + public static function init() | |
| 19 | 22 | { |
| 20 | - add_action('mb-data-load', array('MaxButtons\maxBlocks', 'data') ); | |
| 23 | + | |
| 21 | 24 | } |
| 22 | - | |
| 23 | - public static function data($data) | |
| 25 | + | |
| 26 | + /** Find the block classes */ | |
| 27 | + public static function initBlocks() | |
| 24 | 28 | { |
| 25 | - $new_data = array(); //egalite | |
| 26 | - foreach($data as $block => $fields) | |
| 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) | |
| 27 | 39 | { |
| 28 | - if (is_array($fields)) | |
| 29 | - $new_data = array_merge($new_data, $fields); | |
| 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 | + | |
| 30 | 65 | } |
| 31 | - self::$data = $new_data; | |
| 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); | |
| 32 | 94 | } |
| 33 | - | |
| 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 | + | |
| 34 | 122 | public static function add($block) |
| 35 | 123 | { |
| 36 | - $name = $block->get_name(); | |
| 37 | - self::$blocks[$name] = $block; | |
| 38 | - self::$fields = array_merge(self::$fields, $block->get_fields()); | |
| 124 | + $name = $block->get_name(); | |
| 125 | + | |
| 126 | + static::$blocks[$name] = $block; | |
| 127 | + static::$fields = array_merge(self::$fields, $block->get_fields()); | |
| 39 | 128 | } |
| 40 | - | |
| 41 | - public static function getValue($fieldname) | |
| 129 | + | |
| 130 | + public static function getValue($fieldname) | |
| 42 | 131 | { |
| 43 | 132 | |
| 44 | - if (isset(self::$data[$fieldname])) | |
| 45 | - return self::$data[$fieldname]; | |
| 46 | - if (isset(self::$fields[$fieldname])) | |
| 133 | + if (isset(self::$data[$fieldname])) | |
| 134 | + return self::$data[$fieldname]; | |
| 135 | + if (isset(self::$fields[$fieldname])) | |
| 47 | 136 | return self::$fields[$fieldname]['default']; |
| 48 | - | |
| 49 | - return false; // dunno. | |
| 137 | + | |
| 138 | + return false; // dunno. | |
| 50 | 139 | } |
| 51 | 140 | |
| 52 | - public static function getColorValue($fieldname) | |
| 141 | + public static function getColorValue($fieldname) | |
| 53 | 142 | { |
| 54 | - $value = self::getValue($fieldname); | |
| 55 | - if (! $value ) | |
| 143 | + $value = self::getValue($fieldname); | |
| 144 | + if (! $value ) | |
| 56 | 145 | return false; |
| 57 | - | |
| 58 | - if (substr($value,0,1) !== '#') | |
| 146 | + | |
| 147 | + if (substr($value,0,1) !== '#') | |
| 59 | 148 | { |
| 60 | 149 | $value = '#' . $value; |
| 61 | 150 | } |
| 62 | - | |
| 151 | + | |
| 63 | 152 | return $value; |
| 64 | - | |
| 153 | + | |
| 65 | 154 | } |
| 66 | 155 | |
| 67 | - public static function getDefault($fieldname) | |
| 156 | + public static function getDefault($fieldname) | |
| 68 | 157 | { |
| 69 | - if (isset(self::$fields[$fieldname]['default'])) | |
| 158 | + if (isset(self::$fields[$fieldname]['default'])) | |
| 70 | 159 | return self::$fields[$fieldname]['default']; |
| 71 | - | |
| 160 | + | |
| 72 | 161 | return false; // dunno |
| 73 | - | |
| 162 | + | |
| 74 | 163 | } |
| 75 | 164 | |
| 76 | 165 | |
| 77 | 166 | } |