AmazoLinkenator Settings'; array_unshift($links, $settings_link); return $links; } $plugin = plugin_basename(__FILE__); add_filter("plugin_action_links_$plugin", 'AmazoLinkenator_settings_link' ); // build the class for all of this class AmazoLinkenator_Settings_Page { // Holds the values to be used in the fields callbacks private $options; // start your engines! public function __construct() { add_action( 'admin_menu', array( $this, 'AmazoLinkenator_add_plugin_page' ) ); add_action( 'admin_init', array( $this, 'AmazoLinkenator_page_init' ) ); } // add options page public function AmazoLinkenator_add_plugin_page() { // This page will be under "Settings" add_options_page( 'AmazoLinkenator Settings Admin', 'AmazoLinkenator Settings', 'manage_options', 'AmazoLinkenator_settings', array( $this, 'AmazoLinkenator_create_admin_page' ) ); } // options page callback public function AmazoLinkenator_create_admin_page() { // Set class property $this->options = get_option( 'AmazoLinkenator_options' ); ?>
'input', 'fieldsize' => '50', 'fieldmax' => '50') ); add_settings_field( 'amazon_affiliate_key', 'Your Amazon Affiliate Key', array( $this, 'AmazoLinkenator_amazon_affiliate_key_callback' ), 'AmazoLinkenator_setting_admin', 'AmazoLinkenator_setting_section_id', // Section array('fieldtype' => 'input', 'fieldsize' => '50', 'fieldmax' => '50') ); add_settings_field( 'enable_affiliator_posts', 'Enable AmazoLinkenator for post/page content?', array( $this, 'AmazoLinkenator_enable_affiliator_posts_callback' ), 'AmazoLinkenator_setting_admin', 'AmazoLinkenator_setting_section_id', // Section array('fieldtype' => 'checkbox', 'fieldsize' => null, 'fieldmax' => null ) ); add_settings_field( 'enable_comments', 'Enable AmazoLinkenator for Comments?', array( $this, 'AmazoLinkenator_enable_comments_callback' ), 'AmazoLinkenator_setting_admin', 'AmazoLinkenator_setting_section_id', // Section array('fieldtype' => 'checkbox', 'fieldsize' => null, 'fieldmax' => null ) ); add_settings_field( 'auto_shorten', 'Enable automatic shortening of the URL? auto_shorten', array( $this, 'AmazoLinkenator_auto_shorten_callback' ), 'AmazoLinkenator_setting_admin', 'AmazoLinkenator_setting_section_id', // Section array('fieldtype' => 'text', 'fieldsize' => null, 'fieldmax' => null ) ); add_settings_field( 'bitly_token', 'Enter your Bit.ly Generic Access Token', array( $this, 'AmazoLinkenator_bitly_token_callback' ), 'AmazoLinkenator_setting_admin', 'AmazoLinkenator_setting_section_id', // Section array('fieldtype' => 'input', 'fieldsize' => '50', 'fieldmax' => '50') ); } // sanitize the settings fields on submit // @param array $input Contains all settings fields as array keys public function AmazoLinkenator_sanitize( $input ) { $new_input = array(); if( isset( $input['the_version'] ) ) $new_input['the_version'] = $input['the_version']; if( isset( $input['amazon_affiliate_key'] ) ) $new_input['amazon_affiliate_key'] = sanitize_text_field( $input['amazon_affiliate_key'] ); if( isset( $input['enable_comments'] ) ) $new_input['enable_comments'] = "1"; if( isset( $input['enable_affiliator_posts'] ) ) $new_input['enable_affiliator_posts'] = "1"; if( isset( $input['auto_shorten'] ) ) $new_input['auto_shorten'] = "1"; if( isset( $input['bitly_token'] ) ) $new_input['bitly_token'] = sanitize_text_field( $input['bitly_token'] ); return $new_input; } // print the Section text public function AmazoLinkenator_print_section_info() { print '

Settings for AmazoLinkenator

'; print '

Save your settings once after upgrading to the latest version.

'; } // version number callback public function AmazoLinkenator_the_version_callback() { printf( '', isset( $this->options['the_version'] ) ? esc_attr( $this->options['the_version']) : esc_attr( $this->options['the_version']) ); } // api key callback public function AmazoLinkenator_amazon_affiliate_key_callback() { printf( '
Enter Your Amazon Affiliate Key. Make sure it is correct; it is not validated.
', isset( $this->options['amazon_affiliate_key'] ) ? esc_attr( $this->options['amazon_affiliate_key']) : '' ); } // content checkbox callback public function AmazoLinkenator_enable_affiliator_posts_callback() { printf( "
options[enable_affiliator_posts] , false ) . " />Check if you want to Affiliate the Amazon URLs in page/post content. URLs are affiliated only on content save/update.
", isset($this->options['enable_affiliator_posts'] ) ? '1' : '0' ); } // comment checkbox callback public function AmazoLinkenator_enable_comments_callback() { printf( "
options[enable_comments] , false ) . " />Check if you want to Affiliate the Amazon URLs in post comments. URLs in comments are only affiliated when the comment is saved or updated.
", isset($this->options['enable_comments'] ) ? '1' : '0' ); } // comment checkbox callback public function AmazoLinkenator_auto_shorten_callback() { printf( "
options[auto_shorten] , false ) . " />Check if you want to automatically shorten the URL (great for 'hiding' your Affiliate codes). URLs are only shortened when a post/page/comment is saved or updated. Requires you to get set up an Bit.ly Application here https://bitly.com/a/create_oauth_app , and then get a 'Generic Access Token' here https://bitly.com/a/oauth_apps. Enter your Bit.ly Generic Access Token below.. You can check shortened URLs here for your affiliate code.
", isset($this->options['enable_comments'] ) ? '1' : '0' ); } // comment checkbox callback public function AmazoLinkenator_bitly_token_callback() { printf( '
Enter Your Bit.ly Generic Access Token Make sure it is correct; it is not validated.
', isset( $this->options['bitly_token'] ) ? esc_attr( $this->options['bitly_token']) : '' ); } } if( is_admin() ) // end of the class stuff $my_settings_page = new AmazoLinkenator_Settings_Page(); // ---------------------------------------------------------------------------- // supporting functions // ---------------------------------------------------------------------------- // display the top info part of the page // ---------------------------------------------------------------------------- function AmazoLinkenator_info_top() { //$image = plugin_dir_url( __FILE__ ) . '/assets/AmazoLinkenator_logo.png'; $image1 = plugin_dir_url( __FILE__ ) . '/assets/icon-128x128.png'; $image2 = plugin_dir_url( __FILE__ ) . '/assets/banner-772x250.png'; ?>

AmazoLinkenator will automatically (without any effort on your part) add your Amazon Affiliate code to any Amazon URLs. This will happen for pages, posts, and comments (depending on your settings below). Because you don't have to do anything special to affiliate a URL, this plugin is great for sites with lots of authors.

The big advantage is that if you find an Amazon item that you want to include in a page and post, you don't need to do any extra steps to add your affiliate code. Just paste the Amazon link in the page/post, and Publish. Your Amazon Affiliate link will automatically be added to the product URL.

And AmazoLinkenator also works with any Amazon product links that your site commenters might add. If anyone includes a link in their comment that has their Amazon Affiliate code, it will be replaced with your affiliate code. All automatically!

URLs are affiliated only when posts/pages/comments are saved/updated/submitted. Any prior content will get the current affiliate code if it is re-saved.

All you need is a valid Amazon Affiliate Key; start here. The plugin doesn't check for a valid Amazon Affiliate key. Any URL affiliated will not be re-affiliated, and will stay the same.

Plus, there's an option to automatically shorten the URL. This is great for 'hiding' your Amazon Affilliate link code. You just need a free Bit.ly Generic Access Token (start here).


CellarWeb.com'; // leave this empty if no company 2 $xcompanylink2 = ''; // output echo 'Copyright © ' . $xstartyear . ' - ' . date("Y") . ' by ' . $xname . ' and ' . $xcompanylink1 ; if ($xcompanylink2) { echo ' and '; echo $xcompanylink2; } echo ' , All Rights Reserved. Released under GPL2 license.
This plugin, authors, or CellarWeb.com has no relationship to, or is not sponsored by, or is not approved by Amazon or Bit.ly, other than being an Amazon Affiliate. Amazon has their own copyrights; so does Bit.ly'; return; } // end copyright --------------------------------------------------------- // ---------------------------------------------------------------------------- // ``end of admin area // ---------------------------------------------------------------------------- // ---------------------------------------------------------------------------- // start of operational area that changes the comments box stuff // ---------------------------------------------------------------------------- global $AmazoLinkenator_options; global $affiliate_key; global $commentdata; global $bitly_token; global $shortenurl; $AmazoLinkenator_options = get_option( 'AmazoLinkenator_options' ); $shortenurl = $AmazoLinkenator_options['auto_shorten']; // ---------------------------------------------------------------------------- // set the affiliate_key global thing $affiliate_key = $AmazoLinkenator_options['amazon_affiliate_key']; // set up the filters to process things based on the options settings // preprocess comment after submitted to Affiliate URLs if enabled if ($AmazoLinkenator_options['enable_comments']) { add_filter( 'preprocess_comment' , 'url_affiliate_comment',11 ); } // preprocess posts/pages after submitted to Affiliate URLs if enabled if ($AmazoLinkenator_options['enable_affiliator_posts']) { add_filter( 'wp_insert_post_data' , 'url_affiliate_post', 11 ); } // ---------------------------------------------------------------------------- // ---------------------------------------------------------------------------- // end of add_actions and add_filters for posts/pages with comments open // ---------------------------------------------------------------------------- // ---------------------------------------------------------------------------- // here's where we do the work! // ---------------------------------------------------------------------------- function url_affiliate_comment ($commentdata ) { // Affiliate URLs in comments global $commentdata; global $AmazoLinkenator_options; //$xy = serialize($AmazoLinkenator_options); //if ($AmazoLinkenator_options[auto_shorten] == 1) { $xx = ' yes '; } else {$xx = " no ";} unset( $commentdata['comment_author_url'] ); $xtext = $commentdata['comment_content']; $xaffiliated = AmazoLinkenator_find_the_urls($xtext); $commentdata['comment_content'] = $xaffiliated; return $commentdata; } function url_affiliate_post ($data, $postarr) { // Affiliate URLs in posts and pages $xtext = $data['post_content']; $data['post_content'] = AmazoLinkenator_find_the_urls($xtext); return $data; } // ---------------------------------------------------------------------------- // affiliate the urls function AmazoLinkenator_find_the_urls( $text ) { global $affiliate_key; global $AmazoLinkenator_options; global $bitly_token; global $shortenurl; $shortenurl = $AmazoLinkenator_options['auto_shorten']; // get the urls_allowed value // regex to find all types of urls in the text $regex = "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i" ; // put found urls in the $urls_array array preg_match_all($regex, $text, $urls_array) ; // extract the elements of the first element to get a one-dimension array $urls_array = $urls_array[0]; // remove the first 'urls_allowed' count elements from the $results array // for each $urls_array, find the position in the text, then delete the url from that spot only /* possible Amazon sites amazon.com amazon.co.uk amazon.de amazon.fr amazon.ca amazon.jp */ foreach ($urls_array as $url_item) { if ( strpos(strtolower($url_item),'amazon.')) { if (strpos($text, $url_item)) { $xaffiliated_url = AmazoLinkenator_add_affiliate($url_item); if ($shortenurl) { $xaffiliated_url =bitly_url_shorten($xaffiliated_url, $bitly_token); } else { $xaffiliated_url .= " no short " ; } if (is_null($xaffiliated_url)) {return $text; } // error, so leave it alone $text = str_replace( $url_item,$xaffiliated_url,$text); } } } return $text; // excess urls stripped or replaced } // ---------------------------------------------------------------------------- // atick in the affiliate value function AmazoLinkenator_add_affiliate($url) { global $affiliate_key; global $AmazoLinkenator_options; global $bitly_token; $bitly_token = $AmazoLinkenator_options['bitly_token']; // get the query part of the URL $parts = parse_url($url); // get the page (non-query) part of the URL $page = str_replace($parts['query'],'', $url); // put the query parts into an array parse_str($parts['query'], $query); // check for the affiliate ID if ($query['tag']) { $query['tag'] = $affiliate_key; } else { $query['tag'] = $affiliate_key; } $newquery = http_build_query($query); $newurl = $page . $newquery ; return $newurl; } // ---------------------------------------------------------------------------- // from http://www.sanwebe.com/2013/07/shorten-urls-bit-ly-api-php function bitly_url_shorten($long_url, $bitly_token) { $url = 'https://api-ssl.bitly.com/v3/shorten?access_token='.$bitly_token.'&longUrl='.urlencode($long_url); try { $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 4); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); $output = json_decode(curl_exec($ch)); } catch (Exception $e) { } return $output->data->url; // if(isset($output)){return $output->data->url;} // else {return ' failed: ' . $output->status_code ; } } // ---------------------------------------------------------------------------- // all done! // ----------------------------------------------------------------------------