| 1 |
<?php |
| 2 |
/** |
| 3 |
* ActivityPub Class |
| 4 |
* |
| 5 |
* @author Matthias Pfefferle |
| 6 |
*/ |
| 7 |
class Activitypub { |
| 8 |
/** |
| 9 |
* Return a AS2 JSON version of an author, post or page |
| 10 |
* |
| 11 |
* @param string $template the path to the template object |
| 12 |
* |
| 13 |
* @return string the new path to the JSON template |
| 14 |
*/ |
| 15 |
public static function render_json_template( $template ) { |
| 16 |
if ( ! is_author() && ! is_singular() ) { |
| 17 |
return $template; |
| 18 |
} |
| 19 |
|
| 20 |
if ( is_author() ) { |
| 21 |
$json_template = dirname( __FILE__ ) . '/../templates/json-author.php'; |
| 22 |
} elseif ( is_singular() ) { |
| 23 |
$json_template = dirname( __FILE__ ) . '/../templates/json-post.php'; |
| 24 |
} |
| 25 |
|
| 26 |
global $wp_query; |
| 27 |
|
| 28 |
if ( isset( $wp_query->query_vars['as2'] ) ) { |
| 29 |
return $json_template; |
| 30 |
} |
| 31 |
|
| 32 |
if ( ! isset( $_SERVER['HTTP_ACCEPT'] ) ) { |
| 33 |
return $template; |
| 34 |
} |
| 35 |
|
| 36 |
// interpret accept header |
| 37 |
$pos = stripos( $_SERVER['HTTP_ACCEPT'], ';' ); |
| 38 |
if ( $pos ) { |
| 39 |
$accept_header = substr( $_SERVER['HTTP_ACCEPT'], 0, $pos ); |
| 40 |
} else { |
| 41 |
$accept_header = $_SERVER['HTTP_ACCEPT']; |
| 42 |
} |
| 43 |
// accept header as an array |
| 44 |
$accept = explode( ',', trim( $accept_header ) ); |
| 45 |
|
| 46 |
if ( |
| 47 |
! in_array( 'application/activity+json', $accept, true ) && |
| 48 |
! in_array( 'application/ld+json', $accept, true ) && |
| 49 |
! in_array( 'application/json', $accept, true ) |
| 50 |
) { |
| 51 |
return $template; |
| 52 |
} |
| 53 |
|
| 54 |
return $json_template; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Add the 'photos' query variable so WordPress |
| 59 |
* won't mangle it. |
| 60 |
*/ |
| 61 |
public static function add_query_vars( $vars ) { |
| 62 |
$vars[] = 'as2'; |
| 63 |
|
| 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( 'as2', EP_AUTHORS | EP_PERMALINK | EP_PAGES ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Marks the post as "no webmentions sent yet" |
| 76 |
* |
| 77 |
* @param int $post_id |
| 78 |
*/ |
| 79 |
public static function schedule_post_activity( $post_id ) { |
| 80 |
wp_schedule_single_event( time() + wp_rand( 0, 120 ), 'activitypub_send_post_activity', array( $post_id ) ); |
| 81 |
} |
| 82 |
} |
| 83 |
|