| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Third_Party; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Conditionals\Third_Party\WPML_Conditional; |
| 6 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 7 |
|
| 8 |
/** |
| 9 |
* WPML integration. |
| 10 |
* |
| 11 |
* @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded -- Known false positive with acronyms. Fix expected in YoastCS 3.x. |
| 12 |
*/ |
| 13 |
class WPML implements Integration_Interface { |
| 14 |
|
| 15 |
/** |
| 16 |
* Initializes the integration. |
| 17 |
* |
| 18 |
* This is the place to register hooks and filters. |
| 19 |
* |
| 20 |
* @return void |
| 21 |
*/ |
| 22 |
public function register_hooks() { |
| 23 |
\add_action( 'wpseo_home_url', [ $this, 'filter_home_url_before' ] ); |
| 24 |
\add_filter( 'home_url', [ $this, 'filter_home_url_after' ], 100 ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Returns the conditionals based in which this loadable should be active. |
| 29 |
* |
| 30 |
* @return array |
| 31 |
*/ |
| 32 |
public static function get_conditionals() { |
| 33 |
return [ WPML_Conditional::class ]; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Adds a filter to WPML's wpml_get_home_url filter to ensure we get the unmanipulated home URL. |
| 38 |
* |
| 39 |
* @return void |
| 40 |
*/ |
| 41 |
public function filter_home_url_before() { |
| 42 |
\add_filter( 'wpml_get_home_url', [ $this, 'wpml_get_home_url' ], 10, 2 ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Removes the wpml_get_home_url filter to return the WPML, language-enriched home URL. |
| 47 |
* |
| 48 |
* @param string $home_url The filtered home URL. |
| 49 |
* |
| 50 |
* @return string The unfiltered home URL. |
| 51 |
*/ |
| 52 |
public function filter_home_url_after( $home_url ) { |
| 53 |
\remove_filter( 'wpml_get_home_url', [ $this, 'wpml_get_home_url' ], 10 ); |
| 54 |
|
| 55 |
return $home_url; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Returns the original URL instead of the language-enriched URL. |
| 60 |
* This method gets automatically triggered by the wpml_get_home_url filter. |
| 61 |
* |
| 62 |
* @param string $home_url The url altered by WPML. Unused. |
| 63 |
* @param string $url The url that isn't altered by WPML. |
| 64 |
* |
| 65 |
* @return string The original url. |
| 66 |
*/ |
| 67 |
public function wpml_get_home_url( $home_url, $url ) { |
| 68 |
return $url; |
| 69 |
} |
| 70 |
} |
| 71 |
|