| 1 |
<?php |
| 2 |
namespace MaxButtons; |
| 3 |
defined('ABSPATH') or die('No direct access permitted'); |
| 4 |
|
| 5 |
$collectionBlock["picker"] = "pickerCollectionBlock"; |
| 6 |
|
| 7 |
class pickerCollectionBlock extends collectionBlock |
| 8 |
{ |
| 9 |
protected $blockname = "picker"; |
| 10 |
protected $fields = array("selection" => array("default" => array() ), |
| 11 |
|
| 12 |
); |
| 13 |
|
| 14 |
// get all buttons in collection, set as proper button object/ |
| 15 |
function getButtons() |
| 16 |
{ // should maybe but a 'light init' of buttons? |
| 17 |
$selection = $this->data["selection"]; |
| 18 |
|
| 19 |
$buttonsArray = array(); |
| 20 |
if (count($selection) == 0) |
| 21 |
return $buttonsArray; |
| 22 |
|
| 23 |
$table = maxUtils::get_table_name(); |
| 24 |
$status = 'publish'; |
| 25 |
|
| 26 |
$query_selection = $selection; |
| 27 |
|
| 28 |
$selectmask = implode( ', ', array_fill( 0, count( $query_selection ), '%d' ) ); |
| 29 |
$sql = "SELECT DISTINCT * FROM {$table} WHERE id in (" . $selectmask . ") and status = '%s' "; |
| 30 |
$prepare_values = $query_selection; |
| 31 |
|
| 32 |
array_push($prepare_values, $status); |
| 33 |
|
| 34 |
global $wpdb; |
| 35 |
$sql = $wpdb->prepare($sql, $prepare_values ); |
| 36 |
$results = $wpdb->get_results($sql, ARRAY_A); |
| 37 |
|
| 38 |
$data_array = array(); |
| 39 |
|
| 40 |
foreach ($results as $result) |
| 41 |
{ |
| 42 |
$data_array[$result['id']] = $result; |
| 43 |
} |
| 44 |
|
| 45 |
foreach($selection as $button_id) |
| 46 |
{ |
| 47 |
$button = MB()->getClass("button"); |
| 48 |
|
| 49 |
// performance: bypass the set ( = query ) for every button |
| 50 |
//$button->set($button_id); |
| 51 |
|
| 52 |
$data = isset($data_array[$button_id]) ? $data_array[$button_id] : array(); |
| 53 |
if (count($data) > 0) |
| 54 |
{ |
| 55 |
maxButtons::buttonLoad(array("button_id" => $button_id)); // central registration - from button |
| 56 |
$button->setupData($data); |
| 57 |
} |
| 58 |
else |
| 59 |
continue; // non-existing buttons should not show. |
| 60 |
|
| 61 |
maxButtons::forceNextID(); |
| 62 |
|
| 63 |
$buttonsArray[] = $button; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
return $buttonsArray; |
| 68 |
} |
| 69 |
|
| 70 |
function parse($domObj, $args) |
| 71 |
{ |
| 72 |
|
| 73 |
$buttons = $this->collection->getLoadedButtons(); |
| 74 |
|
| 75 |
$output = ''; |
| 76 |
|
| 77 |
$collectionObj = $domObj->find('.maxcollection', 0); |
| 78 |
|
| 79 |
$btn_display = ''; |
| 80 |
|
| 81 |
foreach($buttons as $button) |
| 82 |
{ |
| 83 |
|
| 84 |
//$button->reloadData(); |
| 85 |
$button_id = $button->getID(); |
| 86 |
|
| 87 |
$document_id = $button->getDocumentID(); |
| 88 |
|
| 89 |
$default_args = array("mode" => "normal", |
| 90 |
"compile" => false, |
| 91 |
"load_type" => "footer", |
| 92 |
); |
| 93 |
$button_args = wp_parse_args($args, $default_args); |
| 94 |
$button_args["load_css"] = $button_args["load_type"]; |
| 95 |
|
| 96 |
$button_args["echo"] = false; // non-optional. |
| 97 |
|
| 98 |
if (isset($args['preview']) && $args["preview"]) |
| 99 |
$button_args["mode"] = "preview"; // buttons work with mode. |
| 100 |
|
| 101 |
$btn_display .= "<span class='mb-collection-item item-$button_id ' data-doc-id='" . $document_id . "'>" . $button->display($button_args) . "</span>"; |
| 102 |
|
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
$collectionObj->innertext = $btn_display; |
| 108 |
|
| 109 |
$domObj->load($domObj->save()); |
| 110 |
|
| 111 |
return $domObj; |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
function save_fields($data, $post) |
| 116 |
{ |
| 117 |
$blockdata = $this->data; |
| 118 |
|
| 119 |
$selection = array(); |
| 120 |
|
| 121 |
$button = MB()->getClass("button"); |
| 122 |
|
| 123 |
if (isset($post["sorted"])) |
| 124 |
{ |
| 125 |
$sorted = array_filter(explode(",",sanitize_text_field($post["sorted"]))); |
| 126 |
$i = 0; |
| 127 |
foreach($sorted as $button_id) |
| 128 |
{ |
| 129 |
|
| 130 |
if (intval($button_id) > 0) |
| 131 |
{ |
| 132 |
$selection[$i] = $button_id; |
| 133 |
$i++; |
| 134 |
|
| 135 |
$set_bool = $button->set($button_id); |
| 136 |
if (! $set_bool) continue; // buttons that don't exist, like virtuals. |
| 137 |
|
| 138 |
$button_data = $button->get(); |
| 139 |
$collection_id = $this->collection->getID(); |
| 140 |
$collection_meta = isset($button_data["meta"]["in_collections"]) ? |
| 141 |
$button_data["meta"]["in_collections"] : array(); |
| 142 |
|
| 143 |
if (! is_array($collection_meta)) |
| 144 |
$collection_meta = maybe_unserialize($collection_meta); |
| 145 |
|
| 146 |
if (is_array($collection_meta)) |
| 147 |
{ |
| 148 |
$key = array_search($collection_id, $collection_meta); |
| 149 |
if ($key === false) |
| 150 |
{ |
| 151 |
$collection_meta[] = $collection_id; |
| 152 |
$button_data["meta"]["in_collections"] = $collection_meta; |
| 153 |
$button->update($button_data); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
/* If a button id is in the previous selection, but not in the current, maybe the button should be deleted if the |
| 160 |
button appears to be auto-generated only for this collection */ |
| 161 |
if (isset($post["previous_selection"])) |
| 162 |
{ |
| 163 |
$previous = array_filter(explode(",",sanitize_text_field($post["previous_selection"]))); |
| 164 |
foreach($previous as $button_id) |
| 165 |
{ |
| 166 |
if (! in_array($button_id, $selection) && intval($button_id) > 0) |
| 167 |
$this->collection->maybe_delete_button($button_id); |
| 168 |
} |
| 169 |
|
| 170 |
} |
| 171 |
$data[$this->blockname]["selection"] = $selection; |
| 172 |
return $data; |
| 173 |
} |
| 174 |
|
| 175 |
function admin_fields() |
| 176 |
{ |
| 177 |
extract($this->data); // admin data |
| 178 |
$btns = $this->collection->editor_getPacks(); |
| 179 |
?> |
| 180 |
|
| 181 |
<?php ob_start(); ?> |
| 182 |
<div id="picker-modal"> |
| 183 |
<div class='picker-packages'> |
| 184 |
|
| 185 |
<ul> |
| 186 |
<?php foreach ($btns as $index => $data) |
| 187 |
{ ?> |
| 188 |
<li><a href="#" data-pack="<?php echo $index ?>"><?php echo $data["tab"]; ?></a></li> |
| 189 |
|
| 190 |
<?php } ?> |
| 191 |
</ul> |
| 192 |
</div> |
| 193 |
<div class='picker-main' > |
| 194 |
</div> |
| 195 |
<div class='picker-inselection'> |
| 196 |
<div class='info'> |
| 197 |
<span class='count'></span> <?php _e("Selected","maxbuttons"); ?> |
| 198 |
<button type="button" name="clear" class='button-link clear-selection'><?php _e("Clear","maxbuttons"); ?></button> |
| 199 |
</div> |
| 200 |
<div class='items'> |
| 201 |
</div> |
| 202 |
<div class='add'> |
| 203 |
<button type="button" name="add-buttons" class="button button-primary"><?php _e("Add selected buttons","maxbuttons"); ?></button> |
| 204 |
</div> |
| 205 |
</div> |
| 206 |
|
| 207 |
|
| 208 |
</div> |
| 209 |
|
| 210 |
<?php |
| 211 |
global $mb_pick_modal; |
| 212 |
$mb_pick_modal = ob_get_contents(); |
| 213 |
|
| 214 |
// print this outside of the main div since it messes with z-index |
| 215 |
add_action('mb-interface-end', maxUtils::namespaceit('mb_print_modal') ); |
| 216 |
function mb_print_modal() |
| 217 |
{ |
| 218 |
global $mb_pick_modal; |
| 219 |
echo $mb_pick_modal; |
| 220 |
} |
| 221 |
ob_end_clean(); |
| 222 |
|
| 223 |
?> |
| 224 |
<div class="mb_tab option-container"> |
| 225 |
<div class="title"> |
| 226 |
<span class="dashicons dashicons-list-view"></span> |
| 227 |
<span class='title'><?php _e("Buttons", "maxbuttons"); ?></span> |
| 228 |
<button name="picker_popup" type="button" class="button-primary"><?php _e("Select Social Share Icons","maxbuttons"); ?></button> |
| 229 |
<span class='right'><button name="save" type="submit" data-form='collection_edit' class="button button-primary"><?php _e("Save All","maxbuttons"); ?></button> |
| 230 |
</span> |
| 231 |
</div> |
| 232 |
<div class="inside"> |
| 233 |
|
| 234 |
<div class="option preview_collection"> |
| 235 |
<label> <?php _e("Current Selection","maxbuttons"); ?></label> |
| 236 |
<p><?php _e("Drag the buttons to change the order of your selection. You can remove the button by clicking on the remove icon.", |
| 237 |
"maxbuttons"); ?> |
| 238 |
</p> |
| 239 |
|
| 240 |
|
| 241 |
<div class="mb_collection_selection"> |
| 242 |
<input type="hidden" name="sorted" value="" /> |
| 243 |
<input type="hidden" name="previous_selection" value="" /> |
| 244 |
|
| 245 |
<div class="sortable buttons"> |
| 246 |
|
| 247 |
<?php |
| 248 |
|
| 249 |
foreach($selection as $button_id) |
| 250 |
{ |
| 251 |
echo "<div class='shortcode-container item' data-id='$button_id'>"; |
| 252 |
$button = MB()->getClass("button"); |
| 253 |
$button->set($button_id); |
| 254 |
echo "<div id='maxbutton-$button_id'>"; |
| 255 |
$button->display(array( "load_css" => "inline", 'mode' => 'preview' )); |
| 256 |
echo "</div>"; |
| 257 |
echo "<div class='button-remove'><span class='dashicons dashicons-no'></span></div>"; |
| 258 |
echo "</div>"; |
| 259 |
} |
| 260 |
|
| 261 |
?> |
| 262 |
|
| 263 |
</div> |
| 264 |
</div> |
| 265 |
</div> <!-- option --> |
| 266 |
</div> <!-- inside --> |
| 267 |
</div> <!-- tab --> |
| 268 |
|
| 269 |
<?php |
| 270 |
|
| 271 |
} |
| 272 |
|
| 273 |
} |
| 274 |
|
| 275 |
?> |
| 276 |
|