Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update from branch #101

Merged
merged 2 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading