diff --git a/includes/class-mastodon-admin.php b/includes/class-mastodon-admin.php index 87009f7..329c62f 100644 --- a/includes/class-mastodon-admin.php +++ b/includes/class-mastodon-admin.php @@ -692,6 +692,15 @@ function ( $post_type ) { } } + if ( isset( $_POST['create_post_format'] ) ) { + $create_post_format = sanitize_text_field( wp_unslash( $_POST['create_post_format'] ) ); + if ( in_array( $create_post_format, $post_formats ) ) { + $app->set_create_post_format( $create_post_format ); + } else { + $app->set_create_post_format( '' ); + } + } + if ( isset( $_POST['view_post_types'] ) && is_array( $_POST['view_post_types'] ) ) { $view_post_types = array( Mastodon_API::ANNOUNCE_CPT => true, diff --git a/includes/class-mastodon-api.php b/includes/class-mastodon-api.php index b20fa9b..451f61d 100644 --- a/includes/class-mastodon-api.php +++ b/includes/class-mastodon-api.php @@ -1997,16 +1997,8 @@ public function api_submit_post( $request ) { $media_ids = $request->get_param( 'media_ids' ); $scheduled_at = $request->get_param( 'scheduled_at' ); - $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' ); - } - $post_format = apply_filters( 'mastodon_api_new_post_format', $app_post_formats[0] ); - + $app = Mastodon_App::get_current_app(); + $post_format = $app->get_create_post_format(); $status = apply_filters( 'mastodon_api_submit_status', null, $status_text, $in_reply_to_id, $media_ids, $post_format, $visibility, $scheduled_at, $request ); return $this->validate_entity( $status, Entity\Status::class ); @@ -2386,16 +2378,8 @@ public function api_edit_post( $request ) { $media_ids = $request->get_param( 'media_ids' ); $scheduled_at = $request->get_param( 'scheduled_at' ); - $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' ); - } - $post_format = apply_filters( 'mastodon_api_new_post_format', $app_post_formats[0] ); - + $app = Mastodon_App::get_current_app(); + $post_format = $app->get_create_post_format(); $status = apply_filters( 'mastodon_api_edit_status', null, $post_id, $status_text, $in_reply_to_id, $media_ids, $post_format, $visibility, $scheduled_at, $request ); return $this->validate_entity( $status, Entity\Status::class ); diff --git a/includes/class-mastodon-app.php b/includes/class-mastodon-app.php index b192a61..07eba8d 100644 --- a/includes/class-mastodon-app.php +++ b/includes/class-mastodon-app.php @@ -107,6 +107,15 @@ public function get_create_post_type() { return $create_post_type; } + public function get_create_post_format( $raw = false ) { + $create_post_format = get_term_meta( $this->term->term_id, 'create_post_format', true ); + if ( ! $create_post_format && ! $raw ) { + $post_formats = $this->get_post_formats(); + $create_post_format = reset( $post_formats ); + } + return $create_post_format; + } + public function get_view_post_types() { $view_post_types = get_term_meta( $this->term->term_id, 'view_post_types', true ); if ( ! $view_post_types ) { @@ -144,6 +153,10 @@ public function set_create_post_type( $create_post_type ) { return update_term_meta( $this->term->term_id, 'create_post_type', $create_post_type ); } + public function set_create_post_format( $create_post_format ) { + return update_term_meta( $this->term->term_id, 'create_post_format', $create_post_format ); + } + public function set_view_post_types( $view_post_types ) { return update_term_meta( $this->term->term_id, 'view_post_types', $view_post_types ); } @@ -256,6 +269,13 @@ public function get_current_settings_text( string $content = '' ) { $content .= PHP_EOL . _x( 'Create new posts as', 'select post type', 'enable-mastodon-apps' ) . ': '; $content .= get_post_type_object( $this->get_create_post_type() )->labels->singular_name; + $content .= PHP_EOL . _x( 'in the post format', 'select post type', 'enable-mastodon-apps' ) . ': '; + foreach ( get_post_format_strings() as $slug => $name ) { + if ( $slug === $this->get_create_post_format() ) { + $content .= $name; + break; + } + } $t = PHP_EOL . __( 'Show these post types', 'enable-mastodon-apps' ) . ': '; foreach ( $this->get_view_post_types() as $post_type ) { if ( in_array( $post_type, array( Mastodon_API::ANNOUNCE_CPT, Mastodon_API::POST_CPT ), true ) ) { diff --git a/templates/app.php b/templates/app.php index 585f727..72c2fdd 100644 --- a/templates/app.php +++ b/templates/app.php @@ -29,6 +29,10 @@ ) ); $app_post_formats = $app->get_post_formats(); +$selected_post_format = $app->get_create_post_format(); +if ( ! $selected_post_format ) { + $selected_post_format = 'standard'; +} ?>