From 49f94816b6a0d97986fa5c7409af3e746c22589e Mon Sep 17 00:00:00 2001 From: Jeremy Dormitzer Date: Sat, 3 Nov 2018 14:30:16 -0400 Subject: [PATCH] Add uninstall hook; decompact actor and object before delivery --- includes/init.php | 24 +++++++++++++++++---- includes/schema.php | 34 ++++++++++++++++++++++++++++- includes/server/actors.php | 9 ++++++++ includes/server/async.php | 2 +- includes/server/deliver.php | 4 ++++ includes/util.php | 43 +++++++++++++++++-------------------- pterotype.php | 6 ++++++ 7 files changed, 93 insertions(+), 29 deletions(-) diff --git a/includes/init.php b/includes/init.php index a3eaf50..dea95c8 100644 --- a/includes/init.php +++ b/includes/init.php @@ -31,7 +31,7 @@ add_action( 'pterotype_init', function() { \pterotype\schema\run_migrations(); \pterotype\actors\initialize_actors(); if ( ! empty( ob_get_contents() ) ) { - \pterotype\util\log( 'init.log', ob_get_contents(), false ); + error_log( ob_get_contents() ); } } ); @@ -40,26 +40,42 @@ add_action( 'pterotype_load', function() { \pterotype\async\init_tasks(); } ); +add_action( 'pterotype_uninstall', function() { + \pterotype\schema\purge_all_data(); +} ); + add_action( 'generate_rewrite_rules', '\pterotype\webfinger\generate_rewrite_rules', 111 ); + add_action( 'parse_request', '\pterotype\webfinger\parse_request', 111 ); + add_filter( 'query_vars', '\pterotype\webfinger\query_vars' ); + add_filter( 'query_vars', '\pterotype\api\query_vars' ); + add_action( 'well_known_webfinger', '\pterotype\webfinger\handle' ); + add_action( 'transition_post_status', '\pterotype\posts\handle_post_status_change', 10, 3 ); + add_action( 'transition_comment_status', '\pterotype\comments\handle_transition_comment_status', 10, 3 ); + add_action( 'comment_post', '\pterotype\comments\handle_comment_post', 10, 2 ); + add_action( 'edit_comment', '\pterotype\comments\handle_edit_comment', 10, 1 ); + add_action( 'template_redirect', '\pterotype\api\handle_non_api_requests' ); + add_action( 'update_option_blogname', function() { \pterotype\identity\update_identity( PTEROTYPE_BLOG_ACTOR_SLUG ); -}); +} ); + add_action( 'update_option_blogdescription', function() { \pterotype\identity\update_identity( PTEROTYPE_BLOG_ACTOR_SLUG ); -}); +} ); + $theme = \get_option( 'stylesheet' ); add_action( "update_option_theme_mods_$theme", function() { \pterotype\identity\update_identity( PTEROTYPE_BLOG_ACTOR_SLUG ); -}); +} ); ?> diff --git a/includes/schema.php b/includes/schema.php index 26e7067..f332d68 100644 --- a/includes/schema.php +++ b/includes/schema.php @@ -1,7 +1,9 @@ set_method( 'POST' ); + $request->set_body( wp_json_encode( $delete ) ); + $request->add_header( 'Content-Type', 'application/ld+json' ); + $server->dispatch( $request ); + $pfx = $wpdb->prefix; + $wpdb->query( $wpdb->prepare( + " + DROP TABLE {$pfx}pterotype_comments, {$pfx}pterotype_keys, + {$pfx}pterotype_blocks, {$pfx}pterotype_shares, + {$pfx}pterotype_following, {$pfx}pterotype_followers, + {$pfx}pterotype_actor_likes, {$pfx}pterotype_object_likes, + {$pfx}pterotype_outbox, {$pfx}pterotype_inbox, + {$pfx}pterotype_objects, {$pfx}pterotype_actors + " + ) ); + \delete_option( 'pterotype_previously_migrated_version' ); +} ?> diff --git a/includes/server/actors.php b/includes/server/actors.php index f4e5ebc..23c073c 100644 --- a/includes/server/actors.php +++ b/includes/server/actors.php @@ -60,6 +60,9 @@ function get_commenter_actor( $row ) { '@context' => array( 'https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1', + array( + 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers', + ), ), 'type' => 'Person', 'id' => get_rest_url( @@ -111,6 +114,9 @@ function get_blog_actor() { '@context' => array( 'https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1', + array( + 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers', + ), ), 'type' => 'Organization', 'id' => get_rest_url( @@ -161,6 +167,9 @@ function get_user_actor( $user ) { '@context' => array( 'https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1', + array( + 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers', + ), ), 'type' => 'Person', 'id' => get_rest_url( null, sprintf( '/pterotype/v1/actor/%s', $handle ) ), diff --git a/includes/server/async.php b/includes/server/async.php index 3b047f7..e3108f2 100644 --- a/includes/server/async.php +++ b/includes/server/async.php @@ -17,7 +17,7 @@ class Send_Accept_Task extends \WP_Async_Task { $actor_slug = $_POST['actor_slug']; $accept = $_POST['accept']; if ( $actor_slug && $accept ) { - sleep( 2 ); + sleep( 5 ); \pterotype\outbox\handle_activity( $actor_slug, $accept ); } } diff --git a/includes/server/deliver.php b/includes/server/deliver.php index c492331..376b52e 100644 --- a/includes/server/deliver.php +++ b/includes/server/deliver.php @@ -125,6 +125,7 @@ function get_recipient_urls( $object, $depth, $acc ) { } function post_activity_to_inboxes( $actor_id, $activity, $recipients ) { + $activity = \pterotype\util\decompact_object( $activity, array( 'actor', 'object' ) ); foreach ( $recipients as $inbox ) { if ( $inbox === 'https://www.w3.org/ns/activitystreams#Public' ) { continue; @@ -150,6 +151,9 @@ function post_activity_to_inboxes( $actor_id, $activity, $recipients ) { 'data_format' => 'body', ); $response = wp_remote_post( $inbox, $args ); + $req_str = print_r( $args, true ); + $res_str = print_r( $response, true ); + \error_log( "$req_str \n\n $res_str" ); if ( is_wp_error( $response ) ) { \error_log( "[Pterotype] Error delivering to $inbox: {$response->get_error_message()}" diff --git a/includes/util.php b/includes/util.php index c424229..8c3630b 100644 --- a/includes/util.php +++ b/includes/util.php @@ -102,33 +102,30 @@ function get_id( $object ) { } } -function get_log_dir() { - return plugin_dir_path( __FILE__ ) . '../log'; +function decompact_object( $object, $fields ) { + return decompact_object_helper( $object, $fields, 0 ); } -function log( $log_file, $str, $append = true ) { - if ( ! WP_DEBUG ) { - return; +function decompact_object_helper( $object, $fields, $depth ) { + if ( $depth == 3 ) { + return $object; } - $log_dir = get_log_dir(); - $log_file = '/' . $log_file; - if ( ! file_exists( $log_dir ) ) { - mkdir( $log_dir, 0777, true ); + if ( ! is_array( $object ) ) { + return $object; } - if ( $append ) { - file_put_contents( $log_dir . $log_file, $str, FILE_APPEND ); - } else { - file_put_contents( $log_dir . $log_file, $str ); + $decompacted = $object; + foreach ( $object as $field => $value ) { + if ( ! in_array( $field, $fields ) ) { + continue; + } + if ( is_array( $value ) ) { + $decompacted[$field] = decompact_object_helper( $value, $fields, $depth + 1 ); + } else if ( filter_var( $value, FILTER_VALIDATE_URL ) ) { + $decompacted[$field] = decompact_object_helper( + dereference_object( $value ), $fields, $depth + 1 + ); + } } -} - -function log_var( $log_file, $var, $append = true ) { - if ( ! WP_DEBUG ) { - return; - } - ob_start(); - var_dump( $var ); - $dump = ob_get_clean(); - log( $log_file, $dump, $append ); + return $decompacted; } ?> diff --git a/pterotype.php b/pterotype.php index 358fe4b..9b41511 100644 --- a/pterotype.php +++ b/pterotype.php @@ -26,6 +26,11 @@ function pterotype_deactivate() { flush_rewrite_rules(); } +function pterotype_uninstall() { + do_action( 'pterotype_uninstall' ); + flush_rewrite_rules(); +} + function pterotype_load() { do_action( 'pterotype_load' ); } @@ -33,4 +38,5 @@ function pterotype_load() { add_action( 'setup_theme', 'pterotype_load' ); register_activation_hook( __FILE__, 'pterotype_init' ); register_deactivation_hook( __FILE__, 'pterotype_deactivate' ); +register_uninstall_hook( __FILE__, 'pterotype_uninstall' ); ?>