| 1 |
<?php |
| 2 |
class AsaCollection { |
| 3 |
|
| 4 |
/** |
| 5 |
* wpdb object |
| 6 |
* @var wpdb $db |
| 7 |
*/ |
| 8 |
protected $db; |
| 9 |
|
| 10 |
protected $db_version = '1.0'; |
| 11 |
|
| 12 |
protected static $_instance; |
| 13 |
|
| 14 |
|
| 15 |
/** |
| 16 |
* @param $wpdb |
| 17 |
* @return AsaCollection |
| 18 |
*/ |
| 19 |
public static function getInstance($wpdb) |
| 20 |
{ |
| 21 |
if (self::$_instance === null) { |
| 22 |
self::$_instance = new self($wpdb); |
| 23 |
} |
| 24 |
return self::$_instance; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* constructor |
| 29 |
*/ |
| 30 |
public function __construct ($wpdb) |
| 31 |
{ |
| 32 |
$this->db = $wpdb; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* initializes the database tables |
| 37 |
*/ |
| 38 |
public function initDB () |
| 39 |
{ |
| 40 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 41 |
|
| 42 |
$t1 = ' |
| 43 |
CREATE TABLE `'. $this->db->prefix .'asa_collection` ( |
| 44 |
`collection_id` int(11) NOT NULL auto_increment, |
| 45 |
`collection_label` varchar(190) NOT NULL, |
| 46 |
PRIMARY KEY (`collection_id`), |
| 47 |
UNIQUE KEY `collection_label` (`collection_label`) |
| 48 |
) |
| 49 |
'; |
| 50 |
dbDelta($t1); |
| 51 |
|
| 52 |
$t2 = ' |
| 53 |
CREATE TABLE `'. $this->db->prefix .'asa_collection_item` ( |
| 54 |
`collection_item_id` int(11) NOT NULL auto_increment, |
| 55 |
`collection_id` int(11) NOT NULL, |
| 56 |
`collection_item_asin` varchar(20) NOT NULL, |
| 57 |
`collection_item_timestamp` datetime NOT NULL, |
| 58 |
PRIMARY KEY (`collection_item_id`), |
| 59 |
UNIQUE KEY `collection_item_asin` (`collection_id`,`collection_item_asin`) |
| 60 |
) |
| 61 |
'; |
| 62 |
dbDelta($t2); |
| 63 |
|
| 64 |
add_option('_asa_db_collections_version', $this->db_version); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* |
| 69 |
*/ |
| 70 |
public function create ($label) |
| 71 |
{ |
| 72 |
if ($this->checkLabel($label) === null) { |
| 73 |
|
| 74 |
$sql = ' |
| 75 |
INSERT INTO `'. $this->db->prefix . AmazonSimpleAdmin::DB_COLL .'` |
| 76 |
(collection_label) |
| 77 |
VALUES |
| 78 |
("'. esc_sql($label) .'") |
| 79 |
'; |
| 80 |
|
| 81 |
return ($this->db->query($sql) === 1); |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* |
| 90 |
*/ |
| 91 |
public function delete ($collection_id) |
| 92 |
{ |
| 93 |
$sql = ' |
| 94 |
DELETE FROM `'. $this->db->prefix . AmazonSimpleAdmin::DB_COLL_ITEM .'` |
| 95 |
WHERE collection_id = '. esc_sql($collection_id) .' |
| 96 |
'; |
| 97 |
|
| 98 |
$this->db->query($sql); |
| 99 |
|
| 100 |
$sql = ' |
| 101 |
DELETE FROM `'. $this->db->prefix . AmazonSimpleAdmin::DB_COLL .'` |
| 102 |
WHERE collection_id = '. esc_sql($collection_id) .' |
| 103 |
'; |
| 104 |
|
| 105 |
$this->db->query($sql); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* @param $collection_id |
| 110 |
* @param $country_code |
| 111 |
*/ |
| 112 |
public function export($collection_id, $country_code) |
| 113 |
{ |
| 114 |
$collections = array(); |
| 115 |
|
| 116 |
if (is_numeric($collection_id)) { |
| 117 |
// single collection |
| 118 |
$collections = array($collection_id); |
| 119 |
} elseif (is_array($collection_id)) { |
| 120 |
// multiple collections |
| 121 |
$collections = $collection_id; |
| 122 |
} |
| 123 |
|
| 124 |
$result = "<asa2_collections>\n"; |
| 125 |
|
| 126 |
foreach ($collections as $collId) { |
| 127 |
|
| 128 |
$collectionName = $this->getLabel($collId); |
| 129 |
$filename = 'ASA1_collection_export_'. date('Y-m-d_H_i_s'); |
| 130 |
$items = $this->getItems($collId); |
| 131 |
|
| 132 |
$result .= "\t<asa2_collection>\n"; |
| 133 |
|
| 134 |
$result .= "\t\t" . '<column name="name">'. sanitize_text_field( $collectionName ) .'</column>' . "\n"; |
| 135 |
$result .= "\t\t<items>\n"; |
| 136 |
|
| 137 |
foreach ($items as $item) { |
| 138 |
$result .= "\t\t\t<item>\n"; |
| 139 |
$result .= "\t\t\t\t<asin>" . sanitize_text_field( $item->collection_item_asin ) . "</asin>\n"; |
| 140 |
$result .= "\t\t\t\t<country_code>" . sanitize_text_field( $country_code ) . "</country_code>\n"; |
| 141 |
|
| 142 |
$result .= "\t\t\t</item>\n"; |
| 143 |
} |
| 144 |
|
| 145 |
$result .= "\t\t</items>\n"; |
| 146 |
|
| 147 |
$result .= "\t</asa2_collection>\n"; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
$result .= "</asa2_collections>\n"; |
| 152 |
|
| 153 |
$xml = new SimpleXMLElement($result); |
| 154 |
|
| 155 |
$filename .= '.xml'; |
| 156 |
|
| 157 |
header('Content-disposition: attachment; filename="'. $filename .'"'); |
| 158 |
header('Content-type: "text/xml"; charset="utf8"'); |
| 159 |
|
| 160 |
// there is no esc_xml function to escape this. all the XML contents is escaped itself, please check above. |
| 161 |
echo $xml->asXML(); |
| 162 |
exit; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* |
| 167 |
*/ |
| 168 |
public function getAll () |
| 169 |
{ |
| 170 |
$collections = array(); |
| 171 |
|
| 172 |
$sql = ' |
| 173 |
SELECT * |
| 174 |
FROM `'. $this->db->prefix . AmazonSimpleAdmin::DB_COLL .'` |
| 175 |
ORDER by collection_label |
| 176 |
'; |
| 177 |
|
| 178 |
$result = $this->db->get_results($sql); |
| 179 |
|
| 180 |
foreach ($result as $row) { |
| 181 |
$collections[$row->collection_id] = $row->collection_label; |
| 182 |
} |
| 183 |
|
| 184 |
return $collections; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* |
| 189 |
*/ |
| 190 |
public function checkAsin ($asin, $collection) |
| 191 |
{ |
| 192 |
$sql = ' |
| 193 |
SELECT collection_item_id as id |
| 194 |
FROM `'. $this->db->prefix . AmazonSimpleAdmin::DB_COLL_ITEM .'` |
| 195 |
WHERE collection_id = "'. esc_sql($collection) .'" |
| 196 |
AND collection_item_asin = "'. esc_sql($asin) .'" |
| 197 |
'; |
| 198 |
|
| 199 |
return $this->db->get_var($sql); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* |
| 204 |
*/ |
| 205 |
public function checkLabel ($label) |
| 206 |
{ |
| 207 |
if (empty($label)) { |
| 208 |
return false; |
| 209 |
} |
| 210 |
|
| 211 |
$sql = ' |
| 212 |
SELECT collection_id |
| 213 |
FROM `'. $this->db->prefix . AmazonSimpleAdmin::DB_COLL .'` |
| 214 |
WHERE collection_label = "'. esc_sql($label) .'" |
| 215 |
'; |
| 216 |
|
| 217 |
return $this->db->get_var($sql); |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* |
| 222 |
*/ |
| 223 |
public function addAsin ($asin, $collection_id) |
| 224 |
{ |
| 225 |
$sql = ' |
| 226 |
INSERT INTO `'. $this->db->prefix . AmazonSimpleAdmin::DB_COLL_ITEM .'` |
| 227 |
(collection_id, collection_item_asin, collection_item_timestamp) |
| 228 |
VALUES |
| 229 |
('. esc_sql($collection_id) .', |
| 230 |
"'. esc_sql($asin) .'", NOW()) |
| 231 |
'; |
| 232 |
|
| 233 |
return ($this->db->query($sql) === 1); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* |
| 238 |
*/ |
| 239 |
public function deleteAsin ($item_id) |
| 240 |
{ |
| 241 |
$sql = ' |
| 242 |
DELETE FROM `'. $this->db->prefix . AmazonSimpleAdmin::DB_COLL_ITEM .'` |
| 243 |
WHERE collection_item_id = '. esc_sql($item_id) .' |
| 244 |
'; |
| 245 |
|
| 246 |
return $this->db->query($sql); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* |
| 251 |
*/ |
| 252 |
public function getLabel ($collection_id) |
| 253 |
{ |
| 254 |
$sql = ' |
| 255 |
SELECT collection_label as label |
| 256 |
FROM `'. $this->db->prefix . AmazonSimpleAdmin::DB_COLL .'` |
| 257 |
WHERE collection_id = "'. esc_sql($collection_id) .'" |
| 258 |
'; |
| 259 |
|
| 260 |
return $this->db->get_var($sql); |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* |
| 265 |
*/ |
| 266 |
public function getId ($collection_label) |
| 267 |
{ |
| 268 |
$sql = ' |
| 269 |
SELECT collection_id |
| 270 |
FROM `'. $this->db->prefix . AmazonSimpleAdmin::DB_COLL .'` |
| 271 |
WHERE collection_label = "'. esc_sql($collection_label) .'" |
| 272 |
'; |
| 273 |
|
| 274 |
return $this->db->get_var($sql); |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* renders a collection form select field |
| 279 |
*/ |
| 280 |
public function getSelectField ($id='collection', $selected=false) |
| 281 |
{ |
| 282 |
$collections = $this->getAll(); |
| 283 |
|
| 284 |
$html = '<select name="'. $id .'" id="'. $id .'">'; |
| 285 |
|
| 286 |
foreach ($collections as $k => $v) { |
| 287 |
if ($k == $selected) { |
| 288 |
$s = ' selected="selected"'; |
| 289 |
} else { |
| 290 |
$s = ''; |
| 291 |
} |
| 292 |
$html .= '<option value="'. $k .'"'. $s .'>'. $v . '</option>'; |
| 293 |
} |
| 294 |
|
| 295 |
$html .= '</select>'; |
| 296 |
|
| 297 |
return $html; |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* get all collection items |
| 302 |
* |
| 303 |
* @param int collection id |
| 304 |
* @param string $output |
| 305 |
* @return array items |
| 306 |
*/ |
| 307 |
public function getItems ($collection_id, $output = OBJECT) |
| 308 |
{ |
| 309 |
$sql = ' |
| 310 |
SELECT collection_item_id, collection_item_asin, |
| 311 |
UNIX_TIMESTAMP(collection_item_timestamp) as timestamp |
| 312 |
FROM `'. $this->db->prefix . AmazonSimpleAdmin::DB_COLL_ITEM .'` |
| 313 |
WHERE collection_id = "'. esc_sql($collection_id) .'" |
| 314 |
ORDER by collection_item_timestamp DESC |
| 315 |
'; |
| 316 |
|
| 317 |
return $this->db->get_results($sql, $output); |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* |
| 322 |
*/ |
| 323 |
public function updateItemTimestamp ($item_id) |
| 324 |
{ |
| 325 |
$sql = ' |
| 326 |
UPDATE `'. $this->db->prefix . AmazonSimpleAdmin::DB_COLL_ITEM .'` |
| 327 |
SET collection_item_timestamp = NOW() |
| 328 |
WHERE collection_item_id = '. esc_sql($item_id) .' |
| 329 |
'; |
| 330 |
|
| 331 |
return $this->db->query($sql); |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* @return bool |
| 336 |
*/ |
| 337 |
public function isEnabled(): bool |
| 338 |
{ |
| 339 |
return $this->db->get_var("SHOW TABLES LIKE '". $this->db->prefix ."asa_collection%'") !== null; |
| 340 |
} |
| 341 |
|
| 342 |
} |