Guest User

WP Js External Link Info

a guest
Dec 20th, 2011
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.92 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: WP Js External Link Info
  4. Plugin URI: http://www.joergschueler.de/dev/wp/external-link-info/
  5. Description: Rewrites and redirect all external links in posts, comments and author links with info page.
  6. Author: Joerg Schueler
  7. Version: 1.20
  8. Author URI: http://www.joergschueler.de
  9. */
  10.  
  11. if ( !defined('WP_CONTENT_URL') ) define('WP_CONTENT_URL', get_option('siteurl') . '/wp-content');
  12. if ( !defined('WP_PLUGIN_URL') )  define('WP_PLUGIN_URL', WP_CONTENT_URL . '/plugins' );
  13. define('WP_JS_BASENAME', plugin_basename(__FILE__) );
  14. define('WP_JS_BASEDIR', dirname( plugin_basename(__FILE__) ) );
  15. define('WP_JS_BLOGNAME', get_settings('blogname'));
  16.  
  17. if ( is_admin() ) { //Adds admin verification
  18.     add_action('admin_menu', 'js_external_link_info_add_pages');
  19.     add_action('admin_init', 'register_js_external_link_info_settings');
  20. } else {
  21.  
  22. }
  23.  
  24. function register_js_external_link_info_settings() { // whitelist options
  25.   register_setting( 'js_external_link_info', 'redirect_file' );
  26.   register_setting( 'js_external_link_info', 'redirect_notblank' );
  27.   register_setting( 'js_external_link_info', 'redirect_questsonly' );
  28.   register_setting( 'js_external_link_info', 'redirect_exclude' );
  29. }
  30.  
  31. function js_external_link_info_options_page() {
  32. ?>
  33. <div class="wrap">
  34. <h2>WP Js External Link Info <? echo __("Options", 'js-external-link-info'); ?></h2>
  35. <em>&nbsp;</em>
  36. <form method="post" action="options.php">
  37. <?php wp_nonce_field('js_external_link_info'); ?>
  38.  
  39. <table class="form-table">
  40.  
  41. <tr>
  42. <td align="right" width="30%"><? echo __("Redirect file", 'js-external-link-info'); ?>:</td>
  43. <td width="70%"><?php echo get_bloginfo('home') . '/'; ?><input type="text" name="redirect_file" value="<?php echo get_option('redirect_file'); ?>" /> <small>(<? echo __("empty for default", 'js-external-link-info'); ?>)</small></td>
  44. </tr>
  45.  
  46. <tr>
  47. <td align="right"><? echo __("Redirect active for", 'js-external-link-info'); ?>:</td>
  48. <td>
  49.   <select name="redirect_questsonly" id="redirect_questsonly">
  50.     <option value='false' <?php if (get_option('redirect_questsonly') != 'true') { echo 'selected' ; } ?>><? echo __("Everybody", 'js-external-link-info'); ?></option>
  51.     <option value='true' <?php if (get_option('redirect_questsonly') == 'true') { echo 'selected' ; } ?>><? echo __("Quests only", 'js-external-link-info'); ?></option>
  52.   </select>
  53. </td>
  54. </tr>
  55.  
  56. <tr>
  57. <td align="right"><? echo __("new Browserwindow", 'js-external-link-info'); ?>:</td>
  58. <td>
  59.   <select name="redirect_notblank" id="redirect_notblank">
  60.     <option value='2' <?php if (get_option('redirect_notblank') == 2) { echo 'selected' ; } ?>><? echo __("No", 'js-external-link-info'); ?></option>
  61.     <option value='0' <?php if (get_option('redirect_notblank') < 1) { echo 'selected' ; } ?>><? echo __("Everybody", 'js-external-link-info'); ?></option>
  62.     <option value='1' <?php if (get_option('redirect_notblank') == 1) { echo 'selected' ; } ?>><? echo __("Quests only", 'js-external-link-info'); ?></option>
  63.   </select>
  64. </td>
  65. </tr>
  66. <tr>
  67. <td align="right" valign="top">
  68.     <? echo __("Excluded domains", 'js-external-link-info'); ?>:<br />
  69.     <small><? echo __("Simply enter the domain to exclude separated by line breaks here. (Example: www.joergschueler.de)", 'js-external-link-info'); ?></small>
  70.   </td>
  71. <td>
  72.   <textarea name="redirect_exclude" rows="5" cols="64"><? echo trim(get_option('redirect_exclude')); ?></textarea>
  73. </td>
  74. </tr>
  75.  
  76. </table>
  77.  
  78. <input type="hidden" name="action" value="update" />
  79. <input type="hidden" name="page_options" value="redirect_questsonly,redirect_file,redirect_notblank,redirect_exclude" />
  80.  
  81. <p class="submit">
  82. <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
  83. </p>
  84. <?php settings_fields( 'js_external_link_info' ); ?>
  85. </form>
  86.  
  87. <hr>
  88.  
  89. <em>&nbsp;</em>
  90.  
  91. <table border="1">
  92. <tr>
  93. <td align="right">
  94.     <form action="http://www.joergschueler.de/dev/wp/external-link-info/donate/" method="post">
  95.     <input type="image" src="https://www.paypal.com/de_DE/DE/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="Jetzt einfach, schnell und sicher online bezahlen � mit PayPal.">
  96.     </form>
  97. </td>
  98. <td><? echo __("If you like this extension please feel free to donate for support and future development.", 'js-external-link-info'); ?></td>
  99. </tr>
  100. </table>
  101.  
  102. <em>&nbsp;</em>
  103.  
  104. <hr>
  105.  
  106. </div>
  107.  
  108. <?php
  109. }
  110.  
  111. function js_external_link_info_add_pages() {
  112.     add_options_page('WP Js External Link Info', 'WP Js External Link Info', 8, 'js_external_link_info', 'js_external_link_info_options_page');
  113.  }
  114.  
  115. function js_external_link_info_content($str, $arg = 1) {
  116.     if (!isset($str)) return $str;
  117.     if (!$arg) return $str;
  118.     return preg_replace_callback('#<a\s([^>]*\s*href\s*=[^>]*)>#i', 'js_external_link_info_replace', $str);
  119. }
  120.  
  121. function js_external_link_info_comment($str, $arg = 1) {
  122.     if (!isset($str)) return $str;
  123.     if (!$arg) return $str;
  124.     return preg_replace_callback('#<a\s([^>]*\s*href\s*=[^>]*)>#i', 'js_external_link_info_replace', $str);
  125. }
  126.  
  127. function js_external_link_check_local($href, $blogurl) {
  128.     $schemes = array('http','ftp');
  129.     $isext = false;
  130.     foreach($schemes as $scheme) {
  131.         if (stripos($href, $scheme) !== false) { $isext = true; }
  132. /* var $href mit href prefix, zB.: href="http://wordpress.org/extend/plugins/wp-js-external-link-info/" */
  133.     }
  134.     if ($isext) {
  135.         $local = ((strpos($href, $blogurl)) || (stripos($href, get_option('redirect_exclude'))));
  136.     } else {
  137.         $local = true;
  138.     }
  139.     return $local;
  140. }
  141.  
  142. function js_external_link_info_replace($matches) {
  143.     $blogurl = get_bloginfo('home') . '/';
  144.          if (get_option('redirect_file') != "") { $redirect_file = $blogurl . get_option('redirect_file'); }
  145.                                            else { $redirect_file = WP_PLUGIN_URL . '/' . WP_JS_BASEDIR . '/redirect.php'; }
  146.     $str = $matches[1];
  147.     preg_match_all('/[^=[:space:]]*\s*=\s*"[^"]*"|[^=[:space:]]*\s*=\s*\'[^\']*\'|[^=[:space:]]*\s*=[^[:space:]]*/', $str, $attr);
  148.     $href_arr = preg_grep('/^href\s*=/i', $attr[0]);
  149.     if (count($href_arr) > 0) {
  150.             $href = array_pop($href_arr);
  151.         if ($href) {
  152.             $local = js_external_link_check_local($href, $blogurl);
  153.                          if ( ( (get_option('redirect_notblank') < 2)
  154.                              && ( (get_option('redirect_notblank') < 1) || (!is_user_logged_in()) ) )
  155.                 && ($local === false) && ($href{6} != "#") ) {
  156.                         $blank = 'target="_blank"';
  157.             }
  158.         } else {
  159.                     $local = TRUE;
  160.                  }
  161.  
  162.         if ( ($local === false) && ($href{6} != "#")
  163.           && ( (get_option('redirect_questsonly') != 'true') || (!is_user_logged_in()) ) ) {
  164.             $href = str_replace('?','%3F',$href);
  165.             $href = str_replace('&','%26',$href);
  166.                          $href = preg_replace('/^(href\s*=\s*[\'"]?)/i', '\1' . $redirect_file . '?blog=' . WP_JS_BLOGNAME . '&url=', $href);
  167.         }
  168.         $attr = preg_grep('/^href\s*=/i', $attr[0], PREG_GREP_INVERT);
  169.     }
  170.         return '<a ' . $blank . join(' ', $attr) . ' ' . $href . '>';
  171. }
  172.  
  173. function js_external_link_info_links($link) { // author link changer
  174.     $blogurl = get_bloginfo('home').'/';
  175.          if (get_option('redirect_file') != "") { $redirect_file = $blogurl . get_option('redirect_file'); }
  176.                                            else { $redirect_file = WP_PLUGIN_URL . '/' . WP_JS_BASEDIR . '/redirect.php'; }
  177.     $local = js_external_link_check_local($link, $blogurl);
  178.     if ($local === false) {
  179.         if ( ( (get_option('redirect_notblank') < 2)
  180.           && ( (get_option('redirect_notblank') < 1) || (!is_user_logged_in()) ) )
  181.           && ($href{6} != "#") ) {
  182.             $blank = 'target="_blank"';
  183.                  }
  184.     }
  185.     if ( ($local === false) && ($href{6} != "#")
  186.       && ( (get_option('redirect_questsonly') != 'true') || (!is_user_logged_in()) ) ) {
  187.         $link = preg_replace("#(.*href\s*=\s*)[\"\']*(.*)[\"\'] (.*)#i", "<a " . $blank . " href='" . $redirect_file . "?blog=" . WP_JS_BLOGNAME . "&url=$2' $3", $link);
  188.     } else {
  189.         $link = preg_replace("#(.*href\s*=\s*)[\"\']*(.*)[\"\'] (.*)#i", "<a " . $blank . " href=$2 $3", $link);
  190.          }
  191.     return $link;
  192. }
  193.  
  194. function js_external_link_info_shortcuts($links, $file) {
  195.     if ( $file == plugin_basename(__FILE__) )
  196.     {
  197.         $links[] = '<a href="options-general.php?page=js_external_link_info">' . __('Settings') . '</a>';
  198.                  $links[] = '<a href="http://www.joergschueler.de/dev/wp/external-link-info/donate/">' . __('Donate','js-external-link-info') . '</a>';
  199.     }
  200.     return $links;
  201. }
  202.  
  203. function js_external_link_info_init() {
  204.     load_plugin_textdomain('js-external-link-info', str_replace(ABSPATH, '', dirname(__FILE__)), dirname(plugin_basename(__FILE__)));
  205.     global $user_ID, $user_identity, $user_level;
  206. }
  207.  
  208. add_action('init', 'js_external_link_info_init');
  209. // the filters
  210. add_filter('the_content', 'js_external_link_info_content');
  211. add_filter('comment_text', 'js_external_link_info_comment');
  212. add_filter('get_comment_author_link', 'js_external_link_info_links');
  213.  
  214. add_filter('plugin_row_meta','js_external_link_info_shortcuts',10,2);
  215.  
  216. ?>
Advertisement
Add Comment
Please, Sign In to add comment