| 1 |
<?php |
| 2 |
namespace MaxButtons; |
| 3 |
defined('ABSPATH') or die('No direct access permitted'); |
| 4 |
|
| 5 |
// basic pack reading functionality for social buttons |
| 6 |
class maxPack |
| 7 |
{ |
| 8 |
protected $default_img_url = ''; |
| 9 |
|
| 10 |
protected $pack_path = ''; // path of the pack with trailing slash |
| 11 |
protected $pack_dir = ''; // the name of the packs directory without path. |
| 12 |
protected $pack_xml = ''; |
| 13 |
protected $pack_url = ''; |
| 14 |
|
| 15 |
|
| 16 |
protected $img = ''; |
| 17 |
protected $name = ''; |
| 18 |
protected $author = ''; |
| 19 |
protected $author_url = ''; |
| 20 |
protected $description = ''; |
| 21 |
protected $is_local = true; |
| 22 |
|
| 23 |
|
| 24 |
public function setPackPath($path) |
| 25 |
{ |
| 26 |
$this->pack_path = $path; |
| 27 |
} |
| 28 |
|
| 29 |
public function getName() |
| 30 |
{ |
| 31 |
return $this->name; |
| 32 |
} |
| 33 |
|
| 34 |
public function getAuthor() |
| 35 |
{ |
| 36 |
return $this->author; |
| 37 |
} |
| 38 |
|
| 39 |
public function getDescription() |
| 40 |
{ |
| 41 |
return $this->description; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
public function load_pack($packdata) |
| 46 |
{ |
| 47 |
$pack_file = $packdata["file"]; |
| 48 |
$pack_img = $packdata["img"]; |
| 49 |
$this->pack_dir = $packdata["dir"]; |
| 50 |
|
| 51 |
$xml = simplexml_load_file($pack_file,null, LIBXML_NOCDATA); |
| 52 |
|
| 53 |
if (! $xml) |
| 54 |
{ |
| 55 |
MB()->add_notice('error', __( sprintf('Pack file could not be loaded', $pack_file), 'maxbuttons')); |
| 56 |
return false; |
| 57 |
} |
| 58 |
$this->pack_xml = $xml; |
| 59 |
|
| 60 |
$pack = $xml->pack[0]; |
| 61 |
$packset = current($pack->attributes()); |
| 62 |
$packset["image"] = $pack_img; |
| 63 |
$packset["is_local"] = $packdata["is_local"]; |
| 64 |
|
| 65 |
$this->set_pack($packset); |
| 66 |
return true; // success |
| 67 |
} |
| 68 |
|
| 69 |
/* Return the full XML of the pack */ |
| 70 |
public function getPackXML() |
| 71 |
{ |
| 72 |
return $this->pack_xml; |
| 73 |
} |
| 74 |
|
| 75 |
/* Set all the pack attributes */ |
| 76 |
function set_pack($pack) |
| 77 |
{ |
| 78 |
$this->img = (isset($pack["image"])) ? $pack["image"] : ''; |
| 79 |
$this->name = (isset($pack["name"])) ? $pack["name"] : ''; |
| 80 |
$this->author = (isset($pack["author"])) ? $pack["author"] : ''; |
| 81 |
$this->authorurl = (isset($pack["author_url"])) ? $pack["author_url"] : ''; |
| 82 |
$this->description = (isset($pack["description"])) ? $pack["description"] : ''; |
| 83 |
$this->is_local = $pack["is_local"]; |
| 84 |
if (isset($pack["pack_dir"])) |
| 85 |
$this->pack_dir = $pack["pack_dir"]; |
| 86 |
if (isset($pack["pack_url"])) |
| 87 |
$this->pack_url = $pack["pack_url"]; |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
/* Parses old format and current format pack XML into a button_array which button setupdata understands */ |
| 93 |
public function parse_pack_button($xmlbutton) |
| 94 |
{ |
| 95 |
$button = MB()->getClass("button"); |
| 96 |
if ( count($xmlbutton->attributes()) > 0) // old button |
| 97 |
{ |
| 98 |
|
| 99 |
$attrs = current($xmlbutton->attributes()); |
| 100 |
|
| 101 |
$data = MB()->getClass("install")->convertOldFields($attrs); |
| 102 |
|
| 103 |
$button_array = array(); |
| 104 |
$button_array["status"] = $data["status"]; |
| 105 |
|
| 106 |
$data = $button->save($data,false); // convert from post var to block struct without dbase save |
| 107 |
|
| 108 |
|
| 109 |
foreach($data as $block => $values) |
| 110 |
{ |
| 111 |
if (is_array($values)) |
| 112 |
$button_array[$block] = json_encode($values); |
| 113 |
else |
| 114 |
$button_array[$block] = $values; |
| 115 |
} |
| 116 |
|
| 117 |
} |
| 118 |
else // new button |
| 119 |
{ |
| 120 |
//$button = MB()->getClass("button"); |
| 121 |
|
| 122 |
$button_array = json_decode(json_encode( (array)$xmlbutton), TRUE); |
| 123 |
|
| 124 |
// $data = $button->save($button_array, false); |
| 125 |
} |
| 126 |
$temp_id = floor(rand(100000,990000)); |
| 127 |
$button_array["id"] = $temp_id; // fingers crossed |
| 128 |
|
| 129 |
// for some reason on json_decode when value is empty it created an empty array instead of string. |
| 130 |
foreach($button_array as $name => $values) |
| 131 |
{ |
| 132 |
if (is_array($values) && count($values) == 0) |
| 133 |
$button_array[$name] = ''; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
// icons from pack |
| 138 |
$icon_array = array(); |
| 139 |
|
| 140 |
if (isset($button_array["icon"])) |
| 141 |
{ |
| 142 |
$icon_array = maybe_unserialize($button_array["icon"]); |
| 143 |
if (! is_array($icon_array)) // moving to json_encode |
| 144 |
$icon_array = json_decode($button_array["icon"], true); |
| 145 |
|
| 146 |
if (isset($icon_array["icon_url"]) && $icon_array["icon_url"] != '') { |
| 147 |
$path = str_replace(array('http:','https:'), '', $this->pack_path); // allow for SSL / HTTP |
| 148 |
$icon_array["icon_url"] = $path . $icon_array["icon_url"]; // pack path to allow for remote images |
| 149 |
} |
| 150 |
if (isset($icon_array['fa_icon_value']) && strlen($icon_array['fa_icon_value']) > 0) |
| 151 |
{ |
| 152 |
$conversion_path = MB()->get_plugin_path() . '/assets/libraries/font-awesome-5/shims.json'; |
| 153 |
$conversion_array = json_decode(file_get_contents($conversion_path), ARRAY_A); |
| 154 |
$old_value = $icon_array['fa_icon_value']; |
| 155 |
$old_value = str_replace('fa-','', $old_value); |
| 156 |
$icon_array['fa_icon_value'] = maxInstall::searchNewFA($old_value, $conversion_array); |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
$button_array['icon'] = json_encode($icon_array); |
| 161 |
} |
| 162 |
|
| 163 |
return $button_array; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
} // class |
| 169 |
|