mdevore

Updated Amazon Associate Filter WP plugin, redux

May 6th, 2016
137
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. Plugin Name: Amazon Associate Filter
  4. Plugin URI:
  5. Description: Scrubs amazon links and replaces (or adds) the affiliate ID set in Amazon Aff options. <b>IMPORTANT NOTE</b>: the default affiliate ID is my affiliate ID. You <i>MUST UPDATE THE AFFILIATE ID USING THE PLUGIN SETTINGS PAGE</i>; otherwise any affiliate sales will be paid out to my account.
  6. Author: Rajan Agaskar
  7. Version: 0.4242
  8. Author URI: http://agaskar.com
  9. */
  10.  
  11. // modified May 2016 by Michael Devore, per http://metatalk.metafilter.com/24072/Amazon-WP-plugin
  12.  
  13. /*
  14. Copyright 2008 Rajan Agaskar
  15.  
  16. This program is free software: you can redistribute it and/or modify
  17. it under the terms of the GNU General Public License as published by
  18. the Free Software Foundation, either version 3 of the License, or
  19. (at your option) any later version.
  20.  
  21. This program is distributed in the hope that it will be useful,
  22. but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. GNU General Public License for more details.
  25.  
  26. You should have received a copy of the GNU General Public License
  27. along with this program. If not, see <http://www.gnu.org/licenses/>.
  28.  
  29. */
  30.  
  31.  
  32. add_filter('the_content','filter_amazon_associate_filter');
  33. add_filter('comment_text','filter_amazon_associate_filter');
  34.  
  35. function filter_amazon_associate_filter($content) {
  36. $affiliate_code=get_option('amazon_associate_filter_id');
  37.  
  38. $new_content=preg_replace(
  39. '/http:\/\/[^>]*?amazon.([^\/]*)\/([^>]*?ASIN|gp\/product|exec\/obidos\/tg\/detail\/-|[^>]*?dp)\/([0-9a-zA-Z]{10})[a-zA-Z0-9#\/\*\-\?\&\%\=\,\._;]*/i',
  40. 'http://www.amazon.$1/dp/$3/?tag='.$affiliate_code,
  41. $content
  42. );
  43. if ($new_content === $content)
  44. {
  45. $new_content=preg_replace(
  46. '/http:\/\/[^>]*?amazon.([^\/]*)\/([^>]*?ASIN|gp\/product|exec\/obidos\/tg\/detail\/-|[^>]*?(dp|[a-zA-Z0-9#\/\*\-\?\&\%\=\,\._;]*e))\/([0-9a-zA-Z]{10})[a-zA-Z0-9#\/\*\-\?\&\%\=\,\._;]*/i',
  47. 'http://www.amazon.$1/$3/$4/?tag='.$affiliate_code,
  48. $content
  49. );
  50. }
  51. $content = $new_content;
  52.  
  53. return $content;
  54. }
  55.  
  56. function set_amazon_associate_filter_options () {
  57. add_option("amazon_associate_filter_id","agaskarcom-20","Your Amazon Associate Code");
  58. }
  59. function modify_menu_amazon_associate_filter () {
  60. add_options_page(
  61. 'Amazon Associate Config', //Title
  62. 'Amazon Associate Config', //Sub-menu title
  63. 'manage_options', //Security
  64. __FILE__, //File to open
  65. 'amazon_associate_filter_options' //Function to call
  66. );
  67. }
  68. function amazon_associate_filter_options () {
  69. echo '<div class="wrap"><h2>Amazon Associate ID Configuration</h2>';
  70. if ($_REQUEST['submit']) {
  71. update_amazon_associate_filter_options();
  72. }
  73. print amazon_associate_filter_form();
  74. echo '</div>';
  75. }
  76. function update_amazon_associate_filter_options() {
  77. $updated = false;
  78. if ($_REQUEST['amazon_associate_filter_id']) {
  79. update_option('amazon_associate_filter_id', $_REQUEST['amazon_associate_filter_id']);
  80. $updated = true;
  81. }
  82. if ($updated) {
  83. echo '<div id="message" class="updated fade">';
  84. echo '<p>Configuration Updated</p>';
  85. echo '</div>';
  86. } else {
  87. echo '<div id="message" class="error fade">';
  88. echo '<p>Unable to update options</p>';
  89. echo '</div>';
  90. }
  91. }
  92.  
  93. function amazon_associate_filter_form () {
  94. $amazon_ass_id = get_option('amazon_associate_filter_id');
  95. $form='<form method="post">
  96. <label>Enter your Amazon Associate ID:
  97. <input type="text" name="amazon_associate_filter_id" value="'.$amazon_ass_id.'" />
  98. </label>
  99. <br />
  100. <input type="submit" name="submit" value="Submit" />
  101. </form>';
  102. return $form;
  103. }
  104.  
  105. add_action('admin_menu','modify_menu_amazon_associate_filter');
  106. register_activation_hook(__FILE__,"set_amazon_associate_filter_options");
  107. register_deactivation_hook(__FILE__,"unset_amazon_associate_filter_options");
  108.  
  109. ?>
RAW Paste Data