Plugin Directory

Changeset 1215633


Ignore:
Timestamp:
08/08/2015 10:34:56 AM (11 years ago)
Author:
akrabat
Message:

Rework to look for the shorter link after WordPress has done its processing. This
means that a shorterlink that represents a date won't affect an archive list.

Set to version 2.1.0

Location:
shorter-links/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • shorter-links/trunk/readme.txt

    r1215504 r1215633  
    55Requires at least: 3.0
    66Tested up to: 4.3
    7 Stable tag: 2.0.6
     7Stable tag: 2.1.0
    88License: New-BSD
    99License URI: http://akrabat.com/license/new-bsd
     
    6262
    6363
     64**2.1.0 - 8 August 2015**
     65Rework to look for the shorter link after WordPress has done its processing. This
     66means that a shorterlink that represents a date won't affect an archive list.
     67
    6468**2.0.6 - 8 August 2015**
    6569Ensure that the short_link is correct when using a post id.
  • shorter-links/trunk/shorter_links.php

    r1215555 r1215633  
    44Plugin URI: http://wordpress.org/extend/plugins/shorter-links/
    55Description: Overrides WordPress' shortlink functionality to allow custom shortcodes per post.
    6 Version: 2.0.6
     6Version: 2.1.0
    77Author: Rob Allen
    88Author URI: http://akrabat.com
     
    1414    const META_FIELD_NAME='Shorter link';
    1515
    16     public function request($query_vars)
     16    /**
     17     * Redirect to a shorter link if we can. This hook is called after WP has done
     18     * its processing, so we can check if we have a 404 to determine if we need to
     19     * look for a shorter link.
     20     *
     21     * @param  WP $wp
     22     * @return null
     23     */
     24    public function redirectToShorterLink(WP $wp)
    1725    {
    18         // check if pagename or category_name matches a short link
    19         $shortLink = '';
    20         if (array_key_exists('pagename', $query_vars)) {
    21             $shortLink = $query_vars['pagename'];
    22         }
    23         if (array_key_exists('category_name', $query_vars)) {
    24             $shortLink = $query_vars['category_name'];
    25         }
    26         if (array_key_exists('year', $query_vars)) {
    27             $shortLink = $query_vars['year'];
    28         }
    29         if(empty($shortLink) && isset($_SERVER['REQUEST_URI'])) {
    30             $shortLink = trim($_SERVER['REQUEST_URI'], '/');
    31         }
    32         if (empty($shortLink)) {
    33             return $query_vars;
    34         }
     26        if (is_404()) {
     27            // WordPress didn't find anything to display, so look for a shortlink
     28            $shortLink = $wp->request;
     29            // $shortLink = preg_replace('#/.*#i', "", $WP->request); // only need first segment
    3530
    36         // try for permalink first
    37         if (is_numeric($shortLink)) {
    38             $post = get_post( $shortLink);
    39             if ($post->post_status == 'publish') {
    40                 $link = get_permalink($shortLink);
    41                 if ($link) {
    42                     wp_redirect($link);
     31            // if it's a number, then test for a post id first
     32            if (is_numeric($shortLink)) {
     33                $post = get_post( $shortLink);
     34                if ($post->post_status == 'publish') {
     35                    $link = get_permalink($shortLink);
     36                    if ($link) {
     37                        wp_redirect($link);
     38                        exit;
     39                    }
     40                }
     41            }
     42
     43            // Look up the post or page with this shorter link
     44            $query = array('meta_key' => self::META_FIELD_NAME, 'meta_value' => $shortLink);
     45            $posts = get_posts($query);
     46            if (count($posts) > 0) {
     47                wp_redirect(get_permalink($posts[0]), 301);
     48                exit;
     49            } else {
     50                $pages = get_pages($query);
     51                if (count($pages) > 0) {
     52                    wp_redirect(get_permalink($pages[0]), 301);
    4353                    exit;
    4454                }
    4555            }
    4656        }
    47 
    48         // Look up the post or page with this shorter link
    49         $query = array('meta_key' => self::META_FIELD_NAME, 'meta_value' => $shortLink);
    50         $posts = get_posts($query);
    51         if (count($posts) > 0) {
    52             wp_redirect(get_permalink($posts[0]), 301);
    53             exit;
    54         } else {
    55             $pages = get_pages($query);
    56             if (count($pages) > 0) {
    57                 wp_redirect(get_permalink($pages[0]), 301);
    58                 exit;
    59             }
    60         }
    61 
    62         return $query_vars;
    6357    }
    6458
     
    128122
    129123$akrabatShorterLinks = new AkrabatShorterLinks();
    130 add_filter('request', array(&$akrabatShorterLinks, 'request'));
    131 add_filter('pre_get_shortlink',  array(&$akrabatShorterLinks, 'pre_get_shortlink'), 10, 4);
    132 add_action('save_post', array(&$akrabatShorterLinks, 'save_post'), 10, 2);
    133 add_action('admin_menu', array(&$akrabatShorterLinks, 'admin_menu'));
    134 add_action('admin_init', array(&$akrabatShorterLinks, 'admin_init'));
     124add_filter('pre_get_shortlink',  array($akrabatShorterLinks, 'pre_get_shortlink'), 10, 4);
     125add_action('wp', array($akrabatShorterLinks, 'redirectToShorterLink'));
     126add_action('save_post', array($akrabatShorterLinks, 'save_post'), 10, 2);
     127add_action('admin_menu', array($akrabatShorterLinks, 'admin_menu'));
     128add_action('admin_init', array($akrabatShorterLinks, 'admin_init'));
    135129
    136130// vim: set filetype=php expandtab tabstop=4 shiftwidth=4 autoindent smartindent:
Note: See TracChangeset for help on using the changeset viewer.