Skip to content

Commit

Permalink
Merge pull request #88 from akirk/get-the-current-app
Browse files Browse the repository at this point in the history
Add ability to get the current Mastodon App
  • Loading branch information
akirk authored Mar 16, 2024
2 parents 2a619d2 + 4f4bbeb commit c9fbb3b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
37 changes: 22 additions & 15 deletions includes/class-mastodon-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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();
}
Expand Down Expand Up @@ -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();
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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' );
}
Expand Down Expand Up @@ -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.
Expand All @@ -2949,17 +2956,17 @@ public function api_announcements() {

$content[] = sprintf(
// Translators: %s is the post formats.
_n( 'Posts with the post format <strong>%s</strong> will appear in this app.', 'Posts with the post formats <strong>%s</strong> 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 <strong>%s</strong> will appear in this app.', 'Posts with the post formats <strong>%s</strong> 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 <strong>%s</strong> 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 <strong>standard</strong> post format in the Enable Mastodon Apps settings.', 'enable-mastodon-apps' );
Expand All @@ -2968,7 +2975,7 @@ public function api_announcements() {
$ret[] = array(
'id' => 1,
'content' => '<h1><strong>' . __( 'Mastodon Apps', 'enable-mastodon-apps' ) . '</strong></h1><p>' . implode( '</p><p>' . PHP_EOL, $content ) . '</p>',
'published_at' => $this->app->get_creation_date(),
'published_at' => $app->get_creation_date(),
'updated_at' => time(),
'starts_at' => null,
'ends_at' => null,
Expand Down
12 changes: 12 additions & 0 deletions includes/class-mastodon-app.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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'] ) {
Expand Down

0 comments on commit c9fbb3b

Please sign in to comment.