| 1 |
<?php |
| 2 |
|
| 3 |
namespace Pronamic\WordPress\PronamicClient; |
| 4 |
|
| 5 |
class YoastModule { |
| 6 |
/** |
| 7 |
* Instance of this class. |
| 8 |
* |
| 9 |
* @var YoastModule |
| 10 |
*/ |
| 11 |
protected static $instance = null; |
| 12 |
|
| 13 |
/** |
| 14 |
* Plugin |
| 15 |
* |
| 16 |
* @var Plugin |
| 17 |
*/ |
| 18 |
private $plugin; |
| 19 |
|
| 20 |
/** |
| 21 |
* Constructs and initialize Yoast module. |
| 22 |
* |
| 23 |
* @param Plugin $plugin |
| 24 |
*/ |
| 25 |
private function __construct( Plugin $plugin ) { |
| 26 |
$this->plugin = $plugin; |
| 27 |
|
| 28 |
\add_action( 'plugins_loaded', [ $this, 'plugins_loaded' ] ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Plugins loaded. |
| 33 |
* |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public function plugins_loaded() { |
| 37 |
if ( ! \defined( 'WPSEO_VERSION' ) ) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
if ( ! \defined( 'PRONAMIC_CLIENT_YOAST_URL' ) ) { |
| 42 |
\define( 'PRONAMIC_CLIENT_YOAST_URL', 'https://yoast.com' ); |
| 43 |
} |
| 44 |
|
| 45 |
\add_filter( 'http_request_args', [ $this, 'http_request_args' ], 1000, 2 ); |
| 46 |
|
| 47 |
if ( \is_admin() ) { |
| 48 |
\add_action( 'current_screen', [ $this, 'current_screen' ] ); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Current screen. |
| 54 |
* |
| 55 |
* @since 1.9.2 |
| 56 |
* @link https://developer.wordpress.org/reference/hooks/current_screen/ |
| 57 |
* @param \WP_Screen $screen Screen. |
| 58 |
* @return void |
| 59 |
*/ |
| 60 |
public function current_screen( $screen ) { |
| 61 |
if ( ! in_array( |
| 62 |
$screen->base, |
| 63 |
[ |
| 64 |
'post', |
| 65 |
'seo_page_wpseo_tools', |
| 66 |
'seo_page_wpseo_workouts', |
| 67 |
'toplevel_page_wpseo_dashboard', |
| 68 |
'wpseo_tools', |
| 69 |
], |
| 70 |
true |
| 71 |
) ) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
\add_action( 'admin_print_scripts', [ $this, 'admin_print_scripts' ] ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* HTTP request arguments. |
| 80 |
* |
| 81 |
* @param array $parsed_args Arguments. |
| 82 |
* @param string $url URL. |
| 83 |
* @return array |
| 84 |
*/ |
| 85 |
public function http_request_args( $parsed_args, $url ) { |
| 86 |
if ( ! \str_starts_with( $url, 'https://my.yoast.com/' ) ) { |
| 87 |
return $parsed_args; |
| 88 |
} |
| 89 |
|
| 90 |
if ( ! \array_key_exists( 'body', $parsed_args ) ) { |
| 91 |
return $parsed_args; |
| 92 |
} |
| 93 |
|
| 94 |
if ( ! \is_array( $parsed_args['body'] ) ) { |
| 95 |
return $parsed_args; |
| 96 |
} |
| 97 |
|
| 98 |
if ( ! \array_key_exists( 'url', $parsed_args['body'] ) ) { |
| 99 |
return $parsed_args; |
| 100 |
} |
| 101 |
|
| 102 |
$parsed_args['body']['url'] = PRONAMIC_CLIENT_YOAST_URL; |
| 103 |
|
| 104 |
return $parsed_args; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Admin print scripts. |
| 109 |
* |
| 110 |
* @since 1.9.1 |
| 111 |
* @link https://stackoverflow.com/questions/7775767/javascript-overriding-xmlhttprequest-open |
| 112 |
* @link https://developer.mozilla.org/en-US/docs/Web/API/URL |
| 113 |
* @link https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams |
| 114 |
* @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments |
| 115 |
*/ |
| 116 |
public function admin_print_scripts() { |
| 117 |
?> |
| 118 |
<script type="text/javascript"> |
| 119 |
( function( open ) { |
| 120 |
XMLHttpRequest.prototype.open = function( method, url ) { |
| 121 |
if ( url.startsWith( 'https://my.yoast.com/' ) ) { |
| 122 |
const url_object = new URL( url ); |
| 123 |
|
| 124 |
url_object.searchParams.set( 'site', <?php echo \wp_json_encode( PRONAMIC_CLIENT_YOAST_URL ); ?> ); |
| 125 |
|
| 126 |
arguments[1] = url_object.toString(); |
| 127 |
} |
| 128 |
|
| 129 |
return open.apply( this, arguments ); |
| 130 |
} |
| 131 |
} )( XMLHttpRequest.prototype.open ); |
| 132 |
</script> |
| 133 |
<?php |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Return an instance of this class. |
| 138 |
* |
| 139 |
* @return object A single instance of this class. |
| 140 |
*/ |
| 141 |
public static function get_instance( $plugin = false ) { |
| 142 |
// If the single instance hasn't been set, set it now. |
| 143 |
if ( null === self::$instance ) { |
| 144 |
self::$instance = new self( $plugin ); |
| 145 |
} |
| 146 |
|
| 147 |
return self::$instance; |
| 148 |
} |
| 149 |
} |
| 150 |
|