prepare migration
This commit is contained in:
parent
ec822535c9
commit
230aaa5b24
5 changed files with 166 additions and 66 deletions
|
@ -32,8 +32,10 @@ function init() {
|
|||
\define( 'ACTIVITYPUB_OBJECT', 'ACTIVITYPUB_OBJECT' );
|
||||
|
||||
require_once \dirname( __FILE__ ) . '/includes/table/class-followers.php';
|
||||
require_once \dirname( __FILE__ ) . '/includes/class-http.php';
|
||||
require_once \dirname( __FILE__ ) . '/includes/class-signature.php';
|
||||
require_once \dirname( __FILE__ ) . '/includes/class-webfinger.php';
|
||||
require_once \dirname( __FILE__ ) . '/includes/class-migration.php';
|
||||
require_once \dirname( __FILE__ ) . '/includes/peer/class-followers.php';
|
||||
require_once \dirname( __FILE__ ) . '/includes/functions.php';
|
||||
|
||||
|
|
93
includes/class-http.php
Normal file
93
includes/class-http.php
Normal file
|
@ -0,0 +1,93 @@
|
|||
<?php
|
||||
namespace Activitypub;
|
||||
|
||||
use WP_Error;
|
||||
|
||||
/**
|
||||
* ActivityPub HTTP Class
|
||||
*
|
||||
* @author Matthias Pfefferle
|
||||
*/
|
||||
class Http {
|
||||
/**
|
||||
* Send a POST Request with the needed HTTP Headers
|
||||
*
|
||||
* @param string $url The URL endpoint
|
||||
* @param string $body The Post Body
|
||||
* @param int $user_id The WordPress User-ID
|
||||
*
|
||||
* @return array|WP_Error The POST Response or an WP_ERROR
|
||||
*/
|
||||
public static function post( $url, $body, $user_id ) {
|
||||
$date = \gmdate( 'D, d M Y H:i:s T' );
|
||||
$digest = Signature::generate_digest( $body );
|
||||
$signature = Signature::generate_signature( $user_id, 'post', $url, $date, $digest );
|
||||
|
||||
$wp_version = \get_bloginfo( 'version' );
|
||||
$user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) );
|
||||
$args = array(
|
||||
'timeout' => 100,
|
||||
'limit_response_size' => 1048576,
|
||||
'redirection' => 3,
|
||||
'user-agent' => "$user_agent; ActivityPub",
|
||||
'headers' => array(
|
||||
'Accept' => 'application/activity+json',
|
||||
'Content-Type' => 'application/activity+json',
|
||||
'Digest' => "SHA-256=$digest",
|
||||
'Signature' => $signature,
|
||||
'Date' => $date,
|
||||
),
|
||||
'body' => $body,
|
||||
);
|
||||
|
||||
$response = \wp_safe_remote_get( $url, $args );
|
||||
$code = \wp_remote_retrieve_response_code( $response );
|
||||
|
||||
if ( 400 >= $code && 500 <= $code ) {
|
||||
$response = new WP_Error( $code, __( 'Failed HTTP Request', 'activitypub' ) );
|
||||
}
|
||||
|
||||
\do_action( 'activitypub_safe_remote_post_response', $response, $url, $body, $user_id );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a GET Request with the needed HTTP Headers
|
||||
*
|
||||
* @param string $url The URL endpoint
|
||||
* @param int $user_id The WordPress User-ID
|
||||
*
|
||||
* @return array|WP_Error The GET Response or an WP_ERROR
|
||||
*/
|
||||
public static function get( $url, $user_id ) {
|
||||
$date = \gmdate( 'D, d M Y H:i:s T' );
|
||||
$signature = Signature::generate_signature( $user_id, 'get', $url, $date );
|
||||
|
||||
$wp_version = \get_bloginfo( 'version' );
|
||||
$user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) );
|
||||
$args = array(
|
||||
'timeout' => apply_filters( 'activitypub_remote_get_timeout', 100 ),
|
||||
'limit_response_size' => 1048576,
|
||||
'redirection' => 3,
|
||||
'user-agent' => "$user_agent; ActivityPub",
|
||||
'headers' => array(
|
||||
'Accept' => 'application/activity+json',
|
||||
'Content-Type' => 'application/activity+json',
|
||||
'Signature' => $signature,
|
||||
'Date' => $date,
|
||||
),
|
||||
);
|
||||
|
||||
$response = \wp_safe_remote_get( $url, $args );
|
||||
$code = \wp_remote_retrieve_response_code( $response );
|
||||
|
||||
if ( 400 >= $code && 500 <= $code ) {
|
||||
$response = new WP_Error( $code, __( 'Failed HTTP Request', 'activitypub' ) );
|
||||
}
|
||||
|
||||
\do_action( 'activitypub_safe_remote_get_response', $response, $url, $user_id );
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
68
includes/class-migration.php
Normal file
68
includes/class-migration.php
Normal file
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
namespace Activitypub;
|
||||
|
||||
class Migration {
|
||||
/**
|
||||
* Which internal datastructure version we are running on.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private static $target_version = '1.0.0';
|
||||
|
||||
public static function get_target_version() {
|
||||
return self::$target_version;
|
||||
}
|
||||
|
||||
public static function get_version() {
|
||||
return get_option( 'activitypub_db_version', 0 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the database structure is up to date.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_latest_version() {
|
||||
return (bool) version_compare(
|
||||
self::get_version(),
|
||||
self::get_target_version(),
|
||||
'=='
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the database structure if necessary.
|
||||
*/
|
||||
public static function maybe_migrate() {
|
||||
if ( self::is_latest_version() ) {
|
||||
//return;
|
||||
}
|
||||
|
||||
$version_from_db = self::get_version();
|
||||
|
||||
//if ( version_compare( $version_from_db, '1.0.0', '<' ) ) {
|
||||
self::migrate_to_1_0_0();
|
||||
//}
|
||||
|
||||
update_option( 'activitypub_db_version', self::$target_version );
|
||||
}
|
||||
|
||||
/**
|
||||
* The Migration for Plugin Version 1.0.0 and DB Version 1.0.0
|
||||
*
|
||||
* @since 5.0.0
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function migrate_to_1_0_0() {
|
||||
foreach ( get_users( array( 'fields' => 'ID' ) ) as $user_id ) {
|
||||
$followes = get_user_meta( $user_id, 'activitypub_followers', true );
|
||||
|
||||
if ( $followes ) {
|
||||
foreach ( $followes as $follower ) {
|
||||
Collection\Followers::add_follower( $user_id, $follower );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -374,20 +374,4 @@ class Followers {
|
|||
|
||||
return array_filter( $results );
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrate Followers
|
||||
*
|
||||
* @param int $user_id The ID of the WordPress User
|
||||
* @return void
|
||||
*/
|
||||
public static function migrate_followers( $user_id ) {
|
||||
$followes = get_user_meta( $user_id, 'activitypub_followers', true );
|
||||
|
||||
if ( $followes ) {
|
||||
foreach ( $followes as $follower ) {
|
||||
self::add_follower( $user_id, $follower );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,58 +33,11 @@ function get_context() {
|
|||
}
|
||||
|
||||
function safe_remote_post( $url, $body, $user_id ) {
|
||||
$date = \gmdate( 'D, d M Y H:i:s T' );
|
||||
$digest = \Activitypub\Signature::generate_digest( $body );
|
||||
$signature = \Activitypub\Signature::generate_signature( $user_id, 'post', $url, $date, $digest );
|
||||
|
||||
$wp_version = \get_bloginfo( 'version' );
|
||||
$user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) );
|
||||
$args = array(
|
||||
'timeout' => 100,
|
||||
'limit_response_size' => 1048576,
|
||||
'redirection' => 3,
|
||||
'user-agent' => "$user_agent; ActivityPub",
|
||||
'headers' => array(
|
||||
'Accept' => 'application/activity+json',
|
||||
'Content-Type' => 'application/activity+json',
|
||||
'Digest' => "SHA-256=$digest",
|
||||
'Signature' => $signature,
|
||||
'Date' => $date,
|
||||
),
|
||||
'body' => $body,
|
||||
);
|
||||
|
||||
$response = \wp_safe_remote_post( $url, $args );
|
||||
|
||||
\do_action( 'activitypub_safe_remote_post_response', $response, $url, $body, $user_id );
|
||||
|
||||
return $response;
|
||||
return \Activitypub\Http::post( $url, $body, $user_id );
|
||||
}
|
||||
|
||||
function safe_remote_get( $url, $user_id ) {
|
||||
$date = \gmdate( 'D, d M Y H:i:s T' );
|
||||
$signature = Signature::generate_signature( $user_id, 'get', $url, $date );
|
||||
|
||||
$wp_version = \get_bloginfo( 'version' );
|
||||
$user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) );
|
||||
$args = array(
|
||||
'timeout' => apply_filters( 'activitypub_remote_get_timeout', 100 ),
|
||||
'limit_response_size' => 1048576,
|
||||
'redirection' => 3,
|
||||
'user-agent' => "$user_agent; ActivityPub",
|
||||
'headers' => array(
|
||||
'Accept' => 'application/activity+json',
|
||||
'Content-Type' => 'application/activity+json',
|
||||
'Signature' => $signature,
|
||||
'Date' => $date,
|
||||
),
|
||||
);
|
||||
|
||||
$response = \wp_safe_remote_get( $url, $args );
|
||||
|
||||
\do_action( 'activitypub_safe_remote_get_response', $response, $url, $user_id );
|
||||
|
||||
return $response;
|
||||
return \Activitypub\Http::get( $url, $user_id );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -149,7 +102,7 @@ function get_remote_metadata_by_actor( $actor ) {
|
|||
return 3;
|
||||
};
|
||||
add_filter( 'activitypub_remote_get_timeout', $short_timeout );
|
||||
$response = \Activitypub\safe_remote_get( $actor, $user_id );
|
||||
$response = Http::get( $actor, $user_id );
|
||||
remove_filter( 'activitypub_remote_get_timeout', $short_timeout );
|
||||
if ( \is_wp_error( $response ) ) {
|
||||
\set_transient( $transient_key, $response, HOUR_IN_SECONDS ); // Cache the error for a shorter period.
|
||||
|
|
Loading…
Reference in a new issue