Skip to content
Open
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
56 changes: 56 additions & 0 deletions includes/class-mastodon-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,24 @@ public function process_admin() {
delete_option( 'mastodon_api_debug_mode' );
}

if ( isset( $_POST['mastodon_api_announcements_group'] ) ) {
$announcement_types = array(
'page' => 'page',
'post' => 'post',
'category' => 'category',
'tag' => 'tag',
);
if ( $announcement_types[ $_POST['mastodon_api_announcements_group'] ] === $_POST['mastodon_api_announcements_group'] ) {
update_option( 'mastodon_api_announcement_type', $announcement_types[ $_POST['mastodon_api_announcements_group'] ] );
} else {
update_option( 'mastodon_api_announcement_type', 'default' );
}
}

if ( isset( $_POST['mastodon_api_announcements_id'] ) ) {
update_option( 'mastodon_api_announcement_id', sanitize_text_field( $_POST['mastodon_api_announcements_id'] ) );
}

if ( isset( $_POST['mastodon_api_default_post_formats'] ) && is_array( $_POST['mastodon_api_default_post_formats'] ) ) {
$default_post_formats = array_filter(
$_POST['mastodon_api_default_post_formats'],
Expand Down Expand Up @@ -327,6 +345,13 @@ public function admin_page() {
$activitypub_installed = isset( $plugins['activitypub/activitypub.php'] );
$friends_installed = isset( $plugins['friends/friends.php'] );

$announcements_type = get_option( 'mastodon_api_announcement_type', 'default' );
$announcements_id = get_option( 'mastodon_api_announcement_id', false );

if ( false === $announcements_type ) {
$announcements_type = 'default';
}

function output_request_log( $request, $rest_nonce ) {
$date = \DateTimeImmutable::createFromFormat( 'U.u', $request['timestamp'] );
$url = add_query_arg(
Expand Down Expand Up @@ -523,6 +548,37 @@ function td_timestamp( $timestamp, $strikethrough_past = false ) {
</details>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e( 'Announcements Type', 'enable-mastodon-apps' ); ?></th>
<td>
<fieldset>
<input type="radio" value="default" name="mastodon_api_announcements_group" id="mastodon_api_annoucments_default"<?php checked( $announcements_type, 'default' ); ?>>
<label for="mastodon_api_announcements_group"><?php esc_html_e( 'Default', 'enable-mastodon-apps' ); ?></label><br>
<input type="radio" value="post" name="mastodon_api_announcements_group" id="mastodon_api_annoucments_post"<?php checked( $announcements_type, 'post' ); ?>>
<label for="mastodon_api_announcements_group"><?php esc_html_e( 'Single Post', 'enable-mastodon-apps' ); ?></label><br>
<input type="radio" value="page" name="mastodon_api_announcements_group" id="mastodon_api_annoucments_page"<?php checked( $announcements_type, 'page' ); ?>>
<label for="mastodon_api_announcements_group"><?php esc_html_e( 'Single Page', 'enable-mastodon-apps' ); ?></label><br>
<input type="radio" value="category" name="mastodon_api_announcements_group" id="mastodon_api_annoucments_category"<?php checked( $announcements_type, 'category' ); ?>>
<label for="mastodon_api_announcements_group"><?php esc_html_e( 'All posts in a category', 'enable-mastodon-apps' ); ?></label><br>
<input type="radio" value="tag" name="mastodon_api_announcements_group" id="mastodon_api_annoucments_tag"<?php checked( $announcements_type, 'tag' ); ?>>
<label for="mastodon_api_announcements_group"><?php esc_html_e( 'All posts with a tag', 'enable-mastodon-apps' ); ?></label><br>
</fieldset>
<p class="description">
<?php esc_html_e( 'Select one of the above to use for the Mastodon annoucments tab.', 'enable-mastodon-apps' ); ?>
</p>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e( 'Announcements ID', 'enable-mastodon-apps' ); ?></th>
<td>
<fieldset>
<input type="text" id="mastodon_api_announcements_id" name="mastodon_api_announcements_id" value="<?php echo sanitize_text_field( $announcements_id ); ?>">
</fieldset>
<p class="description">
<?php esc_html_e( 'Enter the post id, page id, category slug, or tag slug to use for annoucments as selected above.', 'enable-mastodon-apps' ); ?>
</p>
</td>
</tr>
</tbody>
</table>

Expand Down
59 changes: 59 additions & 0 deletions includes/class-mastodon-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,10 @@ private function get_posts( $args, $min_id = null, $max_id = null ) {

$ret = array();
$c = $args['posts_per_page'];
if ( -1 === $c ) {
// -1 means to return all statuses on a single page.
$c = count( $statuses );
}
$next_max_id = false;
foreach ( $statuses as $status ) {
if ( false === $next_max_id ) {
Expand Down Expand Up @@ -1082,6 +1086,7 @@ function ( $user_id ) {
array(
'id' => strval( $post->ID ),
'created_at' => mysql2date( 'Y-m-d\TH:i:s.000P', $post->post_date, false ),
'published_at' => mysql2date( 'Y-m-d\TH:i:s.000P', $post->post_date, false ),
'in_reply_to_id' => null,
'in_reply_to_account_id' => null,
'sensitive' => false,
Expand Down Expand Up @@ -2807,8 +2812,62 @@ public function api_nodeinfo() {
}

public function api_announcements() {
$type = get_option( 'mastodon_api_announcement_type', 'default' );
$ret = array();

if ( false !== $type ) {
$term = get_option( 'mastodon_api_announcement_id', false );

switch ( $type ) {
case 'category':
$args['cat'] = get_cat_ID( $term );
$args['post_type'] = 'post';
$args['posts_per_page'] = -1;

$ret = $this->get_posts( $args );

if ( array() !== $ret ) {
return $ret;
}

break;
case 'tag':
$tag = get_term_by( 'slug', $term, 'post_tag' );
if ( false === $tag ) {
break;
}

$args['tag'] = $tag->ID;
$args['post_type'] = 'post';
$args['posts_per_page'] = -1;

$ret = $this->get_posts( $args );

if ( array() !== $ret ) {
return $ret;
}

break;
case 'page':
$page = get_post( intval( $term ) );
$ret[] = $this->get_status_array( $page );

if ( array() !== $ret ) {
return $ret;
}

break;
case 'post':
$post = get_post( intval( $term ) );
$ret[] = $this->get_status_array( $post );

if ( array() !== $ret ) {
return $ret;
}

break;
}
}
$content = array();
$content[] = sprintf(
// Translators: %1$s is a URL, %2$s is the domain of your blog.
Expand Down