Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Sharing_Options
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 is_available
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 update
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * Reads and saves the sharing buttons' global options.
4 *
5 * @package automattic/jetpack-sharing-likes
6 */
7
8declare( strict_types = 1 );
9
10namespace Automattic\Jetpack\Sharing_Likes\Settings;
11
12/**
13 * Button style, label and link target, stored in `sharing-options['global']`.
14 *
15 * Every write goes through `Sharing_Service`, which only loads with the legacy
16 * sharing buttons, so callers check `is_available()` first.
17 */
18final class Sharing_Options {
19
20    /**
21     * Values `Sharing_Service::set_global_options()` accepts for `button_style`.
22     */
23    public const BUTTON_STYLES = array( 'icon-text', 'icon', 'text', 'official' );
24
25    /**
26     * Whether sharedaddy is loaded to read and save these options.
27     */
28    public static function is_available(): bool {
29        return class_exists( 'Sharing_Service' );
30    }
31
32    /**
33     * The options as sharedaddy applies them, defaults included.
34     *
35     * @return array{button_style: string, sharing_label: string, open_links: string}
36     */
37    public static function get(): array {
38        $global = ( new \Sharing_Service() )->get_global_options();
39
40        return array(
41            'button_style'  => (string) ( $global['button_style'] ?? 'icon-text' ),
42            'sharing_label' => (string) ( $global['sharing_label'] ?? '' ),
43            'open_links'    => (string) ( $global['open_links'] ?? 'same' ),
44        );
45    }
46
47    /**
48     * Save the given options and keep the stored value of every other one.
49     *
50     * `Sharing_Service::set_global_options()` rebuilds the global array from defaults,
51     * so anything the payload leaves out, placement included, would be reset.
52     *
53     * @param array<string, mixed> $changes Options to change, slashed as in `$_POST` and keyed as `set_global_options()` reads them.
54     * @return array<string, mixed> The global options as saved.
55     */
56    public static function update( array $changes ): array {
57        $current = self::get();
58        if ( Placement_Section::is_saved() ) {
59            $current['show'] = Placement_Section::selected_post_types();
60        }
61        $current['sharing_label'] = wp_slash( $current['sharing_label'] );
62        $sharer                   = new \Sharing_Service();
63        $options                  = array_merge( $current, $changes );
64
65        // The default is stored as `false` only on an exact match, checked before unslashing.
66        if ( is_string( $options['sharing_label'] ) && wp_unslash( $options['sharing_label'] ) === $sharer->default_sharing_label ) {
67            $options['sharing_label'] = $sharer->default_sharing_label;
68        }
69
70        return $sharer->set_global_options( $options );
71    }
72}