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.2 |
||
| 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 | 1 | public function __construct() { |
|
| 31 | // modify domain where necessary |
||
| 32 | 1 | add_filter( 'option_blogname', array( $this, 'setup_domain' ), 1 ); |
|
| 33 | 1 | add_filter( 'option_siteurl', array( $this, 'setup_domain' ), 1 ); |
|
| 34 | 1 | add_filter( 'option_home', array( $this, 'setup_domain' ), 1 ); |
|
| 35 | 1 | add_filter( 'stylesheet_uri', array( $this, 'setup_domain' ), 1 ); |
|
| 36 | 1 | add_filter( 'stylesheet_directory_uri', array( $this, 'setup_domain' ), 1 ); |
|
| 37 | 1 | add_filter( 'template_directory_uri', array( $this, 'setup_domain' ), 1 ); |
|
| 38 | 1 | add_filter( 'plugins_url', array( $this, 'setup_domain' ), 1 ); |
|
| 39 | |||
| 40 | // replace various occurences |
||
| 41 | 1 | add_filter( 'the_content', array( $this, 'setup_content' ) ); // content |
|
| 42 | 1 | add_filter( 'widget_text', array( $this, 'setup_content' ) ); // widget text |
|
| 43 | 1 | add_filter( 'upload_dir', array( $this, 'setup_upload_dir' ) ); // wp_upload_dir(); |
|
| 44 | |||
| 45 | // allow developers to support multiple domains in fields that contain only a site URL |
||
| 46 | 1 | add_filter( 'wp_hydra_domain', array( $this, 'setup_domain' ) ); |
|
| 47 | |||
| 48 | // allow developers to support URLs with multiple domains in their content |
||
| 49 | 1 | add_filter( 'wp_hydra_content', array( $this, 'setup_content' ) ); |
|
| 50 | 1 | } |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Replaces original domain with current domain in simple fields. |
||
| 54 | * |
||
| 55 | * @access public |
||
| 56 | * |
||
| 57 | * @param string $url The current URL. |
||
| 58 | * @return string $url The URL with the (maybe) replaced domain. |
||
| 59 | */ |
||
| 60 | public function setup_domain( $url ) { |
||
| 61 | // parse current URL |
||
| 62 | $original_domain_parts = parse_url( $url ); |
||
| 63 | |||
| 64 | // if unable to retrieve the host, skip |
||
| 65 | if ( empty( $original_domain_parts['host'] ) ) { |
||
| 66 | return $url; |
||
| 67 | } |
||
| 68 | |||
| 69 | // get original and current domain |
||
| 70 | $original_domain = $original_domain_parts['host']; |
||
| 71 | $current_domain = $_SERVER['HTTP_HOST']; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 72 | |||
| 73 | // if original and current domain match, skip |
||
| 74 | if ( $original_domain == $current_domain ) { |
||
| 75 | return $url; |
||
| 76 | } |
||
| 77 | |||
| 78 | // prepare original domain and current domain with the current protocol |
||
| 79 | $protocols = array( 'http://', 'https://' ); |
||
| 80 | $current_protocol = ( is_ssl() ? 'https' : 'http' ) . '://'; |
||
| 81 | foreach ( $protocols as $protocol ) { |
||
| 82 | $original_base = $protocol . $original_domain; |
||
| 83 | $new_base = $current_protocol . $current_domain; |
||
| 84 | |||
| 85 | // replace original domain with current domain |
||
| 86 | $url = str_replace( $original_base, $new_base, $url ); |
||
| 87 | } |
||
| 88 | |||
| 89 | return $url; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Replaces original domain with current domain in content. |
||
| 94 | * |
||
| 95 | * @access public |
||
| 96 | * |
||
| 97 | * @param string $content The current content with the original domain. |
||
| 98 | * @return string $content The content with the new domain. |
||
| 99 | */ |
||
| 100 | public function setup_content( $content ) { |
||
| 101 | // get original home URL |
||
| 102 | remove_filter( 'option_home', array( $this, 'setup_domain' ), 1 ); |
||
| 103 | $original_home = home_url( '/' ); |
||
| 104 | add_filter( 'option_home', array( $this, 'setup_domain' ), 1 ); |
||
| 105 | |||
| 106 | // get current home URL |
||
| 107 | $current_home = home_url( '/' ); |
||
| 108 | |||
| 109 | // replace occurences of original URL with current home URL |
||
| 110 | $content = str_replace( $original_home, $current_home, $content ); |
||
| 111 | |||
| 112 | return $content; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Replaces original domain with current domain in wp_upload_dir(). |
||
| 117 | * |
||
| 118 | * @access public |
||
| 119 | * |
||
| 120 | * @param array $upload_dir The current upload dir settings with the original domain. |
||
| 121 | * @return array $upload_dir The upload dir settings with the new domain. |
||
| 122 | */ |
||
| 123 | public function setup_upload_dir( $upload_dir ) { |
||
| 124 | // keys of array element that we'll be updating |
||
| 125 | $keys_to_update = array( |
||
| 126 | 'url', |
||
| 127 | 'baseurl', |
||
| 128 | ); |
||
| 129 | |||
| 130 | // fix all targeted array elements |
||
| 131 | foreach ( $keys_to_update as $key ) { |
||
| 132 | $upload_dir[ $key ] = apply_filters( 'wp_hydra_domain', $upload_dir[ $key ] ); |
||
| 133 | } |
||
| 134 | |||
| 135 | return $upload_dir; |
||
| 136 | } |
||
| 137 | |||
| 138 | } |
||
| 139 | |||
| 140 | // initialize WP Hydra - Polycephaly FTW! |
||
| 141 | global $wp_hydra; |
||
| 142 | $wp_hydra = new WP_Hydra(); |