| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
namespace MaxButtons; |
| 5 |
defined('ABSPATH') or die('No direct access permitted'); |
| 6 |
$blockClass["container"] = "containerBlock"; |
| 7 |
$blockOrder[70][] = "container"; |
| 8 |
|
| 9 |
//use \simple_html_dom as simple_html_dom; |
| 10 |
|
| 11 |
class containerBlock extends maxBlock |
| 12 |
{ |
| 13 |
protected $blockname = "container"; |
| 14 |
|
| 15 |
protected $fields = array("container_enabled" => array("default" => "0"), |
| 16 |
"container_center_div_wrap" => array("default" => "0"), |
| 17 |
|
| 18 |
"container_width" => array("default" => "0", |
| 19 |
"css" => "width", |
| 20 |
"csspart" => "mb-container", |
| 21 |
'unitfield' => 'container_width_unit', |
| 22 |
), |
| 23 |
'container_width_unit' => array('default' => 'pixel'), |
| 24 |
"container_margin_top" => array("default" => "0px", |
| 25 |
"css" => "margin-top", |
| 26 |
"csspart" => "mb-container"), |
| 27 |
"container_margin_right" => array("default" => "0px", |
| 28 |
"css" => "margin-right", |
| 29 |
"csspart" => "mb-container"), |
| 30 |
"container_margin_bottom" => array("default" => "0px", |
| 31 |
"css" => "margin-bottom", |
| 32 |
"csspart" => "mb-container"), |
| 33 |
"container_margin_left" => array("default" => "0px", |
| 34 |
"css" => "margin-left", |
| 35 |
"csspart" => "mb-container"), |
| 36 |
"container_alignment" => array("default" => "", |
| 37 |
"css" => "align", |
| 38 |
"csspart" => "mb-container"), |
| 39 |
); |
| 40 |
|
| 41 |
|
| 42 |
public function parse_button($domObj, $mode = 'normal') |
| 43 |
{ |
| 44 |
$data = $this->getBlockData(); |
| 45 |
$id = $this->data["id"]; |
| 46 |
|
| 47 |
if ($mode == 'editor') |
| 48 |
return $domObj; // in previews no container object |
| 49 |
|
| 50 |
if (isset($data["container_enabled"]) && $data["container_enabled"] == 1) |
| 51 |
{ |
| 52 |
$anchor = $domObj->find("a",0); |
| 53 |
$anchor->outertext = "<span class='maxbutton-" . $id . "-container mb-container'>" . $anchor->outertext . "</span>"; |
| 54 |
|
| 55 |
|
| 56 |
if ($data["container_center_div_wrap"] == 1) // I heard you like wrapping... |
| 57 |
{ |
| 58 |
$anchor->outertext = "<span class='mb-center maxbutton-" . $id . "-center'>" . $anchor->outertext . "</span>"; |
| 59 |
|
| 60 |
} |
| 61 |
// reload the dom model with new divs |
| 62 |
$newhtml = $domObj->save(); |
| 63 |
$domObj = new simple_html_dom(); |
| 64 |
$domObj->load($newhtml); |
| 65 |
} |
| 66 |
|
| 67 |
return $domObj; |
| 68 |
} |
| 69 |
|
| 70 |
public function parse_css($css, $screens, string $mode = 'normal') |
| 71 |
{ |
| 72 |
$css = parent::parse_css($css, $screens, $mode); |
| 73 |
$data = $this->getBlockData(); |
| 74 |
|
| 75 |
$css["mb-container"]["normal"]["display"] = "block"; |
| 76 |
$css["mb-center"]["normal"]["display"] = "block"; |
| 77 |
$css["mb-center"]["normal"]["text-align"] = "center"; |
| 78 |
|
| 79 |
if (isset($css['mb-container']['normal']['align']) && $css['mb-container']['normal']['align'] != '') |
| 80 |
{ |
| 81 |
$stat = explode(":", $css['mb-container']['normal']["align"]); |
| 82 |
$css['mb-container']['normal'][ $stat[0] ] = $stat[1]; |
| 83 |
unset($css['mb-container']['normal']["align"]); |
| 84 |
} |
| 85 |
if ( isset($css['mb-container']['normal']["width"]) && $data["container_width"] == 0) |
| 86 |
{ |
| 87 |
unset($css['mb-container']['normal']["width"]); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
foreach($screens as $screen) |
| 93 |
{ |
| 94 |
if ($screen->id == 'default') |
| 95 |
continue; |
| 96 |
|
| 97 |
if (! isset($css['mb-container']['responsive']) || ! isset($css['mb-container']['responsive'][$screen->id]) || ! isset($css['mb-container']['responsive'][$screen->id]['normal']) ) |
| 98 |
{ |
| 99 |
continue; |
| 100 |
} |
| 101 |
|
| 102 |
if (isset($css['mb-container']['responsive'][$screen->id]['normal']['align']) && $css['mb-container']['responsive'][$screen->id]['normal']['align'] != '') |
| 103 |
{ |
| 104 |
$stat = explode(":", $css['mb-container']['responsive'][$screen->id]['normal']["align"]); |
| 105 |
$css['mb-container']['responsive'][$screen->id]['normal'][ $stat[0] ] = $stat[1]; |
| 106 |
unset($css['mb-container']['responsive'][$screen->id]['normal']["align"]); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return $css; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
public function map_fields($map) |
| 115 |
{ |
| 116 |
$map = parent::map_fields($map); |
| 117 |
$map["container_width_unit"]["func"] = "updateContainerUnit"; |
| 118 |
return $map; |
| 119 |
} |
| 120 |
|
| 121 |
public function admin_fields($screen) |
| 122 |
{ |
| 123 |
$data = $this->getBlockData(); |
| 124 |
|
| 125 |
/*foreach($this->fields as $field => $options) |
| 126 |
{ |
| 127 |
$default = (isset($options["default"])) ? $options["default"] : ''; |
| 128 |
$$field = (isset($data[$field])) ? $data[$field] : $default; |
| 129 |
${$field . "_default"} = $default; |
| 130 |
|
| 131 |
|
| 132 |
} */ |
| 133 |
|
| 134 |
$maxbuttons_container_alignments = array( |
| 135 |
'' => '', |
| 136 |
'display: inline-block' => 'display: inline-block', |
| 137 |
'float: left' => 'float: left', |
| 138 |
'float: right' => 'float: right' |
| 139 |
); |
| 140 |
if ($screen->is_responsive()) |
| 141 |
{ |
| 142 |
$maxbuttons_container_alignments['float: none'] = 'float: none'; |
| 143 |
} |
| 144 |
|
| 145 |
$icon_url = MB()->get_plugin_url() . 'images/icons/' ; |
| 146 |
|
| 147 |
$start_block = new maxField('block_start'); |
| 148 |
$start_block->name = __('container', 'maxbuttons'); |
| 149 |
$start_block->label = __('Container', 'maxbuttons'); |
| 150 |
$screen->addField($start_block); |
| 151 |
|
| 152 |
|
| 153 |
$u_container = new maxField('switch'); |
| 154 |
$u_container->label = __('Use Container', 'maxbuttons'); |
| 155 |
$u_container->name = $screen->getFieldID('container_enabled'); |
| 156 |
$u_container->id = $u_container->name; |
| 157 |
$u_container->help = __('Creates a container around the button which pushes other content', 'maxbuttons'); |
| 158 |
$u_container->is_responsive = false; |
| 159 |
$u_container->value = 1; |
| 160 |
|
| 161 |
$u_container->checked = checked( $screen->getValue($u_container->id), 1, false); |
| 162 |
$screen->addField($u_container, 'start', 'end'); |
| 163 |
|
| 164 |
$fspacer = new maxField('spacer'); |
| 165 |
$fspacer->name = ''; |
| 166 |
$fspacer->is_responsive = false; |
| 167 |
$screen->addField($fspacer, 'start'); |
| 168 |
|
| 169 |
$wrap_cont = new maxField('switch'); |
| 170 |
$wrap_cont->name = $screen->getFieldID('container_center_div_wrap'); |
| 171 |
$wrap_cont->id = $wrap_cont->name; |
| 172 |
$wrap_cont->value = 1; |
| 173 |
$wrap_cont->checked = checked( $screen->getValue($wrap_cont->name), 1, false); |
| 174 |
$wrap_cont->label = __('Center the container', 'maxbuttons'); |
| 175 |
$wrap_cont->is_responsive = false; |
| 176 |
$screen->addField($wrap_cont, '', 'end'); |
| 177 |
|
| 178 |
$unit = $screen->getValue($screen->getFieldID('container_width_unit')); |
| 179 |
$unit = ($unit == 'percent') ? '%' : 'px'; |
| 180 |
|
| 181 |
|
| 182 |
$container_width = new maxField('number'); |
| 183 |
$container_width->name = $screen->getFieldID('container_width'); |
| 184 |
$container_width->id = $container_width->name; |
| 185 |
$container_width->min = 0; |
| 186 |
$container_width->value = maxUtils::strip_px( $screen->getValue($container_width->id) ); |
| 187 |
$container_width->label = __('Width', 'maxbuttons'); |
| 188 |
$container_width->inputclass = 'small'; |
| 189 |
$container_width->after_input = '<span class="unit">' . $unit . '</span>'; |
| 190 |
$screen->addField($container_width, 'start', 'end'); |
| 191 |
|
| 192 |
$size_spacer = new maxField('spacer'); |
| 193 |
$size_spacer->label = __('Width Unit', 'maxbuttons'); |
| 194 |
$size_spacer->name = 'size_unit_spacer'; |
| 195 |
|
| 196 |
$screen->addField($size_spacer, 'start', ''); |
| 197 |
|
| 198 |
// Units for width |
| 199 |
$cwidth_unit_px = new maxField('radio'); |
| 200 |
$cwidth_unit_px->label = __('px', 'maxbuttons'); |
| 201 |
$cwidth_unit_px->name = $screen->getFieldID('container_width_unit'); |
| 202 |
$cwidth_unit_px->id = $screen->getFieldID('cwidth_unit_px'); |
| 203 |
$cwidth_unit_px->value = 'pixel'; |
| 204 |
//$wsize_unit_px->before_input = '<label>width</label>'; |
| 205 |
$cwidth_unit_px->checked = checked( $screen->getValue($cwidth_unit_px->name), 'pixel', false); |
| 206 |
$screen->addField($cwidth_unit_px, 'group_start', ''); |
| 207 |
|
| 208 |
$cwidth_unit_perc = new maxField('radio'); |
| 209 |
$cwidth_unit_perc->label = __('%', 'maxbuttons'); |
| 210 |
$cwidth_unit_perc->name = $screen->getFieldID('container_width_unit'); |
| 211 |
$cwidth_unit_perc->id = $screen->getFieldID('cwidth_unit_perc'); |
| 212 |
$cwidth_unit_perc->value = 'percent'; |
| 213 |
$cwidth_unit_perc->checked = checked( $screen->getValue($cwidth_unit_perc->name), 'percent', false); |
| 214 |
$screen->addField($cwidth_unit_perc, '', array('group_end', 'end')); |
| 215 |
|
| 216 |
// Margin - trouble |
| 217 |
$ptop = new maxField('number'); |
| 218 |
$ptop->label = __('Margin', 'maxbuttons'); |
| 219 |
$ptop->id = $screen->getFieldID('container_margin_top'); |
| 220 |
$ptop->name = $ptop->id; |
| 221 |
$ptop->min = 0; |
| 222 |
$ptop->inputclass = 'tiny'; |
| 223 |
$ptop->before_input = '<img src="' . $icon_url . 'p_top.png" title="' . __("Margin Top","maxbuttons") . '" >'; |
| 224 |
$ptop->value = maxUtils::strip_px($screen->getValue($ptop->id)); |
| 225 |
$screen->addField($ptop, 'start'); |
| 226 |
|
| 227 |
$pright = new maxField('number'); |
| 228 |
$pright->id = $screen->getFieldID('container_margin_right'); |
| 229 |
$pright->name = $pright->id; |
| 230 |
$pright->min = 0; |
| 231 |
$pright->inputclass = 'tiny'; |
| 232 |
$pright->before_input = '<img src="' . $icon_url . 'p_right.png" class="icon padding" title="' . __("Margin Right","maxbuttons") . '" >'; |
| 233 |
$pright->value = maxUtils::strip_px($screen->getValue($pright->id)); |
| 234 |
$screen->addField($pright); |
| 235 |
|
| 236 |
$pbottom = new maxField('number'); |
| 237 |
$pbottom->id = $screen->getFieldID('container_margin_bottom'); |
| 238 |
$pbottom->name = $pbottom->id; |
| 239 |
$pbottom->min = 0; |
| 240 |
$pbottom->inputclass = 'tiny'; |
| 241 |
$pbottom->before_input = '<img src="' . $icon_url . 'p_bottom.png" class="icon padding" title="' . __("Margin Bottom","maxbuttons") . '" >'; |
| 242 |
$pbottom->value = maxUtils::strip_px($screen->getValue($pbottom->id)); |
| 243 |
$screen->addField($pbottom); |
| 244 |
|
| 245 |
$pleft = new maxField('number'); |
| 246 |
$pleft->id = $screen->getFieldID('container_margin_left'); |
| 247 |
$pleft->name = $pleft->id; |
| 248 |
$pleft->min = 0; |
| 249 |
$pleft->inputclass = 'tiny'; |
| 250 |
$pleft->before_input = '<img src="' . $icon_url . 'p_left.png" class="icon padding" title="' . __("Margin Left","maxbuttons") . '" >'; |
| 251 |
$pleft->value = maxUtils::strip_px($screen->getValue($pleft->id)); |
| 252 |
$screen->addField($pleft, '', 'end'); |
| 253 |
|
| 254 |
$align = new maxField('option_select'); |
| 255 |
$align->label = __('Alignment','maxbuttons'); |
| 256 |
$align->name = $screen->getFieldID('container_alignment'); |
| 257 |
$align->id = $align->name; |
| 258 |
$align->selected = $screen->getValue($align->id); |
| 259 |
$align->options = $maxbuttons_container_alignments; // maxUtils::selectify($align->name, $maxbuttons_container_alignments, $align->value); |
| 260 |
$align->help = __('Float can help to align the button and other content on the same line', 'maxbuttons'); |
| 261 |
$screen->addField($align, 'start', 'end'); |
| 262 |
|
| 263 |
$this->sidebar($screen); |
| 264 |
$endblock = new maxField('block_end'); |
| 265 |
$screen->addField($endblock); |
| 266 |
|
| 267 |
} // admin_fields |
| 268 |
|
| 269 |
} // class |
| 270 |
|
| 271 |
|
| 272 |
?> |
| 273 |
|