Skip to content

Commit 213efba

Browse files
authored
Move remaining screen options logic to Screen_Options class (#1957)
1 parent b0c4459 commit 213efba

File tree

2 files changed

+80
-81
lines changed

2 files changed

+80
-81
lines changed

includes/wp-admin/class-screen-options.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class Screen_Options {
1616
*/
1717
public static function init() {
1818
\add_filter( 'set-screen-option', array( self::class, 'set_per_page_option' ), 10, 3 );
19+
\add_filter( 'screen_settings', array( self::class, 'add_screen_option' ), 10, 2 );
20+
\add_filter( 'screen_options_show_submit', array( self::class, 'screen_options_show_submit' ), 10, 2 );
1921
}
2022

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

8890
return $status;
8991
}
92+
93+
/**
94+
* Add screen options.
95+
*
96+
* @param string $screen_settings The screen settings.
97+
* @param object $screen The screen object.
98+
*
99+
* @return string The screen settings.
100+
*/
101+
public static function add_screen_option( $screen_settings, $screen ) {
102+
if ( 'settings_page_activitypub' !== $screen->id ) {
103+
return $screen_settings;
104+
}
105+
106+
// Verify screen options nonce.
107+
if ( isset( $_POST['screenoptionnonce'] ) ) {
108+
$nonce = \sanitize_text_field( \wp_unslash( $_POST['screenoptionnonce'] ) );
109+
if ( ! \wp_verify_nonce( $nonce, 'screen-options-nonce' ) ) {
110+
return $screen_settings;
111+
}
112+
}
113+
114+
$screen_options = array(
115+
'activitypub_show_welcome_tab' => __( 'Welcome Page', 'activitypub' ),
116+
'activitypub_show_advanced_tab' => __( 'Advanced Settings', 'activitypub' ),
117+
);
118+
119+
/**
120+
* Filters Activitypub settings screen options.
121+
*
122+
* @param string[] $screen_options Screen options. An array of user meta keys and screen option labels.
123+
*/
124+
$screen_options = \apply_filters( 'activitypub_screen_options', $screen_options );
125+
if ( empty( $screen_options ) ) {
126+
return $screen_settings;
127+
}
128+
129+
foreach ( $screen_options as $option => $label ) {
130+
if ( isset( $_POST[ $option ] ) ) {
131+
$value = \sanitize_text_field( \wp_unslash( $_POST[ $option ] ) );
132+
\update_user_meta( \get_current_user_id(), $option, empty( $value ) ? 0 : 1 );
133+
}
134+
}
135+
136+
ob_start();
137+
?>
138+
<fieldset>
139+
<legend class="screen-layout"><?php \esc_html_e( 'Settings Pages', 'activitypub' ); ?></legend>
140+
<div class="metabox-prefs-container">
141+
<?php foreach ( $screen_options as $option => $label ) : ?>
142+
<label for="<?php echo \esc_attr( $option ); ?>">
143+
<input name="<?php echo \esc_attr( $option ); ?>" type="hidden" value="0" />
144+
<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 ) ); ?> />
145+
<?php echo \esc_html( $label ); ?>
146+
</label>
147+
<?php endforeach; ?>
148+
</div>
149+
</fieldset>
150+
<?php
151+
152+
return ob_get_clean();
153+
}
154+
155+
/**
156+
* Show the submit button on the screen options page.
157+
*
158+
* @param bool $show_submit Whether to show the submit button.
159+
* @param object $screen The screen object.
160+
*
161+
* @return bool Whether to show the submit button.
162+
*/
163+
public static function screen_options_show_submit( $show_submit, $screen ) {
164+
if ( 'settings_page_activitypub' !== $screen->id ) {
165+
return $show_submit;
166+
}
167+
168+
return true;
169+
}
90170
}

includes/wp-admin/class-settings.php

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ public static function init() {
2424
\add_action( 'admin_menu', array( self::class, 'add_settings_page' ) );
2525

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

3129
/**
@@ -546,85 +544,6 @@ public static function handle_welcome_query_arg() {
546544
}
547545
}
548546

549-
/**
550-
* Add screen option.
551-
*
552-
* @param string $screen_settings The screen settings.
553-
* @param object $screen The screen object.
554-
*
555-
* @return string The screen settings.
556-
*/
557-
public static function add_screen_option( $screen_settings, $screen ) {
558-
if ( 'settings_page_activitypub' !== $screen->id ) {
559-
return $screen_settings;
560-
}
561-
562-
// Verify screen options nonce.
563-
if ( isset( $_POST['screenoptionnonce'] ) ) {
564-
$nonce = \sanitize_text_field( \wp_unslash( $_POST['screenoptionnonce'] ) );
565-
if ( ! \wp_verify_nonce( $nonce, 'screen-options-nonce' ) ) {
566-
return $screen_settings;
567-
}
568-
}
569-
570-
$screen_options = array(
571-
'activitypub_show_welcome_tab' => __( 'Welcome Page', 'activitypub' ),
572-
'activitypub_show_advanced_tab' => __( 'Advanced Settings', 'activitypub' ),
573-
);
574-
575-
/**
576-
* Filters Activitypub settings screen options.
577-
*
578-
* @param string[] $screen_options Screen options. An array of user meta keys and screen option labels.
579-
*/
580-
$screen_options = \apply_filters( 'activitypub_screen_options', $screen_options );
581-
if ( empty( $screen_options ) ) {
582-
return $screen_settings;
583-
}
584-
585-
foreach ( $screen_options as $option => $label ) {
586-
if ( isset( $_POST[ $option ] ) ) {
587-
$value = \sanitize_text_field( \wp_unslash( $_POST[ $option ] ) );
588-
\update_user_meta( \get_current_user_id(), $option, empty( $value ) ? 0 : 1 );
589-
}
590-
}
591-
592-
ob_start();
593-
?>
594-
<fieldset>
595-
<legend class="screen-layout"><?php \esc_html_e( 'Settings Pages', 'activitypub' ); ?></legend>
596-
<p><?php \esc_html_e( 'Some settings pages can be shown or hidden by using the checkboxes.', 'activitypub' ); ?></p>
597-
<div class="metabox-prefs-container">
598-
<?php foreach ( $screen_options as $option => $label ) : ?>
599-
<label for="<?php echo \esc_attr( $option ); ?>">
600-
<input name="<?php echo \esc_attr( $option ); ?>" type="hidden" value="0" />
601-
<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 ) ); ?> />
602-
<?php echo \esc_html( $label ); ?>
603-
</label>
604-
<?php endforeach; ?>
605-
</div>
606-
</fieldset>
607-
<?php
608-
609-
return ob_get_clean();
610-
}
611-
612-
/**
613-
* Show the submit button on the screen options page.
614-
*
615-
* @param bool $show_submit Whether to show the submit button.
616-
* @param object $screen The screen object.
617-
*
618-
* @return bool Whether to show the submit button.
619-
*/
620-
public static function screen_options_show_submit( $show_submit, $screen ) {
621-
if ( 'settings_page_activitypub' !== $screen->id ) {
622-
return $show_submit;
623-
}
624-
625-
return true;
626-
}
627-
628547
/**
629548
* Returns an array of recommended plugins for ActivityPub.
630549
*/

0 commit comments

Comments
 (0)