Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
GlobalStylesService
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 5
182
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
6
 set_color_palette
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 transform_color_palette
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
20
 get_user_global_styles
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 update_active_global_styles
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace NewfoldLabs\WP\Module\Onboarding\Services;
4
5class GlobalStylesService {
6
7    /**
8     * Decoded global styles post content object.
9     *
10     * @var array
11     */
12    protected array $global_styles;
13
14    /**
15     * Active global styles post ID.
16     *
17     * @var int
18     */
19    protected int $global_styles_id;
20
21    /**
22     * Global Styles Service constructor.
23     *
24     * @return GlobalStylesService
25     */
26    public function __construct() {
27        $user_global_styles = self::get_user_global_styles();
28        if ( $user_global_styles ) {
29            $this->global_styles    = json_decode( $user_global_styles['post_content'], true );
30            $this->global_styles_id = $user_global_styles['ID'];
31        }
32        return $this;
33    }
34
35    /**
36     * Set the color palette.
37     *
38     * @param array $color_palette The color palette to set.
39     * @return array The color palette on success, \WP_Error on failure.
40     */
41    public function set_color_palette( array $color_palette ): array|\WP_Error {
42        try {
43            $color_palette = self::transform_color_palette( $color_palette );
44            if ( empty( $color_palette ) ) {
45                throw new \Exception( 'Invalid color palette' );
46            }
47
48            // Update the color palette in the global styles.
49            $this->global_styles['settings']['color']['palette']['theme'] = $color_palette;
50
51            $result = $this->update_active_global_styles();
52            if ( is_wp_error( $result ) ) {
53                throw new \Exception( $result->get_error_message() );
54            }
55
56            return $color_palette;
57        } catch ( \Exception $e ) {
58            return new \WP_Error( 'set_color_palette_error', $e->getMessage() );
59        }
60    }
61
62    /**
63     * Transform color palette object to ensure it is valid.
64     *
65     * @param array $color_palette The color palette to process.
66     * @return array The processed color palette.
67     */
68    public static function transform_color_palette( array $color_palette ) {
69        $result = array();
70
71        foreach ( $color_palette as $color ) {
72            if ( ! isset( $color['slug'] ) || ! isset( $color['color'] ) ) {
73                continue;
74            }
75
76            $color_slug  = str_replace( '_', '-', $color['slug'] );
77            $color_name  = $color['name'] ?? $color_slug;
78            $color_value = $color['color'];
79
80            $result[] = array(
81                'slug'  => $color_slug,
82                'name'  => $color_name,
83                'color' => $color_value,
84            );
85        }
86
87        return $result;
88    }
89
90    /**
91     * Get the user global styles.
92     *
93     * @return array The user global styles.
94     */
95    public function get_user_global_styles(): array {
96        return \WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( wp_get_theme() );
97    }
98
99    /**
100     * Update global styles.
101     *
102     * @param int|null   $post_id The global styles post id to update. If not provided, the active global styles will be updated.
103     * @param array|null $post_content array of global styles to inject into the post content. If not provided, the active global styles will be updated.
104     * @return int|\WP_Error The updated post id on success, \WP_Error on failure.
105     */
106    protected function update_active_global_styles( ?array $post_content = null, ?int $post_id = null ): int|\WP_Error {
107        $post_id      = $post_id ?? $this->global_styles_id;
108        $post_content = $post_content ?? $this->global_styles;
109
110        $result = wp_update_post( [
111            'ID'           => $post_id,
112            'post_content' => wp_json_encode( $post_content ),
113        ], true );
114        if ( is_wp_error( $result ) ) {
115            return $result;
116        }
117
118        wp_clean_theme_json_cache();
119
120        return $result;
121    }
122}
123// $sitegen_state = \get_option( Options::get_option_name( 'state_sitegen' ) );