| 1 |
<?php |
| 2 |
/** |
| 3 |
* Screen Options file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\WP_Admin; |
| 9 |
|
| 10 |
/** |
| 11 |
* ActivityPub Screen Options Class. |
| 12 |
*/ |
| 13 |
class Screen_Options { |
| 14 |
/** |
| 15 |
* Initialize the class. |
| 16 |
*/ |
| 17 |
public static function init() { |
| 18 |
\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 ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Add settings list screen options. |
| 25 |
* |
| 26 |
* @see Menu::admin_menu() |
| 27 |
*/ |
| 28 |
public static function add_settings_list_options() { |
| 29 |
$tab = \sanitize_text_field( \wp_unslash( $_GET['tab'] ?? 'welcome' ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 30 |
|
| 31 |
switch ( $tab ) { |
| 32 |
case 'followers': |
| 33 |
self::add_followers_list_options(); |
| 34 |
break; |
| 35 |
case 'following': |
| 36 |
self::add_following_list_options(); |
| 37 |
break; |
| 38 |
case 'blocked-actors': |
| 39 |
self::add_blocked_actors_list_options(); |
| 40 |
break; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Add follower list screen options. |
| 46 |
* |
| 47 |
* @see Menu::admin_menu() |
| 48 |
*/ |
| 49 |
public static function add_followers_list_options() { |
| 50 |
\add_screen_option( |
| 51 |
'per_page', |
| 52 |
array( |
| 53 |
'label' => \__( 'Followers per page', 'activitypub' ), |
| 54 |
'default' => 20, |
| 55 |
'option' => 'activitypub_followers_per_page', |
| 56 |
) |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Add screen options for following list. |
| 62 |
* |
| 63 |
* @see Menu::admin_menu() |
| 64 |
*/ |
| 65 |
public static function add_following_list_options() { |
| 66 |
\add_screen_option( |
| 67 |
'per_page', |
| 68 |
array( |
| 69 |
'label' => \__( 'Following per page', 'activitypub' ), |
| 70 |
'default' => 20, |
| 71 |
'option' => 'activitypub_following_per_page', |
| 72 |
) |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Add screen options for blocked actors list. |
| 78 |
* |
| 79 |
* @see Menu::admin_menu() |
| 80 |
*/ |
| 81 |
public static function add_blocked_actors_list_options() { |
| 82 |
\add_screen_option( |
| 83 |
'per_page', |
| 84 |
array( |
| 85 |
'label' => \__( 'Blocked Actors per page', 'activitypub' ), |
| 86 |
'default' => 20, |
| 87 |
'option' => 'activitypub_blocked_actors_per_page', |
| 88 |
) |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Set per_page screen options. |
| 94 |
* |
| 95 |
* @param mixed $status Screen option value. Default false to skip. |
| 96 |
* @param string $option The option name. |
| 97 |
* @param mixed $value The option value. |
| 98 |
* @return int |
| 99 |
*/ |
| 100 |
public static function set_per_page_option( $status, $option, $value ) { |
| 101 |
if ( 'activitypub_followers_per_page' === $option || 'activitypub_following_per_page' === $option || 'activitypub_blocked_actors_per_page' === $option ) { |
| 102 |
$value = (int) $value; |
| 103 |
|
| 104 |
if ( $value > 0 && $value <= 100 ) { |
| 105 |
return $value; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
return $status; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Add screen options. |
| 114 |
* |
| 115 |
* @param string $screen_settings The screen settings. |
| 116 |
* @param object $screen The screen object. |
| 117 |
* |
| 118 |
* @return string The screen settings. |
| 119 |
*/ |
| 120 |
public static function add_screen_option( $screen_settings, $screen ) { |
| 121 |
if ( 'settings_page_activitypub' !== $screen->id ) { |
| 122 |
return $screen_settings; |
| 123 |
} |
| 124 |
|
| 125 |
// No screen options on followers, following, and blocked-actors tabs. The per_page screen options interfere with them. |
| 126 |
if ( \in_array( \sanitize_text_field( \wp_unslash( $_GET['tab'] ?? 'welcome' ) ), array( 'followers', 'following', 'blocked-actors' ), true ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 127 |
return $screen_settings; |
| 128 |
} |
| 129 |
|
| 130 |
// Verify screen options nonce. |
| 131 |
if ( isset( $_POST['screenoptionnonce'] ) ) { |
| 132 |
$nonce = \sanitize_text_field( \wp_unslash( $_POST['screenoptionnonce'] ) ); |
| 133 |
if ( ! \wp_verify_nonce( $nonce, 'screen-options-nonce' ) ) { |
| 134 |
return $screen_settings; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
$screen_options = array( |
| 139 |
'activitypub_show_welcome_tab' => \__( 'Welcome Page', 'activitypub' ), |
| 140 |
'activitypub_show_advanced_tab' => \__( 'Advanced Settings', 'activitypub' ), |
| 141 |
); |
| 142 |
|
| 143 |
/** |
| 144 |
* Filters Activitypub settings screen options. |
| 145 |
* |
| 146 |
* @param string[] $screen_options Screen options. An array of user meta keys and screen option labels. |
| 147 |
*/ |
| 148 |
$screen_options = \apply_filters( 'activitypub_screen_options', $screen_options ); |
| 149 |
if ( empty( $screen_options ) ) { |
| 150 |
return $screen_settings; |
| 151 |
} |
| 152 |
|
| 153 |
foreach ( $screen_options as $option => $label ) { |
| 154 |
if ( isset( $_POST[ $option ] ) ) { |
| 155 |
$value = \sanitize_text_field( \wp_unslash( $_POST[ $option ] ) ); |
| 156 |
\update_user_meta( \get_current_user_id(), $option, empty( $value ) ? 0 : 1 ); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
\ob_start(); |
| 161 |
?> |
| 162 |
<fieldset> |
| 163 |
<legend class="screen-layout"><?php \esc_html_e( 'Settings Pages', 'activitypub' ); ?></legend> |
| 164 |
<div class="metabox-prefs-container"> |
| 165 |
<?php foreach ( $screen_options as $option => $label ) : ?> |
| 166 |
<label for="<?php echo \esc_attr( $option ); ?>"> |
| 167 |
<input name="<?php echo \esc_attr( $option ); ?>" type="hidden" value="0" /> |
| 168 |
<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 ) ); ?> /> |
| 169 |
<?php echo \esc_html( $label ); ?> |
| 170 |
</label> |
| 171 |
<?php endforeach; ?> |
| 172 |
</div> |
| 173 |
</fieldset> |
| 174 |
<?php |
| 175 |
|
| 176 |
return \ob_get_clean(); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Show the submit button on the screen options page. |
| 181 |
* |
| 182 |
* @param bool $show_submit Whether to show the submit button. |
| 183 |
* @param object $screen The screen object. |
| 184 |
* |
| 185 |
* @return bool Whether to show the submit button. |
| 186 |
*/ |
| 187 |
public static function screen_options_show_submit( $show_submit, $screen ) { |
| 188 |
if ( 'settings_page_activitypub' !== $screen->id ) { |
| 189 |
return $show_submit; |
| 190 |
} |
| 191 |
|
| 192 |
return true; |
| 193 |
} |
| 194 |
} |
| 195 |
|