| 1 |
<?php |
| 2 |
if ( ! function_exists( 'url_to_authorid' ) ) : |
| 3 |
/** |
| 4 |
* Examine a url and try to determine the author ID it represents. |
| 5 |
* |
| 6 |
* Checks are supposedly from the hosted site blog. |
| 7 |
* |
| 8 |
* @param string $url Permalink to check. |
| 9 |
* |
| 10 |
* @return int User ID, or 0 on failure. |
| 11 |
*/ |
| 12 |
function url_to_authorid( $url ) { |
| 13 |
global $wp_rewrite; |
| 14 |
|
| 15 |
// check if url hase the same host |
| 16 |
if ( parse_url( site_url(), PHP_URL_HOST ) != parse_url( $url, PHP_URL_HOST ) ) { |
| 17 |
return 0; |
| 18 |
} |
| 19 |
|
| 20 |
// first, check to see if there is a 'author=N' to match against |
| 21 |
if ( preg_match( '/[?&]author=(\d+)/i', $url, $values ) ) { |
| 22 |
$id = absint( $values[1] ); |
| 23 |
if ( $id ) { |
| 24 |
return $id; |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
// check to see if we are using rewrite rules |
| 29 |
$rewrite = $wp_rewrite->wp_rewrite_rules(); |
| 30 |
|
| 31 |
// not using rewrite rules, and 'author=N' method failed, so we're out of options |
| 32 |
if ( empty( $rewrite ) ) { |
| 33 |
return 0; |
| 34 |
} |
| 35 |
|
| 36 |
// generate rewrite rule for the author url |
| 37 |
$author_rewrite = $wp_rewrite->get_author_permastruct(); |
| 38 |
$author_regexp = str_replace( '%author%', '', $author_rewrite ); |
| 39 |
|
| 40 |
// match the rewrite rule with the passed url |
| 41 |
if ( preg_match( '/https?:\/\/(.+)' . preg_quote( $author_regexp, '/' ) . '([^\/]+)/i', $url, $match ) ) { |
| 42 |
$user = get_user_by( 'slug', $match[2] ); |
| 43 |
if ( $user ) { |
| 44 |
return $user->ID; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
return 0; |
| 49 |
} |
| 50 |
endif; |
| 51 |
|
| 52 |
if ( ! function_exists( 'get_user_by_various' ) ) : |
| 53 |
/** |
| 54 |
* Convenience method to get user data by ID, username, object or from current user. |
| 55 |
* |
| 56 |
* @param mixed $id_or_name_or_object the username, ID or object. If not provided, the current user will be used. |
| 57 |
* |
| 58 |
* @return bool|object False on failure, User DB row object |
| 59 |
* |
| 60 |
* @author Will Norris |
| 61 |
* |
| 62 |
* @see get_user_by_various() # DiSo OpenID-Plugin |
| 63 |
*/ |
| 64 |
function get_user_by_various( $id_or_name_or_object = null ) { |
| 65 |
if ( null === $id_or_name_or_object ) { |
| 66 |
$user = wp_get_current_user(); |
| 67 |
if ( null == $user ) { |
| 68 |
return false; |
| 69 |
} |
| 70 |
return $user; |
| 71 |
} elseif ( is_object( $id_or_name_or_object ) ) { |
| 72 |
return $id_or_name_or_object; |
| 73 |
} elseif ( is_numeric( $id_or_name_or_object ) ) { |
| 74 |
return get_user_by( 'id', $id_or_name_or_object ); |
| 75 |
} else { |
| 76 |
return get_user_by( 'login', $id_or_name_or_object ); |
| 77 |
} |
| 78 |
} |
| 79 |
endif; |
| 80 |
|
| 81 |
/** |
| 82 |
* Build WebFinger endpoint |
| 83 |
* |
| 84 |
* @return string The WebFinger URL |
| 85 |
*/ |
| 86 |
function get_webfinger_endpoint() { |
| 87 |
global $wp_rewrite; |
| 88 |
|
| 89 |
$permalink = $wp_rewrite->get_feed_permastruct(); |
| 90 |
if ( '' != $permalink ) { |
| 91 |
$url = home_url( '/.well-known/webfinger' ); |
| 92 |
} else { |
| 93 |
$url = add_query_arg( 'well-known', 'webfinger', home_url( '/' ) ); |
| 94 |
} |
| 95 |
|
| 96 |
return $url; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Returns all WebFinger "resources" |
| 101 |
* |
| 102 |
* @param mixed $id_or_name_or_object |
| 103 |
* |
| 104 |
* @return string The user-resource |
| 105 |
*/ |
| 106 |
function get_webfinger_resource( $id_or_name_or_object, $with_protocol = true ) { |
| 107 |
return Webfinger::get_user_resource( $id_or_name_or_object, $with_protocol ); |
| 108 |
} |
| 109 |
|