Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve New Post Setting and explicit set a New post format #220

Merged
merged 7 commits into from
Feb 26, 2025
Merged
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
9 changes: 9 additions & 0 deletions includes/class-mastodon-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 4 additions & 20 deletions includes/class-mastodon-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down
20 changes: 20 additions & 0 deletions includes/class-mastodon-app.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 );
}
Expand Down Expand Up @@ -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 ) ) {
Expand Down
59 changes: 48 additions & 11 deletions templates/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
?>
<div class="enable-mastodon-apps-settings enable-mastodon-apps-registered-apps-page <?php echo $args['enable_debug'] ? 'enable-debug' : 'disable-debug'; ?>">
<form method="post">
Expand Down Expand Up @@ -140,6 +144,17 @@
<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>
<label>
<?php echo esc_html( _x( 'in the post format', 'select post format', 'enable-mastodon-apps' ) ); ?>
<select name="create_post_format">
<option value="" <?php selected( ! $app->get_create_post_format( true ) ); ?>><?php esc_html_e( 'First selected above', 'enable-mastodon-apps' ); ?> (<?php echo esc_html( get_post_format_strings()[ $selected_post_format ] ); ?>)</option>

<?php foreach ( get_post_format_strings() as $format => $label ) : ?>
<option value="<?php echo esc_attr( $format ); ?>" <?php selected( $format, $app->get_create_post_format( true ) ); ?><?php disabled( ! empty( $app_post_formats ) && ! in_array( $format, $app_post_formats, true ) ); ?>><?php echo esc_html( $label ); ?></option>
<?php endforeach; ?>
</select>
</label>

<p class="description">
<span><?php esc_html_e( 'When posting through the app, this post type will be created.', 'enable-mastodon-apps' ); ?></span>
<br>
Expand Down Expand Up @@ -217,17 +232,7 @@
)
);
?>
</button>
<script>
document.getElementById( 'toggle_all_post_formats' ).onclick = function ( event ) {
document.querySelectorAll( '.post-formats input[type="checkbox"]' ).forEach( function ( element ) {
element.checked = ! element.checked;
} );

event.preventDefault();
return false;
}
</script>
</button>

<h3><?php esc_html_e( 'Access Tokens', 'enable-mastodon-apps' ); ?></h3>

Expand Down Expand Up @@ -282,3 +287,35 @@

</form>
</div>
<script type="text/javascript">
document.addEventListener( 'click', function ( event ) {
function updatePostFormatDropdown( el ) {
const postFormats = document.querySelectorAll( '.post-formats input[type="checkbox"]:checked' );
const postFormatSelect = document.querySelector( 'select[name="create_post_format"]' );
const options = postFormatSelect.querySelectorAll( 'option' );

for ( const option of options ) {
if ( option.value === '' ) {
option.textContent = option.textContent.replace( /\(.*\)/, '(' + postFormats[0].nextSibling.textContent.trim() + ')' );
}
if ( option.value === el.value ) {
option.disabled = ! el.checked;
if ( ! el.checked && postFormatSelect.value === el.value ) {
postFormatSelect.value = '';
}
}
}
}

if ( event.target.matches( '.post-formats input[type="checkbox"]' ) ) {
return updatePostFormatDropdown( event.target );
}
if ( event.target.id === 'toggle_all_post_formats' ) {
document.querySelectorAll( '.post-formats input[type="checkbox"]' ).forEach( function ( element ) {
element.checked = ! element.checked;
updatePostFormatDropdown( element );
} );
return;
}
});
</script>
49 changes: 42 additions & 7 deletions templates/registered-apps.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@
<thead>
<th><?php esc_html_e( 'Name', 'enable-mastodon-apps' ); ?></th>
<th class="debug-hide"><?php esc_html_e( 'Redirect URI', 'enable-mastodon-apps' ); ?></th>
<th><?php esc_html_e( 'Scope', 'enable-mastodon-apps' ); ?></th>
<th class="debug-hide"><?php esc_html_e( 'Post Formats', 'enable-mastodon-apps' ); ?></th>
<th><?php echo esc_html_x( 'Create new posts as', 'select post type', 'enable-mastodon-apps' ); ?></th>
<th><?php echo esc_html_x( 'in the post format', 'select post format', 'enable-mastodon-apps' ); ?></th>
<th><?php esc_html_e( 'Post Formats', 'enable-mastodon-apps' ); ?></th>
<th class="debug-hide"><?php esc_html_e( 'Scope', 'enable-mastodon-apps' ); ?></th>
<th><?php esc_html_e( 'Last Used', 'enable-mastodon-apps' ); ?></th>
<th><?php esc_html_e( 'Created', 'enable-mastodon-apps' ); ?></th>
<th></th>
Expand All @@ -68,16 +70,49 @@
?>
</td>
<td class="debug-hide"><?php echo wp_kses( implode( '<br/>', is_array( $app->get_redirect_uris() ) ? $app->get_redirect_uris() : explode( ',', $app->get_redirect_uris() ) ), array( 'br' => array() ) ); ?></td>
<td><?php echo esc_html( $app->get_scopes() ); ?></td>
<td class="debug-hide">
<td>
<?php
foreach ( get_post_format_strings() as $format => $label ) {
if ( in_array( $format, $app->get_post_formats() ) ) {
echo esc_html( $label ) . '<br>';
$_post_type = get_post_type_object( $app->get_create_post_type() );
echo esc_html( $_post_type->labels->singular_name );
?>
</td>
<td>
<?php
if ( ! $app->get_create_post_format() ) {
echo esc_html_x( 'Standard', 'Post format' ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain

} else {
foreach ( get_post_format_strings() as $slug => $name ) {
if ( $slug === $app->get_create_post_format() ) {
echo esc_html( $name );
break;
}
}
}
?>
</td>
<td>
<?php

$post_formats = $app->get_post_formats();
if ( empty( $post_formats ) ) {
echo esc_html( __( 'All' ) ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
} else {
echo esc_html(
implode(
', ',
array_map(
function ( $slug ) {
return get_post_format_strings()[ $slug ];
},
$post_formats
)
)
);
}
?>
</td>
<td class="debug-hide"><?php echo esc_html( $app->get_scopes() ); ?></td>
<?php td_timestamp( $app->get_last_used() ); ?>
<?php td_timestamp( $app->get_creation_date() ); ?>
<td><a href="<?php echo esc_url( $app->get_admin_page() ); ?>"><?php esc_html_e( 'Details', 'enable-mastodon-apps' ); ?></a></td>
Expand Down
1 change: 1 addition & 0 deletions tests/test-statuses-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ public function submit_status_data_provider() {
*/
public function test_submit_status( $status, $new_format, $new_post_type, $disable_blocks, $expected_title, $expected_content ) {
$this->app->set_post_formats( $new_format );
$this->app->set_create_post_format( $new_format );
$this->app->set_create_post_type( $new_post_type );
$this->app->set_disable_blocks( $disable_blocks );

Expand Down