Plugin Directory

Changeset 457443


Ignore:
Timestamp:
10/30/2011 08:43:59 PM (14 years ago)
Author:
MarcusPope
Message:

Updated plugin to be backwards compatible with php<5.3. modified links to support and donations

Location:
root-relative-urls/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • root-relative-urls/trunk/readme.txt

    r456526 r457443  
    11=== Root Relative URLs ===
    22Contributors: marcuspope
    3 Donate link: http://www.springbox.com/contact/
     3Donate link: http://www.marcuspope.com/
    44Tags: url, links, admin, multisite, content, permalink
    55Requires at least: 3.2.1
     
    9191== Changelog ==
    9292
     93= 1.1 =
     94* Converted plugin to be < php5.3 compatible by removing usage of function references
     95
    9396= 1.0 =
    9497* Initial creation of plugin
  • root-relative-urls/trunk/sb_root_relative_urls.php

    r456526 r457443  
    33/*
    44Plugin Name: Root Relative URLs
    5 Plugin URI: http://www.springbox.com/wordpress/
     5Plugin URI: http://www.marcuspope.com/wordpress/
    66Description: A Wordpress Plugin that converts all URL formats to root relative URL's to enable seamless transitioning
    77between staging and production host environments without requiring hackish content find-replace or host/NAT
    88spoofing strategies.
    9 Author: Marcus E. Pope, Springbox.com, marcuspope
     9Author: Marcus E. Pope, marcuspope
    1010Author URI: http://www.marcuspope.com
    1111Version: 1.0
    1212
    13 Copyright 2011 Marcus E. Pope (email : marcus.pope@springbox.com)
     13Copyright 2011 Marcus E. Pope (email : me@marcuspope.com)
    1414
    1515This program is free software; you can redistribute it and/or modify
     
    2929
    3030//Ideally this function runs before any other plugin
    31 add_action('plugins_loaded', array('SB_WP_Root_Relative_URLS', 'init'), 1);
     31add_action('plugins_loaded', array('MP_WP_Root_Relative_URLS', 'init'), 1);
    3232
    33 class SB_WP_Root_Relative_URLS {
     33class MP_WP_Root_Relative_URLS {
    3434    //Wordpress Root Relative URL Hack - this allows for accessing a Wordpress site from multiple domain names and
    3535    //munges absolute urls provided to the wysiwyg editor to be root relative instead of absolute
    3636    //generally displays urls throughout the admin area as root relative instead of absolute
    37 
    3837    static function add_actions($tag_arry, $func, $p = 10, $a = 1) {
    3938        //allows for multiple tags to bind to the same funciton call
     
    5756    }
    5857
     58    static function dynamic_absolute_url($url) {
     59        //These URL's cannot be reformmated into root-relative urls for various reasons.  Either they are indescriminantly
     60        //used in export functions or RSS feeds.  Or because they are literally checked against the domain of URLs stored
     61        //in the database - a needless process for any website.
     62        $url = parse_url($url);
     63        $relative = ltrim(@$url['path'], '/') . (isset($url['query']) ? "?" . $url['query'] : '');
     64        return MP_WP_Root_Relative_URLS::scheme( 'http://' . $_SERVER['HTTP_HOST'] . (!empty($relative) ? '/' . $relative: '') );
     65    }
     66
     67    static function dynamic_rss_absolute_url($info, $type = 'url') {
     68        //Generates dynamic absolute URI's for RSS Feeds
     69        //get_bloginfo_rss returns multiple types of info, the_permalink_rss only returns a url.
     70        //so when type is not passed in, consider it a url, otherwise only parse when type == url
     71        if ($type == 'url') {
     72            return MP_WP_Root_Relative_URLS::dynamic_absolute_url($info);
     73        }
     74        return $info;
     75    }
     76
     77    static function proper_root_relative_url($url) {
     78        //This method is used for urls that can be acceptably reformatted into root-relative urls without causing issues
     79        //related to other deficiencies in the wp core.
     80        $url = parse_url($url);
     81        return '/' . ltrim(@$url['path'], '/') . (isset($url['query']) ? "?" . $url['query'] : '');
     82    }
     83
     84    static function proper_multisite_path_comparison($redirect_bool) {
     85        //Prevents infinite loop caused by the path matching but not the domain when making network admin requests
     86        global $current_blog, $current_site;
     87
     88        //don't worry about domain name mismatch as long as the paths are correct
     89        if ($redirect_bool &&
     90            $current_blog->path == $current_site->path) {
     91            $redirect_bool = false;
     92        }
     93
     94        return $redirect_bool;
     95    }
     96
     97    static function root_relative_image_urls($html, $id, $caption, $title, $align, $url, $size, $alt) {
     98        //Same as media_send_to_editor but images are handled separately
     99        $p = parse_url($url);
     100        $root = $p['scheme'] . "://" . $p['host'];
     101        $html = str_ireplace($root, '', $html);
     102        return $html;
     103    }
     104
     105    static function root_relative_media_urls($html, $id, $att) {
     106        //Filter out host from embed urls
     107        $p = parse_url($att['url']);
     108        $root = $p['scheme'] . "://" . $p['host'];
     109        $html = str_ireplace($root, '', $html);
     110        return $html;
     111    }
     112
    59113    static function init() {
    60114        //Setup all hooks / filters for either dynamically replacing the host part of a URL with the current host
    61115        //or for stripping the scheme + host + port altogether
    62         SB_WP_Root_Relative_URLS::add_actions(array('option_siteurl', 'blog_option_siteurl', 'option_home', 'admin_url', 'home_url', 'includes_url', 'site_url', 'site_option_siteurl', 'network_home_url', 'network_site_url'), function($url) {
    63             $url = parse_url($url);
    64             $remainder = ltrim(@$url['path'], '/') . (isset($url['query']) ? "?" . $url['query'] : '');
    65             return SB_WP_Root_Relative_URLS::scheme( 'http://' . $_SERVER['HTTP_HOST'] . (!empty($remainder) ? '/' . $remainder : '') );
    66         });
     116        MP_WP_Root_Relative_URLS::add_actions(
     117            array(
     118                'option_siteurl',
     119                'blog_option_siteurl',
     120                'option_home',
     121                'admin_url',
     122                'home_url',
     123                'includes_url',
     124                'site_url',
     125                'site_option_siteurl',
     126                'network_home_url',
     127                'network_site_url'
     128            ),
     129            array(
     130                'MP_WP_Root_Relative_URLS',
     131                'dynamic_absolute_url'
     132            ),
     133            1
     134        );
    67135
    68         SB_WP_Root_Relative_URLS::add_actions(array('post_link', 'page_link', 'attachment_link', 'post_type_link', 'wp_get_attachment_url'), function($url) {
    69             $url = parse_url($url);
    70             return '/' . ltrim(@$url['path'], '/') . (isset($url['query']) ? "?" . $url['query'] : '');
    71         });
     136        MP_WP_Root_Relative_URLS::add_actions(
     137            array(
     138                'post_link',
     139                'page_link',
     140                'attachment_link',
     141                'post_type_link',
     142                'wp_get_attachment_url'
     143            ),
     144            array(
     145                'MP_WP_Root_Relative_URLS',
     146                'proper_root_relative_url'
     147            ),
     148            1
     149        );
    72150
    73         SB_WP_Root_Relative_URLS::add_actions(array('get_bloginfo_rss', 'the_permalink_rss'), function($info, $type = 'url') {
    74             //Generate Absolute URI's for RSS Feeds
    75             //get_bloginfo_rss returns multiple types of info, the_permalink_rss only returns a url.
    76             //so when type is not passed in, consider it a url, otherwise only parse when type == url
    77             if ($type == 'url') {
    78                 $url = parse_url($info);
    79                 return SB_WP_Root_Relative_URLS::scheme( 'http://' . $_SERVER['HTTP_HOST'] . '/' .  ltrim(@$url['path'], '/') . (isset($url['query']) ? "?" . $url['query'] : '') );
    80             }
    81             return $info;
    82         }, 10, 2);
     151        MP_WP_Root_Relative_URLS::add_actions(
     152            array(
     153                'get_bloginfo_rss',
     154                'the_permalink_rss'
     155            ),
     156            array(
     157                'MP_WP_Root_Relative_URLS',
     158                'dynamic_rss_absolute_url'
     159            ),
     160            1, //high priority
     161            2  //supply second parameter for type checking
     162        );
    83163
    84164        //HACK: This plugin actually won't work for MU Sites until either of the following conditions are true:
     
    86166            //2. You deal with the consequences of patching a core file yourself using the above patch reference
    87167        //Regardless of the above, this plugin only supports path-based MU installations - at this point domain-based MU installations are not supported
    88         add_filter('redirect_network_admin_request', function($redirect_bool) {
    89             //Prevents infinite loop caused by the path matching but not the domain when making network admin requests
    90             global $current_blog, $current_site;
     168        add_filter(
     169            'redirect_network_admin_request',
     170            array(
     171                'MP_WP_Root_Relative_URLS',
     172                'proper_multisite_path_comparison'
     173            )
     174        );
    91175
    92             //don't worry about domain name mismatch as long as the paths are correct
    93             if ($redirect_bool &&
    94                 $current_blog->path == $current_site->path) {
    95                 $redirect_bool = false;
    96             }
     176        add_filter(
     177            'image_send_to_editor',
     178            array(
     179                'MP_WP_Root_Relative_URLS',
     180                'root_relative_image_urls'
     181            ),
     182            1, //high priority
     183            8  //eight params? wow
     184        );
    97185
    98             return $redirect_bool;
    99         });
    100 
    101         add_filter('image_send_to_editor', function($html, $id, $caption, $title, $align, $url, $size, $alt) {
    102             //Same as media_send_to_editor but images are handled separately
    103             $p = parse_url($url);
    104             $root = $p['scheme'] . "://" . $p['host'];
    105             $html = str_ireplace($root, '', $html);
    106             return $html;
    107         }, 10, 8);
    108 
    109         add_filter('media_send_to_editor', function($html, $id, $att) {
    110             //Filter out host from embed urls
    111             $p = parse_url($att['url']);
    112             $root = $p['scheme'] . "://" . $p['host'];
    113             $html = str_ireplace($root, '', $html);
    114             return $html;
    115         }, 10, 3);
     186        add_filter(
     187            'media_send_to_editor',
     188            array(
     189                'MP_WP_Root_Relative_URLS',
     190                'root_relative_media_urls'
     191            ),
     192            1,
     193            3
     194        );
    116195    }
    117196}
Note: See TracChangeset for help on using the changeset viewer.