This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.php
216 lines (177 loc) · 6.37 KB
/
command.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
if ( ! class_exists( 'WP_CLI' ) ) {
return;
}
/**
* Get the id with post type or taxonomy by URL.
*
* ## OPTIONS
*
* <url>
*
* ## EXAMPLES
*
* wp get-by-url [your-url-with-slug]
*
* @accesspublic
* @param array $args
* @return
*/
function codeat_get_by_url( $args ){
$url = esc_html( $args[0] );
$hasext = pathinfo( $url, PATHINFO_EXTENSION );
if ( !empty( $hasext ) ) {
$url = preg_replace( "/^(.+)-\d+?x\d+?(\.\w+)$/i","$1$2", $url );
$media = attachment_url_to_postid( $url );
if ( $media !== 0 ) {
WP_CLI::log( 'post | ' . $media . ' | attachment' );
return;
}
}
$post = url_to_post( $url );
if ( is_object( $post ) ) {
WP_CLI::log( 'post | ' . $post->ID . ' | ' . $post->post_type );
return;
}
$url_path = codeat_clean_url( $url );
$url_parameters = explode( '/', $url_path );
$last_slug = $url_parameters[ count( $url_parameters ) -1 ];
$term_slug = $url_parameters[ 0 ];
$taxonomies = get_taxonomies( array( '_builtin' => true ), 'objects' );
if ( is_array( $taxonomies ) ) {
foreach ( $taxonomies as $taxonomy ) {
if ( !is_array( $taxonomy->rewrite ) ) {
continue;
}
if ( $taxonomy->rewrite[ 'slug' ] === $term_slug ) {
$tax = get_term_by( 'slug', $last_slug, $taxonomy->name );
if( is_object( $tax ) ) {
WP_CLI::log( 'term | ' . $tax->term_id . ' | ' . $tax->taxonomy );
return;
}
}
}
}
};
// Based on the code of url_to_postid to get the clean url
function codeat_clean_url( $url ) {
global $wp_rewrite;
$url_host = str_replace( 'www.', '', parse_url( $url, PHP_URL_HOST ) );
$home_url_host = str_replace( 'www.', '', parse_url( home_url(), PHP_URL_HOST ) );
// Bail early if the URL does not belong to this site.
if ( $url_host && $url_host !== $home_url_host ) {
return 0;
}
// First, check to see if there is a 'p=N' or 'page_id=N' to match against
if ( preg_match('#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values) ) {
$id = absint($values[2]);
if ( $id )
return $id;
}
// Get rid of the #anchor
$url_split = explode('#', $url);
$url = $url_split[0];
// Get rid of URL ?query=string
$url_split = explode('?', $url);
$url = $url_split[0];
// Set the correct URL scheme.
$scheme = parse_url( home_url(), PHP_URL_SCHEME );
$url = set_url_scheme( $url, $scheme );
// Add 'www.' if it is absent and should be there
if ( false !== strpos(home_url(), '://www.') && false === strpos($url, '://www.') )
$url = str_replace('://', '://www.', $url);
// Strip 'www.' if it is present and shouldn't be
if ( false === strpos(home_url(), '://www.') )
$url = str_replace('://www.', '://', $url);
if ( trim( $url, '/' ) === home_url() && 'page' == get_option( 'show_on_front' ) ) {
$page_on_front = get_option( 'page_on_front' );
if ( $page_on_front && get_post( $page_on_front ) instanceof WP_Post ) {
return (int) $page_on_front;
}
}
// Check to see if we are using rewrite rules
$rewrite = $wp_rewrite->wp_rewrite_rules();
// Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
if ( empty($rewrite) )
return 0;
// Strip 'index.php/' if we're not using path info permalinks
if ( !$wp_rewrite->using_index_permalinks() )
$url = str_replace( $wp_rewrite->index . '/', '', $url );
if ( false !== strpos( trailingslashit( $url ), home_url( '/' ) ) ) {
// Chop off http://domain.com/[path]
$url = str_replace(home_url(), '', $url);
} else {
// Chop off /path/to/blog
$home_path = parse_url( home_url( '/' ) );
$home_path = isset( $home_path['path'] ) ? $home_path['path'] : '' ;
$url = preg_replace( sprintf( '#^%s#', preg_quote( $home_path ) ), '', trailingslashit( $url ) );
}
// Trim leading and lagging slashes
$url = trim($url, '/');
return $url;
}
// Fork of url_to_postid to get the object
function url_to_post( $url ) {
global $wp_rewrite;
// Check to see if we are using rewrite rules
$rewrite = $wp_rewrite->wp_rewrite_rules();
// Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
if ( empty($rewrite) )
return 0;
$post_type_query_vars = array();
foreach ( get_post_types( array() , 'objects' ) as $post_type => $t ) {
if ( ! empty( $t->query_var ) )
$post_type_query_vars[ $t->query_var ] = $post_type;
}
// Look for matches.
$url = $request = codeat_clean_url( $url );
$request_match = $request;
foreach ( (array)$rewrite as $match => $query) {
// If the requesting file is the anchor of the match, prepend it
// to the path info.
if ( !empty($url) && ($url != $request) && (strpos($match, $url) === 0) )
$request_match = $url . '/' . $request;
if ( preg_match("#^$match#", $request_match, $matches) ) {
if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
// This is a verbose page match, let's check to be sure about it.
$page = get_page_by_path( $matches[ $varmatch[1] ] );
if ( ! $page ) {
continue;
}
$post_status_obj = get_post_status_object( $page->post_status );
if ( ! $post_status_obj->public && ! $post_status_obj->protected
&& ! $post_status_obj->private && $post_status_obj->exclude_from_search ) {
continue;
}
}
// Got a match.
// Trim the query of everything up to the '?'.
$query = preg_replace("!^.+\?!", '', $query);
// Substitute the substring matches into the query.
$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
// Filter out non-public query vars
global $wp;
parse_str( $query, $query_vars );
$query = array();
foreach ( (array) $query_vars as $key => $value ) {
if ( in_array( $key, $wp->public_query_vars ) ){
$query[$key] = $value;
if ( isset( $post_type_query_vars[$key] ) ) {
$query['post_type'] = $post_type_query_vars[$key];
$query['name'] = $value;
}
}
}
// Resolve conflicts between posts with numeric slugs and date archive queries.
$query = wp_resolve_numeric_slug_conflicts( $query );
// Do the query
$query = new WP_Query( $query );
if ( ! empty( $query->posts ) && $query->is_singular )
return $query->post;
else
return 0;
}
}
return 0;
}
WP_CLI::add_command( 'get-by-url', 'codeat_get_by_url' );