Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/wp-rocket/inc/functions/posts.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
3
+
defined( 'ABSPATH' ) || exit;
4
+
5
+
/**
6
+
* Get the permalink post
7
+
*
8
+
* @since 1.3.1
9
+
*
10
+
* @source : get_sample_permalink() in wp-admin/includes/post.php
11
+
*
12
+
* @param int $id The post ID.
13
+
* @param string $title The post title.
14
+
* @param string $name The post name.
15
+
*
16
+
* @return array
17
+
*/
18
+
function get_rocket_sample_permalink( $id, $title = null, $name = null ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
19
+
$post = get_post( $id );
20
+
if ( ! $post ) {
21
+
return [ '', '' ];
22
+
}
23
+
24
+
$ptype = get_post_type_object( $post->post_type );
25
+
26
+
$original_status = $post->post_status;
27
+
$original_date = $post->post_date;
28
+
$original_name = $post->post_name;
29
+
30
+
// Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published.
31
+
if ( in_array( $post->post_status, [ 'draft', 'pending' ], true ) ) {
32
+
$post->post_status = 'publish';
33
+
$post->post_name = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID );
34
+
}
35
+
36
+
// If the user wants to set a new name -- override the current one.
37
+
// Note: if empty name is supplied -- use the title instead, see #6072.
38
+
if ( ! is_null( $name ) ) {
39
+
$post->post_name = sanitize_title( $name ? $name : $title, $post->ID );
40
+
}
41
+
42
+
$post->post_name = wp_unique_post_slug( $post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent );
43
+
44
+
$post->filter = 'sample';
45
+
46
+
$permalink = get_permalink( $post, false );
47
+
48
+
// Replace custom post_type Token with generic pagename token for ease of use.
49
+
$permalink = str_replace( "%$post->post_type%", '%pagename%', $permalink );
50
+
51
+
// Handle page hierarchy.
52
+
if ( $ptype->hierarchical ) {
53
+
$uri = get_page_uri( $post );
54
+
$uri = untrailingslashit( $uri );
55
+
$uri = strrev( stristr( strrev( $uri ), '/' ) );
56
+
$uri = untrailingslashit( $uri );
57
+
58
+
/** This filter is documented in wp-admin/edit-tag-form.php */
59
+
$uri = apply_filters( 'editable_slug', $uri, $post ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
60
+
if ( ! empty( $uri ) ) {
61
+
$uri .= '/';
62
+
}
63
+
$permalink = str_replace( '%pagename%', "{$uri}%pagename%", $permalink );
64
+
}
65
+
66
+
/** This filter is documented in wp-admin/edit-tag-form.php */
67
+
$permalink = [ $permalink, apply_filters( 'editable_slug', $post->post_name, $post ) ]; // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals
68
+
$post->post_status = $original_status;
69
+
$post->post_date = $original_date;
70
+
$post->post_name = $original_name;
71
+
unset( $post->filter );
72
+
73
+
return $permalink;
74
+
}
75
+
76
+
if ( ! function_exists( 'rocket_url_to_postid' ) ) {
77
+
/**
78
+
* Get the post ID from the URL.
79
+
*
80
+
* @param string $url URL of the page.
81
+
* @param array|string[] $search_in_post_statuses Post statuses to search in.
82
+
* @return float|int Post ID.
83
+
*/
84
+
function rocket_url_to_postid( string $url, array $search_in_post_statuses = [ 'publish', 'private' ] ) {
85
+
global $wp_rewrite;
86
+
87
+
/**
88
+
* Filters the URL to derive the post ID from.
89
+
*
90
+
* @since 2.2.0
91
+
*
92
+
* @param string $url The URL to derive the post ID from.
93
+
*/
94
+
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
95
+
$url = apply_filters( 'url_to_postid', $url );
96
+
97
+
$url_host = wp_parse_url( $url, PHP_URL_HOST );
98
+
99
+
if ( is_string( $url_host ) ) {
100
+
$url_host = str_replace( 'www.', '', $url_host );
101
+
} else {
102
+
$url_host = '';
103
+
}
104
+
105
+
$home_url_host = wp_parse_url( home_url(), PHP_URL_HOST );
106
+
107
+
if ( is_string( $home_url_host ) ) {
108
+
$home_url_host = str_replace( 'www.', '', $home_url_host );
109
+
} else {
110
+
$home_url_host = '';
111
+
}
112
+
113
+
// Bail early if the URL does not belong to this site.
114
+
if ( $url_host && $url_host !== $home_url_host ) {
115
+
return 0;
116
+
}
117
+
118
+
// First, check to see if there is a 'p=N' or 'page_id=N' to match against.
119
+
if ( preg_match( '#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values ) ) {
120
+
$id = absint( $values[2] );
121
+
if ( $id ) {
122
+
if ( empty( $search_in_post_statuses ) || ! in_array( get_post_status( $id ), $search_in_post_statuses, true ) ) {
123
+
return 0;
124
+
}
125
+
return $id;
126
+
}
127
+
}
128
+
129
+
// Get rid of the #anchor.
130
+
$url_split = explode( '#', $url );
131
+
$url = $url_split[0];
132
+
133
+
// Get rid of URL ?query=string.
134
+
$url_split = explode( '?', $url );
135
+
$url = $url_split[0];
136
+
137
+
// Set the correct URL scheme.
138
+
$scheme = wp_parse_url( home_url(), PHP_URL_SCHEME );
139
+
$url = set_url_scheme( $url, $scheme );
140
+
141
+
// Add 'www.' if it is absent and should be there.
142
+
if ( str_contains( home_url(), '://www.' ) && ! str_contains( $url, '://www.' ) ) {
143
+
$url = str_replace( '://', '://www.', $url );
144
+
}
145
+
146
+
// Strip 'www.' if it is present and shouldn't be.
147
+
if ( ! str_contains( home_url(), '://www.' ) ) {
148
+
$url = str_replace( '://www.', '://', $url );
149
+
}
150
+
151
+
if ( trim( $url, '/' ) === home_url() && 'page' === get_option( 'show_on_front' ) ) {
152
+
$page_on_front = get_option( 'page_on_front' );
153
+
154
+
if ( $page_on_front && get_post( $page_on_front ) instanceof WP_Post ) {
155
+
if ( empty( $search_in_post_statuses ) || ! in_array( get_post_status( (int) $page_on_front ), $search_in_post_statuses, true ) ) {
156
+
return 0;
157
+
}
158
+
return (int) $page_on_front;
159
+
}
160
+
}
161
+
162
+
// Check to see if we are using rewrite rules.
163
+
$rewrite = $wp_rewrite->wp_rewrite_rules();
164
+
165
+
// Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options.
166
+
if ( empty( $rewrite ) ) {
167
+
return 0;
168
+
}
169
+
170
+
// Strip 'index.php/' if we're not using path info permalinks.
171
+
if ( ! $wp_rewrite->using_index_permalinks() ) {
172
+
$url = str_replace( $wp_rewrite->index . '/', '', $url );
173
+
}
174
+
175
+
if ( str_contains( trailingslashit( $url ), home_url( '/' ) ) ) {
176
+
// Chop off http://domain.com/[path].
177
+
$url = str_replace( home_url(), '', $url );
178
+
} else {
179
+
// Chop off /path/to/blog.
180
+
$home_path = wp_parse_url( home_url( '/' ) );
181
+
$home_path = isset( $home_path['path'] ) ? $home_path['path'] : '';
182
+
$url = preg_replace( sprintf( '#^%s#', preg_quote( $home_path, '#' ) ), '', trailingslashit( $url ) );
183
+
}
184
+
185
+
// Trim leading and lagging slashes.
186
+
$url = trim( $url, '/' );
187
+
188
+
$request = $url;
189
+
$post_type_query_vars = [];
190
+
191
+
foreach ( get_post_types( [], 'objects' ) as $post_type => $t ) {
192
+
if ( ! empty( $t->query_var ) ) {
193
+
$post_type_query_vars[ $t->query_var ] = $post_type;
194
+
}
195
+
}
196
+
197
+
// Look for matches.
198
+
$request_match = $request;
199
+
foreach ( (array) $rewrite as $match => $query ) {
200
+
/*
201
+
* If the requesting file is the anchor of the match,
202
+
* prepend it to the path info.
203
+
*/
204
+
if ( ! empty( $url ) && ( $url !== $request ) && str_starts_with( $match, $url ) ) {
205
+
$request_match = $url . '/' . $request;
206
+
}
207
+
208
+
if ( preg_match( "#^$match#", $request_match, $matches ) ) {
209
+
210
+
if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
211
+
// This is a verbose page match, let's check to be sure about it.
212
+
$page = get_page_by_path( $matches[ $varmatch[1] ] );
213
+
if ( ! $page ) {
214
+
continue;
215
+
}
216
+
217
+
$post_status_obj = get_post_status_object( $page->post_status );
218
+
if ( ! $post_status_obj->public && ! $post_status_obj->protected
219
+
&& ! $post_status_obj->private && $post_status_obj->exclude_from_search ) {
220
+
continue;
221
+
}
222
+
}
223
+
224
+
/*
225
+
* Got a match.
226
+
* Trim the query of everything up to the '?'.
227
+
*/
228
+
$query = preg_replace( '!^.+\?!', '', $query );
229
+
230
+
// Substitute the substring matches into the query.
231
+
$query = addslashes( WP_MatchesMapRegex::apply( $query, $matches ) );
232
+
233
+
// Filter out non-public query vars.
234
+
global $wp;
235
+
parse_str( $query, $query_vars );
236
+
$query = [];
237
+
foreach ( (array) $query_vars as $key => $value ) {
238
+
if ( in_array( (string) $key, $wp->public_query_vars, true ) ) {
239
+
$query[ $key ] = $value;
240
+
if ( isset( $post_type_query_vars[ $key ] ) ) {
241
+
$query['post_type'] = $post_type_query_vars[ $key ];
242
+
$query['name'] = $value;
243
+
}
244
+
}
245
+
}
246
+
247
+
// Resolve conflicts between posts with numeric slugs and date archive queries.
248
+
$query = wp_resolve_numeric_slug_conflicts( $query );
249
+
250
+
if ( ! empty( $search_in_post_statuses ) ) {
251
+
$query['post_status'] = $search_in_post_statuses;
252
+
}
253
+
254
+
/**
255
+
* Filters WP_Query class passed args.
256
+
*
257
+
* @param array $query WP_Query passed args.
258
+
* @param string $url The URL to derive the post ID from.
259
+
*/
260
+
$query = (array) apply_filters( 'rocket_url_to_postid_query_args', $query, $url );
261
+
262
+
$query['no_found_rows'] = true;
263
+
$query['update_post_term_cache'] = false;
264
+
$query['update_post_meta_cache'] = false;
265
+
266
+
// Do the query.
267
+
$query = new WP_Query( $query );
268
+
269
+
if ( ! empty( $query->posts ) && $query->is_singular ) {
270
+
return $query->post->ID;
271
+
} else {
272
+
return 0;
273
+
}
274
+
}
275
+
}
276
+
return 0;
277
+
}
278
+
}
279
+