'http://www.amazon.ca/exec/obidos/ASIN/%s/%s',
'DE' => 'http://www.amazon.de/exec/obidos/ASIN/%s/%s',
'FR' => 'http://www.amazon.fr/exec/obidos/ASIN/%s/%s',
'JP' => 'http://www.amazon.jp/exec/obidos/ASIN/%s/%s',
'UK' => 'http://www.amazon.co.uk/exec/obidos/ASIN/%s/%s',
'US' => 'http://www.amazon.com/exec/obidos/ASIN/%s/%s',
);
/**
* available template placeholders
*/
protected $tpl_placeholder = array(
'SmallImageUrl',
'SmallImageWidth',
'SmallImageHeight',
'MediumImageUrl',
'MediumImageWidth',
'MediumImageHeight',
'LargeImageUrl',
'LargeImageWidth',
'LargeImageHeight',
'Label',
'Manufacturer',
'Publisher',
'Title',
'AmazonUrl',
'TotalOffers',
'LowestOfferPrice',
'LowestOfferCurrency',
'AmazonPrice',
'AmazonCurrency',
'AmazonAvailability',
'AmazonLogoSmallUrl',
'AmazonLogoLargeUrl',
'DetailPageURL',
'Platform'
);
/**
* my tracking id's which will be used if the user doesn't have one
* (for all my good programming work :)
*/
protected $my_tacking_id = array(
'DE' => 'ichdigital-21',
'UK' => 'ichdigitaluk-21',
'US' => 'ichdigitalus-21'
);
/**
* template placeholder prefix
*/
protected $tpl_prefix = '{$';
/**
* template placeholder postfix
*/
protected $tpl_postfix = '}';
/**
* AmazonSimpleAdmin bb tag regex
*/
protected $bb_regex = '#\[asa(.*)\]([\w-]+)\[/asa\]#i';
/**
* AmazonSimpleAdmin bb tag regex
*/
protected $bb_regex_collection = '#\[asa_collection(.*)\]([\w-]+)\[/asa_collection\]#i';
/**
* my Amazon Access Key ID
*/
protected $amazon_api_key_internal = '0TA14MJ6AS7KEC5KN582';
/**
* user's Amazon Access Key ID
*/
protected $amazon_api_key;
/**
* user's Amazon Tracking ID
*/
protected $amazon_tracking_id;
/**
* selected country code
*/
protected $amazon_country_code;
/**
* product preview status
*/
protected $product_preview;
protected $task;
/**
* wpdb object
*/
protected $db;
/**
* collection object
*/
protected $collection;
protected $error = array();
protected $success = array();
/**
* the amazon webservice object
*/
protected $amazon;
/**
* constructor
*/
public function __construct ($wpdb)
{
$libdir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lib';
set_include_path(get_include_path() . PATH_SEPARATOR . $libdir);
if (isset($_GET['task'])) {
$this->task = strip_tags($_GET['task']);
}
$this->db = $wpdb;
require_once 'Zend/Service/Amazon.php';
// Hook for adding admin menus
add_action('admin_menu', array($this, 'createAdminMenu'));
// Hook for adding content filter
add_filter('the_content', array($this, 'parseContent'), 1);
//wp_enqueue_script( 'listman' );
$this->_getAmazonUserData();
if ($this->product_preview == '1') {
add_action('wp_footer', array($this, 'addProductPreview'));
}
$this->amazon = $this->connect();
}
/**
* connects the amazon webservice
*/
protected function connect ()
{
$amazon_api_key = $this->amazon_api_key_internal;
if (!empty($this->amazon_api_key)) {
$amazon_api_key = $this->amazon_api_key;
}
$country_code = null;
if (!empty($this->amazon_country_code)) {
$country_code = $this->amazon_country_code;
}
try {
if ($country_code !== null) {
$amazon = new Zend_Service_Amazon($amazon_api_key, $country_code);
} else {
$amazon = new Zend_Service_Amazon($amazon_api_key);
}
return $amazon;
} catch (Exception $e) {
//echo $e->getMessage();
return null;
}
}
/**
* action function for above hook
*
*/
public function createAdminMenu ()
{
// Add a new submenu under Options:
add_options_page('AmazonSimpleAdmin', 'AmazonSimpleAdmin', 8, basename(__FILE__), array($this, 'createOptionsPage'));
add_action('admin_head', array($this, 'getOptionsHead'));
wp_enqueue_script( 'listman' );
}
/**
* creates the AmazonSimpleAdmin admin page
*
*/
public function createOptionsPage ()
{
echo '
AmazonSimpleAdmin
';
echo $this->getTabMenu($this->task);
$this->_displayDispatcher($this->task);
}
/**
*
*/
protected function getTabMenu ($task)
{
$nav = '';
return $nav;
}
/**
* the actual options page content
*
*/
protected function _displayDispatcher ($task)
{
switch ($task) {
case 'collections':
require_once(dirname(__FILE__) . '/AsaCollection.php');
$this->collection = new AsaCollection($this->db);
$params = array();
if (isset($_POST['submit_new_asin'])) {
$asin = strip_tags($_POST['new_asin']);
$collection_id = strip_tags($_POST['collection']);
$item = $this->_getItem($asin);
if ($item === null) {
// invalid asin
$this->error['submit_new_asin'] = 'invalid ASIN';
} else if ($this->collection->checkAsin($asin, $collection_id) !== null) {
// asin already added to this collection
$this->error['submit_new_asin'] = 'ASIN already added to collection '.
$this->collection->getLabel($collection_id) . '';
} else {
if ($this->collection->addAsin($asin, $collection_id) === true) {
$this->success['submit_new_asin'] = ''. $item->Title .
' added to collection '.
$this->collection->getLabel($collection_id) . '';
}
}
} else if (isset($_POST['submit_manage_collection'])) {
$collection_id = strip_tags($_POST['select_manage_collection']);
$params['collection_items'] = $this->collection->getItems($collection_id);
$params['collection_id'] = $collection_id;
} else if (isset($_GET['select_manage_collection']) && isset($_GET['update_timestamp'])) {
$item_id = strip_tags($_GET['update_timestamp']);
$this->collection->updateItemTimestamp($item_id);
$collection_id = strip_tags($_GET['select_manage_collection']);
$params['collection_items'] = $this->collection->getItems($collection_id);
$params['collection_id'] = $collection_id;
} else if (isset($_POST['submit_delete_collection'])) {
$collection_id = strip_tags($_POST['select_manage_collection']);
$collection_label = $this->collection->getLabel($collection_id);
if ($collection_label !== null) {
$this->collection->delete($collection_id);
}
$this->success['manage_collection'] = 'collection deleted: '.
$collection_label . '';
} else if (isset($_POST['submit_new_collection'])) {
$collection_label = strip_tags($_POST['new_collection']);
if (empty($collection_label)) {
$this->error['submit_new_collection'] = 'Invalid collection label';
} else {
if ($this->collection->create($collection_label) == true) {
$this->success['submit_new_collection'] = 'New collection '.
''. $collection_label . ' created';
} else {
$this->error['submit_new_collection'] = 'This collection already exists';
}
}
} else if (isset($_POST['submit_collection_init']) &&
isset($_POST['activate_collections'])) {
$this->collection->initDB();
}
//var_dump($this->db->get_var("SHOW TABLES LIKE '%asa_collection%'"));
if ($this->db->get_var("SHOW TABLES LIKE '%asa_collection%'") === null) {
$this->_displayCollectionsSetup();
} else {
$this->_displayCollectionsPage($params);
}
break;
case 'usage':
$this->_displayUsagePage();
break;
default:
if (count($_POST) > 0) {
$_asa_amazon_api_key = strip_tags($_POST['_asa_amazon_api_key']);
$_asa_amazon_tracking_id = strip_tags($_POST['_asa_amazon_tracking_id']);
$_asa_product_preview = strip_tags($_POST['_asa_product_preview']);
update_option('_asa_amazon_api_key', $_asa_amazon_api_key);
update_option('_asa_amazon_tracking_id', $_asa_amazon_tracking_id);
update_option('_asa_product_preview', $_asa_product_preview);
if (isset($_POST['_asa_amazon_country_code'])) {
$_asa_amazon_country_code = strip_tags($_POST['_asa_amazon_country_code']);
if ($_asa_amazon_country_code == '0') {
$_asa_amazon_country_code = '';
}
update_option('_asa_amazon_country_code', $_asa_amazon_country_code);
}
}
$this->_displaySetupPage();
}
}
/**
* collections setup screen
*
*/
protected function _displayCollectionsSetup ()
{
?>