Plugin Directory

Changeset 3248503


Ignore:
Timestamp:
02/28/2025 12:49:05 PM (10 days ago)
Author:
akirk
Message:

enable-mastodon-apps 1.3.0

Location:
enable-mastodon-apps/trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • enable-mastodon-apps/trunk/README.md

    r3245135 r3248503  
    77- Requires PHP: 7.4
    88- License: [GPLv2 or later](http://www.gnu.org/licenses/gpl-2.0.html)
    9 - Stable tag: 1.2.1
     9- Stable tag: 1.3.0
    1010
    1111Allow accessing your WordPress with Mastodon clients. Just enter your own blog URL as your instance.
     
    101101## Changelog
    102102
     103### 1.3.0
     104- Fix small errors on app page ([#224])
     105- Show internal CPTs if debug is turned on ([#223])
     106- Add setting for a default post format ([#221])
     107- Improve New Post Setting and explicit set a New post format ([#220], [#222])
     108- Assign all post formats to EMA announcements ([#218])
     109
    103110### 1.2.1
    104111- Fixed Boost, Like and Comment notifications ([#216])
     
    181188- Fix OAuth rewrite path ([#130])
    182189
     190[#224]: https://github.com/akirk/enable-mastodon-apps/pull/224
     191[#223]: https://github.com/akirk/enable-mastodon-apps/pull/223
     192[#222]: https://github.com/akirk/enable-mastodon-apps/pull/222
     193[#221]: https://github.com/akirk/enable-mastodon-apps/pull/221
     194[#220]: https://github.com/akirk/enable-mastodon-apps/pull/220
     195[#218]: https://github.com/akirk/enable-mastodon-apps/pull/218
    183196[#216]: https://github.com/akirk/enable-mastodon-apps/pull/216
    184197[#214]: https://github.com/akirk/enable-mastodon-apps/pull/214
  • enable-mastodon-apps/trunk/enable-mastodon-apps.php

    r3245135 r3248503  
    44 * Plugin author: Alex Kirk
    55 * Plugin URI: https://github.com/akirk/enable-mastodon-apps
    6  * Version: 1.2.1
     6 * Version: 1.3.0
    77 *
    88 * Description: Allow accessing your WordPress with Mastodon clients. Just enter your own blog URL as your instance.
     
    2121define( 'ENABLE_MASTODON_APPS_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
    2222define( 'ENABLE_MASTODON_APPS_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
    23 define( 'ENABLE_MASTODON_APPS_VERSION', '1.2.1' );
     23define( 'ENABLE_MASTODON_APPS_VERSION', '1.3.0' );
    2424
    2525require __DIR__ . '/vendor/bshaffer/oauth2-server-php/src/OAuth2/Autoloader.php';
  • enable-mastodon-apps/trunk/includes/class-mastodon-admin.php

    r3245133 r3248503  
    5757                'title'   => __( 'Settings' ), // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
    5858                'content' =>
    59                     '<p><strong>' . esc_html__( 'Hide posts through Mastodon apps from appearing on the WordPress frontend', 'enable-mastodon-apps' ) . '</strong><br>' .
    60                     '<span>' . esc_html__( 'Setting this depends on your use case:', 'enable-mastodon-apps' ) . '</span>' .
     59                    '<ul><li><strong>' . esc_html__( 'Hide posts through Mastodon apps from appearing on the WordPress frontend', 'enable-mastodon-apps' ) . '</strong>' .
     60
    6161                    '<ul>' .
    62                     '<li>' . esc_html__( 'If you want to use Mastodon apps to read just the posts on your site (for example in a blog with many authors, and you are interested in what the other authors post), and post to the site directly, uncheck the box.', 'enable-mastodon-apps' ) . '</li>' .
    63                     '<li>' .
     62                    '<li><span>' .
     63                    __( 'Check this if you want keep your use of Mastodon separate from your activity on your site.', 'enable-mastodon-apps' ) . '</span> <span>' .
     64                    __( 'Posting through a Mastodon app would still save the post in your WordPress but invisibly to visitors and subscribers of your WordPress.', 'enable-mastodon-apps' ) . '</span> <span>' .
    6465                    wp_kses(
    6566                        sprintf(
    66                             // translators: Links to the plugins.
    67                             __( 'If you want to use Mastodon apps to post to Mastodon (when combining with the <a href="%1$s">Friends plugin</a> and <a href="%2$s">ActivityPub plugin</a>) but want to avoid posting it visibly to your site (which for example would be sent to your subscribers), leave it checked.', 'enable-mastodon-apps' ),
    68                             'https://wordpress.org/plugins/friends/" target="_blank',
     67                            // translators: Links to the plugin.
     68                            __( 'Remember that for posting to Mastodon, you\'ll need the <a href="%s">ActivityPub plugin</a>.', 'enable-mastodon-apps' ),
    6969                            'https://wordpress.org/plugins/activitypub/" target="_blank'
    7070                        ),
    7171                        array(
    72                         'a' => array(
    73                         'href'   => true,
    74                         'target' => true,
     72                            'a' => array(
     73                                'href'   => true,
     74                                'target' => true,
     75                            ),
     76                        )
     77                    ) . '</span> <span>' .
     78                    wp_kses(
     79                        sprintf(
     80                            // translators: Links to the plugin.
     81                            __( 'If you also want to follow people and interact with them, please also install the <a href="%s">Friends plugin</a>.', 'enable-mastodon-apps' ),
     82                            'https://wordpress.org/plugins/friends/" target="_blank'
    7583                        ),
     84                        array(
     85                            'a' => array(
     86                                'href'   => true,
     87                                'target' => true,
     88                            ),
    7689                        )
    7790                    ) .
    78                     '</li>' .
    79                     '<li>' .
     91                    '</span></li>' .
     92                    // translators: %s: a post type name.
     93                    '<li>' . wp_kses( sprintf( __( 'With this setting, newly registered apps will be configured to save new posts as %s.', 'enable-mastodon-apps' ), '<tt>' . $ema_post_cpt->labels->singular_name . '</tt>' ), array( 'tt' => true ) ) . '</span> <span>' .
     94                    // translators: %s: a post type name.
     95                    wp_kses( sprintf( __( 'You can see and edit these posts in the sidebar menu of WP Admin under %s.', 'enable-mastodon-apps' ), '<a href="' . self_admin_url( 'edit.php?post_type=' . $ema_post_cpt->name ) . '">' . $ema_post_cpt->labels->name . '</a>' ), array( 'a' => array( 'href' => true ) ) ) . '</span> <span>' .
     96                    esc_html__( 'This setting can be changed individually per app in its individual settings page.', 'enable-mastodon-apps' ) . '</span></li>' .
     97                    '</ul>' .
     98                    '<li><strong>' . esc_html__( 'Show posts through Mastodon apps on the WordPress frontend', 'enable-mastodon-apps' ) . '</strong>' .
     99                    '<ul>' .
     100                    '<li>' . esc_html__( 'Check this if you want to use Mastodon apps to post to the site directly.', 'enable-mastodon-apps' ) . '</span> <span>' .
     101                    // translators: %s: a post format name.
     102                    wp_kses( sprintf( __( 'Your posts will appear in your RSS feed and be sent to your subscribers unless you specify a post format, for example %s.', 'enable-mastodon-apps' ), '<tt>' . _x( 'Status', 'Post format' ) . '</tt>' ), array( 'tt' => true ) ) . '</span> <span>' . // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
     103                    esc_html__( 'That way, those posts will only appear in the specific RSS feed, and are typically not sent to subcribers.', 'enable-mastodon-apps' ) . '</span> <span>' .
    80104                    wp_kses(
    81105                        sprintf(
    82                             // translators: Links to the plugins.
    83                             __( 'If you do want to expose such posts also on your site (for example in a <a href="%s">POSSE</a> use case), uncheck the box.', 'enable-mastodon-apps' ),
    84                             'https://indieweb.org/POSSE" target="_blank'
     106                            // translators: Links to the plugin.
     107                            __( 'Like above, if you combine it with the <a href="%s">ActivityPub plugin</a>, people on Mastodon will be able to read your posts.', 'enable-mastodon-apps' ),
     108                            'https://wordpress.org/plugins/activitypub/" target="_blank'
    85109                        ),
    86110                        array(
    87                         'a' => array(
    88                         'href'   => true,
    89                         'target' => true,
     111                            'a' => array(
     112                                'href'   => true,
     113                                'target' => true,
     114                            ),
     115                        )
     116                    ) . '</span> <span>' .
     117                    wp_kses(
     118                        sprintf(
     119                            // translators: Links to the plugin.
     120                            __( 'Also, with the <a href="%s">Friends plugin</a>, you will be able to follow people and interact with them', 'enable-mastodon-apps' ),
     121                            'https://wordpress.org/plugins/friends/" target="_blank'
    90122                        ),
     123                        array(
     124                            'a' => array(
     125                                'href'   => true,
     126                                'target' => true,
     127                            ),
    91128                        )
    92129                    ) .
    93                     '</li>' .
    94                     '</ul>' .
    95                     '<span>' .
    96                     wp_kses(
    97                         sprintf(
    98                             // translators: %1$s and %2$s: a post type.
    99                             __( 'When checked, new posts made through Mastodon apps will have the post type %1$s, otherwise %2$s when unchecked.', 'enable-mastodon-apps' ),
    100                             '<strong>' . $ema_post_cpt->labels->singular_name . '</strong>',
    101                             '<strong>' . $post_cpt->labels->singular_name . '</strong>'
    102                         ),
    103                         array( 'strong' => true )
    104                     ) .
    105                     '</span>' .
    106                     '</p>',
     130                    // translators: %s: a post type name.
     131                    '<li>' . wp_kses( sprintf( __( 'With this setting, newly registered apps will be configured to save new posts as %s.', 'enable-mastodon-apps' ), '<tt>' . $post_cpt->labels->singular_name . '</tt>' ), array( 'tt' => true ) ) .
     132                    '</span> <span>' . esc_html__( 'This setting can be changed individually per app in its individual settings page.', 'enable-mastodon-apps' ) . '</span></li>' .
     133                    '</ul>',
    107134            )
    108135        );
     
    130157        if ( isset( $_POST['app'] ) ) {
    131158            $app = Mastodon_App::get_by_client_id( sanitize_text_field( wp_unslash( $_POST['app'] ) ) );
    132             if ( $app ) {
     159            if ( $app && ! is_wp_error( $app ) ) {
    133160                return $this->process_admin_app_page( $app );
    134161            }
     
    156183        if ( isset( $_GET['app'] ) ) {
    157184            $app = Mastodon_App::get_by_client_id( sanitize_text_field( wp_unslash( $_GET['app'] ) ) );
    158             if ( $app ) {
     185            if ( $app && ! is_wp_error( $app ) ) {
    159186                return $this->admin_app_page( $app );
    160187            }
     
    193220
    194221    public function process_admin_settings_page() {
    195         if ( isset( $_POST['mastodon_api_enable_logins'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
     222        if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'enable-mastodon-apps' ) ) {
     223            return;
     224        }
     225
     226        if ( isset( $_POST['mastodon_api_enable_logins'] ) ) {
    196227            delete_option( 'mastodon_api_disable_logins' );
    197228        } else {
    198229            update_option( 'mastodon_api_disable_logins', true );
     230        }
     231
     232        if ( isset( $_POST['mastodon_api_default_create_post_format'] ) ) {
     233            $post_format = sanitize_text_field( wp_unslash( $_POST['mastodon_api_default_create_post_format'] ) );
     234            if ( in_array( $post_format, get_post_format_slugs(), true ) ) {
     235                update_option( 'mastodon_api_default_create_post_format', $post_format, false );
     236            } else {
     237                delete_option( 'mastodon_api_default_create_post_format' );
     238            }
    199239        }
    200240
     
    215255        $default_ema_post_type = apply_filters( 'mastodon_api_default_post_type', \Enable_Mastodon_Apps\Mastodon_API::POST_CPT );
    216256
    217         if ( isset( $_POST['mastodon_api_posting_cpt'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
     257        if ( isset( $_POST['mastodon_api_posting_cpt'] ) && intval( $_POST['mastodon_api_posting_cpt'] ) === 1 ) {
    218258            delete_option( 'mastodon_api_posting_cpt' );
    219259
     
    225265                }
    226266            }
    227         } else {
     267        } elseif ( isset( $_POST['mastodon_api_posting_cpt'] ) && intval( $_POST['mastodon_api_posting_cpt'] ) === 0 ) {
    228268            update_option( 'mastodon_api_posting_cpt', 'post', false );
    229269
     
    237277        }
    238278
    239         if ( isset( $_POST['mastodon_api_disable_ema_announcements'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
     279        if ( isset( $_POST['mastodon_api_disable_ema_announcements'] ) ) {
    240280            delete_option( 'mastodon_api_disable_ema_announcements' );
    241281        } else {
     
    243283        }
    244284
    245         if ( isset( $_POST['mastodon_api_disable_ema_app_settings_changes'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
     285        if ( isset( $_POST['mastodon_api_disable_ema_app_settings_changes'] ) ) {
    246286            delete_option( 'mastodon_api_disable_ema_app_settings_changes' );
    247287        } else {
     
    249289        }
    250290
    251         if ( isset( $_POST['mastodon_api_enable_debug'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
     291        if ( isset( $_POST['mastodon_api_enable_debug'] ) ) {
    252292            update_option( 'mastodon_api_enable_debug', true );
    253293        } else {
     
    267307
    268308    public function process_admin_debug_page() {
    269         if ( isset( $_POST['mastodon_api_debug_mode'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
     309        if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'enable-mastodon-apps' ) ) {
     310            return;
     311        }
     312
     313        if ( isset( $_POST['mastodon_api_debug_mode'] ) ) {
    270314            update_option( 'mastodon_api_debug_mode', time() + 5 * MINUTE_IN_SECONDS );
    271315        } else {
    272316            delete_option( 'mastodon_api_debug_mode' );
    273317        }
    274         if ( isset( $_POST['mastodon_api_auto_app_reregister'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
     318        if ( isset( $_POST['mastodon_api_auto_app_reregister'] ) ) {
    275319            update_option( 'mastodon_api_auto_app_reregister', true );
    276320        } else {
     
    288332
    289333    public function process_admin_registered_apps_page() {
    290         // phpcs:ignore WordPress.Security.NonceVerification.Missing
     334        if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'enable-mastodon-apps' ) ) {
     335            return;
     336        }
     337
    291338        if ( isset( $_POST['delete-code'] ) ) {
    292              // phpcs:ignore WordPress.Security.NonceVerification.Missing
    293339            $deleted = $this->oauth->get_code_storage()->expireAuthorizationCode( sanitize_text_field( wp_unslash( $_POST['delete-code'] ) ) );
    294340            add_settings_error(
     
    305351        }
    306352
    307         // phpcs:ignore WordPress.Security.NonceVerification.Missing
    308353        if ( isset( $_POST['delete-token'] ) ) {
    309             // phpcs:ignore WordPress.Security.NonceVerification.Missing
    310             $deleted = $this->oauth->get_token_storage()->unsetAccessToken( sanitize_text_field( wp_unslash( $_POST['delete-token'] ) ) );
     354                $deleted = $this->oauth->get_token_storage()->unsetAccessToken( sanitize_text_field( wp_unslash( $_POST['delete-token'] ) ) );
    311355            add_settings_error(
    312356                'enable-mastodon-apps',
     
    322366        }
    323367
    324         // phpcs:ignore WordPress.Security.NonceVerification.Missing
    325368        if ( isset( $_POST['delete-app'] ) ) {
    326             // phpcs:ignore WordPress.Security.NonceVerification.Missing
    327             $deleted = Mastodon_App::get_by_client_id( sanitize_text_field( wp_unslash( $_POST['delete-app'] ) ) )->delete();
     369                $deleted = Mastodon_App::get_by_client_id( sanitize_text_field( wp_unslash( $_POST['delete-app'] ) ) )->delete();
    328370            add_settings_error(
    329371                'enable-mastodon-apps',
     
    339381        }
    340382
    341         // phpcs:ignore WordPress.Security.NonceVerification.Missing
    342383        if ( isset( $_POST['clear-app-logs'] ) ) {
    343             // phpcs:ignore WordPress.Security.NonceVerification.Missing
    344             $deleted = Mastodon_App::get_by_client_id( sanitize_text_field( wp_unslash( $_POST['clear-app-logs'] ) ) )->delete_last_requests();
     384                $deleted = Mastodon_App::get_by_client_id( sanitize_text_field( wp_unslash( $_POST['clear-app-logs'] ) ) )->delete_last_requests();
    345385            if ( $deleted ) {
    346386                add_settings_error(
     
    360400            return;
    361401        }
    362         // phpcs:ignore WordPress.Security.NonceVerification.Missing
    363402        if ( isset( $_POST['clear-all-app-logs'] ) ) {
    364403            $total_deleted = 0;
     
    391430        }
    392431
    393         // phpcs:ignore WordPress.Security.NonceVerification.Missing
    394432        if ( isset( $_POST['delete-outdated'] ) ) {
    395433            $apps    = Mastodon_App::get_all();
     
    457495        }
    458496
    459         // phpcs:ignore WordPress.Security.NonceVerification.Missing
    460497        if ( isset( $_POST['delete-never-used'] ) ) {
    461498            $deleted = 0;
     
    500537        }
    501538
    502         // phpcs:ignore WordPress.Security.NonceVerification.Missing
    503539        if ( isset( $_POST['delete-apps-without-tokens'] ) ) {
    504540            $app_tokens = array();
     
    529565            return;
    530566        }
    531         // phpcs:disable WordPress.Security.NonceVerification.Missing
     567
    532568        if ( isset( $_POST['app_post_formats'] ) && is_array( $_POST['app_post_formats'] ) ) {
    533569            // phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
     
    602638
    603639    public function process_admin_app_page( Mastodon_App $app ) {
     640        if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'enable-mastodon-apps' ) ) {
     641            return;
     642        }
    604643
    605644        if ( isset( $_POST['delete-app'] ) && $_POST['delete-app'] === $app->get_client_id() ) {
     
    693732        }
    694733
     734        if ( isset( $_POST['create_post_format'] ) ) {
     735            $create_post_format = sanitize_text_field( wp_unslash( $_POST['create_post_format'] ) );
     736            if ( in_array( $create_post_format, $post_formats ) ) {
     737                $app->set_create_post_format( $create_post_format );
     738            } else {
     739                $app->set_create_post_format( '' );
     740            }
     741        }
     742
    695743        if ( isset( $_POST['view_post_types'] ) && is_array( $_POST['view_post_types'] ) ) {
    696744            $view_post_types = array(
     
    747795
    748796        if ( ! get_option( 'mastodon_api_disable_ema_announcements' ) ) {
     797            $post_id = false;
    749798            if ( ! $old_version ) {
    750799                $title = __( 'Welcome to Enable Mastodon Apps!', 'enable-mastodon-apps' );
     
    779828                $content .= sprintf( __( 'If you enjoy using this plugin, please let us know at the <a href=%s>EMA WordPress.org plugin page</a>.', 'enable-mastodon-apps' ), '"https://wordpress.org/plugins/enable-mastodon-apps/"' );
    780829
    781                 wp_insert_post(
     830                $post_id = wp_insert_post(
    782831                    array(
    783832                        'post_type'    => Mastodon_API::ANNOUNCE_CPT,
     
    788837                    )
    789838                );
     839
    790840            } else {
    791841                $readme = file_get_contents( ENABLE_MASTODON_APPS_PLUGIN_DIR . 'README.md' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
     
    811861                    $changes = wp_kses( $changes, array( 'a' => array( 'href' => true ) ) );
    812862
    813                     wp_insert_post(
     863                    $post_id = wp_insert_post(
    814864                        array(
    815865                            'post_type'    => Mastodon_API::ANNOUNCE_CPT,
     
    820870                    );
    821871                }
     872            }
     873            if ( $post_id ) {
     874                // Assign all post formats so that it will be shown regardless of the app's (potentially later changed) post format settings.
     875                wp_set_object_terms(
     876                    $post_id,
     877                    array_map(
     878                        function ( $slug ) {
     879                            return 'post-format-' . $slug;
     880                        },
     881                        get_post_format_slugs()
     882                    ),
     883                    'post_format'
     884                );
    822885            }
    823886        }
  • enable-mastodon-apps/trunk/includes/class-mastodon-api.php

    r3245133 r3248503  
    192192            ),
    193193            'description'  => __( 'Posted through a Mastodon app.', 'enable-mastodon-apps' ),
    194             'public'       => ! get_option( 'mastodon_api_posting_cpt' ) || ( defined( 'WP_DEBUG' ) && WP_DEBUG ),
     194            'public'       => ! get_option( 'mastodon_api_posting_cpt' ) || ( defined( 'WP_DEBUG' ) && WP_DEBUG ) || get_option( 'mastodon_api_enable_debug' ),
    195195            'show_in_rest' => false,
    196196            'rewrite'      => false,
     
    207207            ),
    208208            'description'  => __( 'Announcement by the Enable Mastodon Apps plugin.', 'enable-mastodon-apps' ),
    209             'public'       => defined( 'WP_DEBUG' ) && WP_DEBUG,
     209            'public'       => ( defined( 'WP_DEBUG' ) && WP_DEBUG ) || get_option( 'mastodon_api_enable_debug' ),
    210210            'show_in_rest' => false,
    211211            'rewrite'      => false,
     
    19981998        $scheduled_at = $request->get_param( 'scheduled_at' );
    19991999
    2000         $app              = Mastodon_App::get_current_app();
    2001         $app_post_formats = array();
    2002         if ( $app ) {
    2003             $app_post_formats = $app->get_post_formats();
    2004         }
    2005         if ( empty( $app_post_formats ) ) {
    2006             $app_post_formats = array( 'status' );
    2007         }
    2008         $post_format = apply_filters( 'mastodon_api_new_post_format', $app_post_formats[0] );
    2009 
     2000        $app = Mastodon_App::get_current_app();
     2001        $post_format = $app->get_create_post_format();
    20102002        $status = apply_filters( 'mastodon_api_submit_status', null, $status_text, $in_reply_to_id, $media_ids, $post_format, $visibility, $scheduled_at, $request );
    20112003
     
    23872379        $scheduled_at = $request->get_param( 'scheduled_at' );
    23882380
    2389         $app              = Mastodon_App::get_current_app();
    2390         $app_post_formats = array();
    2391         if ( $app ) {
    2392             $app_post_formats = $app->get_post_formats();
    2393         }
    2394         if ( empty( $app_post_formats ) ) {
    2395             $app_post_formats = array( 'status' );
    2396         }
    2397         $post_format = apply_filters( 'mastodon_api_new_post_format', $app_post_formats[0] );
    2398 
     2381        $app = Mastodon_App::get_current_app();
     2382        $post_format = $app->get_create_post_format();
    23992383        $status = apply_filters( 'mastodon_api_edit_status', null, $post_id, $status_text, $in_reply_to_id, $media_ids, $post_format, $visibility, $scheduled_at, $request );
    24002384
     
    29632947        );
    29642948
    2965         $post_formats = $app->get_post_formats();
    2966         $content[]    = sprintf(
    2967             // Translators: %s is the post formats.
    2968             _n( 'Posts with the post format <strong>%s</strong> will appear in this app.', 'Posts with the post formats <strong>%s</strong> will appear in this app.', count( $app->get_post_formats() ), 'enable-mastodon-apps' ),
    2969             implode( ', ', $post_formats )
    2970         );
    2971 
    2972         $content[] = sprintf(
    2973             // Translators: %s is the post format.
    2974             __( 'If you create a new note in this app, it will be created with the <strong>%s</strong> post format.', 'enable-mastodon-apps' ),
    2975             reset( $post_formats )
    2976         );
    2977 
    2978         if ( 'standard' === reset( $post_formats ) ) {
    2979             $content[] = __( 'If you want to create a post in WordPress with a title, add a new line after the title. The first line will then appear as the title of the post.', 'enable-mastodon-apps' );
    2980         } else {
    2981             $content[] = __( 'Because a new post is not created in the standard post format, it will be published without title. To change this, select the <strong>standard</strong> post format in the Enable Mastodon Apps settings.', 'enable-mastodon-apps' );
    2982         }
     2949        $content[] = $app->get_current_settings_text( __( 'Settings for this app:', 'enable-mastodon-apps' ) );
     2950
     2951        // translators: %s: settings page URL.
     2952        $content[] = sprintf( __( 'Change the <a href=%s>settings for this app here</a>.', 'enable-mastodon-apps' ), '"' . esc_url( admin_url( 'options-general.php?page=enable-mastodon-apps&app=' . $app->get_client_id() ) ) . '"' );
    29832953
    29842954        $ret[] = array(
  • enable-mastodon-apps/trunk/includes/class-mastodon-app.php

    r3245133 r3248503  
    108108    }
    109109
     110    public function get_create_post_format( $raw = false ) {
     111        $create_post_format = get_term_meta( $this->term->term_id, 'create_post_format', true );
     112        if ( ! $create_post_format && ! $raw ) {
     113            $post_formats = $this->get_post_formats();
     114            $create_post_format = reset( $post_formats );
     115        }
     116        return $create_post_format;
     117    }
     118
    110119    public function get_view_post_types() {
    111120        $view_post_types = get_term_meta( $this->term->term_id, 'view_post_types', true );
     
    143152    public function set_create_post_type( $create_post_type ) {
    144153        return update_term_meta( $this->term->term_id, 'create_post_type', $create_post_type );
     154    }
     155
     156    public function set_create_post_format( $create_post_format ) {
     157        return update_term_meta( $this->term->term_id, 'create_post_format', $create_post_format );
    145158    }
    146159
     
    235248
    236249    /**
     250     * Get the text to display to the user about the current settings.
     251     *
     252     * @param      string $content  The content.
     253     *
     254     * @return     string  The current settings.
     255     */
     256    public function get_current_settings_text( string $content = '' ) {
     257        $post_formats = $this->get_post_formats();
     258        $post_format_strings = array_filter(
     259            get_post_format_strings(),
     260            function ( $slug ) use ( $post_formats ) {
     261                return in_array( $slug, $post_formats, true );
     262            },
     263            ARRAY_FILTER_USE_KEY
     264        );
     265
     266        if ( empty( $post_format_strings ) ) {
     267            // translators: %s is a list of post formats.
     268            $content .= PHP_EOL . sprintf( _n( 'Post Format: %s', 'Post Formats: %s', count( get_post_format_strings() ), 'enable-mastodon-apps' ), __( 'All' ) ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
     269        } else {
     270            // translators: %s is a list of post formats.
     271            $content .= PHP_EOL . sprintf( _n( 'Post Format: %s', 'Post Formats: %s', count( $post_format_strings ), 'enable-mastodon-apps' ), implode( ', ', $post_format_strings ) );
     272        }
     273
     274        if ( empty( $post_format_strings ) ) {
     275            $post_format_strings = get_post_format_strings();
     276        }
     277
     278        $content .= PHP_EOL . _x( 'Create new posts as', 'select post type', 'enable-mastodon-apps' ) . ': ';
     279        $content .= get_post_type_object( $this->get_create_post_type() )->labels->singular_name;
     280        $content .= ' ' . _x( 'in the post format', 'select post type', 'enable-mastodon-apps' ) . ': ';
     281        if ( $this->get_create_post_format() && isset( $post_format_strings[ $this->get_create_post_format() ] ) ) {
     282            $content .= $post_format_strings[ $this->get_create_post_format() ];
     283        } else {
     284            $content .= reset( $post_format_strings );
     285        }
     286
     287        $t = PHP_EOL . __( 'Show these post types', 'enable-mastodon-apps' ) . ': ';
     288        foreach ( $this->get_view_post_types() as $post_type ) {
     289            if ( in_array( $post_type, array( Comment_CPT::CPT ), true ) ) {
     290                continue;
     291            }
     292            $content .= $t . get_post_type_object( $post_type )->labels->name;
     293            $t = ', ';
     294        }
     295
     296        if ( $this->get_disable_blocks() ) {
     297            $content .= PHP_EOL . __( 'Automatic conversion to blocks is disabled', 'enable-mastodon-apps' );
     298        }
     299
     300        return trim( $content );
     301    }
     302
     303    /**
    237304     * Posts current settings as an announcement just for this app.
    238305     *
     
    258325            $intro = __( 'The current settings for this app are:', 'enable-mastodon-apps' );
    259326        }
    260         $content = $intro;
    261 
    262         $post_formats = $this->get_post_formats();
    263         $t = PHP_EOL . __( 'Post Formats', 'enable-mastodon-apps' ) . ': ';
    264         foreach ( get_post_format_strings() as $slug => $name ) {
    265             if ( ! in_array( $slug, $post_formats, true ) ) {
    266                 continue;
    267             }
    268             $content .= $t . $name;
    269             $t = ', ';
    270         }
    271         if ( ', ' !== $t ) {
    272             $content .= $t . __( 'All' ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
    273         }
    274 
    275         $content .= PHP_EOL . _x( 'Create new posts as', 'select post type', 'enable-mastodon-apps' ) . ': ';
    276         $content .= get_post_type_object( $this->get_create_post_type() )->labels->singular_name;
    277         $t = PHP_EOL . __( 'Show these post types', 'enable-mastodon-apps' ) . ': ';
    278         foreach ( $this->get_view_post_types() as $post_type ) {
    279             if ( in_array( $post_type, array( Mastodon_API::ANNOUNCE_CPT, Mastodon_API::POST_CPT ), true ) ) {
    280                 continue;
    281             }
    282             $content .= $t . get_post_type_object( $post_type )->labels->name;
    283             $t = ', ';
    284         }
    285 
    286         if ( $this->get_disable_blocks() ) {
    287             $content .= PHP_EOL . __( 'Automatic conversion to blocks is disabled', 'enable-mastodon-apps' );
    288         }
     327        $content = $this->get_current_settings_text( $intro );
    289328
    290329        $previous_posts = get_posts(
     
    305344        }
    306345
    307         return wp_insert_post(
     346        $post_id = wp_insert_post(
    308347            array(
    309348                'post_type'    => Mastodon_API::ANNOUNCE_CPT,
     
    316355            )
    317356        );
     357        if ( $post_id ) {
     358            // Assign all post formats so that it will be shown regardless of the app's (potentially later changed) post format settings.
     359            wp_set_object_terms(
     360                $post_id,
     361                array_map(
     362                    function ( $slug ) {
     363                        return 'post-format-' . $slug;
     364                    },
     365                    get_post_format_slugs()
     366                ),
     367                'post_format'
     368            );
     369        }
     370        return $post_id;
    318371    }
    319372
     
    756809        );
    757810
     811        $post_formats = array();
     812        if ( get_option( 'mastodon_api_default_create_post_format' ) ) {
     813            $post_formats[] = get_option( 'mastodon_api_default_create_post_format' );
     814        }
    758815        /**
    759816         * Post formats to be enabled for new apps.
     
    771828         * ```
    772829         */
    773         $post_formats = apply_filters( 'mastodon_api_new_app_post_formats', array(), $app_metadata );
     830        $post_formats = apply_filters( 'mastodon_api_new_app_post_formats', $post_formats, $app_metadata );
     831
    774832        $app_metadata['query_args'] = array( 'post_formats' => $post_formats );
    775833
     
    778836        if ( ! in_array( $app_metadata['create_post_type'], $view_post_types ) ) {
    779837            $view_post_types[] = $app_metadata['create_post_type'];
     838        }
     839
     840        if ( get_option( 'mastodon_api_default_create_post_format' ) && in_array( get_option( 'mastodon_api_default_create_post_format' ), $post_formats ) ) {
     841            $app_metadata['create_post_format'] = get_option( 'mastodon_api_default_create_post_format' );
    780842        }
    781843
  • enable-mastodon-apps/trunk/includes/handler/class-status.php

    r3245133 r3248503  
    131131        $post = get_post( $object_id );
    132132
    133         if ( Mastodon_API::ANNOUNCE_CPT === $post->post_type ) {
     133        if ( $post && Mastodon_API::ANNOUNCE_CPT === $post->post_type ) {
    134134            $meta = get_post_meta( $post->ID, 'ema_app_id', true );
    135135            $app = Mastodon_App::get_current_app();
  • enable-mastodon-apps/trunk/templates/app.php

    r3245133 r3248503  
    3030);
    3131$app_post_formats = $app->get_post_formats();
     32$selected_post_format = $app->get_create_post_format();
     33if ( ! $selected_post_format ) {
     34    $selected_post_format = 'standard';
     35}
    3236?>
    3337<div class="enable-mastodon-apps-settings enable-mastodon-apps-registered-apps-page <?php echo $args['enable_debug'] ? 'enable-debug' : 'disable-debug'; ?>">
     
    141145                            <?php endforeach; ?>
    142146                        </select>
     147                        <label>
     148                            <?php echo esc_html( _x( 'in the post format', 'select post format', 'enable-mastodon-apps' ) ); ?>
     149                            <select name="create_post_format">
     150                                <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>
     151
     152                                <?php foreach ( get_post_format_strings() as $format => $label ) : ?>
     153                                    <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>
     154                                <?php endforeach; ?>
     155                            </select>
     156                        </label>
     157
    143158                        <p class="description">
    144159                            <span><?php esc_html_e( 'When posting through the app, this post type will be created.', 'enable-mastodon-apps' ); ?></span>
     
    218233            );
    219234            ?>
    220             </button>
    221         <script>
    222             document.getElementById( 'toggle_all_post_formats' ).onclick = function ( event ) {
    223                 document.querySelectorAll( '.post-formats input[type="checkbox"]' ).forEach( function ( element ) {
    224                     element.checked = ! element.checked;
    225                 } );
    226 
    227                 event.preventDefault();
    228                 return false;
    229             }
    230         </script>
     235        </button>
    231236
    232237        <h3><?php esc_html_e( 'Access Tokens', 'enable-mastodon-apps' ); ?></h3>
     
    283288</form>
    284289</div>
     290<script type="text/javascript">
     291document.addEventListener( 'click', function ( event ) {
     292    function updatePostFormatDropdown( el ) {
     293        const postFormats = document.querySelectorAll( '.post-formats input[type="checkbox"]:checked' );
     294        const postFormatSelect = document.querySelector( 'select[name="create_post_format"]' );
     295        const options = postFormatSelect.querySelectorAll( 'option' );
     296
     297        for ( const option of options ) {
     298            if ( option.value === el.value ) {
     299                option.disabled = ! el.checked;
     300                if ( ! el.checked && postFormatSelect.value === el.value ) {
     301                    postFormatSelect.value = '';
     302                }
     303            }
     304        }
     305        const selectedPostFormat = postFormats.length ? postFormats[0] : document.querySelector( '.post-formats input[type="checkbox"]' );
     306        options[0].textContent = options[0].textContent.replace( /\(.*\)/, '(' + selectedPostFormat.nextSibling.textContent.trim() + ')' );
     307    }
     308
     309    if ( event.target.matches( '.post-formats input[type="checkbox"]' ) ) {
     310        return updatePostFormatDropdown( event.target );
     311    }
     312    if ( event.target.id === 'toggle_all_post_formats' ) {
     313        event.preventDefault();
     314        document.querySelectorAll( '.post-formats input[type="checkbox"]' ).forEach( function ( element ) {
     315            element.checked = ! element.checked;
     316            updatePostFormatDropdown( element );
     317        } );
     318        return;
     319    }
     320});
     321</script>
  • enable-mastodon-apps/trunk/templates/registered-apps.php

    r3157710 r3248503  
    4343                <th><?php esc_html_e( 'Name', 'enable-mastodon-apps' ); ?></th>
    4444                <th class="debug-hide"><?php esc_html_e( 'Redirect URI', 'enable-mastodon-apps' ); ?></th>
    45                 <th><?php esc_html_e( 'Scope', 'enable-mastodon-apps' ); ?></th>
    46                 <th class="debug-hide"><?php esc_html_e( 'Post Formats', 'enable-mastodon-apps' ); ?></th>
     45                <th><?php echo esc_html_x( 'Create new posts as', 'select post type', 'enable-mastodon-apps' ); ?></th>
     46                <th><?php echo esc_html_x( 'in the post format', 'select post format', 'enable-mastodon-apps' ); ?></th>
     47                <th><?php esc_html_e( 'Post Formats', 'enable-mastodon-apps' ); ?></th>
     48                <th class="debug-hide"><?php esc_html_e( 'Scope', 'enable-mastodon-apps' ); ?></th>
    4749                <th><?php esc_html_e( 'Last Used', 'enable-mastodon-apps' ); ?></th>
    4850                <th><?php esc_html_e( 'Created', 'enable-mastodon-apps' ); ?></th>
     
    6971                        </td>
    7072                        <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>
    71                         <td><?php echo esc_html( $app->get_scopes() ); ?></td>
    72                         <td class="debug-hide">
     73                        <td>
    7374                            <?php
    74                             foreach ( get_post_format_strings() as $format => $label ) {
    75                                 if ( in_array( $format, $app->get_post_formats() ) ) {
    76                                     echo esc_html( $label ) . '<br>';
     75                            $_post_type = get_post_type_object( $app->get_create_post_type() );
     76                            echo esc_html( $_post_type->labels->singular_name );
     77                            ?>
     78                        </td>
     79                        <td>
     80                            <?php
     81                            if ( ! $app->get_create_post_format() ) {
     82                                echo esc_html_x( 'Standard', 'Post format' ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
     83
     84                            } else {
     85                                foreach ( get_post_format_strings() as $slug => $name ) {
     86                                    if ( $slug === $app->get_create_post_format() ) {
     87                                        echo esc_html( $name );
     88                                        break;
     89                                    }
    7790                                }
    7891                            }
    7992                            ?>
    8093                        </td>
     94                        <td>
     95                            <?php
     96
     97                            $post_formats = $app->get_post_formats();
     98                            if ( empty( $post_formats ) ) {
     99                                echo esc_html( __( 'All' ) ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain
     100                            } else {
     101                                echo esc_html(
     102                                    implode(
     103                                        ', ',
     104                                        array_map(
     105                                            function ( $slug ) {
     106                                                return get_post_format_strings()[ $slug ];
     107                                            },
     108                                            $post_formats
     109                                        )
     110                                    )
     111                                );
     112                            }
     113                            ?>
     114                        </td>
     115                        <td class="debug-hide"><?php echo esc_html( $app->get_scopes() ); ?></td>
    81116                        <?php td_timestamp( $app->get_last_used() ); ?>
    82117                        <?php td_timestamp( $app->get_creation_date() ); ?>
  • enable-mastodon-apps/trunk/templates/settings.php

    r3245133 r3248503  
    3030                </tr>
    3131                <tr>
    32                     <th scope="row"><?php esc_html_e( 'Posting', 'enable-mastodon-apps' ); ?></th>
     32                    <th scope="row" rowspan="2"><?php esc_html_e( 'Posting', 'enable-mastodon-apps' ); ?></th>
    3333                    <td>
    3434                        <fieldset>
     35                            <label for="mastodon_api_no_posting_cpt">
     36                                <input name="mastodon_api_posting_cpt" type="radio" id="mastodon_api_no_posting_cpt" value="1" <?php checked( ! get_option( 'mastodon_api_posting_cpt' ) ); ?> />
     37                                <span><?php esc_html_e( 'Hide posts through Mastodon apps from appearing on the WordPress frontend', 'enable-mastodon-apps' ); ?></span>
     38                            </label>
    3539                            <label for="mastodon_api_posting_cpt">
    36                                 <input name="mastodon_api_posting_cpt" type="checkbox" id="mastodon_api_posting_cpt" value="1" <?php checked( ! get_option( 'mastodon_api_posting_cpt' ) ); ?> />
    37                                 <span><?php esc_html_e( 'Hide posts through Mastodon apps from appearing on the WordPress frontend', 'enable-mastodon-apps' ); ?></span>
     40                                <input name="mastodon_api_posting_cpt" type="radio" id="mastodon_api_posting_cpt" value="0" <?php checked( 'post' === get_option( 'mastodon_api_posting_cpt' ) ); ?> />
     41                                <span><?php esc_html_e( 'Show posts through Mastodon apps on the WordPress frontend', 'enable-mastodon-apps' ); ?></span>
    3842                            </label>
    3943                        </fieldset>
     
    7175                            </span>
    7276                        </p>
     77                    </td>
     78                </tr>
     79                <tr>
     80                    <td>
     81                        <fieldset>
     82                            <label for="mastodon_api_default_create_post_format">
     83                                <?php esc_html_e( 'For new apps, pre-select this post format:', 'enable-mastodon-apps' ); ?>
     84                            </label>
     85                            <select name="mastodon_api_default_create_post_format" id="mastodon_api_default_create_post_format">
     86                                <option value="all" <?php selected( ! get_option( 'mastodon_api_default_create_post_format' ) ); ?>><?php esc_html_e( 'None', 'enable-mastodon-apps' ); ?></option>
     87                                <?php foreach ( get_post_format_strings() as $format => $label ) : ?>
     88                                    <option value="<?php echo esc_attr( $format ); ?>" <?php selected( $format, get_option( 'mastodon_api_default_create_post_format' ) ); ?>><?php echo esc_html( $label ); ?></option>
     89                                <?php endforeach; ?>
     90                            </select>
     91                            <p class="description">
     92                                <?php esc_html_e( 'If no post format is selected, posts in all post formats will be displayed.', 'enable-mastodon-apps' ); ?>
     93                            </p>
     94                        </fieldset>
    7395                    </td>
    7496                </tr>
Note: See TracChangeset for help on using the changeset viewer.