Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.

Commit e77cefc

Browse files
committed
rewritten all the engine
1 parent 441bbf2 commit e77cefc

File tree

1 file changed

+166
-18
lines changed

1 file changed

+166
-18
lines changed

command.php

Lines changed: 166 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,24 @@
2121
* @param array $assoc_args
2222
* @return
2323
*/
24-
function codeat_get_by_url( $args ){
25-
$url = parse_url( esc_html( $args[0] ) );
26-
$slug = $url[ 'path' ];
27-
$split_slug = array_filter( explode( '/', $slug ), 'strlen' );
28-
$last_slug = $split_slug[ count( $split_slug ) ];
29-
$term_slug = $split_slug[ count( $split_slug ) -1 ];
24+
function codeat_get_by_url( $args ){
25+
$url = esc_html( $args[0] );
3026

31-
$post = get_posts( array(
32-
'name' => $last_slug,
33-
'posts_per_page' => 1,
34-
));
35-
if( isset( $post[ 0 ] ) ) {
36-
$post = $post[ 0 ];
37-
if ( is_object( $post ) ) {
38-
WP_CLI::log( 'post | ' . $post->ID . ' | ' . $post->post_type );
39-
return;
40-
}
27+
$post = url_to_post( $url );
28+
if ( is_object( $post ) ) {
29+
WP_CLI::log( 'post | ' . $post->ID . ' | ' . $post->post_type );
30+
return;
4131
}
4232

43-
$taxonomies = get_taxonomies( array(), 'objects' );
33+
$url_path = codeat_clean_url( $url );
34+
$url_parameters = explode( '/', $url_path );
35+
$last_slug = $url_parameters[ count( $url_parameters ) -1 ];
36+
$term_slug = $url_parameters[ 0 ];
37+
38+
$taxonomies = get_taxonomies( array( '_builtin' => true ), 'objects' );
4439
foreach ( $taxonomies as $taxonomy ) {
4540
if ( $taxonomy->rewrite[ 'slug' ] === $term_slug ) {
46-
$tax = get_term_by( 'slug', $last_slug, $term_slug );
41+
$tax = get_term_by( 'name', $last_slug, $taxonomy->name );
4742
if( is_object( $tax ) ) {
4843
WP_CLI::log( 'term | ' . $tax->term_id . ' | ' . $tax->taxonomy );
4944
return;
@@ -52,4 +47,157 @@ function codeat_get_by_url( $args ){
5247
}
5348
};
5449

50+
// Based on the code of url_to_postid to get the clean url
51+
function codeat_clean_url( $url ) {
52+
global $wp_rewrite;
53+
54+
$url_host = str_replace( 'www.', '', parse_url( $url, PHP_URL_HOST ) );
55+
$home_url_host = str_replace( 'www.', '', parse_url( home_url(), PHP_URL_HOST ) );
56+
57+
// Bail early if the URL does not belong to this site.
58+
if ( $url_host && $url_host !== $home_url_host ) {
59+
return 0;
60+
}
61+
62+
// First, check to see if there is a 'p=N' or 'page_id=N' to match against
63+
if ( preg_match('#[?&](p|page_id|attachment_id)=(\d+)#', $url, $values) ) {
64+
$id = absint($values[2]);
65+
if ( $id )
66+
return $id;
67+
}
68+
69+
// Get rid of the #anchor
70+
$url_split = explode('#', $url);
71+
$url = $url_split[0];
72+
73+
// Get rid of URL ?query=string
74+
$url_split = explode('?', $url);
75+
$url = $url_split[0];
76+
77+
// Set the correct URL scheme.
78+
$scheme = parse_url( home_url(), PHP_URL_SCHEME );
79+
$url = set_url_scheme( $url, $scheme );
80+
81+
// Add 'www.' if it is absent and should be there
82+
if ( false !== strpos(home_url(), '://www.') && false === strpos($url, '://www.') )
83+
$url = str_replace('://', '://www.', $url);
84+
85+
// Strip 'www.' if it is present and shouldn't be
86+
if ( false === strpos(home_url(), '://www.') )
87+
$url = str_replace('://www.', '://', $url);
88+
89+
if ( trim( $url, '/' ) === home_url() && 'page' == get_option( 'show_on_front' ) ) {
90+
$page_on_front = get_option( 'page_on_front' );
91+
92+
if ( $page_on_front && get_post( $page_on_front ) instanceof WP_Post ) {
93+
return (int) $page_on_front;
94+
}
95+
}
96+
97+
// Check to see if we are using rewrite rules
98+
$rewrite = $wp_rewrite->wp_rewrite_rules();
99+
100+
// Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
101+
if ( empty($rewrite) )
102+
return 0;
103+
104+
// Strip 'index.php/' if we're not using path info permalinks
105+
if ( !$wp_rewrite->using_index_permalinks() )
106+
$url = str_replace( $wp_rewrite->index . '/', '', $url );
107+
108+
if ( false !== strpos( trailingslashit( $url ), home_url( '/' ) ) ) {
109+
// Chop off http://domain.com/[path]
110+
$url = str_replace(home_url(), '', $url);
111+
} else {
112+
// Chop off /path/to/blog
113+
$home_path = parse_url( home_url( '/' ) );
114+
$home_path = isset( $home_path['path'] ) ? $home_path['path'] : '' ;
115+
$url = preg_replace( sprintf( '#^%s#', preg_quote( $home_path ) ), '', trailingslashit( $url ) );
116+
}
117+
118+
// Trim leading and lagging slashes
119+
$url = trim($url, '/');
120+
121+
return $url;
122+
}
123+
124+
// Fork of url_to_postid to get the object
125+
function url_to_post( $url ) {
126+
global $wp_rewrite;
127+
128+
// Check to see if we are using rewrite rules
129+
$rewrite = $wp_rewrite->wp_rewrite_rules();
130+
131+
// Not using rewrite rules, and 'p=N' and 'page_id=N' methods failed, so we're out of options
132+
if ( empty($rewrite) )
133+
return 0;
134+
135+
$post_type_query_vars = array();
136+
137+
foreach ( get_post_types( array() , 'objects' ) as $post_type => $t ) {
138+
if ( ! empty( $t->query_var ) )
139+
$post_type_query_vars[ $t->query_var ] = $post_type;
140+
}
141+
142+
// Look for matches.
143+
$url = $request = codeat_clean_url( $url );
144+
$request_match = $request;
145+
foreach ( (array)$rewrite as $match => $query) {
146+
147+
// If the requesting file is the anchor of the match, prepend it
148+
// to the path info.
149+
if ( !empty($url) && ($url != $request) && (strpos($match, $url) === 0) )
150+
$request_match = $url . '/' . $request;
151+
152+
if ( preg_match("#^$match#", $request_match, $matches) ) {
153+
154+
if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
155+
// This is a verbose page match, let's check to be sure about it.
156+
$page = get_page_by_path( $matches[ $varmatch[1] ] );
157+
if ( ! $page ) {
158+
continue;
159+
}
160+
161+
$post_status_obj = get_post_status_object( $page->post_status );
162+
if ( ! $post_status_obj->public && ! $post_status_obj->protected
163+
&& ! $post_status_obj->private && $post_status_obj->exclude_from_search ) {
164+
continue;
165+
}
166+
}
167+
168+
// Got a match.
169+
// Trim the query of everything up to the '?'.
170+
$query = preg_replace("!^.+\?!", '', $query);
171+
172+
// Substitute the substring matches into the query.
173+
$query = addslashes(WP_MatchesMapRegex::apply($query, $matches));
174+
175+
// Filter out non-public query vars
176+
global $wp;
177+
parse_str( $query, $query_vars );
178+
$query = array();
179+
foreach ( (array) $query_vars as $key => $value ) {
180+
if ( in_array( $key, $wp->public_query_vars ) ){
181+
$query[$key] = $value;
182+
if ( isset( $post_type_query_vars[$key] ) ) {
183+
$query['post_type'] = $post_type_query_vars[$key];
184+
$query['name'] = $value;
185+
}
186+
}
187+
}
188+
189+
// Resolve conflicts between posts with numeric slugs and date archive queries.
190+
$query = wp_resolve_numeric_slug_conflicts( $query );
191+
192+
// Do the query
193+
$query = new WP_Query( $query );
194+
if ( ! empty( $query->posts ) && $query->is_singular )
195+
return $query->post;
196+
else
197+
return 0;
198+
}
199+
}
200+
return 0;
201+
}
202+
55203
WP_CLI::add_command( 'get-by-url', 'codeat_get_by_url' );

0 commit comments

Comments
 (0)