From cec4ed2e3f25913f36bb9043ef63a9fefe6c376a Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Thu, 4 May 2023 15:17:05 +0200 Subject: [PATCH] init follower update scheduler --- activitypub.php | 127 ++++++++++++++++----------------- includes/class-activitypub.php | 58 +++++++++++++++ 2 files changed, 120 insertions(+), 65 deletions(-) diff --git a/activitypub.php b/activitypub.php index d61af85..1e0d44b 100644 --- a/activitypub.php +++ b/activitypub.php @@ -29,67 +29,66 @@ function init() { \define( 'ACTIVITYPUB_PLUGIN_BASENAME', plugin_basename( __FILE__ ) ); \define( 'ACTIVITYPUB_PLUGIN_FILE', plugin_dir_path( __FILE__ ) . '/' . basename( __FILE__ ) ); - require_once \dirname( __FILE__ ) . '/includes/table/followers-list.php'; - require_once \dirname( __FILE__ ) . '/includes/class-signature.php'; - require_once \dirname( __FILE__ ) . '/includes/class-webfinger.php'; - require_once \dirname( __FILE__ ) . '/includes/peer/class-followers.php'; - require_once \dirname( __FILE__ ) . '/includes/functions.php'; + \define( 'ACTIVITYPUB_OBJECT', 'ACTIVITYPUB_OBJECT' ); - require_once \dirname( __FILE__ ) . '/includes/model/class-activity.php'; - require_once \dirname( __FILE__ ) . '/includes/model/class-post.php'; - - require_once \dirname( __FILE__ ) . '/includes/class-activity-dispatcher.php'; + Migration::init(); Activity_Dispatcher::init(); - - require_once \dirname( __FILE__ ) . '/includes/class-activitypub.php'; Activitypub::init(); + Collection\Followers::init(); // Configure the REST API route - require_once \dirname( __FILE__ ) . '/includes/rest/class-outbox.php'; Rest\Outbox::init(); - - require_once \dirname( __FILE__ ) . '/includes/rest/class-inbox.php'; Rest\Inbox::init(); - - require_once \dirname( __FILE__ ) . '/includes/rest/class-followers.php'; Rest\Followers::init(); - - require_once \dirname( __FILE__ ) . '/includes/rest/class-following.php'; Rest\Following::init(); - - require_once \dirname( __FILE__ ) . '/includes/rest/class-webfinger.php'; Rest\Webfinger::init(); - // load NodeInfo endpoints only if blog is public - if ( true === (bool) \get_option( 'blog_public', 1 ) ) { - require_once \dirname( __FILE__ ) . '/includes/rest/class-nodeinfo.php'; - Rest\NodeInfo::init(); - } - - require_once \dirname( __FILE__ ) . '/includes/class-admin.php'; Admin::init(); - - require_once \dirname( __FILE__ ) . '/includes/class-hashtag.php'; Hashtag::init(); - - require_once \dirname( __FILE__ ) . '/includes/class-shortcodes.php'; Shortcodes::init(); - - require_once \dirname( __FILE__ ) . '/includes/class-mention.php'; Mention::init(); - - require_once \dirname( __FILE__ ) . '/includes/class-debug.php'; Debug::init(); - - require_once \dirname( __FILE__ ) . '/includes/class-health-check.php'; Health_Check::init(); - - if ( \WP_DEBUG ) { - require_once \dirname( __FILE__ ) . '/includes/debug.php'; - } } \add_action( 'plugins_loaded', '\Activitypub\init' ); +spl_autoload_register( + function ( $class ) { + $base_dir = trailingslashit( __DIR__ ) . 'includes' . DIRECTORY_SEPARATOR; + $base = 'activitypub'; + + $class = strtolower( $class ); + + if ( strncmp( $class, $base, strlen( $base ) ) === 0 ) { + $class = str_replace( 'activitypub\\', '', $class ); + + if ( strpos( $class, '\\' ) ) { + list( $sub_dir, $class ) = explode( '\\', $class ); + $base_dir = $base_dir . $sub_dir . DIRECTORY_SEPARATOR; + } + + $filename = 'class-' . str_replace( '_', '-', $class ); + $file = $base_dir . $filename . '.php'; + + if ( file_exists( $file ) ) { + require_once $file; + } + } + } +); + +require_once \dirname( __FILE__ ) . '/includes/functions.php'; + +// load NodeInfo endpoints only if blog is public +if ( true === (bool) \get_option( 'blog_public', 1 ) ) { + require_once \dirname( __FILE__ ) . '/includes/rest/class-nodeinfo.php'; + Rest\NodeInfo::init(); +} + +if ( \WP_DEBUG ) { + require_once \dirname( __FILE__ ) . '/includes/debug.php'; +} + /** * Add plugin settings link */ @@ -102,34 +101,32 @@ function plugin_settings_link( $actions ) { return \array_merge( $settings_link, $actions ); } -\add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), '\Activitypub\plugin_settings_link' ); +\add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), __NAMESPACE__ . '\plugin_settings_link' ); -/** - * Add rewrite rules - */ -function add_rewrite_rules() { - if ( ! \class_exists( 'Webfinger' ) ) { - \add_rewrite_rule( '^.well-known/webfinger', 'index.php?rest_route=/activitypub/1.0/webfinger', 'top' ); - } +\register_activation_hook( + __FILE__, + array( + __NAMESPACE__ . '\Activitypub', + 'activate', + ) +); - if ( ! \class_exists( 'Nodeinfo' ) || ! (bool) \get_option( 'blog_public', 1 ) ) { - \add_rewrite_rule( '^.well-known/nodeinfo', 'index.php?rest_route=/activitypub/1.0/nodeinfo/discovery', 'top' ); - \add_rewrite_rule( '^.well-known/x-nodeinfo2', 'index.php?rest_route=/activitypub/1.0/nodeinfo2', 'top' ); - } +\register_deactivation_hook( + __FILE__, + array( + __NAMESPACE__ . '\Activitypub', + 'deactivate', + ) +); - \add_rewrite_endpoint( 'activitypub', EP_AUTHORS | EP_PERMALINK | EP_PAGES ); -} -\add_action( 'init', '\Activitypub\add_rewrite_rules', 1 ); +register_uninstall_hook( + __FILE__, + array( + __NAMESPACE__ . '\Activitypub', + 'uninstall', + ) +); -/** - * Flush rewrite rules; - */ -function flush_rewrite_rules() { - \Activitypub\add_rewrite_rules(); - \flush_rewrite_rules(); -} -\register_activation_hook( __FILE__, '\Activitypub\flush_rewrite_rules' ); -\register_deactivation_hook( __FILE__, '\flush_rewrite_rules' ); /** * Only load code that needs BuddyPress to run once BP is loaded and initialized. diff --git a/includes/class-activitypub.php b/includes/class-activitypub.php index 16a1f22..fd5aa1e 100644 --- a/includes/class-activitypub.php +++ b/includes/class-activitypub.php @@ -25,6 +25,40 @@ class Activitypub { \add_action( 'transition_post_status', array( self::class, 'schedule_post_activity' ), 33, 3 ); \add_action( 'wp_trash_post', array( self::class, 'trash_post' ), 1 ); \add_action( 'untrash_post', array( self::class, 'untrash_post' ), 1 ); + + \add_action( 'init', array( self::class, 'add_rewrite_rules' ) ); + } + + /** + * Activation Hook + * + * @return void + */ + public static function activate() { + self::flush_rewrite_rules(); + + if ( ! \wp_next_scheduled( 'activitypub_update_followers' ) ) { + \wp_schedule_event( time(), 'hourly', 'activitypub_update_followers' ); + } + } + + /** + * Deactivation Hook + * + * @return void + */ + public static function deactivate() { + self::flush_rewrite_rules(); + + wp_unschedule_hook( 'activitypub_update_followers' ); + } + + /** + * Uninstall Hook + * + * @return void + */ + public static function uninstall() { } /** @@ -204,4 +238,28 @@ class Activitypub { public static function untrash_post( $post_id ) { \delete_post_meta( $post_id, 'activitypub_canonical_url' ); } + + /** + * Add rewrite rules + */ + public static function add_rewrite_rules() { + if ( ! \class_exists( 'Webfinger' ) ) { + \add_rewrite_rule( '^.well-known/webfinger', 'index.php?rest_route=/activitypub/1.0/webfinger', 'top' ); + } + + if ( ! \class_exists( 'Nodeinfo' ) || (bool) \get_option( 'blog_public', 1 ) ) { + \add_rewrite_rule( '^.well-known/nodeinfo', 'index.php?rest_route=/activitypub/1.0/nodeinfo/discovery', 'top' ); + \add_rewrite_rule( '^.well-known/x-nodeinfo2', 'index.php?rest_route=/activitypub/1.0/nodeinfo2', 'top' ); + } + + \add_rewrite_endpoint( 'activitypub', EP_AUTHORS | EP_PERMALINK | EP_PAGES ); + } + + /** + * Flush rewrite rules; + */ + public static function flush_rewrite_rules() { + self::add_rewrite_rules(); + \flush_rewrite_rules(); + } }