| 1 |
<?php |
| 2 |
/** |
| 3 |
* Style Presets |
| 4 |
* |
| 5 |
* Registers the Style Presets Custom Post Type for storing |
| 6 |
* visual styling configurations per style (Button, Icon, Cover). |
| 7 |
* |
| 8 |
* @package CTC |
| 9 |
* @since 5.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace CTC\Global_Injector; |
| 13 |
|
| 14 |
/** |
| 15 |
* Style Presets |
| 16 |
* |
| 17 |
* @since 5.0.0 |
| 18 |
*/ |
| 19 |
class Style_Presets { |
| 20 |
|
| 21 |
/** |
| 22 |
* Instance |
| 23 |
* |
| 24 |
* @since 5.0.0 |
| 25 |
* @var object Class object. |
| 26 |
*/ |
| 27 |
private static $instance; |
| 28 |
|
| 29 |
/** |
| 30 |
* Post type name |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
const POST_TYPE = 'ctc-style-preset'; |
| 35 |
|
| 36 |
/** |
| 37 |
* Maximum presets per style for free users |
| 38 |
* |
| 39 |
* @var int |
| 40 |
*/ |
| 41 |
const MAX_FREE_PRESETS_PER_STYLE = 3; |
| 42 |
|
| 43 |
/** |
| 44 |
* Initiator |
| 45 |
* |
| 46 |
* @since 5.0.0 |
| 47 |
* @return object initialized object of class. |
| 48 |
*/ |
| 49 |
public static function get() { |
| 50 |
if ( null === self::$instance ) { |
| 51 |
self::$instance = new self(); |
| 52 |
} |
| 53 |
return self::$instance; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Constructor |
| 58 |
* |
| 59 |
* @since 5.0.0 |
| 60 |
*/ |
| 61 |
public function __construct() { |
| 62 |
add_action( 'init', [ $this, 'register_post_type' ] ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Register custom post type |
| 67 |
* |
| 68 |
* @since 5.0.0 |
| 69 |
* @return void |
| 70 |
*/ |
| 71 |
public function register_post_type() { |
| 72 |
$labels = [ |
| 73 |
'name' => _x( 'Style Presets', 'post type general name', 'ctc' ), |
| 74 |
'singular_name' => _x( 'Style Preset', 'post type singular name', 'ctc' ), |
| 75 |
'menu_name' => _x( 'Style Presets', 'admin menu', 'ctc' ), |
| 76 |
'add_new' => _x( 'Add New', 'style preset', 'ctc' ), |
| 77 |
'add_new_item' => __( 'Add New Style Preset', 'ctc' ), |
| 78 |
'edit_item' => __( 'Edit Style Preset', 'ctc' ), |
| 79 |
'new_item' => __( 'New Style Preset', 'ctc' ), |
| 80 |
'view_item' => __( 'View Style Preset', 'ctc' ), |
| 81 |
'search_items' => __( 'Search Style Presets', 'ctc' ), |
| 82 |
'not_found' => __( 'No style presets found', 'ctc' ), |
| 83 |
'not_found_in_trash' => __( 'No style presets found in trash', 'ctc' ), |
| 84 |
]; |
| 85 |
|
| 86 |
$args = [ |
| 87 |
'labels' => $labels, |
| 88 |
'public' => false, |
| 89 |
'publicly_queryable' => false, |
| 90 |
'show_ui' => false, |
| 91 |
'show_in_menu' => false, |
| 92 |
'query_var' => false, |
| 93 |
'rewrite' => false, |
| 94 |
'capability_type' => 'post', |
| 95 |
'has_archive' => false, |
| 96 |
'hierarchical' => false, |
| 97 |
'menu_position' => null, |
| 98 |
'supports' => [ 'title' ], |
| 99 |
'show_in_rest' => false, // We use custom REST endpoints. |
| 100 |
]; |
| 101 |
|
| 102 |
register_post_type( self::POST_TYPE, $args ); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Get default presets (hardcoded) |
| 107 |
* |
| 108 |
* @since 5.0.0 |
| 109 |
* @return array Default presets by style. |
| 110 |
*/ |
| 111 |
public static function get_default_presets() { |
| 112 |
return [ |
| 113 |
'button' => [ |
| 114 |
'id' => 'default', |
| 115 |
'title' => __( 'System Default', 'ctc' ), |
| 116 |
'visual_style' => 'button', |
| 117 |
'is_default' => true, |
| 118 |
'text_color' => '#ffffff', |
| 119 |
'bg_color' => '#4f46e5', |
| 120 |
'border_radius' => 6, |
| 121 |
'font_size' => 13, |
| 122 |
'padding_x' => 16, |
| 123 |
'padding_y' => 8, |
| 124 |
], |
| 125 |
'icon' => [ |
| 126 |
'id' => 'default', |
| 127 |
'title' => __( 'System Default', 'ctc' ), |
| 128 |
'visual_style' => 'icon', |
| 129 |
'is_default' => true, |
| 130 |
'icon_color' => '#6b7280', |
| 131 |
'icon_hover_color' => '#4f46e5', |
| 132 |
'bg_color' => '#ffffff', |
| 133 |
'border_color' => '#e5e7eb', |
| 134 |
'icon_size' => 16, |
| 135 |
'padding' => 8, |
| 136 |
'border_radius' => 8, |
| 137 |
'border_width' => 1, |
| 138 |
], |
| 139 |
'cover' => [ |
| 140 |
'id' => 'default', |
| 141 |
'title' => __( 'System Default', 'ctc' ), |
| 142 |
'visual_style' => 'cover', |
| 143 |
'is_default' => true, |
| 144 |
'overlay_color' => '#0f172a', |
| 145 |
'text_color' => '#0f172a', |
| 146 |
'badge_bg_color' => '#ffffff', |
| 147 |
'hover_overlay_color' => '#4f46e5', |
| 148 |
'overlay_opacity' => 10, |
| 149 |
'blur' => 2, |
| 150 |
'font_size' => 10, |
| 151 |
'badge_radius' => 9999, |
| 152 |
], |
| 153 |
]; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Get presets count by style |
| 158 |
* |
| 159 |
* @since 5.0.0 |
| 160 |
* @param string $visual_style Style type (button, icon, cover). |
| 161 |
* @return int Number of presets for this style. |
| 162 |
*/ |
| 163 |
public static function get_preset_count_by_style( $visual_style ) { |
| 164 |
$query = new \WP_Query( |
| 165 |
[ |
| 166 |
'post_type' => self::POST_TYPE, |
| 167 |
'post_status' => 'publish', |
| 168 |
'posts_per_page' => -1, |
| 169 |
'meta_query' => [ |
| 170 |
[ |
| 171 |
'key' => 'visual_style', |
| 172 |
'value' => $visual_style, |
| 173 |
], |
| 174 |
], |
| 175 |
'fields' => 'ids', |
| 176 |
] |
| 177 |
); |
| 178 |
|
| 179 |
return $query->found_posts; |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Check if user can create preset for style |
| 184 |
* |
| 185 |
* @since 5.0.0 |
| 186 |
* @param string $visual_style Style type. |
| 187 |
* @param bool $is_pro Whether user has Pro. |
| 188 |
* @return bool |
| 189 |
*/ |
| 190 |
public static function can_create_preset( $visual_style, $is_pro = false ) { |
| 191 |
if ( $is_pro ) { |
| 192 |
return true; |
| 193 |
} |
| 194 |
|
| 195 |
$count = self::get_preset_count_by_style( $visual_style ); |
| 196 |
return $count < self::MAX_FREE_PRESETS_PER_STYLE; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Load all presets |
| 201 |
* |
| 202 |
* @since 5.0.0 |
| 203 |
* @return array All presets grouped by style. |
| 204 |
*/ |
| 205 |
public static function load_all_presets() { |
| 206 |
$presets = [ |
| 207 |
'button' => [], |
| 208 |
'icon' => [], |
| 209 |
'cover' => [], |
| 210 |
]; |
| 211 |
|
| 212 |
$query = new \WP_Query( |
| 213 |
[ |
| 214 |
'post_type' => self::POST_TYPE, |
| 215 |
'post_status' => 'publish', |
| 216 |
'posts_per_page' => -1, |
| 217 |
'orderby' => 'date', |
| 218 |
'order' => 'ASC', |
| 219 |
] |
| 220 |
); |
| 221 |
|
| 222 |
if ( $query->have_posts() ) { |
| 223 |
foreach ( $query->posts as $post ) { |
| 224 |
$preset = self::format_preset( $post ); |
| 225 |
$style = $preset['visual_style']; |
| 226 |
if ( isset( $presets[ $style ] ) ) { |
| 227 |
$presets[ $style ][] = $preset; |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
return $presets; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Format preset data from post |
| 237 |
* |
| 238 |
* @since 5.0.0 |
| 239 |
* @param \WP_Post $post Post object. |
| 240 |
* @return array Formatted preset data. |
| 241 |
*/ |
| 242 |
public static function format_preset( $post ) { |
| 243 |
$id = $post->ID; |
| 244 |
$visual_style = get_post_meta( $id, 'visual_style', true ) ?: 'button'; |
| 245 |
|
| 246 |
$base = [ |
| 247 |
'id' => $id, |
| 248 |
'title' => $post->post_title, |
| 249 |
'visual_style' => $visual_style, |
| 250 |
'is_default' => false, |
| 251 |
]; |
| 252 |
|
| 253 |
// Load style-specific meta fields. |
| 254 |
switch ( $visual_style ) { |
| 255 |
case 'icon': |
| 256 |
return array_merge( |
| 257 |
$base, |
| 258 |
[ |
| 259 |
'icon_color' => get_post_meta( $id, 'icon_color', true ) ?: '#6b7280', |
| 260 |
'icon_hover_color' => get_post_meta( $id, 'icon_hover_color', true ) ?: '#4f46e5', |
| 261 |
'bg_color' => get_post_meta( $id, 'bg_color', true ) ?: '#ffffff', |
| 262 |
'border_color' => get_post_meta( $id, 'border_color', true ) ?: '#e5e7eb', |
| 263 |
'icon_size' => (int) get_post_meta( $id, 'icon_size', true ) ?: 16, |
| 264 |
'padding' => (int) get_post_meta( $id, 'padding', true ) ?: 8, |
| 265 |
'border_radius' => (int) get_post_meta( $id, 'border_radius', true ) ?: 8, |
| 266 |
'border_width' => (int) get_post_meta( $id, 'border_width', true ) ?: 1, |
| 267 |
] |
| 268 |
); |
| 269 |
|
| 270 |
case 'cover': |
| 271 |
return array_merge( |
| 272 |
$base, |
| 273 |
[ |
| 274 |
'overlay_color' => get_post_meta( $id, 'overlay_color', true ) ?: '#0f172a', |
| 275 |
'text_color' => get_post_meta( $id, 'text_color', true ) ?: '#0f172a', |
| 276 |
'badge_bg_color' => get_post_meta( $id, 'badge_bg_color', true ) ?: '#ffffff', |
| 277 |
'hover_overlay_color' => get_post_meta( $id, 'hover_overlay_color', true ) ?: '#4f46e5', |
| 278 |
'overlay_opacity' => (int) get_post_meta( $id, 'overlay_opacity', true ) ?: 10, |
| 279 |
'blur' => (int) get_post_meta( $id, 'blur', true ) ?: 2, |
| 280 |
'font_size' => (int) get_post_meta( $id, 'font_size', true ) ?: 10, |
| 281 |
'badge_radius' => (int) get_post_meta( $id, 'badge_radius', true ) ?: 9999, |
| 282 |
] |
| 283 |
); |
| 284 |
|
| 285 |
case 'button': |
| 286 |
default: |
| 287 |
return array_merge( |
| 288 |
$base, |
| 289 |
[ |
| 290 |
'text_color' => get_post_meta( $id, 'text_color', true ) ?: '#ffffff', |
| 291 |
'bg_color' => get_post_meta( $id, 'bg_color', true ) ?: '#4f46e5', |
| 292 |
'border_color' => get_post_meta( $id, 'border_color', true ) ?: '#4338ca', |
| 293 |
'border_radius' => (int) get_post_meta( $id, 'border_radius', true ) ?: 6, |
| 294 |
'font_size' => (int) get_post_meta( $id, 'font_size', true ) ?: 13, |
| 295 |
'padding_x' => (int) get_post_meta( $id, 'padding_x', true ) ?: 16, |
| 296 |
'padding_y' => (int) get_post_meta( $id, 'padding_y', true ) ?: 8, |
| 297 |
] |
| 298 |
); |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
|