Skip to content

Commit a743140

Browse files
committed
Add options for announcements
Use a page, post, category, or tag for announcements.
1 parent 3382371 commit a743140

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

includes/class-mastodon-admin.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,24 @@ public function process_admin() {
263263
delete_option( 'mastodon_api_debug_mode' );
264264
}
265265

266+
if ( isset( $_POST['mastodon_api_announcements_group'] ) ) {
267+
$announcement_types = array(
268+
'page' => 'page',
269+
'post' => 'post',
270+
'category' => 'category',
271+
'tag' => 'tag',
272+
);
273+
if ( $announcement_types[ $_POST['mastodon_api_announcements_group'] ] === $_POST['mastodon_api_announcements_group'] ) {
274+
update_option( 'mastodon_api_announcement_type', $announcement_types[ $_POST['mastodon_api_announcements_group'] ] );
275+
} else {
276+
update_option( 'mastodon_api_announcement_type', 'default' );
277+
}
278+
}
279+
280+
if ( isset( $_POST['mastodon_api_announcements_id'] ) ) {
281+
update_option( 'mastodon_api_announcement_id', sanitize_text_field( $_POST['mastodon_api_announcements_id'] ) );
282+
}
283+
266284
if ( isset( $_POST['mastodon_api_default_post_formats'] ) && is_array( $_POST['mastodon_api_default_post_formats'] ) ) {
267285
$default_post_formats = array_filter(
268286
$_POST['mastodon_api_default_post_formats'],
@@ -327,6 +345,13 @@ public function admin_page() {
327345
$activitypub_installed = isset( $plugins['activitypub/activitypub.php'] );
328346
$friends_installed = isset( $plugins['friends/friends.php'] );
329347

348+
$announcements_type = get_option( 'mastodon_api_announcement_type', 'default' );
349+
$announcements_id = get_option( 'mastodon_api_announcement_id', false );
350+
351+
if ( false === $announcements_type ) {
352+
$announcements_type = 'default';
353+
}
354+
330355
function output_request_log( $request, $rest_nonce ) {
331356
$date = \DateTimeImmutable::createFromFormat( 'U.u', $request['timestamp'] );
332357
$url = add_query_arg(
@@ -523,6 +548,37 @@ function td_timestamp( $timestamp, $strikethrough_past = false ) {
523548
</details>
524549
</td>
525550
</tr>
551+
<tr>
552+
<th scope="row"><?php esc_html_e( 'Announcements Type', 'enable-mastodon-apps' ); ?></th>
553+
<td>
554+
<fieldset>
555+
<input type="radio" value="default" name="mastodon_api_announcements_group" id="mastodon_api_annoucments_default"<?php checked( $announcements_type, 'default' ); ?>>
556+
<label for="mastodon_api_announcements_group"><?php esc_html_e( 'Default', 'enable-mastodon-apps' ); ?></label><br>
557+
<input type="radio" value="post" name="mastodon_api_announcements_group" id="mastodon_api_annoucments_post"<?php checked( $announcements_type, 'post' ); ?>>
558+
<label for="mastodon_api_announcements_group"><?php esc_html_e( 'Single Post', 'enable-mastodon-apps' ); ?></label><br>
559+
<input type="radio" value="page" name="mastodon_api_announcements_group" id="mastodon_api_annoucments_page"<?php checked( $announcements_type, 'page' ); ?>>
560+
<label for="mastodon_api_announcements_group"><?php esc_html_e( 'Single Page', 'enable-mastodon-apps' ); ?></label><br>
561+
<input type="radio" value="category" name="mastodon_api_announcements_group" id="mastodon_api_annoucments_category"<?php checked( $announcements_type, 'category' ); ?>>
562+
<label for="mastodon_api_announcements_group"><?php esc_html_e( 'All posts in a category', 'enable-mastodon-apps' ); ?></label><br>
563+
<input type="radio" value="tag" name="mastodon_api_announcements_group" id="mastodon_api_annoucments_tag"<?php checked( $announcements_type, 'tag' ); ?>>
564+
<label for="mastodon_api_announcements_group"><?php esc_html_e( 'All posts with a tag', 'enable-mastodon-apps' ); ?></label><br>
565+
</fieldset>
566+
<p class="description">
567+
<?php esc_html_e( 'Select one of the above to use for the Mastodon annoucments tab.', 'enable-mastodon-apps' ); ?>
568+
</p>
569+
</td>
570+
</tr>
571+
<tr>
572+
<th scope="row"><?php esc_html_e( 'Announcements ID', 'enable-mastodon-apps' ); ?></th>
573+
<td>
574+
<fieldset>
575+
<input type="text" id="mastodon_api_announcements_id" name="mastodon_api_announcements_id" value="<?php echo sanitize_text_field( $announcements_id ); ?>">
576+
</fieldset>
577+
<p class="description">
578+
<?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' ); ?>
579+
</p>
580+
</td>
581+
</tr>
526582
</tbody>
527583
</table>
528584

includes/class-mastodon-api.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,10 @@ private function get_posts( $args, $min_id = null, $max_id = null ) {
945945

946946
$ret = array();
947947
$c = $args['posts_per_page'];
948+
if ( -1 === $c ) {
949+
// -1 means to return all statuses on a single page.
950+
$c = count( $statuses );
951+
}
948952
$next_max_id = false;
949953
foreach ( $statuses as $status ) {
950954
if ( false === $next_max_id ) {
@@ -1082,6 +1086,7 @@ function ( $user_id ) {
10821086
array(
10831087
'id' => strval( $post->ID ),
10841088
'created_at' => mysql2date( 'Y-m-d\TH:i:s.000P', $post->post_date, false ),
1089+
'published_at' => mysql2date( 'Y-m-d\TH:i:s.000P', $post->post_date, false ),
10851090
'in_reply_to_id' => null,
10861091
'in_reply_to_account_id' => null,
10871092
'sensitive' => false,
@@ -2807,8 +2812,62 @@ public function api_nodeinfo() {
28072812
}
28082813

28092814
public function api_announcements() {
2815+
$type = get_option( 'mastodon_api_announcement_type', 'default' );
28102816
$ret = array();
28112817

2818+
if ( false !== $type ) {
2819+
$term = get_option( 'mastodon_api_announcement_id', false );
2820+
2821+
switch ( $type ) {
2822+
case 'category':
2823+
$args['cat'] = get_cat_ID( $term );
2824+
$args['post_type'] = 'post';
2825+
$args['posts_per_page'] = -1;
2826+
2827+
$ret = $this->get_posts( $args );
2828+
2829+
if ( array() !== $ret ) {
2830+
return $ret;
2831+
}
2832+
2833+
break;
2834+
case 'tag':
2835+
$tag = get_term_by( 'slug', $term, 'post_tag' );
2836+
if ( false === $tag ) {
2837+
break;
2838+
}
2839+
2840+
$args['tag'] = $tag->ID;
2841+
$args['post_type'] = 'post';
2842+
$args['posts_per_page'] = -1;
2843+
2844+
$ret = $this->get_posts( $args );
2845+
2846+
if ( array() !== $ret ) {
2847+
return $ret;
2848+
}
2849+
2850+
break;
2851+
case 'page':
2852+
$page = get_post( intval( $term ) );
2853+
$ret[] = $this->get_status_array( $page );
2854+
2855+
if ( array() !== $ret ) {
2856+
return $ret;
2857+
}
2858+
2859+
break;
2860+
case 'post':
2861+
$post = get_post( intval( $term ) );
2862+
$ret[] = $this->get_status_array( $post );
2863+
2864+
if ( array() !== $ret ) {
2865+
return $ret;
2866+
}
2867+
2868+
break;
2869+
}
2870+
}
28122871
$content = array();
28132872
$content[] = sprintf(
28142873
// Translators: %1$s is a URL, %2$s is the domain of your blog.

0 commit comments

Comments
 (0)