| 1 |
<?php |
| 2 |
namespace Activitypub\Peer; |
| 3 |
|
| 4 |
/** |
| 5 |
* ActivityPub Users DB-Class |
| 6 |
* |
| 7 |
* @author Matthias Pfefferle |
| 8 |
*/ |
| 9 |
class Users { |
| 10 |
|
| 11 |
/** |
| 12 |
* Undocumented function |
| 13 |
* |
| 14 |
* @return void |
| 15 |
*/ |
| 16 |
public static function get_user_by_various( $data ) { |
| 17 |
|
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Examine a url and try to determine the author ID it represents. |
| 22 |
* |
| 23 |
* Checks are supposedly from the hosted site blog. |
| 24 |
* |
| 25 |
* @param string $url Permalink to check. |
| 26 |
* |
| 27 |
* @return int User ID, or 0 on failure. |
| 28 |
*/ |
| 29 |
public static function url_to_authorid( $url ) { |
| 30 |
global $wp_rewrite; |
| 31 |
|
| 32 |
// check if url hase the same host |
| 33 |
if ( \wp_parse_url( \site_url(), \PHP_URL_HOST ) !== \wp_parse_url( $url, \PHP_URL_HOST ) ) { |
| 34 |
return 0; |
| 35 |
} |
| 36 |
|
| 37 |
// first, check to see if there is a 'author=N' to match against |
| 38 |
if ( \preg_match( '/[?&]author=(\d+)/i', $url, $values ) ) { |
| 39 |
$id = \absint( $values[1] ); |
| 40 |
if ( $id ) { |
| 41 |
return $id; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
// check to see if we are using rewrite rules |
| 46 |
$rewrite = $wp_rewrite->wp_rewrite_rules(); |
| 47 |
|
| 48 |
// not using rewrite rules, and 'author=N' method failed, so we're out of options |
| 49 |
if ( empty( $rewrite ) ) { |
| 50 |
return 0; |
| 51 |
} |
| 52 |
|
| 53 |
// generate rewrite rule for the author url |
| 54 |
$author_rewrite = $wp_rewrite->get_author_permastruct(); |
| 55 |
$author_regexp = \str_replace( '%author%', '', $author_rewrite ); |
| 56 |
|
| 57 |
// match the rewrite rule with the passed url |
| 58 |
if ( \preg_match( '/https?:\/\/(.+)' . \preg_quote( $author_regexp, '/' ) . '([^\/]+)/i', $url, $match ) ) { |
| 59 |
$user = \get_user_by( 'slug', $match[2] ); |
| 60 |
if ( $user ) { |
| 61 |
return $user->ID; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return 0; |
| 66 |
} |
| 67 |
} |
| 68 |
|