diff --git a/includes/class-mastodon-api.php b/includes/class-mastodon-api.php index 649168fb..a8de4d39 100644 --- a/includes/class-mastodon-api.php +++ b/includes/class-mastodon-api.php @@ -767,9 +767,8 @@ public function public_api_permission( $request ) { } return true; } - $this->app = Mastodon_App::get_by_client_id( $token['client_id'] ); - $this->app->was_used( $request ); wp_set_current_user( $token['user_id'] ); + Mastodon_App::set_current_app( $token['client_id'], $request ); return is_user_logged_in(); } @@ -790,7 +789,7 @@ public function log_404s( $template ) { if ( ! empty( $_SERVER['HTTP_AUTHORIZATION'] ) ) { $this->oauth->get_token(); } - $app = $this->app; + $app = Mastodon_App::get_current_app(); if ( ! $app ) { $app = Mastodon_App::get_debug_app(); } @@ -852,9 +851,8 @@ public function logged_in_permission( $request ) { } OAuth2\Access_Token_Storage::was_used( $token['access_token'] ); - $this->app = Mastodon_App::get_by_client_id( $token['client_id'] ); wp_set_current_user( $token['user_id'] ); - $this->app->was_used( $request ); + Mastodon_App::set_current_app( $token['client_id'], $request ); return is_user_logged_in(); } @@ -865,8 +863,7 @@ public function have_token_permission( $request ) { return is_user_logged_in(); } OAuth2\Access_Token_Storage::was_used( $token['access_token'] ); - $this->app = Mastodon_App::get_by_client_id( $token['client_id'] ); - $this->app->was_used( $request ); + Mastodon_App::set_current_app( $token['client_id'], $request ); return true; } @@ -942,8 +939,9 @@ private function get_posts_query_args( $request ) { } } - if ( $this->app ) { - $args = $this->app->modify_wp_query_args( $args ); + $app = Mastodon_App::get_current_app(); + if ( $app ) { + $args = $app->modify_wp_query_args( $args ); } else { $args['tax_query'] = array( array( @@ -1415,7 +1413,11 @@ public function api_submit_post( $request ) { $post_data['post_type'] = 'post'; $post_data['post_title'] = ''; - $app_post_formats = $this->app->get_post_formats(); + $app = Mastodon_App::get_current_app(); + $app_post_formats = array(); + if ( $app ) { + $app_post_formats = $app->get_post_formats(); + } if ( empty( $app_post_formats ) ) { $app_post_formats = array( 'status' ); } @@ -2939,6 +2941,11 @@ public function api_nodeinfo() { public function api_announcements() { $ret = array(); + $app = Mastodon_App::get_current_app(); + if ( ! $app ) { + return $ret; + } + $content = array(); $content[] = sprintf( // Translators: %1$s is a URL, %2$s is the domain of your blog. @@ -2949,17 +2956,17 @@ public function api_announcements() { $content[] = sprintf( // Translators: %s is the post formats. - _n( 'Posts with the post format %s will appear in this app.', 'Posts with the post formats %s will appear in this app.', count( $this->app->get_post_formats() ), 'enable-mastodon-apps' ), - implode( ', ', $this->app->get_post_formats() ) + _n( 'Posts with the post format %s will appear in this app.', 'Posts with the post formats %s will appear in this app.', count( $app->get_post_formats() ), 'enable-mastodon-apps' ), + implode( ', ', $app->get_post_formats() ) ); $content[] = sprintf( // Translators: %s is the post format. __( 'If you create a new note in this app, it will be created with the %s post format.', 'enable-mastodon-apps' ), - reset( $this->app->get_post_formats() ) + reset( $app->get_post_formats() ) ); - if ( 'standard' === reset( $this->app->get_post_formats() ) ) { + if ( 'standard' === reset( $app->get_post_formats() ) ) { $content[] = __( 'If you want to create a post in WordPress with a title, add a new line after the title. The first line will then appear as the title of the post.', 'enable-mastodon-apps' ); } else { $content[] = __( 'Because a new post is not created in the standard post format, it will be published without title. To change this, select the standard post format in the Enable Mastodon Apps settings.', 'enable-mastodon-apps' ); @@ -2968,7 +2975,7 @@ public function api_announcements() { $ret[] = array( 'id' => 1, 'content' => '

' . __( 'Mastodon Apps', 'enable-mastodon-apps' ) . '

' . implode( '

' . PHP_EOL, $content ) . '

', - 'published_at' => $this->app->get_creation_date(), + 'published_at' => $app->get_creation_date(), 'updated_at' => time(), 'starts_at' => null, 'ends_at' => null, diff --git a/includes/class-mastodon-app.php b/includes/class-mastodon-app.php index 7fe4c93e..fb6a2306 100644 --- a/includes/class-mastodon-app.php +++ b/includes/class-mastodon-app.php @@ -25,6 +25,8 @@ class Mastodon_App { */ private $term; + private static $current_app = null; + const DEBUG_CLIENT_ID = 'enable-mastodon-apps'; const TAXONOMY = 'mastodon-app'; const VALID_SCOPES = array( @@ -169,6 +171,16 @@ public function was_used( $request, $additional_debug_data = array() ) { update_term_meta( $this->term->term_id, 'last_used', time() ); } + public static function set_current_app( $client_id, $request ) { + self::$current_app = Mastodon_App::get_by_client_id( $client_id ); + self::$current_app->was_used( $request ); + return self::$current_app; + } + + public static function get_current_app() { + return self::$current_app; + } + public function is_outdated() { foreach ( OAuth2\Access_Token_Storage::getAll() as $token ) { if ( $token['client_id'] === $this->get_client_id() && ! $token['expired'] ) {