Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
135 / 135 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| Settings_Controller | |
100.00% |
135 / 135 |
|
100.00% |
6 / 6 |
30 | |
100.00% |
1 / 1 |
| register_routes | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
1 | |||
| get_item | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| update_item | |
100.00% |
36 / 36 |
|
100.00% |
1 / 1 |
13 | |||
| available_settings | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
11 | |||
| read | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
| get_item_schema | |
100.00% |
44 / 44 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * The settings route behind Settings > Sharing. |
| 4 | * |
| 5 | * @package automattic/jetpack-sharing-likes |
| 6 | */ |
| 7 | |
| 8 | declare( strict_types = 1 ); |
| 9 | |
| 10 | namespace Automattic\Jetpack\Sharing_Likes\REST; |
| 11 | |
| 12 | use Automattic\Jetpack\Sharing_Likes\Settings\Comment_Likes_Section; |
| 13 | use Automattic\Jetpack\Sharing_Likes\Settings\Environment; |
| 14 | use Automattic\Jetpack\Sharing_Likes\Settings\Likes_Options; |
| 15 | use Automattic\Jetpack\Sharing_Likes\Settings\Likes_Section; |
| 16 | use Automattic\Jetpack\Sharing_Likes\Settings\Placement_Section; |
| 17 | use Automattic\Jetpack\Sharing_Likes\Settings\Section_State; |
| 18 | use Automattic\Jetpack\Sharing_Likes\Settings\Sharing_Options; |
| 19 | use Automattic\Jetpack\Sharing_Likes\Settings\Sharing_Resources; |
| 20 | use Automattic\Jetpack\Sharing_Likes\Settings\Sharing_Section; |
| 21 | use Automattic\Jetpack\Sharing_Likes\Settings\Twitter_Site_Tag; |
| 22 | use WP_Error; |
| 23 | use WP_REST_Request; |
| 24 | use WP_REST_Response; |
| 25 | use WP_REST_Server; |
| 26 | |
| 27 | /** |
| 28 | * Reads and saves every setting the screen shows, and only those. |
| 29 | * |
| 30 | * A setting its section does not render is left out of reads and refused on writes, |
| 31 | * so the API offers no way back where `BLOCK_CALL_TO_ACTION` deliberately has none. |
| 32 | */ |
| 33 | final class Settings_Controller extends Controller { |
| 34 | |
| 35 | /** |
| 36 | * The options `Sharing_Options::update()` saves together. |
| 37 | */ |
| 38 | private const SHARING_OPTIONS = array( 'button_style', 'sharing_label' ); |
| 39 | |
| 40 | /** |
| 41 | * Register the route. |
| 42 | */ |
| 43 | public function register_routes() { |
| 44 | register_rest_route( |
| 45 | $this->namespace, |
| 46 | '/' . Endpoints::BASE . '/settings', |
| 47 | array( |
| 48 | array( |
| 49 | 'methods' => WP_REST_Server::READABLE, |
| 50 | 'callback' => array( $this, 'get_item' ), |
| 51 | 'permission_callback' => array( $this, 'permission_check' ), |
| 52 | ), |
| 53 | array( |
| 54 | 'methods' => WP_REST_Server::EDITABLE, |
| 55 | 'callback' => array( $this, 'update_item' ), |
| 56 | 'permission_callback' => array( $this, 'permission_check' ), |
| 57 | // Core skips its validate callback for `null`, then sanitizes it to `false` or `''`; this one validates first. |
| 58 | 'args' => array_map( |
| 59 | function ( $arg ) { |
| 60 | return array_merge( $arg, array( 'sanitize_callback' => 'rest_parse_request_arg' ) ); |
| 61 | }, |
| 62 | $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ) |
| 63 | ), |
| 64 | ), |
| 65 | ) |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Every setting this site's screen shows. |
| 71 | * |
| 72 | * @param WP_REST_Request $request Request. |
| 73 | * @return WP_REST_Response |
| 74 | */ |
| 75 | public function get_item( $request ) { |
| 76 | unset( $request ); |
| 77 | |
| 78 | return rest_ensure_response( self::read( self::available_settings() ) ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Save the settings the request carries and leave every other one as stored. |
| 83 | * |
| 84 | * @param WP_REST_Request $request Request. |
| 85 | * @return WP_REST_Response|WP_Error |
| 86 | */ |
| 87 | public function update_item( $request ) { |
| 88 | $sent = array(); |
| 89 | |
| 90 | foreach ( array_keys( $this->get_item_schema()['properties'] ) as $key ) { |
| 91 | if ( $request->has_param( $key ) ) { |
| 92 | $sent[ $key ] = $request->get_param( $key ); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | $unavailable = array_values( array_diff( array_keys( $sent ), self::available_settings() ) ); |
| 97 | |
| 98 | if ( $unavailable ) { |
| 99 | return new WP_Error( |
| 100 | 'rest_sharing_likes_setting_unavailable', |
| 101 | /* translators: %s: comma-separated list of setting names. */ |
| 102 | sprintf( __( 'These settings are not available on this site: %s.', 'jetpack-sharing-likes' ), implode( ', ', $unavailable ) ), |
| 103 | array( |
| 104 | 'status' => 400, |
| 105 | 'params' => $unavailable, |
| 106 | ) |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | // First, because it is the one write that can fail, and a failure should leave everything else as it was. |
| 111 | if ( isset( $sent['comment_likes_enabled'] ) && ! Comment_Likes_Section::update( $sent['comment_likes_enabled'] ) ) { |
| 112 | return new WP_Error( |
| 113 | 'rest_sharing_likes_comment_likes_unchanged', |
| 114 | __( 'Comment Likes could not be switched on or off on this site.', 'jetpack-sharing-likes' ), |
| 115 | array( 'status' => 409 ) |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | $sharing = array_intersect_key( $sent, array_flip( self::SHARING_OPTIONS ) ); |
| 120 | |
| 121 | if ( $sharing ) { |
| 122 | // `set_global_options()` unslashes the label, as it was written for `$_POST`. |
| 123 | if ( isset( $sharing['sharing_label'] ) ) { |
| 124 | $sharing['sharing_label'] = wp_slash( $sharing['sharing_label'] ); |
| 125 | } |
| 126 | |
| 127 | Sharing_Options::update( $sharing ); |
| 128 | } |
| 129 | |
| 130 | // After the sharing options, which rewrite the global array placement lives in. |
| 131 | if ( isset( $sent['show'] ) ) { |
| 132 | Placement_Section::update( $sent['show'] ); |
| 133 | } |
| 134 | |
| 135 | if ( isset( $sent['likes_enabled'] ) ) { |
| 136 | Likes_Options::set_likes_enabled( $sent['likes_enabled'] ); |
| 137 | } |
| 138 | |
| 139 | if ( isset( $sent['reblogs_enabled'] ) ) { |
| 140 | Likes_Options::set_reblogs_enabled( $sent['reblogs_enabled'] ); |
| 141 | } |
| 142 | |
| 143 | if ( isset( $sent['twitter_site_tag'] ) ) { |
| 144 | Twitter_Site_Tag::update( $sent['twitter_site_tag'] ); |
| 145 | } |
| 146 | |
| 147 | if ( isset( $sent['disable_resources'] ) ) { |
| 148 | Sharing_Resources::update( $sent['disable_resources'] ); |
| 149 | } |
| 150 | |
| 151 | return $this->get_item( $request ); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * The settings the screen shows on this site, following the sections that render them. |
| 156 | * |
| 157 | * @return string[] |
| 158 | */ |
| 159 | private static function available_settings(): array { |
| 160 | $sharing_state = Sharing_Section::state(); |
| 161 | $likes_state = Likes_Section::state(); |
| 162 | $likes_configure = Section_State::configures( $likes_state ); |
| 163 | $comment_likes_follow = Environment::comment_likes_follow_likes_settings(); |
| 164 | $settings = array(); |
| 165 | |
| 166 | // With the Like buttons section showing no options, the Comment Likes section carries the sitewide default. |
| 167 | if ( $likes_configure || $comment_likes_follow ) { |
| 168 | $settings[] = 'likes_enabled'; |
| 169 | } |
| 170 | |
| 171 | if ( $likes_configure && Environment::is_simple_site() ) { |
| 172 | $settings[] = 'reblogs_enabled'; |
| 173 | } |
| 174 | |
| 175 | // Comments have no Like block to move to, so no section variant takes this one away. |
| 176 | if ( Environment::likes_supported() ) { |
| 177 | $settings[] = 'comment_likes_enabled'; |
| 178 | } |
| 179 | |
| 180 | if ( Sharing_Options::is_available() && Section_State::configures( $sharing_state ) ) { |
| 181 | array_push( $settings, ...self::SHARING_OPTIONS ); |
| 182 | } |
| 183 | |
| 184 | if ( Section_State::shows_placement( $sharing_state, $likes_state, $comment_likes_follow ) ) { |
| 185 | $settings[] = 'show'; |
| 186 | } |
| 187 | |
| 188 | if ( Twitter_Site_Tag::is_available() ) { |
| 189 | $settings[] = 'twitter_site_tag'; |
| 190 | } |
| 191 | |
| 192 | if ( Sharing_Resources::is_available() ) { |
| 193 | $settings[] = 'disable_resources'; |
| 194 | } |
| 195 | |
| 196 | return $settings; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Current values of the given settings. |
| 201 | * |
| 202 | * @param string[] $settings Setting names. |
| 203 | * @return array<string, mixed> |
| 204 | */ |
| 205 | private static function read( array $settings ): array { |
| 206 | $values = array(); |
| 207 | |
| 208 | if ( array_intersect( self::SHARING_OPTIONS, $settings ) ) { |
| 209 | $values = Sharing_Options::get(); |
| 210 | // Stored through `wp_kses()`, which encodes a bare `&`; the screen shows it decoded. |
| 211 | $values['sharing_label'] = wp_specialchars_decode( $values['sharing_label'], ENT_QUOTES ); |
| 212 | } |
| 213 | |
| 214 | $values['likes_enabled'] = Likes_Options::likes_enabled_sitewide(); |
| 215 | $values['reblogs_enabled'] = Likes_Options::reblogs_enabled_sitewide(); |
| 216 | $values['comment_likes_enabled'] = Environment::comment_likes_enabled(); |
| 217 | $values['show'] = Placement_Section::selected_post_types(); |
| 218 | $values['twitter_site_tag'] = (string) get_option( Twitter_Site_Tag::OPTION, '' ); |
| 219 | $values['disable_resources'] = (bool) get_option( Sharing_Resources::OPTION ); |
| 220 | |
| 221 | return array_intersect_key( $values, array_flip( $settings ) ); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * The settings record. |
| 226 | * |
| 227 | * @return array<string, mixed> |
| 228 | */ |
| 229 | public function get_item_schema() { |
| 230 | if ( $this->schema ) { |
| 231 | return $this->schema; |
| 232 | } |
| 233 | |
| 234 | $this->schema = array( |
| 235 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 236 | 'title' => 'sharing-likes-settings', |
| 237 | 'type' => 'object', |
| 238 | 'properties' => array( |
| 239 | 'likes_enabled' => array( |
| 240 | 'description' => __( 'Whether posts show Like buttons.', 'jetpack-sharing-likes' ), |
| 241 | 'type' => 'boolean', |
| 242 | ), |
| 243 | 'reblogs_enabled' => array( |
| 244 | 'description' => __( 'Whether posts show Reblog buttons. WordPress.com Simple only.', 'jetpack-sharing-likes' ), |
| 245 | 'type' => 'boolean', |
| 246 | ), |
| 247 | 'comment_likes_enabled' => array( |
| 248 | 'description' => __( 'Whether readers can like individual comments.', 'jetpack-sharing-likes' ), |
| 249 | 'type' => 'boolean', |
| 250 | ), |
| 251 | 'button_style' => array( |
| 252 | 'description' => __( 'How sharing buttons look.', 'jetpack-sharing-likes' ), |
| 253 | 'type' => 'string', |
| 254 | 'enum' => Sharing_Options::BUTTON_STYLES, |
| 255 | ), |
| 256 | 'sharing_label' => array( |
| 257 | 'description' => __( 'Text shown above the sharing buttons.', 'jetpack-sharing-likes' ), |
| 258 | 'type' => 'string', |
| 259 | ), |
| 260 | 'show' => array( |
| 261 | 'description' => __( 'Post types the buttons appear on, plus "index" for the front page, archives and search results.', 'jetpack-sharing-likes' ), |
| 262 | 'type' => 'array', |
| 263 | 'items' => array( 'type' => 'string' ), |
| 264 | ), |
| 265 | 'twitter_site_tag' => array( |
| 266 | 'description' => __( 'X (Twitter) username of the site owner, without the @.', 'jetpack-sharing-likes' ), |
| 267 | 'type' => 'string', |
| 268 | ), |
| 269 | 'disable_resources' => array( |
| 270 | 'description' => __( 'Whether the sharing buttons skip their own CSS and JS.', 'jetpack-sharing-likes' ), |
| 271 | 'type' => 'boolean', |
| 272 | ), |
| 273 | ), |
| 274 | ); |
| 275 | |
| 276 | return $this->schema; |
| 277 | } |
| 278 | } |