Skip to content

Commit

Permalink
Merge pull request #204 from akirk/announce-cpt
Browse files Browse the repository at this point in the history
  • Loading branch information
akirk authored Feb 19, 2025
2 parents d643cc7 + 85cfda3 commit e1d704a
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 19 deletions.
Binary file added assets/banner-1544x500.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/banner-772x250.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/banner.pxd
Binary file not shown.
Binary file added assets/logo-128x128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo-256x256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo-512x512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/logo.pxd
Binary file not shown.
25 changes: 24 additions & 1 deletion includes/class-mastodon-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,10 @@ function ( $post_type ) {
}

if ( isset( $_POST['view_post_types'] ) && is_array( $_POST['view_post_types'] ) ) {
$view_post_types = array();
$view_post_types = array(
Mastodon_API::ANNOUNCE_CPT => true,
Mastodon_API::POST_CPT => true,
);
foreach ( wp_unslash( $_POST['view_post_types'] ) as $post_type ) {
if ( isset( $post_types[ $post_type ] ) ) {
$view_post_types[ $post_type ] = true;
Expand All @@ -681,6 +684,26 @@ function ( $post_type ) {
}
$app->set_view_post_types( array_keys( $view_post_types ) );
}

wp_insert_post(
array(
'post_type' => Mastodon_API::ANNOUNCE_CPT,
'post_title' => __( 'Only Visible to You', 'enable-mastodon-apps' ),
'post_content' =>
sprintf(
// translators: %s: app name.
__( 'The settings for %s were changed as follows:', 'enable-mastodon-apps' ),
$app->get_client_name()
) . PHP_EOL .
'Post Formats: ' . implode( ', ', $post_formats ) . PHP_EOL .
'Create Post Type: ' . $create_post_type . PHP_EOL .
'Post Types: ' . implode( ', ', array_keys( $view_post_types ) ),
'post_status' => 'publish',
'meta_input' => array(
'ema_app_id' => $app->get_client_id(),
),
)
);
}

public function admin_app_page( Mastodon_App $app ) {
Expand Down
20 changes: 17 additions & 3 deletions includes/class-mastodon-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Mastodon_API {
const REMAP_TAXONOMY = 'mastodon-api-remap';
const CPT = 'enable-mastodon-apps';
const POST_CPT = 'ema-post';
const ANNOUNCE_CPT = 'ema-announce';

/**
* Constructor
Expand Down Expand Up @@ -182,7 +183,6 @@ public function register_custom_post_types() {
'show_in_rest' => false,
'rewrite' => false,
);

register_post_type( self::CPT, $args );

$args = array(
Expand All @@ -198,8 +198,22 @@ public function register_custom_post_types() {
'menu_icon' => 'dashicons-megaphone',
'supports' => array( 'post-formats', 'comments', 'revisions', 'author' ),
);

register_post_type( self::POST_CPT, $args );

$args = array(
'labels' => array(
'name' => __( 'EMA Announcements', 'enable-mastodon-apps' ),
'singular_name' => __( 'EMA Announcement', 'enable-mastodon-apps' ),
'menu_name' => __( 'EMA Announcements', 'enable-mastodon-apps' ),
),
'description' => __( 'Announcement by the Enable Mastodon Apps plugin.', 'enable-mastodon-apps' ),
'public' => false,
'show_in_rest' => false,
'rewrite' => false,
'menu_icon' => 'dashicons-megaphone',
'supports' => array( 'post-formats' ),
);
register_post_type( self::ANNOUNCE_CPT, $args );
}

public function activitypub_support_post_types( $post_types ) {
Expand Down Expand Up @@ -2453,7 +2467,7 @@ public function api_get_post( $request ) {
*/
$status = apply_filters( 'mastodon_api_status', null, $post_id, array() );

if ( $status->id !== $request->get_param( 'post_id' ) && isset( $status->reblog ) && $status->reblog->id === $request->get_param( 'post_id' ) ) {
if ( $status && $status->id !== $request->get_param( 'post_id' ) && isset( $status->reblog ) && $status->reblog->id === $request->get_param( 'post_id' ) ) {
$status = $status->reblog;
}

Expand Down
10 changes: 10 additions & 0 deletions includes/class-mastodon-app.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,19 @@ public function get_view_post_types() {
if ( ! $view_post_types ) {
$view_post_types = 'post';
}

if ( ! is_array( $view_post_types ) ) {
$view_post_types = array( $view_post_types );
}

if ( ! in_array( Mastodon_API::ANNOUNCE_CPT, $view_post_types, true ) ) {
$view_post_types[] = Mastodon_API::ANNOUNCE_CPT;
}

if ( ! in_array( Mastodon_API::POST_CPT, $view_post_types, true ) ) {
$view_post_types[] = Mastodon_API::POST_CPT;
}

return $view_post_types;
}

Expand Down
31 changes: 31 additions & 0 deletions includes/handler/class-account.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,40 @@ public function __construct() {

public function register_hooks() {
add_filter( 'mastodon_api_account', array( $this, 'api_account' ), 10, 2 );
add_filter( 'mastodon_api_account', array( $this, 'api_account_ema' ), 10, 4 );
add_filter( 'mastodon_api_account', array( $this, 'api_account_ensure_numeric_id' ), 100, 2 );
}

public function api_account_ema( $account, $user_id, $request = null, $post = null ) {
if ( is_null( $post ) || ! is_object( $post ) || \Enable_Mastodon_Apps\Mastodon_API::ANNOUNCE_CPT !== $post->post_type ) {
return $account;
}
$user_id = \Enable_Mastodon_Apps\Mastodon_API::remap_user_id( -30 );
$ema_php = ENABLE_MASTODON_APPS_PLUGIN_DIR . '/enable-mastodon-apps.php';
$account = new Account_Entity();
$account->id = strval( $user_id );
$account->username = 'Enable Mastodon Apps';
$account->display_name = 'Enable Mastodon Apps';
$account->avatar = plugin_dir_url( $ema_php ) . 'logo-256x256.png';
$account->avatar_static = plugin_dir_url( $ema_php ) . 'logo-256x256.png';
$account->acct = '[email protected]';
$account->note = 'Enable Mastodon Apps plugin';
$account->created_at = new \DateTime( '@' . filemtime( $ema_php ) );
$account->statuses_count = 0;
$account->last_status_at = new \DateTime( $post->post_date_gmt );
$account->url = 'https://wordpress.org/plugins/enable-mastodon-apps/';

$account->source = array(
'privacy' => 'public',
'sensitive' => false,
'language' => 'en',
'note' => 'Enable Mastodon Apps plugin',
'fields' => array(),
);

return $account;
}

public function api_account( $user_data, $user_id ) {
if ( $user_data instanceof Account_Entity ) {
return $user_data;
Expand Down
16 changes: 15 additions & 1 deletion includes/handler/class-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,19 @@ public function api_status( ?Status_Entity $status, int $object_id, array $data
if ( $status instanceof Status_Entity ) {
return $status;
}

$post = get_post( $object_id );

if ( Mastodon_API::ANNOUNCE_CPT === $post->post_type ) {
$meta = get_post_meta( $post->ID, 'ema_app_id', true );
if ( $meta ) {
$app = Mastodon_App::get_current_app();
if ( ! $app || $app->get_client_id() !== $meta ) {
return null;
}
}
}

if ( isset( $data['comment'] ) && $data['comment'] instanceof \WP_Comment ) {
$comment = $data['comment'];
$account = apply_filters( 'mastodon_api_account', null, $comment->user_id, null, $comment );
Expand Down Expand Up @@ -159,7 +170,10 @@ public function api_status( ?Status_Entity $status, int $object_id, array $data
$status->created_at = new \DateTime( $post->post_date_gmt, new \DateTimeZone( 'UTC' ) );
$status->visibility = 'public';
$status->uri = get_permalink( $post->ID );
$status->content = $post->post_title . PHP_EOL . $post->post_content;
$status->content = $post->post_content;
if ( ! empty( $post->post_title ) && trim( $post->post_title ) ) {
$status->content = '<strong>' . esc_html( $post->post_title ) . '</strong>' . PHP_EOL . $status->content;
}
$status->account = $account;
$media_attachments = $this->get_block_attachments( $post );
foreach ( $media_attachments as $media_id => $html ) {
Expand Down
Binary file added logo-256x256.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 23 additions & 13 deletions templates/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@
);

$rest_nonce = wp_create_nonce( 'wp_rest' );
$_post_types = \get_post_types( array( 'show_ui' => true ), 'objects' );
if ( ! isset( $_post_types[ \Enable_Mastodon_Apps\Mastodon_Api::POST_CPT ] ) ) {
$_post_types = array_merge(
array(
\Enable_Mastodon_Apps\Mastodon_Api::POST_CPT => get_post_type_object( \Enable_Mastodon_Apps\Mastodon_Api::POST_CPT ),
),
$_post_types
);
}
$_post_types = array_merge(
array(
\Enable_Mastodon_Apps\Mastodon_Api::ANNOUNCE_CPT => get_post_type_object( \Enable_Mastodon_Apps\Mastodon_Api::ANNOUNCE_CPT ),
\Enable_Mastodon_Apps\Mastodon_Api::POST_CPT => get_post_type_object( \Enable_Mastodon_Apps\Mastodon_Api::POST_CPT ),
),
\get_post_types( array( 'show_ui' => true ), 'objects' ),
);

$app = $args['app'];
$confirm = esc_html(
Expand Down Expand Up @@ -132,9 +130,13 @@
<td>
<select name="create_post_type">
<?php
foreach ( $_post_types as $post_type ) : // phpcs:ignore
foreach ( $_post_types as $_post_type ) :
if ( \Enable_Mastodon_Apps\Mastodon_Api::ANNOUNCE_CPT === $_post_type->name ) {
continue;
}

?>
<option value="<?php echo esc_attr( $post_type->name ); ?>" <?php selected( $post_type->name, $app->get_create_post_type() ); ?>><?php echo esc_html( $post_type->labels->singular_name ); ?></option>
<option value="<?php echo esc_attr( $_post_type->name ); ?>" <?php selected( $_post_type->name, $app->get_create_post_type() ); ?>><?php echo esc_html( $_post_type->labels->singular_name ); ?></option>
<?php endforeach; ?>
</select>
<p class="description">
Expand Down Expand Up @@ -174,8 +176,16 @@
<th scope="row" class="view-post-type"><?php esc_html_e( 'Show these post types', 'enable-mastodon-apps' ); ?></th>
<td>
<fieldset>
<?php foreach ( $_post_types as $post_type ) : /* phpcs:ignore */ ?>
<label><input type="checkbox" name="view_post_types[]" value="<?php echo esc_attr( $post_type->name ); ?>"<?php checked( in_array( $post_type->name, $app->get_view_post_types(), true ) ); ?> /> <?php echo esc_html( $post_type->label ); ?></label>
<?php
foreach ( $_post_types as $_post_type ) :
if ( \Enable_Mastodon_Apps\Mastodon_Api::POST_CPT === $_post_type->name || \Enable_Mastodon_Apps\Mastodon_Api::ANNOUNCE_CPT === $_post_type->name ) {
?>
<label title="<?php esc_attr_e( 'Always enabled.', 'enable-mastodon-apps' ); ?> <?php echo esc_attr( $_post_type->description ); ?>"><input type="checkbox" name="view_post_types[]" value="<?php echo esc_attr( $_post_type->name ); ?>" checked="checked" disabled="disabled"> <?php echo esc_html( $_post_type->label ); ?></label>
<?php
continue;
}
?>
<label title="<?php echo esc_attr( $_post_type->description ); ?>"><input type="checkbox" name="view_post_types[]" value="<?php echo esc_attr( $_post_type->name ); ?>"<?php checked( in_array( $_post_type->name, $app->get_view_post_types(), true ) ); ?> /> <?php echo esc_html( $_post_type->label ); ?></label>
<?php endforeach; ?>
</fieldset>
<p class="description">
Expand Down
3 changes: 2 additions & 1 deletion tests/test-activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ public function test_activitypub_submit_status_reply() {
)
);
$this->assertNotEmpty( $outbox );
$this->assertEquals( $outbox[0]->post_title, home_url( '?c=' . $comment->comment_ID ) );
$json = json_decode( $outbox[0]->post_content );
$this->assertEquals( $json->url, home_url( '?c=' . $comment->comment_ID ) );
do_action( 'activitypub_process_outbox' );

$this->assertEquals( 'federate', substr( get_comment_meta( $comment->comment_ID, 'activitypub_status', true ), 0, 8 ) );
Expand Down

0 comments on commit e1d704a

Please sign in to comment.