<commit_before><?php
/*
Plugin Name: willnorris.com
Description: Personal tweaks to WordPress I want to always be loaded on willnorris.com, regardless of what theme I use.
Author: <NAME>
Author URI: http://willnorris.com/
*/

require_once dirname( __FILE__ ) . '/willnorris/referral.php';

class WJN_Personal {

  public function __construct() {
    add_action('init', array( $this, 'init' ));
  }

  /**
   * Initialize the plugin, registering WordPess hooks.
   */
  public function init() {
    // shortcodes
    add_shortcode('my_age', array($this, 'my_age'));
    add_shortcode('safe_email', 'willnorris_safe_email');
    add_shortcode('no_amps', array($this, 'no_amps'));
    add_shortcode('recent_posts', array($this, 'recent_posts'));

    add_action('http_api_curl', array($this, 'http_api_curl'));
    add_action('wp', array($this, 'cleanup_plugins'));
    add_filter('intermediate_image_sizes_advanced', array($this, 'filter_image_sizes'));

    // ensure proper redirect status code is returned
    add_filter('wp_redirect_status', create_function('$s', 'status_header($s); return $s;'));

    // Hum Extensions
    add_filter('hum_redirect', array($this, 'hum_google_analytics'), 99, 2);
    add_filter('hum_legacy_redirect', array($this, '_add_google_analytics'), 99, 2);
    add_filter('hum_redirect_base_c', create_function('', 'return "http://code.willnorris.com/";'));
    add_filter('hum_redirect_base_w', create_function('', 'return "http://wiki.willnorris.com/";'));
    add_filter('amazon_affiliate_id', create_function('', 'return "willnorris-20";'));
    add_filter('hum_legacy_id', array($this, 'legacy_shortlinks'), 10, 2);

    add_filter('template_redirect', array($this, 'googleplus_shortlinks'), 0);
  }

  /**
   * Shortcode for displaying my age, in years.
   */
  function my_age() {
    $now = getdate();
    $age = $now['year'] - 1982;

    if ($now['mon'] < 7 && $now['mday'] < 30) {
      $age -= 1;
    }

    return $age;
  }

  /**
   * Handle 'safe_email' shortcode which converts email address into spambot-safe link.
   */
  function safe_email($atts, $content=null) {
    $attr = '';
    if ($atts) {
      foreach($atts as $k => $v) {
        if ($v) {
          $attr .= ' ' . $k . '="' . esc_attr($v) .'"';
        }
      }
    }
    return '<a' . $attr . ' href="mailto:' . antispambot($content) . '">' . antispambot($content) . '</a>';
  }

  function no_amps($atts, $content) {
    return preg_replace('/&#038;/', '&', $content);
  }

  function recent_posts($atts, $content) {
    $posts = '';
    $args = array('posts_per_page' => 20, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true);
    $count = 0;
    $r = new WP_Query();
    $r->query($args);
    while ($r->have_posts()) {
      $r->the_post();
      if (get_post_format() != '') continue;

      $posts .= sprintf('
        <li>
          <a href="%1$s">%2$s</a>
          <time datetime="%3$s">%4$s</time>
          </li>',
        get_permalink(), get_the_title(), get_the_date('c'), get_the_date());

      if (++$count >= 10) break;
    }
    return '<ul class="post-list">' . $posts . '</ul>';
  }

  /**
   * Use CURL_CA_BUNDLE environment variable to update libcurl's cacert bundle.
   */
  function http_api_curl($handle) {
    if ( getenv('CURL_CA_BUNDLE') ) {
      curl_setopt($handle, CURLOPT_CAINFO, getenv('CURL_CA_BUNDLE'));
    }
  }

  function cleanup_plugins() {
    // move SmartyPants filter after do_shortcodes
    foreach( array('category_description', 'list_cats', 'comment_author', 'comment_text',
                   'single_post_title', 'the_title', 'the_content', 'the_excerpt') as $filter ) {
        $priority = has_filter($filter, 'SmartyPants');
        if ( $priority !== false ) {
          remove_filter($filter, 'SmartyPants', $priority);
          add_filter($filter, 'SmartyPants', 12);
        }
    }
  }

  /**
   * Remove standard image sizes so that these sizes are not
   * created during the Media Upload process
   *
   * Tested with WP 3.2.1
   *
   * Hooked to intermediate_image_sizes_advanced filter
   * See wp_generate_attachment_metadata( $attachment_id, $file ) in wp-admin/includes/image.php
   *
   * @param $sizes, array of default and added image sizes
   * @return $sizes, modified array of image sizes
   * @author <NAME> http://www.studiograsshopper.ch
   */
  function filter_image_sizes( $sizes) {
    unset( $sizes['thumbnail']);
    unset( $sizes['medium']);
    unset( $sizes['large']);

    return $sizes;
  }

  function legacy_shortlinks($post_id, $path) {
    list($subtype, $id) = explode('/', $path, 2);
    if ( $subtype == 'p' ) {
      $post_id = $id;
    }
    return $post_id;
  }

  /**
   * Handle shortlinks to Google+ content.  URLs that begin with the path segment '+' or 'plus' and
   * which have not already been handled by WordPress are redirected to Google+.  If the remaning
   * path looks like a Google+ post ID, construct a permalink URL.  Otherwise, just append the
   * remaining path.
   */
  function googleplus_shortlinks() {
    if ( is_404() ) {
      global $wp;

      if ( strpos($wp->request, '/') !== false ) {
        list($type, $id) = @explode('/', $wp->request, 2);
      } else {
        $type = $wp->request;
        $id = null;
      }

      if ( $type == '+' || $type == 'plus' ) {
        $url = 'https://plus.google.com/' . GOOGLE_PLUS_ID;
        if ( $id ) {
          if ( strpos('/', $id) === false && preg_match('/[0-9A-Z]/', $id) ) {
            $url .= '/posts/' . $id;
          } else {
            $url .= '/' . $id;
          }
        }
        wp_redirect($url, 301);
        exit;
      }
    }
  }

  /**
   * Append Google Analytics tracking codes for shortlink redirects to local content.
   */
  function hum_google_analytics($url, $type) {
    $local_types = Hum::local_types();
    if ( $url && in_array($type, $local_types) && GOOGLE_ANALYTICS_ID ) {
      $url = $this->_add_google_analytics($url);
    }
    return $url;
  }

  function _add_google_analytics($url) {
    $ga_codes = array(
      'utm_source' => 'hum',
      'utm_medium' => 'shortlink',
      'utm_campaign' => 'willnorris'
    );
    return add_query_arg($ga_codes, $url);
  }
}

new WJN_Personal;
<commit_msg>update google analytics tracking<commit_after><?php
/*
Plugin Name: willnorris.com
Description: Personal tweaks to WordPress I want to always be loaded on willnorris.com, regardless of what theme I use.
Author: <NAME>
Author URI: http://willnorris.com/
*/

require_once dirname( __FILE__ ) . '/willnorris/referral.php';

class WJN_Personal {

  public function __construct() {
    add_action('init', array( $this, 'init' ));
  }

  /**
   * Initialize the plugin, registering WordPess hooks.
   */
  public function init() {
    // shortcodes
    add_shortcode('my_age', array($this, 'my_age'));
    add_shortcode('safe_email', 'willnorris_safe_email');
    add_shortcode('no_amps', array($this, 'no_amps'));
    add_shortcode('recent_posts', array($this, 'recent_posts'));

    add_action('http_api_curl', array($this, 'http_api_curl'));
    add_action('wp', array($this, 'cleanup_plugins'));
    add_filter('intermediate_image_sizes_advanced', array($this, 'filter_image_sizes'));

    // ensure proper redirect status code is returned
    add_filter('wp_redirect_status', create_function('$s', 'status_header($s); return $s;'));

    add_action('analytics_tracking_js', array($this, 'analytics_tracking_js'));

    // Hum Extensions
    add_filter('hum_redirect', array($this, 'hum_google_analytics'), 99, 3);
    add_filter('hum_legacy_redirect', array($this, '_add_google_analytics'), 99);
    add_filter('hum_redirect_base_c', create_function('', 'return "http://code.willnorris.com/";'));
    add_filter('hum_redirect_base_w', create_function('', 'return "http://wiki.willnorris.com/";'));
    add_filter('amazon_affiliate_id', create_function('', 'return "willnorris-20";'));
    add_filter('hum_legacy_id', array($this, 'legacy_shortlinks'), 10, 2);

    add_filter('template_redirect', array($this, 'googleplus_shortlinks'), 0);
  }

  /**
   * Shortcode for displaying my age, in years.
   */
  function my_age() {
    $now = getdate();
    $age = $now['year'] - 1982;

    if ($now['mon'] < 7 && $now['mday'] < 30) {
      $age -= 1;
    }

    return $age;
  }

  /**
   * Handle 'safe_email' shortcode which converts email address into spambot-safe link.
   */
  function safe_email($atts, $content=null) {
    $attr = '';
    if ($atts) {
      foreach($atts as $k => $v) {
        if ($v) {
          $attr .= ' ' . $k . '="' . esc_attr($v) .'"';
        }
      }
    }
    return '<a' . $attr . ' href="mailto:' . antispambot($content) . '">' . antispambot($content) . '</a>';
  }

  function no_amps($atts, $content) {
    return preg_replace('/&#038;/', '&', $content);
  }

  function recent_posts($atts, $content) {
    $posts = '';
    $args = array('posts_per_page' => 20, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true);
    $count = 0;
    $r = new WP_Query();
    $r->query($args);
    while ($r->have_posts()) {
      $r->the_post();
      if (get_post_format() != '') continue;

      $posts .= sprintf('
        <li>
          <a href="%1$s">%2$s</a>
          <time datetime="%3$s">%4$s</time>
          </li>',
        get_permalink(), get_the_title(), get_the_date('c'), get_the_date());

      if (++$count >= 10) break;
    }
    return '<ul class="post-list">' . $posts . '</ul>';
  }

  /**
   * Use CURL_CA_BUNDLE environment variable to update libcurl's cacert bundle.
   */
  function http_api_curl($handle) {
    if ( getenv('CURL_CA_BUNDLE') ) {
      curl_setopt($handle, CURLOPT_CAINFO, getenv('CURL_CA_BUNDLE'));
    }
  }

  function cleanup_plugins() {
    // move SmartyPants filter after do_shortcodes
    foreach( array('category_description', 'list_cats', 'comment_author', 'comment_text',
                   'single_post_title', 'the_title', 'the_content', 'the_excerpt') as $filter ) {
        $priority = has_filter($filter, 'SmartyPants');
        if ( $priority !== false ) {
          remove_filter($filter, 'SmartyPants', $priority);
          add_filter($filter, 'SmartyPants', 12);
        }
    }
  }

  /**
   * Remove standard image sizes so that these sizes are not
   * created during the Media Upload process
   *
   * Tested with WP 3.2.1
   *
   * Hooked to intermediate_image_sizes_advanced filter
   * See wp_generate_attachment_metadata( $attachment_id, $file ) in wp-admin/includes/image.php
   *
   * @param $sizes, array of default and added image sizes
   * @return $sizes, modified array of image sizes
   * @author <NAME> http://www.studiograsshopper.ch
   */
  function filter_image_sizes( $sizes) {
    unset( $sizes['thumbnail']);
    unset( $sizes['medium']);
    unset( $sizes['large']);

    return $sizes;
  }

  function legacy_shortlinks($post_id, $path) {
    list($subtype, $id) = explode('/', $path, 2);
    if ( $subtype == 'p' ) {
      $post_id = $id;
    }
    return $post_id;
  }

  /**
   * Handle shortlinks to Google+ content.  URLs that begin with the path segment '+' or 'plus' and
   * which have not already been handled by WordPress are redirected to Google+.  If the remaning
   * path looks like a Google+ post ID, construct a permalink URL.  Otherwise, just append the
   * remaining path.
   */
  function googleplus_shortlinks() {
    if ( is_404() ) {
      global $wp;

      if ( strpos($wp->request, '/') !== false ) {
        list($type, $id) = @explode('/', $wp->request, 2);
      } else {
        $type = $wp->request;
        $id = null;
      }

      if ( $type == '+' || $type == 'plus' ) {
        $url = 'https://plus.google.com/' . GOOGLE_PLUS_ID;
        if ( $id ) {
          if ( strpos('/', $id) === false && preg_match('/[0-9A-Z]/', $id) ) {
            $url .= '/posts/' . $id;
          } else {
            $url .= '/' . $id;
          }
        }
        wp_redirect($url, 301);
        exit;
      }
    }
  }

  function analytics_tracking_js() {
    echo "  _gaq.push(['_setAllowAnchor', true]);\n";
  }

  /**
   * Append Google Analytics tracking codes for shortlink redirects to local content.
   */
  function hum_google_analytics($url, $type, $id) {
    $local_types = Hum::local_types();
    if ( $url && in_array($type, $local_types) && GOOGLE_ANALYTICS_ID ) {
      $url = $this->_add_google_analytics($url, "$type/$id");
    }
    return $url;
  }

  function _add_google_analytics($url, $id=null) {
    if ( $id ) {
      $source = 'hum';
    } else {
      global $wp;
      $id = $wp->request;
      $source = 'hum_legacy';
    }

    $ga_codes = array(
      'utm_source' => $source,
      'utm_medium' => $id,
      'utm_campaign' => 'hum'
    );

    $query = build_query($ga_codes);
    return $url . '#' . $query;
  }
}

new WJN_Personal;

{'fraction_non_alphanumeric': 0.17102506401800263, 'fraction_numerical': 0.007216574842864903, 'mean_word_length': 2.9655384615384617, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 60, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1547070', 'n_tokens_mistral': 4535, 'n_tokens_neox': 4358, 'n_words': 974}