Skip to content

Commit

Permalink
Add setting for a default post format
Browse files Browse the repository at this point in the history
  • Loading branch information
akirk committed Feb 26, 2025
1 parent 8a8287f commit d7eb51d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 40 deletions.
53 changes: 31 additions & 22 deletions includes/class-mastodon-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,25 @@ public function admin_welcome_page() {
}

public function process_admin_settings_page() {
if ( isset( $_POST['mastodon_api_enable_logins'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'enable-mastodon-apps' ) ) {
return;
}

if ( isset( $_POST['mastodon_api_enable_logins'] ) ) {
delete_option( 'mastodon_api_disable_logins' );
} else {
update_option( 'mastodon_api_disable_logins', true );
}

if ( isset( $_POST['mastodon_api_default_create_post_format'] ) ) {
$post_format = sanitize_text_field( wp_unslash( $_POST['mastodon_api_default_create_post_format'] ) );
if ( in_array( $post_format, get_post_format_slugs(), true ) ) {
update_option( 'mastodon_api_default_create_post_format', $post_format, false );
} else {
delete_option( 'mastodon_api_default_create_post_format' );
}
}

/**
* The default post type for posting from Mastodon apps when the configured to do so.
*
Expand All @@ -214,7 +227,7 @@ public function process_admin_settings_page() {
*/
$default_ema_post_type = apply_filters( 'mastodon_api_default_post_type', \Enable_Mastodon_Apps\Mastodon_API::POST_CPT );

if ( isset( $_POST['mastodon_api_posting_cpt'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( isset( $_POST['mastodon_api_posting_cpt'] ) ) {
delete_option( 'mastodon_api_posting_cpt' );

if ( defined( 'ACTIVITYPUB_PLUGIN_VERSION' ) ) {
Expand All @@ -236,19 +249,19 @@ public function process_admin_settings_page() {
}
}

if ( isset( $_POST['mastodon_api_disable_ema_announcements'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( isset( $_POST['mastodon_api_disable_ema_announcements'] ) ) {
delete_option( 'mastodon_api_disable_ema_announcements' );
} else {
update_option( 'mastodon_api_disable_ema_announcements', true, false );
}

if ( isset( $_POST['mastodon_api_disable_ema_app_settings_changes'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( isset( $_POST['mastodon_api_disable_ema_app_settings_changes'] ) ) {
delete_option( 'mastodon_api_disable_ema_app_settings_changes' );
} else {
update_option( 'mastodon_api_disable_ema_app_settings_changes', true, false );
}

if ( isset( $_POST['mastodon_api_enable_debug'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( isset( $_POST['mastodon_api_enable_debug'] ) ) {
update_option( 'mastodon_api_enable_debug', true );
} else {
delete_option( 'mastodon_api_enable_debug' );
Expand All @@ -266,12 +279,16 @@ public function admin_settings_page() {
}

public function process_admin_debug_page() {
if ( isset( $_POST['mastodon_api_debug_mode'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'enable-mastodon-apps' ) ) {
return;
}

if ( isset( $_POST['mastodon_api_debug_mode'] ) ) {
update_option( 'mastodon_api_debug_mode', time() + 5 * MINUTE_IN_SECONDS );
} else {
delete_option( 'mastodon_api_debug_mode' );
}
if ( isset( $_POST['mastodon_api_auto_app_reregister'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( isset( $_POST['mastodon_api_auto_app_reregister'] ) ) {
update_option( 'mastodon_api_auto_app_reregister', true );
} else {
delete_option( 'mastodon_api_auto_app_reregister' );
Expand All @@ -287,9 +304,11 @@ public function admin_tester_page() {
}

public function process_admin_registered_apps_page() {
// phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'enable-mastodon-apps' ) ) {
return;
}

if ( isset( $_POST['delete-code'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Missing
$deleted = $this->oauth->get_code_storage()->expireAuthorizationCode( sanitize_text_field( wp_unslash( $_POST['delete-code'] ) ) );
add_settings_error(
'enable-mastodon-apps',
Expand All @@ -304,10 +323,8 @@ public function process_admin_registered_apps_page() {
return;
}

// phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( isset( $_POST['delete-token'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Missing
$deleted = $this->oauth->get_token_storage()->unsetAccessToken( sanitize_text_field( wp_unslash( $_POST['delete-token'] ) ) );
$deleted = $this->oauth->get_token_storage()->unsetAccessToken( sanitize_text_field( wp_unslash( $_POST['delete-token'] ) ) );
add_settings_error(
'enable-mastodon-apps',
'deleted-tokens',
Expand All @@ -321,10 +338,8 @@ public function process_admin_registered_apps_page() {
return;
}

// phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( isset( $_POST['delete-app'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Missing
$deleted = Mastodon_App::get_by_client_id( sanitize_text_field( wp_unslash( $_POST['delete-app'] ) ) )->delete();
$deleted = Mastodon_App::get_by_client_id( sanitize_text_field( wp_unslash( $_POST['delete-app'] ) ) )->delete();
add_settings_error(
'enable-mastodon-apps',
'deleted-apps',
Expand All @@ -338,10 +353,8 @@ public function process_admin_registered_apps_page() {
return;
}

// phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( isset( $_POST['clear-app-logs'] ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Missing
$deleted = Mastodon_App::get_by_client_id( sanitize_text_field( wp_unslash( $_POST['clear-app-logs'] ) ) )->delete_last_requests();
$deleted = Mastodon_App::get_by_client_id( sanitize_text_field( wp_unslash( $_POST['clear-app-logs'] ) ) )->delete_last_requests();
if ( $deleted ) {
add_settings_error(
'enable-mastodon-apps',
Expand All @@ -359,7 +372,6 @@ public function process_admin_registered_apps_page() {
}
return;
}
// phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( isset( $_POST['clear-all-app-logs'] ) ) {
$total_deleted = 0;
foreach ( Mastodon_App::get_all() as $app ) {
Expand Down Expand Up @@ -390,7 +402,6 @@ public function process_admin_registered_apps_page() {
return;
}

// phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( isset( $_POST['delete-outdated'] ) ) {
$apps = Mastodon_App::get_all();
$deleted = OAuth2\Access_Token_Storage::cleanupOldTokens();
Expand Down Expand Up @@ -456,7 +467,6 @@ public function process_admin_registered_apps_page() {
return;
}

// phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( isset( $_POST['delete-never-used'] ) ) {
$deleted = 0;
foreach ( Mastodon_App::get_all() as $app ) {
Expand Down Expand Up @@ -499,7 +509,6 @@ public function process_admin_registered_apps_page() {
return;
}

// phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( isset( $_POST['delete-apps-without-tokens'] ) ) {
$app_tokens = array();
foreach ( OAuth2\Access_Token_Storage::getAll() as $token => $data ) {
Expand Down
48 changes: 31 additions & 17 deletions includes/class-mastodon-app.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,30 +255,35 @@ public function was_used( $request, $additional_debug_data = array() ) {
*/
public function get_current_settings_text( string $content = '' ) {
$post_formats = $this->get_post_formats();
$t = PHP_EOL . __( 'Post Formats', 'enable-mastodon-apps' ) . ': ';
foreach ( get_post_format_strings() as $slug => $name ) {
if ( ! in_array( $slug, $post_formats, true ) ) {
continue;
}
$content .= $t . $name;
$t = ', ';
$post_format_strings = array_filter(
get_post_format_strings(),
function ( $slug ) use ( $post_formats ) {
return in_array( $slug, $post_formats, true );
},
ARRAY_FILTER_USE_KEY
);

$content .= PHP_EOL . __( 'Post Formats', 'enable-mastodon-apps' ) . ': ' . implode( ', ', $post_format_strings );
if ( empty( $post_format_strings ) ) {
$content .= __( 'All' ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
}
if ( ', ' !== $t ) {
$content .= $t . __( 'All' ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain

if ( empty( $post_format_strings ) ) {
$post_format_strings = get_post_format_strings();
}

$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;
}
$content .= ' ' . _x( 'in the post format', 'select post type', 'enable-mastodon-apps' ) . ': ';
if ( $this->get_create_post_format() && isset( $post_format_strings[ $this->get_create_post_format() ] ) ) {
$content .= $post_format_strings[ $this->get_create_post_format() ];
} else {
$content .= reset( $post_format_strings );
}

$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 ) ) {
if ( in_array( $post_type, array( Comment_CPT::CPT ), true ) ) {
continue;
}
$content .= $t . get_post_type_object( $post_type )->labels->name;
Expand Down Expand Up @@ -800,6 +805,10 @@ public static function save( $client_name, array $redirect_uris, $scopes, $websi
'website'
);

$post_formats = array();
if ( get_option( 'mastodon_api_default_create_post_format' ) ) {
$post_formats[] = get_option( 'mastodon_api_default_create_post_format' );
}
/**
* Post formats to be enabled for new apps.
*
Expand All @@ -815,7 +824,8 @@ public static function save( $client_name, array $redirect_uris, $scopes, $websi
* } );
* ```
*/
$post_formats = apply_filters( 'mastodon_api_new_app_post_formats', array(), $app_metadata );
$post_formats = apply_filters( 'mastodon_api_new_app_post_formats', $post_formats, $app_metadata );

$app_metadata['query_args'] = array( 'post_formats' => $post_formats );

$app_metadata['create_post_type'] = get_option( 'mastodon_api_posting_cpt', apply_filters( 'mastodon_api_default_post_type', \Enable_Mastodon_Apps\Mastodon_API::POST_CPT ) );
Expand All @@ -824,6 +834,10 @@ public static function save( $client_name, array $redirect_uris, $scopes, $websi
$view_post_types[] = $app_metadata['create_post_type'];
}

if ( get_option( 'mastodon_api_default_create_post_format' ) && in_array( get_option( 'mastodon_api_default_create_post_format' ), $post_formats ) ) {
$app_metadata['create_post_format'] = get_option( 'mastodon_api_default_create_post_format' );
}

/**
* Standard post types that the app can view.
*
Expand Down
20 changes: 19 additions & 1 deletion templates/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e( 'Posting', 'enable-mastodon-apps' ); ?></th>
<th scope="row" rowspan="2"><?php esc_html_e( 'Posting', 'enable-mastodon-apps' ); ?></th>
<td>
<fieldset>
<label for="mastodon_api_posting_cpt">
Expand Down Expand Up @@ -72,6 +72,24 @@
</p>
</td>
</tr>
<tr>
<td>
<fieldset>
<label for="mastodon_api_default_create_post_format">
<?php esc_html_e( 'For new apps, pre-select this post format:', 'enable-mastodon-apps' ); ?>
</label>
<select name="mastodon_api_default_create_post_format" id="mastodon_api_default_create_post_format">
<option value="all" <?php selected( ! get_option( 'mastodon_api_default_create_post_format' ) ); ?>><?php esc_html_e( 'None', 'enable-mastodon-apps' ); ?></option>
<?php foreach ( get_post_format_strings() as $format => $label ) : ?>
<option value="<?php echo esc_attr( $format ); ?>" <?php selected( $format, get_option( 'mastodon_api_default_create_post_format' ) ); ?>><?php echo esc_html( $label ); ?></option>
<?php endforeach; ?>
</select>
<p class="description">
<?php esc_html_e( 'If no post format is selected, posts in all post formats will be displayed.', 'enable-mastodon-apps' ); ?>
</p>
</fieldset>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e( 'Status Messages', 'enable-mastodon-apps' ); ?></th>
<td>
Expand Down

0 comments on commit d7eb51d

Please sign in to comment.