|
| 1 | +<?php |
| 2 | + |
| 3 | +class Activity_Pub { |
| 4 | + public static function render_profile( $template ) { |
| 5 | + if ( ! is_author() ) { |
| 6 | + return $template; |
| 7 | + } |
| 8 | + |
| 9 | + $json_template = dirname( __FILE__ ) . '/../templates/author-profile.php'; |
| 10 | + |
| 11 | + global $wp_query; |
| 12 | + |
| 13 | + if ( isset( $wp_query->query_vars['activitypub'] ) ) { |
| 14 | + return $json_template; |
| 15 | + } |
| 16 | + |
| 17 | + if ( ! isset( $_SERVER['HTTP_ACCEPT'] ) ) { |
| 18 | + return $template; |
| 19 | + } |
| 20 | + |
| 21 | + // interpret accept header |
| 22 | + $pos = stripos( $_SERVER['HTTP_ACCEPT'], ';' ); |
| 23 | + if ( $pos ) { |
| 24 | + $accept_header = substr( $_SERVER['HTTP_ACCEPT'], 0, $pos ); |
| 25 | + } else { |
| 26 | + $accept_header = $_SERVER['HTTP_ACCEPT']; |
| 27 | + } |
| 28 | + // accept header as an array |
| 29 | + $accept = explode( ',', trim( $accept_header ) ); |
| 30 | + |
| 31 | + if ( |
| 32 | + ! in_array( 'application/activity+json', $accept, true ) && |
| 33 | + ! in_array( 'application/ld+json', $accept, true ) |
| 34 | + ) { |
| 35 | + return $template; |
| 36 | + } |
| 37 | + |
| 38 | + return $json_template; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Add WebFinger discovery links |
| 43 | + * |
| 44 | + * @param array $array the jrd array |
| 45 | + * @param string $resource the WebFinger resource |
| 46 | + * @param WP_User $user the WordPress user |
| 47 | + */ |
| 48 | + public static function add_webfinger_discovery( $array, $resource, $user ) { |
| 49 | + $array['links'][] = array( |
| 50 | + 'rel' => 'self', |
| 51 | + 'type' => 'aplication/activity+json', |
| 52 | + 'href' => get_author_posts_url( $user->ID ), |
| 53 | + ); |
| 54 | + |
| 55 | + return $array; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Add the 'photos' query variable so WordPress |
| 60 | + * won't mangle it. |
| 61 | + */ |
| 62 | + public static function add_query_vars( $vars ) { |
| 63 | + $vars[] = 'activitypub'; |
| 64 | + return $vars; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Add our rewrite endpoint to permalinks and pages. |
| 69 | + */ |
| 70 | + public static function add_rewrite_endpoint() { |
| 71 | + add_rewrite_endpoint( 'activitypub', EP_AUTHORS ); |
| 72 | + } |
| 73 | +} |
0 commit comments