| 1 |
<?php |
| 2 |
namespace MaxButtons; |
| 3 |
defined('ABSPATH') or die('No direct access permitted'); |
| 4 |
$collectionClass["social"] = "socialCollection"; |
| 5 |
|
| 6 |
class socialCollection extends maxCollection |
| 7 |
{ |
| 8 |
protected $collection_type = "social"; |
| 9 |
protected $uses_blocks = array("picker", "basic","layout", "social","preview", "export"); |
| 10 |
|
| 11 |
public function __construct() |
| 12 |
{ |
| 13 |
parent::__construct(); |
| 14 |
add_filter("mb_col_save_collection_data", array($this,"save_collection"), 10, 2 ); |
| 15 |
|
| 16 |
} |
| 17 |
|
| 18 |
// check if button is real button , or insert if not. |
| 19 |
public function save_collection($data, $post) |
| 20 |
{ |
| 21 |
|
| 22 |
$selection = $data["picker"]["selection"]; |
| 23 |
$pack_buttons = false; |
| 24 |
|
| 25 |
foreach($selection as $index => $button_id) |
| 26 |
{ |
| 27 |
|
| 28 |
if (isset($post["button-data-$button_id"])) |
| 29 |
{ |
| 30 |
$post_data = json_decode(base64_decode($post["button-data-$button_id"]), true); |
| 31 |
|
| 32 |
// check if real = false is set - this is a button from a pack. |
| 33 |
if (isset($post_data["meta"]["is_virtual"]) && $post_data["meta"]["is_virtual"] == true) |
| 34 |
{ |
| 35 |
$pack_buttons = true; // after dealing with virtual button id's, reload is needed |
| 36 |
|
| 37 |
// if so, save button |
| 38 |
$user = wp_get_current_user(); |
| 39 |
|
| 40 |
$button = MB()->getClass("button"); |
| 41 |
$button->clear(); |
| 42 |
|
| 43 |
$post_data["name"] = $post_data["basic"]["name"]; |
| 44 |
$post_data["status"] = $post_data["basic"]["status"]; |
| 45 |
|
| 46 |
// set the proper meta's and user edited = false; |
| 47 |
$post_data["meta"]["is_virtual"] = false; // off to the real world |
| 48 |
$post_data["meta"]["user_edited"] = false; |
| 49 |
$post_data["meta"]["created"] = time(); |
| 50 |
$post_data["meta"]["user_created"] = $user->user_login; |
| 51 |
$post_data["meta"]["in_collections"] = array($this->collection_id); |
| 52 |
$post_data["id"] = 0; // new button |
| 53 |
|
| 54 |
$button->setupData($post_data); |
| 55 |
|
| 56 |
$new_button_id = $button->update($post_data); |
| 57 |
|
| 58 |
// save a reference to collection. |
| 59 |
$data["picker"]["selection"][$index] = $new_button_id; |
| 60 |
|
| 61 |
// update social options to new button id |
| 62 |
$social = $data["social"]; |
| 63 |
|
| 64 |
|
| 65 |
foreach($social as $option => $options) // social-option - data |
| 66 |
{ |
| 67 |
foreach($options as $index => $option_data) // index (count) - data |
| 68 |
{ |
| 69 |
foreach($option_data as $option_id => $opt_data) // button_id - data |
| 70 |
{ |
| 71 |
//echo $option_id . " - $button_id (OPT / BUTT)<BR>"; |
| 72 |
if ($option_id == $button_id) |
| 73 |
{ |
| 74 |
$social["social-option"][$index][$new_button_id] = $opt_data; |
| 75 |
unset($social["social-option"][$index][$button_id]); |
| 76 |
|
| 77 |
|
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
$data["social"] = $social; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if ($pack_buttons) |
| 89 |
{ |
| 90 |
add_filter("collections_ajax_force_reload", function () { return true; }); |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
return $data; |
| 95 |
|
| 96 |
// return $this->collection_id; |
| 97 |
//exit(); |
| 98 |
} |
| 99 |
|
| 100 |
function editor_getPacks() |
| 101 |
{ |
| 102 |
$packs = parent::editor_getPacks(); |
| 103 |
|
| 104 |
// Collect packs from dynamic amount of dirs |
| 105 |
$path = MB()->get_plugin_path() . "assets/packs/" ; |
| 106 |
$packs = array_merge($this->getSocialPacks($path), $packs ); |
| 107 |
return $packs; |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
function getSocialPacks($path) |
| 112 |
{ |
| 113 |
if (! is_dir($path)) |
| 114 |
{ |
| 115 |
MB()->add_notice("error", "Could not load asset packs"); |
| 116 |
return; |
| 117 |
} |
| 118 |
$dirhandle = opendir($path); |
| 119 |
$found_packs = array(); |
| 120 |
while (false !== ($name = readdir($dirhandle))) { |
| 121 |
if ($name != '.' && $name != '..') |
| 122 |
{ |
| 123 |
$found_packs[] = $name; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
sort($found_packs); |
| 128 |
|
| 129 |
foreach($found_packs as $pack) |
| 130 |
{ |
| 131 |
$packsClass = MB()->getClass("pack"); |
| 132 |
|
| 133 |
$socialpack = array("file" => $path . $pack . "/" . $pack . ".xml", |
| 134 |
"img" => '', |
| 135 |
"dir" => '', |
| 136 |
"is_local" => true, |
| 137 |
); |
| 138 |
$result = $packsClass->load_pack($socialpack); |
| 139 |
if(! $result) // failed. |
| 140 |
{ |
| 141 |
continue; |
| 142 |
} |
| 143 |
$packs[$pack]['tab'] = $packsClass->getName(); |
| 144 |
$packs[$pack]["func"] = array($this, 'editor_getSocialButtons'); |
| 145 |
$packs[$pack]["rel"] = "assets/packs/"; |
| 146 |
$packs[$pack]["path"] = $path . $pack; |
| 147 |
|
| 148 |
|
| 149 |
} |
| 150 |
return $packs; |
| 151 |
} |
| 152 |
|
| 153 |
/* public function editor_getButtons() |
| 154 |
{ |
| 155 |
$buttons = parent::editor_getButtons(); |
| 156 |
|
| 157 |
$buttons = $this->editor_getSocialButtons($buttons); |
| 158 |
|
| 159 |
return $buttons; |
| 160 |
} */ |
| 161 |
|
| 162 |
protected function editor_getSocialButtons($packname, $data = array() ) |
| 163 |
{ |
| 164 |
$buttons = array(); |
| 165 |
|
| 166 |
$packsClass = MB()->getClass("pack"); |
| 167 |
|
| 168 |
$packs = $this->editor_getPacks(); |
| 169 |
$data = $packs[$packname]; |
| 170 |
|
| 171 |
|
| 172 |
//$packName = isset($data["tab"]) ? $data["tab"] : ''; |
| 173 |
$packpath = $data["path"]; // isset($data["rel"]) ? $data["rel"] : ''; |
| 174 |
$plugin_path = MB()->get_plugin_path(true); |
| 175 |
$rel = str_replace($plugin_path,'',$packpath); |
| 176 |
|
| 177 |
|
| 178 |
$socialpath = MB()->get_plugin_path(true) . $rel . "/"; |
| 179 |
$socialurl = MB()->get_plugin_url(true) . $rel . "/"; |
| 180 |
|
| 181 |
$socialpack = array("file" => $socialpath . "$packname.xml", |
| 182 |
"img" => '', |
| 183 |
"dir" => $socialurl, |
| 184 |
"is_local" => true, |
| 185 |
); |
| 186 |
|
| 187 |
|
| 188 |
$packsClass->setPackPath($socialurl); |
| 189 |
$packsClass->load_pack($socialpack); |
| 190 |
$xml = $packsClass->getPackXML(); |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
foreach( $xml->maxbutton as $xmlbutton) |
| 195 |
{ |
| 196 |
|
| 197 |
$button_array = $packsClass->parse_pack_button($xmlbutton); |
| 198 |
|
| 199 |
$button = MB()->getClass('button'); |
| 200 |
$button->clear(); // also ensures all fields are loaded |
| 201 |
|
| 202 |
$meta = array("is_virtual" => true, // not a real button |
| 203 |
"created_source" => "collection"); |
| 204 |
$button_array["meta"]= json_encode($meta); |
| 205 |
|
| 206 |
$button->setupData($button_array); |
| 207 |
|
| 208 |
|
| 209 |
if (isset($button_array["collection"])) |
| 210 |
{ |
| 211 |
$coldata = $button_array["collection"]; |
| 212 |
|
| 213 |
// overloading the class to retrieve collection data later on social-block at adding time. |
| 214 |
$button->setData("collection", $coldata); |
| 215 |
|
| 216 |
} |
| 217 |
|
| 218 |
$buttons[] = $button; |
| 219 |
|
| 220 |
} |
| 221 |
return $buttons; |
| 222 |
} |
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
?> |
| 228 |
|