_db_table = $wpdb->prefix . "amz_servers"; $this->_pluginURL = get_option('siteurl') . '/wp-content/plugins/amazon-search'; $this->_ip2Nation = new AMZ_ip2nation; } function getSearch() { if (!isset($this->_search)) { require_once( 'amz-searchBox.php' ); $this->_search = new AMZ_Search; } return $this->_search; } function isCURLinstalled() { return function_exists( 'curl_init' ); } function addHeaderCode() { echo '' . "\n"; // check for CSS file specific to this theme $template = get_option('template'); if ( $template !== "default" ) { $theme_css = getcwd() . '/wp-content/plugins/amazon-search/css/' . $template . '.css'; if ( is_readable( $theme_css ) ) echo '' . "\n"; } } function db_install() { global $wpdb; $table_name = $this->_db_table; if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) { $sql = "CREATE TABLE {$table_name} ( id enum('com','ca','uk','fr','de','jp') NOT NULL default 'com', server enum('amazon.com','amazon.ca','amazon.co.uk','amazon.fr','amazon.de','amazon.co.jp') NOT NULL default 'amazon.com', assoc_server enum('affiliate-program.amazon.com','associates.amazon.ca','affiliate-program.amazon.co.uk','partenaires.amazon.fr','partnernet.amazon.de','affiliate.amazon.co.jp') NOT NULL default 'affiliate-program.amazon.com', default_tag enum('imol-20','imol0a-20','imol-21','imol08-21','imol05-21','imol-22') NOT NULL default 'imol-20', user_tag varchar(32) default NULL, PRIMARY KEY (id), UNIQUE KEY server (server) ) PACK_KEYS=1 "; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); $_DATA[] = "INSERT IGNORE INTO {$table_name} VALUES ('com', 'amazon.com', 'affiliate-program.amazon.com', 'imol-20', '')"; $_DATA[] = "INSERT IGNORE INTO {$table_name} VALUES ('ca', 'amazon.ca', 'associates.amazon.ca', 'imol0a-20', '')"; $_DATA[] = "INSERT IGNORE INTO {$table_name} VALUES ('uk', 'amazon.co.uk', 'affiliate-program.amazon.co.uk', 'imol-21', '')"; $_DATA[] = "INSERT IGNORE INTO {$table_name} VALUES ('fr', 'amazon.fr', 'partenaires.amazon.fr', 'imol08-21', '')"; $_DATA[] = "INSERT IGNORE INTO {$table_name} VALUES ('de', 'amazon.de', 'partnernet.amazon.de', 'imol05-21', '')"; $_DATA[] = "INSERT IGNORE INTO {$table_name} VALUES ('jp', 'amazon.co.jp', 'affiliate.amazon.co.jp', 'imol-22', '')"; foreach ($_DATA as $insert) { $results = $wpdb->query( $insert ); } add_option("AMZ_db_version", $this->_version); } } function getAdminOptions() { $adminOptions = array( 'contribute' => '4', 'default_category' => 'Blended', 'default_search' => 'WordPress', 'default_server' => 'com', 'image_size' => 'Medium', 'search_count' => '0', 'xsl' => $this->_pluginURL . '/xsl/wp-amz-search.xsl', 'use_text_links' => true, 'use_ip2nation' => true, 'use_searching' => true, 'use_tinymce' => true, ); $savedOptions = get_option($this->_adminOptionsName); $dirty = false; if (!empty($savedOptions)) { foreach ($savedOptions as $key => $option) { if ( $adminOptions[$key] != $option ) { $dirty = true; $adminOptions[$key] = $option; } } } if ( $dirty ) update_option($this->_adminOptionsName, $adminOptions); if ( !$this->isCURLinstalled() ) $adminOptions['use_searching'] = false; return $adminOptions; } function getServerInfo( $serverID ) { global $wpdb; $_AMZ_PREFS = $this->getAdminOptions(); // Get the server info $sql = 'SELECT * FROM ' . $this->_db_table . ' WHERE id="' . $serverID .'"'; $server_info = $wpdb->get_row($sql, ARRAY_A); if ( $server_info['user_tag'] == NULL ) { $server_info['user_tag'] = $server_info['default_tag']; } else if ( $_AMZ_PREFS['contribute'] > 0 ) { $_AMZ_PREFS['search_count'] += 1; if ( $_AMZ_PREFS['search_count'] >= $_AMZ_PREFS['contribute'] ) { $server_info['user_tag'] = $server_info['default_tag']; $_AMZ_PREFS['search_count'] = 0; } // Update the search count update_option($this->_adminOptionsName, $_AMZ_PREFS); } return $server_info; } function printAdminPage() { global $wpdb; $display = ''; $_AMZ_PREFS = $this->getAdminOptions(); // Check for and save updated prefs if ( isset( $_POST['Save'] ) ) { check_admin_referer('amz_search-admin_options'); $new_prefs = $_POST['new_prefs']; // workaround for HTML being braindead - it only passes checkboxes which are 'on' $_AMZ_PREFS['use_text_links'] = false; $_AMZ_PREFS['use_ip2nation'] = false; $_AMZ_PREFS['use_searching'] = false; $_AMZ_PREFS['use_tinymce'] = false; foreach ( $new_prefs as $pref => $value ) { if (isset($new_prefs[$pref])) { $_AMZ_PREFS[$pref] = $new_prefs[$pref]; } } if ( !$this->isCURLinstalled() ) $_AMZ_PREFS['use_searching'] = false; update_option($this->_adminOptionsName, $_AMZ_PREFS); $results = $wpdb->get_results('SELECT * FROM ' . $this->_db_table, ARRAY_A); foreach ($results as $server_info) { $associate_id = trim( $_POST['usertag'][$server_info['id']] ); $sql = 'UPDATE ' . $this->_db_table . " SET user_tag='$associate_id' WHERE id='{$server_info['id']}'"; $wpdb->query($sql); } $display .= '

Settings Updated.

'; } $display .= '
'; $display .= $this->_nonce_field('amz_search-admin_options'); // Submit $display .= '
Amazon Search v' . $this->_version . ' by Andy Maloney
'; // Amazon Associate prefs $display .= '
Associate IDs
'; $results = $wpdb->get_results('SELECT * FROM ' . $this->_db_table, ARRAY_A); foreach ($results as $server_info) { $flag_url = $this->_pluginURL . '/images/flag_' . $server_info['id'] . '.gif'; $alt_flag = $server_info['id'] . ' flag'; $display .= ''; $display .= ''; $display .= ''; $display .= ''; $display .= ''; $display .= ''; $display .= ''; } $display .= '
Default Amazon Server Associate ID Web Site
'; $display .= ''; $display .= '' . $alt_flag . '' . $server_info['server'] . ''; $display .= $server_info['assoc_server'] . '
'; // Contribute to Andy prefs $display .= '
Development Contribution

Please help support development of this plugin by using Andy\'s associate ids for some of your searches.

Use Andy\'s associate id every searches [or page displays if you are using text links - see below].

'; // Text links prefs $display .= '
Text Links
'; $display .= ''; $display .= ''; $display .= ''; $display .= ''; if ( $this->_ip2Nation->isDBinstalled() ) { $display .= ''; $display .= ''; $display .= ''; $display .= ''; } else { $display .= ''; $display .= ''; $display .= ''; } $display .= ''; $display .= ''; $display .= ''; $display .= ''; $display .= '
Use Text LinksAllows you to use tags of the form [amazon ISBN=###]Title[/amazon] in your posts.
These will be converted to Amazon links using your associate IDs from above.
Use ip2NationUse the ip2nation database to look up the visitor\'s IP and use the best Amazon store based on location and language.
Use ip2NationIf you install the ip2nation database, we can use geolocation on the visitor\'s IP to pick the best Amazon store based on location and language.
Add Editor ButtonsAllows you to turn off the Amazon buttons in the TinyMCE editor and quicktag toolbar.
'; // Search prefs $display .= '
Searching'; if ( !$this->isCURLinstalled() ) { $display .= '
Your HTTP server does not support cURL. The search facility of this plugin will not work and has been disabled. Text links will not be affected.
'; } $display .= ''; $display .= ''; $display .= ''; $display .= ''; $display .= ''; $display .= ''; $display .= ''; $display .= ''; $display .= ''; $display .= ''; $display .= ''; $display .= ''; $display .= ''; $display .= ''; $display .= ''; $display .= ''; $display .= '
Use SearchingAllows you to turn off the search capability and widget so you just use text links.
Default Category'; $display .= $this->buildCategoryOptionList('new_prefs[default_category]', $_AMZ_PREFS['default_category']); $display .= '
Default Search Term
XSL file [full URL]
Image Size
'; // Submit $display .= '
[Andy\'s site]
'; echo $display; } // my own nonce functions because I don't want to echo the stuff immediately... function _nonce_field($action = -1, $name = "_wpnonce", $referer = true) { $name = attribute_escape($name); $ret = ''; if ( $referer ) $ret .= $this->_referer_field(); return $ret; } function _referer_field() { $ref = attribute_escape($_SERVER['REQUEST_URI']); $ret = ''; if ( wp_get_original_referer() ) { $original_ref = attribute_escape(stripslashes(wp_get_original_referer())); $ret .= ''; } return $ret; } function buildCategoryOptionList( $name, $selected ) { $category = ''; return $category; } // Convert a string of the form "a=b c=d" into an associative array // From here: function parseNameValues($text) { $values = array(); if (preg_match_all('/([^=\s]+)=("(?P[^"]+)"|' . '\'(?P[^\']+)\'|(?P.+?)\b)/', $text, $matches, PREG_SET_ORDER)) { foreach ($matches as $match) $values[strtolower(trim($match[1]))] = trim(@$match['value1'] . @$match['value2'] . @$match['value3']); } return $values; } // Links are of the form: [amazon isbn=0123456789]Title[/amazon] // You may also use asin in place of isbn. // These will be converted to amazon links pointing at your default server using your associate ID for that server. function createLinks( $content ) { if ( preg_match_all($this->_linkRegEx, $content, $matches) ) { $_AMZ_PREFS = $this->getAdminOptions(); $server_code = $_AMZ_PREFS['default_server']; if ( $_AMZ_PREFS['use_ip2nation'] ) $server_code = $this->_ip2Nation->getAmazonServerFromIP( $server_code ); $server_info = $this->getServerInfo( $server_code ); $amazon_server = $server_info['server']; $ass_id = $server_info['user_tag']; for ($i=0; $i < count($matches[0]); $i++) { $match = $matches[0][$i]; // the whole text that was matched $args = strip_tags( trim($matches[1][$i]) ); // of the form "a=b c=d" $title = $matches[2][$i]; // the title of the link if (empty($args) || empty($title)) continue; $nameValues = $this->parseNameValues( $args ); $asin = $nameValues['asin']; $isbn = $nameValues['isbn']; // check if we should use ISBN if ( empty($asin) ) $asin = $isbn; if ( empty($asin) ) continue; $newlink = '' . $title . ''; $content = str_replace($match, $newlink, $content); } } return $content; } } } // Initialize the admin panel function AMZ_Search_adminPage() { global $gAMZ_Tools; if (!isset($gAMZ_Tools)) return; if (function_exists('add_options_page')) add_options_page('Amazon Search', 'Amazon Search', 9, basename(__FILE__), array(&$gAMZ_Tools, 'printAdminPage')); } // Handle all our main registrations and actions if (class_exists('AMZ_Tools')) { $gAMZ_Tools = new AMZ_Tools(); register_activation_hook( __FILE__, array(&$gAMZ_Tools, 'db_install') ); $_AMZ_PREFS = $gAMZ_Tools->getAdminOptions(); // add handling of [amazon][/amazon] links if ( $_AMZ_PREFS['use_text_links'] ) add_filter('the_content', array(&$gAMZ_Tools, 'createLinks'), 1); add_action('wp_head', array(&$gAMZ_Tools, 'addHeaderCode'), 1); add_action('admin_menu', 'AMZ_Search_adminPage'); if ( $_AMZ_PREFS['use_tinymce'] ) require_once( 'amz-tinymce.php' ); if ( $_AMZ_PREFS['use_searching'] ) $gAMZ_Tools->getSearch()->addActions(); } ?>