tyxla /
WP-Hydra
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Plugin Name: WP Hydra |
||
| 4 | * Plugin URI: https://wordpress.org/plugins/wp-hydra/ |
||
| 5 | * Description: Allows one WordPress installation to be resolved and browsed at multiple domains. |
||
| 6 | * Version: 1.0.4 |
||
| 7 | * Author: tyxla |
||
| 8 | * Author URI: http://marinatanasov.com/ |
||
| 9 | * Plugin URI: https://github.com/tyxla/WP-Hydra |
||
| 10 | * License: GPL2 |
||
| 11 | * Requires at least: 3.0.1 |
||
| 12 | * Tested up to: 4.4 |
||
| 13 | */ |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Main WP_Hydra class. |
||
| 17 | * |
||
| 18 | * Replaces the original domain with the current domain, where necessary. |
||
| 19 | * All of the magic is hooked upon WP_Hydra object initialization. |
||
| 20 | */ |
||
| 21 | class WP_Hydra { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Constructor. |
||
| 25 | * |
||
| 26 | * Hooks all of the domain replacement functionality. |
||
| 27 | * |
||
| 28 | * @access public |
||
| 29 | */ |
||
| 30 | public function __construct() { |
||
| 31 | // modify domain where necessary |
||
| 32 | add_filter( 'option_blogname', array( $this, 'setup_domain' ), 1 ); |
||
| 33 | add_filter( 'option_siteurl', array( $this, 'setup_domain' ), 1 ); |
||
| 34 | add_filter( 'option_home', array( $this, 'setup_domain' ), 1 ); |
||
| 35 | add_filter( 'stylesheet_uri', array( $this, 'setup_domain' ), 1 ); |
||
| 36 | add_filter( 'stylesheet_directory_uri', array( $this, 'setup_domain' ), 1 ); |
||
| 37 | add_filter( 'template_directory_uri', array( $this, 'setup_domain' ), 1 ); |
||
| 38 | add_filter( 'plugins_url', array( $this, 'setup_domain' ), 1 ); |
||
| 39 | |||
| 40 | // replace occurences in content |
||
| 41 | add_filter( 'the_content', array( $this, 'setup_content' ) ); |
||
| 42 | |||
| 43 | // replace occurences in widget text |
||
| 44 | add_filter( 'widget_text', array( $this, 'setup_content' ) ); |
||
| 45 | |||
| 46 | // replace occurences in wp_upload_dir(); |
||
| 47 | add_filter( 'upload_dir', array( $this, 'setup_upload_dir' ) ); |
||
| 48 | |||
| 49 | // allow developers to support multiple domains in fields that contain only a site URL |
||
| 50 | add_filter( 'wp_hydra_domain', array( $this, 'setup_domain' ) ); |
||
| 51 | |||
| 52 | // allow developers to support URLs with multiple domains in their content |
||
| 53 | add_filter( 'wp_hydra_content', array( $this, 'setup_content' ) ); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Replaces original domain with current domain in simple fields. |
||
| 58 | * |
||
| 59 | * @access public |
||
| 60 | * |
||
| 61 | * @param string $url The current URL. |
||
| 62 | * @return string $url The URL with the (maybe) replaced domain. |
||
| 63 | */ |
||
| 64 | public function setup_domain( $url ) { |
||
| 65 | // parse current URL |
||
| 66 | $original_domain_parts = parse_url( $url ); |
||
| 67 | |||
| 68 | // if unable to retrieve the host, skip |
||
| 69 | if ( empty( $original_domain_parts['host'] ) ) { |
||
| 70 | return $url; |
||
| 71 | } |
||
| 72 | |||
| 73 | // get original and current domain |
||
| 74 | $original_domain = $original_domain_parts['host']; |
||
| 75 | $current_domain = $_SERVER['HTTP_HOST']; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 76 | |||
| 77 | // if original and current domain match, skip |
||
| 78 | if ( $original_domain == $current_domain ) { |
||
| 79 | return $url; |
||
| 80 | } |
||
| 81 | |||
| 82 | // prepare original domain and current domain with the current protocol |
||
| 83 | $protocols = array( 'http://', 'https://' ); |
||
| 84 | $current_protocol = ( is_ssl() ? 'https' : 'http' ) . '://'; |
||
| 85 | foreach ( $protocols as $protocol ) { |
||
| 86 | $original_base = $protocol . $original_domain; |
||
| 87 | $new_base = $current_protocol . $current_domain; |
||
| 88 | |||
| 89 | // replace original domain with current domain |
||
| 90 | $url = str_replace( $original_base, $new_base, $url ); |
||
| 91 | } |
||
| 92 | |||
| 93 | return $url; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Replaces original domain with current domain in content. |
||
| 98 | * |
||
| 99 | * @access public |
||
| 100 | * |
||
| 101 | * @param string $content The current content with the original domain. |
||
| 102 | * @return string $content The content with the new domain. |
||
| 103 | */ |
||
| 104 | public function setup_content( $content ) { |
||
| 105 | // get original home URL |
||
| 106 | remove_filter( 'option_home', array( $this, 'setup_domain' ), 1 ); |
||
| 107 | $original_home = home_url( '/' ); |
||
| 108 | add_filter( 'option_home', array( $this, 'setup_domain' ), 1 ); |
||
| 109 | |||
| 110 | // get current home URL |
||
| 111 | $current_home = home_url( '/' ); |
||
| 112 | |||
| 113 | // replace occurences of original URL with current home URL |
||
| 114 | $content = str_replace( $original_home, $current_home, $content ); |
||
| 115 | |||
| 116 | return $content; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Replaces original domain with current domain in wp_upload_dir(). |
||
| 121 | * |
||
| 122 | * @access public |
||
| 123 | * |
||
| 124 | * @param array $upload_dir The current upload dir settings with the original domain. |
||
| 125 | * @return array $upload_dir The upload dir settings with the new domain. |
||
| 126 | */ |
||
| 127 | public function setup_upload_dir( $upload_dir ) { |
||
| 128 | // keys of array element that we'll be updating |
||
| 129 | $keys_to_update = array( |
||
| 130 | 'url', |
||
| 131 | 'baseurl', |
||
| 132 | ); |
||
| 133 | |||
| 134 | // fix all targeted array elements |
||
| 135 | foreach ( $keys_to_update as $key ) { |
||
| 136 | $upload_dir[ $key ] = apply_filters( 'wp_hydra_domain', $upload_dir[ $key ] ); |
||
| 137 | } |
||
| 138 | |||
| 139 | return $upload_dir; |
||
| 140 | } |
||
| 141 | |||
| 142 | } |
||
| 143 | |||
| 144 | // initialize WP Hydra - Polycephaly FTW! |
||
| 145 | global $wp_hydra; |
||
| 146 | $wp_hydra = new WP_Hydra(); |