Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/seo-by-rank-math/includes/rest/class-headless.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
/**
3
+
* Add support for headless WP.
4
+
*
5
+
* @since 1.0.15
6
+
* @package RankMath
7
+
* @subpackage RankMath\Rest
8
+
* @author Rank Math <support@rankmath.com>
9
+
*/
10
+
11
+
namespace RankMath\Rest;
12
+
13
+
use WP_Error;
14
+
use WP_REST_Server;
15
+
use WP_REST_Request;
16
+
use WP_REST_Response;
17
+
use WP_REST_Controller;
18
+
use RankMath\Helpers\Url;
19
+
use RankMath\Helper;
20
+
use RankMath\Frontend\Frontend;
21
+
22
+
defined( 'ABSPATH' ) || exit;
23
+
24
+
/**
25
+
* Front class.
26
+
*/
27
+
class Headless extends WP_REST_Controller {
28
+
29
+
/**
30
+
* Whether the request is for the homepage.
31
+
*
32
+
* @var boolean
33
+
*/
34
+
public $is_home = false;
35
+
36
+
/**
37
+
* Constructor.
38
+
*/
39
+
public function __construct() {
40
+
$this->namespace = \RankMath\Rest\Rest_Helper::BASE;
41
+
}
42
+
43
+
/**
44
+
* Registers the routes for the objects of the controller.
45
+
*/
46
+
public function register_routes() {
47
+
if ( ! Helper::get_settings( 'general.headless_support' ) ) {
48
+
return;
49
+
}
50
+
51
+
register_rest_route(
52
+
$this->namespace,
53
+
'/getHead',
54
+
[
55
+
'methods' => WP_REST_Server::READABLE,
56
+
'callback' => [ $this, 'get_head' ],
57
+
'permission_callback' => '__return_true',
58
+
'args' => [
59
+
'url' => [
60
+
'type' => 'string',
61
+
'required' => true,
62
+
'description' => esc_html__( 'URL to get HTML tags for.', 'rank-math' ),
63
+
'validate_callback' => [ $this, 'is_valid_url' ],
64
+
],
65
+
],
66
+
]
67
+
);
68
+
}
69
+
70
+
/**
71
+
* Get all tags that go in the <head>. Useful for headless WP installations.
72
+
*
73
+
* @param WP_REST_Request $request Request object, should include the "url" parameter.
74
+
*
75
+
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
76
+
*/
77
+
public function get_head( WP_REST_Request $request ) {
78
+
$resp = new WP_REST_Response();
79
+
$url = $request->get_param( 'url' );
80
+
81
+
$html = $this->get_html_head( $url );
82
+
83
+
$resp->set_status( 200 );
84
+
$resp->set_data(
85
+
[
86
+
'success' => true,
87
+
'head' => $html,
88
+
89
+
]
90
+
);
91
+
return $resp;
92
+
}
93
+
94
+
/**
95
+
* Return Rank Math head HTML output for the given URL.
96
+
*
97
+
* @param string $url Request URL.
98
+
*
99
+
* @return string
100
+
*/
101
+
private function get_html_head( $url ) {
102
+
$this->setup_post_head( $url );
103
+
104
+
ob_start();
105
+
do_action( 'wp' );
106
+
do_action( 'rank_math/head' );
107
+
return ob_get_clean();
108
+
}
109
+
110
+
/**
111
+
* Prepare head output for a URL.
112
+
*
113
+
* @param string $url Request URL.
114
+
*
115
+
* @return void
116
+
*/
117
+
private function setup_post_head( $url ) {
118
+
// Setup WordPress.
119
+
$_SERVER['REQUEST_URI'] = $this->generate_request_uri( $url );
120
+
remove_all_actions( 'wp' );
121
+
remove_all_actions( 'parse_request' );
122
+
wp();
123
+
124
+
if ( $this->is_home ) {
125
+
$GLOBALS['wp_query']->is_home = true;
126
+
}
127
+
128
+
remove_filter( 'option_rewrite_rules', [ $this, 'fix_query_notice' ] );
129
+
header( 'Content-Type: application/json; charset=UTF-8' );
130
+
131
+
// Setup Rank Math.
132
+
rank_math()->variables->setup();
133
+
rank_math()->manager->load_modules();
134
+
new Frontend();
135
+
}
136
+
137
+
/**
138
+
* Generate $_SERVER['REQUEST_URI'] value based on input URL.
139
+
*
140
+
* @param string $url Input URL.
141
+
* @return string
142
+
*/
143
+
public function generate_request_uri( $url ) {
144
+
$quoted = preg_quote( rtrim( home_url(), '/' ), '/' );
145
+
$request_uri = preg_replace( sprintf( '/^%s/i', $quoted ), '', rtrim( $url, '/' ) );
146
+
if ( empty( $request_uri ) ) {
147
+
$request_uri = '/';
148
+
$this->is_home = true;
149
+
$front_page_id = get_option( 'page_on_front' );
150
+
if ( 'page' === get_option( 'show_on_front' ) && $front_page_id ) {
151
+
$this->is_home = false;
152
+
$request_uri = get_post_field( 'post_name', $front_page_id );
153
+
}
154
+
155
+
add_filter( 'option_rewrite_rules', [ $this, 'fix_query_notice' ] );
156
+
}
157
+
158
+
return $request_uri;
159
+
}
160
+
161
+
/**
162
+
* Filter rewrite_rules to avoid a PHP notice.
163
+
*
164
+
* @param array $rules Original rules.
165
+
* @return array
166
+
*/
167
+
public function fix_query_notice( $rules ) {
168
+
if ( ! is_array( $rules ) || isset( $rules['$'] ) ) {
169
+
return $rules;
170
+
}
171
+
172
+
global $wp_rewrite;
173
+
$rules['$'] = $wp_rewrite->index;
174
+
return $rules;
175
+
}
176
+
177
+
/**
178
+
* Check if provided URL is valid and internal.
179
+
*
180
+
* @param string $url URL.
181
+
*
182
+
* @return boolean
183
+
*/
184
+
public function is_valid_url( $url ) {
185
+
$url = preg_replace_callback(
186
+
'/[^\x20-\x7f]/',
187
+
function ( $matcher ) {
188
+
return rawurlencode( $matcher[0] );
189
+
},
190
+
$url
191
+
);
192
+
193
+
return Url::is_url( $url ) && ! Url::is_external( $url );
194
+
}
195
+
}
196
+