assets
1 week ago
BuilderPanel.php
1 week ago
Compiler.php
1 week ago
Engine.php
1 week ago
FrontendEnqueue.php
1 week ago
Migrator.php
1 week ago
Palettes.php
1 week ago
PreviewDraft.php
1 week ago
RestController.php
1 week ago
Sanitizer.php
1 week ago
Schema.php
1 week ago
Templates.php
1 week ago
Palettes.php
193 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Style Customizer v2 — reusable custom colour palettes (v2 store + live v1 carry-over). |
| 4 | * |
| 5 | * @package EverestForms\Addons\StyleCustomizer\V2 |
| 6 | * @since x.x.x |
| 7 | */ |
| 8 | |
| 9 | namespace EverestForms\Addons\StyleCustomizer\V2; |
| 10 | |
| 11 | defined( 'ABSPATH' ) || exit; |
| 12 | |
| 13 | /** |
| 14 | * Reusable custom colour palettes. |
| 15 | */ |
| 16 | final class Palettes { |
| 17 | |
| 18 | /** |
| 19 | * Option holding v2 user-created custom palettes (newest first). |
| 20 | */ |
| 21 | const USER_OPTION = 'everest_forms_style_v2_user_palettes'; |
| 22 | |
| 23 | /** |
| 24 | * The v1 custom-palette option, read (never written) for carry-over. |
| 25 | */ |
| 26 | const LEGACY_OPTION = 'everest_forms_custom_color_palettes'; |
| 27 | |
| 28 | /** |
| 29 | * Id prefix for a v1 carry-over palette (suffixed with its index). |
| 30 | */ |
| 31 | const LEGACY_PREFIX = 'legacy-palette-'; |
| 32 | |
| 33 | /** |
| 34 | * The six palette slots, in display order, each with a fallback colour. Hardcoded (not read |
| 35 | * from Schema) because this class runs inside the `evf_style_palettes` filter. |
| 36 | */ |
| 37 | const SLOTS = array( |
| 38 | 'form_background' => '#ffffff', |
| 39 | 'field_background' => '#f6f6f6', |
| 40 | 'field_label' => '#333333', |
| 41 | 'field_sublabel' => '#666666', |
| 42 | 'button_text' => '#ffffff', |
| 43 | 'button_background' => '#3951a5', |
| 44 | ); |
| 45 | |
| 46 | /** |
| 47 | * Hook the custom palettes into the engine's palette list. |
| 48 | */ |
| 49 | public static function register() { |
| 50 | add_filter( 'evf_style_palettes', array( __CLASS__, 'inject' ) ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Prepend the custom palettes to the built-in list. |
| 55 | * |
| 56 | * @param array $palettes Built-in palette definitions. |
| 57 | * @return array |
| 58 | */ |
| 59 | public static function inject( $palettes ) { |
| 60 | if ( ! is_array( $palettes ) ) { |
| 61 | return $palettes; |
| 62 | } |
| 63 | return array_merge( self::all_custom(), $palettes ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Every custom palette — v2 user palettes (newest first) then any v1 carry-over — normalized |
| 68 | * to a built-in-palette shape with `is_custom`/`is_pro` set. |
| 69 | * |
| 70 | * @return array |
| 71 | */ |
| 72 | public static function all_custom() { |
| 73 | $out = array(); |
| 74 | foreach ( (array) get_option( self::USER_OPTION, array() ) as $entry ) { |
| 75 | $palette = self::normalize_entry( $entry ); |
| 76 | if ( $palette ) { |
| 77 | $out[] = $palette; |
| 78 | } |
| 79 | } |
| 80 | return array_merge( $out, self::legacy_palettes() ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * v1 custom palette(s), read live from {@see self::LEGACY_OPTION} with stable index-based ids. |
| 85 | * |
| 86 | * @return array |
| 87 | */ |
| 88 | protected static function legacy_palettes() { |
| 89 | $stored = get_option( self::LEGACY_OPTION, array() ); |
| 90 | if ( ! is_array( $stored ) ) { |
| 91 | return array(); |
| 92 | } |
| 93 | $out = array(); |
| 94 | foreach ( $stored as $i => $entry ) { |
| 95 | if ( empty( $entry['colors'] ) || ! is_array( $entry['colors'] ) ) { |
| 96 | continue; |
| 97 | } |
| 98 | $name = isset( $entry['label'] ) ? (string) $entry['label'] : __( 'My palette', 'everest-forms' ); |
| 99 | $out[] = array( |
| 100 | 'id' => self::LEGACY_PREFIX . (int) $i, |
| 101 | 'name' => $name, |
| 102 | 'is_pro' => true, |
| 103 | 'is_custom' => true, |
| 104 | 'colors' => self::sanitize_colors( $entry['colors'] ), |
| 105 | ); |
| 106 | } |
| 107 | return $out; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Normalize a stored {@see self::USER_OPTION} entry, or null if malformed. |
| 112 | * |
| 113 | * @param mixed $entry Stored entry. |
| 114 | * @return array|null |
| 115 | */ |
| 116 | protected static function normalize_entry( $entry ) { |
| 117 | if ( ! is_array( $entry ) || empty( $entry['id'] ) || empty( $entry['colors'] ) || ! is_array( $entry['colors'] ) ) { |
| 118 | return null; |
| 119 | } |
| 120 | return array( |
| 121 | 'id' => (string) $entry['id'], |
| 122 | 'name' => isset( $entry['name'] ) ? (string) $entry['name'] : __( 'My palette', 'everest-forms' ), |
| 123 | 'is_pro' => true, |
| 124 | 'is_custom' => true, |
| 125 | 'colors' => self::sanitize_colors( $entry['colors'] ), |
| 126 | ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Delete a custom palette. A `legacy-palette-{i}` id removes the entry from the v1 option. |
| 131 | * |
| 132 | * @param string $id Palette id. |
| 133 | * @return bool Whether anything was removed. |
| 134 | */ |
| 135 | public static function delete( $id ) { |
| 136 | $id = (string) $id; |
| 137 | |
| 138 | if ( 0 === strpos( $id, self::LEGACY_PREFIX ) ) { |
| 139 | return self::delete_legacy( (int) substr( $id, strlen( self::LEGACY_PREFIX ) ) ); |
| 140 | } |
| 141 | |
| 142 | $stored = get_option( self::USER_OPTION, array() ); |
| 143 | if ( ! is_array( $stored ) ) { |
| 144 | return false; |
| 145 | } |
| 146 | $next = array_values( |
| 147 | array_filter( |
| 148 | $stored, |
| 149 | static function ( $entry ) use ( $id ) { |
| 150 | return ! isset( $entry['id'] ) || (string) $entry['id'] !== $id; |
| 151 | } |
| 152 | ) |
| 153 | ); |
| 154 | if ( count( $next ) === count( $stored ) ) { |
| 155 | return false; |
| 156 | } |
| 157 | update_option( self::USER_OPTION, $next, false ); |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Remove one entry from the v1 option, re-indexing the survivors. |
| 163 | * |
| 164 | * @param int $index Index in {@see self::LEGACY_OPTION}. |
| 165 | * @return bool |
| 166 | */ |
| 167 | protected static function delete_legacy( $index ) { |
| 168 | $stored = get_option( self::LEGACY_OPTION, array() ); |
| 169 | if ( ! is_array( $stored ) || ! isset( $stored[ $index ] ) ) { |
| 170 | return false; |
| 171 | } |
| 172 | unset( $stored[ $index ] ); |
| 173 | update_option( self::LEGACY_OPTION, array_values( $stored ) ); |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Coerce a raw colour map to exactly the six known slots, each validated via |
| 179 | * {@see Sanitizer::color()}; unknown slots dropped, missing/invalid fall to the slot default. |
| 180 | * |
| 181 | * @param mixed $colors Raw colour map. |
| 182 | * @return array |
| 183 | */ |
| 184 | public static function sanitize_colors( $colors ) { |
| 185 | $colors = is_array( $colors ) ? $colors : array(); |
| 186 | $out = array(); |
| 187 | foreach ( self::SLOTS as $slot => $fallback ) { |
| 188 | $out[ $slot ] = Sanitizer::color( isset( $colors[ $slot ] ) ? $colors[ $slot ] : '', $fallback ); |
| 189 | } |
| 190 | return $out; |
| 191 | } |
| 192 | } |
| 193 |