Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Core_Bg_Override
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 4
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 do_upgrade
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
42
 restore_theme_mods
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
30
 __call
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName,Squiz.Commenting.FileComment.Missing
2
3/**
4 * Core Background Override class.
5 */
6class Core_Bg_Override {
7
8    /**
9     * Holds the theme mods before the Customizer saves
10     *
11     * @var array
12     */
13    private $mods = array();
14
15    /**
16     * The Core Background properties we're interested in
17     *
18     * @var array
19     */
20    private $props = array(
21        'background_image',
22        'background_color',
23        'background_repeat',
24        'background_position_x',
25        'background_attachment',
26        'background_image_thumb',
27    );
28
29    /**
30     * The name of the theme_mod we'll save our background properties in
31     *
32     * @var string
33     */
34    private $post_upgrade_parity_mod = 'custom-colors-parity';
35
36    /**
37     * Crude constructor. Sets up the actions for whether we're in a save override context
38     * or the default display override context
39     *
40     * @param boolean $save_overrides Whether or not we're in "save override" context.
41     */
42    public function __construct( $save_overrides = false ) {
43        $this->mods = get_theme_mods();
44
45        if ( $save_overrides ) {
46            add_action( 'shutdown', array( $this, 'restore_theme_mods' ) );
47        } else {
48            foreach ( $this->props as $prop ) {
49                add_filter( "theme_mod_$prop", array( $this, $prop ) );
50            }
51        }
52    }
53
54    /**
55     * Called when we want the user upgrades to Custom Design. Permanently sets the parity
56     * we were heretofore achieving with filters.
57     *
58     * @return void
59     */
60    public function do_upgrade() {
61        // first, let's ditch those filters that we don't need, however this was invoked.
62        remove_action( 'shutdown', array( $this, 'restore_theme_mods' ) );
63        foreach ( $this->props as $prop ) {
64                remove_filter( "theme_mod_$prop", array( $this, $prop ) );
65        }
66
67        // now, time for parity
68        $previewed_bg_mods = get_theme_mod( $this->post_upgrade_parity_mod, false );
69        // nothing set? bail.
70        if ( ! is_array( $previewed_bg_mods ) ) {
71            return;
72        }
73
74        // if the preview mod had it active, set it. otherwise, remove it
75        foreach ( $this->props as $prop ) {
76            if ( isset( $previewed_bg_mods[ $prop ] ) ) {
77                set_theme_mod( $prop, $previewed_bg_mods[ $prop ] );
78            } else {
79                remove_theme_mod( $prop );
80            }
81        }
82
83        // background_image is a special case - we need to set it to a blank string
84        // even if we didn't have something - otherwise we might get stuck with a default bg image
85        if ( ! isset( $previewed_bg_mods['background_image'] ) ) {
86            set_theme_mod( 'background_image', '' );
87        }
88
89        // remove our preview mod
90        remove_theme_mod( $this->post_upgrade_parity_mod );
91    }
92
93    /**
94     * This is where we restore the background theme mods that were saved
95     * while previewing Colors and save the previewed mods for future display parity.
96     *
97     * @return void
98     */
99    public function restore_theme_mods() {
100        $pre_save            = $this->mods;
101        $post_save           = get_theme_mods();
102        $post_upgrade_parity = array();
103
104        foreach ( $this->props as $prop ) {
105
106            // we're going to store the saved value for usage after the upgrade has been purchased.
107            if ( isset( $post_save[ $prop ] ) ) {
108                $post_upgrade_parity[ $prop ] = $post_save[ $prop ];
109            }
110
111            if ( ! isset( $pre_save[ $prop ] ) ) {
112                // case 1: it wasn't set originally. unset it again.
113                remove_theme_mod( $prop );
114            } elseif ( $post_save[ $prop ] !== $pre_save[ $prop ] ) {
115                // case 2: the value has changed. change it back.
116                set_theme_mod( $prop, $pre_save[ $prop ] );
117            }
118        }
119
120        set_theme_mod( $this->post_upgrade_parity_mod, $post_upgrade_parity );
121    }
122
123    /**
124     * Magic method for our preview filters for all core bg theme_mods. DRY FTW.
125     *
126     * @param  string $name The method we're faking. Conveniently also the property we're overriding.
127     *                      We do this for all members of $this->props.
128     * @param  array  $args The arguments called on the method.
129     * @return mixed string|bool The overridden value, if one is applicable, otherwise false.
130     */
131    public function __call( $name, $args ) {
132        $value     = $args[0];
133        $overrides = get_theme_mod( $this->post_upgrade_parity_mod, false );
134        // we don't have any overrides. Just return
135        if ( ! is_array( $overrides ) ) {
136            return $value;
137        }
138        return $overrides[ $name ] ?? $value;
139    }
140}