Skip to content

Move remaining screen options logic to Screen_Options class #1957

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

Merged
merged 4 commits into from
Jul 18, 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
80 changes: 80 additions & 0 deletions includes/wp-admin/class-screen-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Screen_Options {
*/
public static function init() {
\add_filter( 'set-screen-option', array( self::class, 'set_per_page_option' ), 10, 3 );
\add_filter( 'screen_settings', array( self::class, 'add_screen_option' ), 10, 2 );
\add_filter( 'screen_options_show_submit', array( self::class, 'screen_options_show_submit' ), 10, 2 );
}

/**
Expand Down Expand Up @@ -87,4 +89,82 @@ public static function set_per_page_option( $status, $option, $value ) {

return $status;
}

/**
* Add screen options.
*
* @param string $screen_settings The screen settings.
* @param object $screen The screen object.
*
* @return string The screen settings.
*/
public static function add_screen_option( $screen_settings, $screen ) {
if ( 'settings_page_activitypub' !== $screen->id ) {
return $screen_settings;
}

// Verify screen options nonce.
if ( isset( $_POST['screenoptionnonce'] ) ) {
$nonce = \sanitize_text_field( \wp_unslash( $_POST['screenoptionnonce'] ) );
if ( ! \wp_verify_nonce( $nonce, 'screen-options-nonce' ) ) {
return $screen_settings;
}
}

$screen_options = array(
'activitypub_show_welcome_tab' => __( 'Welcome Page', 'activitypub' ),
'activitypub_show_advanced_tab' => __( 'Advanced Settings', 'activitypub' ),
);

/**
* Filters Activitypub settings screen options.
*
* @param string[] $screen_options Screen options. An array of user meta keys and screen option labels.
*/
$screen_options = \apply_filters( 'activitypub_screen_options', $screen_options );
if ( empty( $screen_options ) ) {
return $screen_settings;
}

foreach ( $screen_options as $option => $label ) {
if ( isset( $_POST[ $option ] ) ) {
$value = \sanitize_text_field( \wp_unslash( $_POST[ $option ] ) );
\update_user_meta( \get_current_user_id(), $option, empty( $value ) ? 0 : 1 );
}
}

ob_start();
?>
<fieldset>
<legend class="screen-layout"><?php \esc_html_e( 'Settings Pages', 'activitypub' ); ?></legend>
<div class="metabox-prefs-container">
<?php foreach ( $screen_options as $option => $label ) : ?>
<label for="<?php echo \esc_attr( $option ); ?>">
<input name="<?php echo \esc_attr( $option ); ?>" type="hidden" value="0" />
<input name="<?php echo \esc_attr( $option ); ?>" type="checkbox" id="<?php echo \esc_attr( $option ); ?>" value="1" <?php \checked( 1, \get_user_meta( \get_current_user_id(), $option, true ) ); ?> />
<?php echo \esc_html( $label ); ?>
</label>
<?php endforeach; ?>
</div>
</fieldset>
<?php

return ob_get_clean();
}

/**
* Show the submit button on the screen options page.
*
* @param bool $show_submit Whether to show the submit button.
* @param object $screen The screen object.
*
* @return bool Whether to show the submit button.
*/
public static function screen_options_show_submit( $show_submit, $screen ) {
if ( 'settings_page_activitypub' !== $screen->id ) {
return $show_submit;
}

return true;
}
}
81 changes: 0 additions & 81 deletions includes/wp-admin/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public static function init() {
\add_action( 'admin_menu', array( self::class, 'add_settings_page' ) );

\add_action( 'load-settings_page_activitypub', array( self::class, 'handle_welcome_query_arg' ) );
\add_filter( 'screen_settings', array( self::class, 'add_screen_option' ), 10, 2 );
\add_filter( 'screen_options_show_submit', array( self::class, 'screen_options_show_submit' ), 10, 2 );
}

/**
Expand Down Expand Up @@ -546,85 +544,6 @@ public static function handle_welcome_query_arg() {
}
}

/**
* Add screen option.
*
* @param string $screen_settings The screen settings.
* @param object $screen The screen object.
*
* @return string The screen settings.
*/
public static function add_screen_option( $screen_settings, $screen ) {
if ( 'settings_page_activitypub' !== $screen->id ) {
return $screen_settings;
}

// Verify screen options nonce.
if ( isset( $_POST['screenoptionnonce'] ) ) {
$nonce = \sanitize_text_field( \wp_unslash( $_POST['screenoptionnonce'] ) );
if ( ! \wp_verify_nonce( $nonce, 'screen-options-nonce' ) ) {
return $screen_settings;
}
}

$screen_options = array(
'activitypub_show_welcome_tab' => __( 'Welcome Page', 'activitypub' ),
'activitypub_show_advanced_tab' => __( 'Advanced Settings', 'activitypub' ),
);

/**
* Filters Activitypub settings screen options.
*
* @param string[] $screen_options Screen options. An array of user meta keys and screen option labels.
*/
$screen_options = \apply_filters( 'activitypub_screen_options', $screen_options );
if ( empty( $screen_options ) ) {
return $screen_settings;
}

foreach ( $screen_options as $option => $label ) {
if ( isset( $_POST[ $option ] ) ) {
$value = \sanitize_text_field( \wp_unslash( $_POST[ $option ] ) );
\update_user_meta( \get_current_user_id(), $option, empty( $value ) ? 0 : 1 );
}
}

ob_start();
?>
<fieldset>
<legend class="screen-layout"><?php \esc_html_e( 'Settings Pages', 'activitypub' ); ?></legend>
<p><?php \esc_html_e( 'Some settings pages can be shown or hidden by using the checkboxes.', 'activitypub' ); ?></p>
<div class="metabox-prefs-container">
<?php foreach ( $screen_options as $option => $label ) : ?>
<label for="<?php echo \esc_attr( $option ); ?>">
<input name="<?php echo \esc_attr( $option ); ?>" type="hidden" value="0" />
<input name="<?php echo \esc_attr( $option ); ?>" type="checkbox" id="<?php echo \esc_attr( $option ); ?>" value="1" <?php \checked( 1, \get_user_meta( \get_current_user_id(), $option, true ) ); ?> />
<?php echo \esc_html( $label ); ?>
</label>
<?php endforeach; ?>
</div>
</fieldset>
<?php

return ob_get_clean();
}

/**
* Show the submit button on the screen options page.
*
* @param bool $show_submit Whether to show the submit button.
* @param object $screen The screen object.
*
* @return bool Whether to show the submit button.
*/
public static function screen_options_show_submit( $show_submit, $screen ) {
if ( 'settings_page_activitypub' !== $screen->id ) {
return $show_submit;
}

return true;
}

/**
* Returns an array of recommended plugins for ActivityPub.
*/
Expand Down
Loading