| 1 |
<?php |
| 2 |
namespace Templately\Core; |
| 3 |
|
| 4 |
use Templately\Core\Developer\NetworkAdmin; |
| 5 |
use Templately\Core\Developer\ApiManager; |
| 6 |
use Templately\Core\Developer\TestEndpoints; |
| 7 |
use Templately\Utils\Base; |
| 8 |
|
| 9 |
/** |
| 10 |
* Developer Feature Controller |
| 11 |
* |
| 12 |
* Centralized management of all developer-only features and functionality. |
| 13 |
* This class serves as the main controller for developer mode features. |
| 14 |
* |
| 15 |
* @since 3.3.4 |
| 16 |
*/ |
| 17 |
class Developer extends Base { |
| 18 |
|
| 19 |
/** |
| 20 |
* @var ApiManager |
| 21 |
*/ |
| 22 |
public $api_manager; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var NetworkAdmin |
| 26 |
*/ |
| 27 |
public $network_admin; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var TestEndpoints |
| 31 |
*/ |
| 32 |
public $test_endpoints; |
| 33 |
|
| 34 |
/** |
| 35 |
* Cached constant override status captured before constants are defined |
| 36 |
* |
| 37 |
* @var array|null |
| 38 |
*/ |
| 39 |
private static $cached_override_status = null; |
| 40 |
|
| 41 |
/** |
| 42 |
* Available developer constants and their descriptions |
| 43 |
* |
| 44 |
* @var array |
| 45 |
*/ |
| 46 |
private static $available_constants = [ |
| 47 |
'TEMPLATELY_DEV' => [ |
| 48 |
'label' => 'Development Mode', |
| 49 |
'description' => 'Enable general development mode features', |
| 50 |
'type' => 'boolean', |
| 51 |
], |
| 52 |
'TEMPLATELY_DEV_API' => [ |
| 53 |
'label' => 'Development API', |
| 54 |
'description' => 'Use development API endpoints instead of production', |
| 55 |
'type' => 'boolean', |
| 56 |
], |
| 57 |
'TEMPLATELY_DEBUG_LOG' => [ |
| 58 |
'label' => 'Debug Logging', |
| 59 |
'description' => 'Enable detailed debug logging for troubleshooting', |
| 60 |
'default' => true, |
| 61 |
'type' => 'boolean', |
| 62 |
], |
| 63 |
'TEMPLATELY_IGNORE_AI_ANIMATE' => [ |
| 64 |
'label' => 'Ignore AI Animations', |
| 65 |
'description' => 'Skip AI animation effects for faster development', |
| 66 |
'default' => true, |
| 67 |
'type' => 'boolean', |
| 68 |
], |
| 69 |
'TEMPLATELY_DEV_AI_ALL_IMAGES' => [ |
| 70 |
'label' => 'Show All AI Image Types', |
| 71 |
'description' => 'Show all image attachment types in AI conversations including backgrounds, icons, and other non-photo images. By default, only photo-type attachments are displayed.', |
| 72 |
'default' => true, |
| 73 |
'type' => 'boolean', |
| 74 |
], |
| 75 |
'IMPORT_DEBUG' => [ |
| 76 |
'label' => 'Import Debug', |
| 77 |
'description' => 'Enable debugging for import operations', |
| 78 |
'type' => 'boolean', |
| 79 |
], |
| 80 |
'TEMPLATELY_HTTP_RETRY' => [ |
| 81 |
'label' => 'HTTP Retry Count', |
| 82 |
'description' => 'Number of times to retry failed HTTP requests', |
| 83 |
'type' => 'number', |
| 84 |
'min' => 1, |
| 85 |
'max' => 10, |
| 86 |
], |
| 87 |
'WP_DEBUG' => [ |
| 88 |
'label' => 'WordPress Debug', |
| 89 |
'description' => 'Enable WordPress debug mode', |
| 90 |
'default' => true, |
| 91 |
'type' => 'boolean', |
| 92 |
], |
| 93 |
'SCRIPT_DEBUG' => [ |
| 94 |
'label' => 'Script Debug', |
| 95 |
'description' => 'Use unminified scripts for debugging', |
| 96 |
'default' => true, |
| 97 |
'type' => 'boolean', |
| 98 |
], |
| 99 |
]; |
| 100 |
|
| 101 |
/** |
| 102 |
* Initialize developer functionality |
| 103 |
*/ |
| 104 |
public function __construct() { |
| 105 |
// Only initialize if developer mode is enabled |
| 106 |
if ( ! $this->is_developer_mode_enabled() ) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
// Cache override status before defining constants |
| 111 |
self::get_constant_override_status(); |
| 112 |
|
| 113 |
$this->define_constants(); |
| 114 |
|
| 115 |
$this->init_hooks(); |
| 116 |
$this->init_modules(); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Check if developer mode is enabled |
| 121 |
* |
| 122 |
* @return bool |
| 123 |
*/ |
| 124 |
public static function is_developer_mode_enabled() { |
| 125 |
return defined( 'TEMPLATELY_DEVELOPER_MODE' ) && constant( 'TEMPLATELY_DEVELOPER_MODE' ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Initialize hooks for developer functionality |
| 130 |
*/ |
| 131 |
private function init_hooks() { |
| 132 |
// Filter for developer mode localized data |
| 133 |
add_filter( 'templately_admin_localized_data', [ $this, 'filter_localized_data' ] ); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Initialize developer modules |
| 138 |
*/ |
| 139 |
private function init_modules() { |
| 140 |
// Initialize API manager for development API endpoint handling |
| 141 |
$this->api_manager = new ApiManager(); |
| 142 |
|
| 143 |
// Initialize network admin functionality |
| 144 |
$this->network_admin = NetworkAdmin::get_instance(); |
| 145 |
|
| 146 |
// Initialize test endpoints for Playwright testing |
| 147 |
$this->test_endpoints = new TestEndpoints(); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Define developer constants |
| 152 |
*/ |
| 153 |
private function define_constants() { |
| 154 |
$settings = self::get_settings(); |
| 155 |
foreach ( self::$available_constants as $constant => $args ) { |
| 156 |
if ( defined( $constant ) ) { |
| 157 |
continue; |
| 158 |
} |
| 159 |
if ( !empty( $settings[ $constant ] ) ) { |
| 160 |
define( $constant, $settings[ $constant ] ); |
| 161 |
} elseif ( !isset($settings[ $constant ]) && isset( $args['default'] ) ) { |
| 162 |
define( $constant, $args['default'] ); |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Filter localized data to add developer mode flag |
| 169 |
* |
| 170 |
* @param array $data The localized data array |
| 171 |
* @return array Modified localized data |
| 172 |
*/ |
| 173 |
public function filter_localized_data( $data ) { |
| 174 |
$data['developer_mode'] = $this->is_developer_mode_enabled(); |
| 175 |
|
| 176 |
// Add developer-related constants to localized data |
| 177 |
$data['log'] = defined( 'TEMPLATELY_DEBUG_LOG' ) && constant( 'TEMPLATELY_DEBUG_LOG' ); |
| 178 |
$data['dev_mode'] = defined( 'TEMPLATELY_DEV' ) && constant( 'TEMPLATELY_DEV' ); |
| 179 |
$data['ai_animate_ignore_mode'] = defined( 'TEMPLATELY_IGNORE_AI_ANIMATE' ) && constant( 'TEMPLATELY_IGNORE_AI_ANIMATE' ); |
| 180 |
$data['dev_ai_all_images'] = defined( 'TEMPLATELY_DEV_AI_ALL_IMAGES' ) && constant( 'TEMPLATELY_DEV_AI_ALL_IMAGES' ); |
| 181 |
|
| 182 |
return $data; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Get current developer settings with priority order: |
| 187 |
* 1. Defined constant value (highest priority) |
| 188 |
* 2. Saved database value (fallback) |
| 189 |
* 3. Default value from constant definition (final fallback) |
| 190 |
* |
| 191 |
* @return array |
| 192 |
*/ |
| 193 |
public static function get_settings() { |
| 194 |
$final_settings = []; |
| 195 |
|
| 196 |
// Get saved settings from database |
| 197 |
$saved_settings = get_option( 'templately_developer_settings', [] ); |
| 198 |
|
| 199 |
foreach ( self::$available_constants as $constant => $args ) { |
| 200 |
$default = $args['default'] ?? false; |
| 201 |
|
| 202 |
// Priority 1: Use defined constant value (highest priority) |
| 203 |
if ( defined( $constant ) ) { |
| 204 |
$final_settings[ $constant ] = constant( $constant ); |
| 205 |
} |
| 206 |
// Priority 2: Use saved database value (fallback) |
| 207 |
elseif ( isset( $saved_settings[ $constant ] ) ) { |
| 208 |
$final_settings[ $constant ] = $saved_settings[ $constant ]; |
| 209 |
} |
| 210 |
// Priority 3: Use default value (final fallback) |
| 211 |
else { |
| 212 |
$final_settings[ $constant ] = $default; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
return $final_settings; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Update developer settings in database |
| 221 |
* |
| 222 |
* @param array $settings The settings to save |
| 223 |
* @return bool Success status |
| 224 |
*/ |
| 225 |
public static function update_settings( $settings ) { |
| 226 |
// Validate and sanitize settings |
| 227 |
$sanitized_settings = self::sanitize_settings( $settings ); |
| 228 |
|
| 229 |
if ( empty( $sanitized_settings ) ) { |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
// Save to database |
| 234 |
update_option( 'templately_developer_settings', $sanitized_settings ); |
| 235 |
|
| 236 |
return true; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Sanitize developer settings |
| 241 |
* |
| 242 |
* @param array $settings Raw settings |
| 243 |
* @return array Sanitized settings |
| 244 |
*/ |
| 245 |
public static function sanitize_settings( $settings ) { |
| 246 |
$sanitized = []; |
| 247 |
|
| 248 |
foreach ( self::$available_constants as $constant => $args ) { |
| 249 |
if ( isset( $settings[ $constant ] ) ) { |
| 250 |
if ( $constant === 'TEMPLATELY_HTTP_RETRY' ) { |
| 251 |
$sanitized[ $constant ] = max( $args['min'], min( $args['max'], intval( $settings[ $constant ] ) ) ); |
| 252 |
} else { |
| 253 |
$sanitized[ $constant ] = rest_sanitize_boolean( $settings[ $constant ] ); |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
return $sanitized; |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Get available developer constants |
| 263 |
* |
| 264 |
* @return array |
| 265 |
*/ |
| 266 |
public static function get_available_constants() { |
| 267 |
return self::$available_constants; |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* Get constant override status for each developer constant |
| 272 |
* |
| 273 |
* Returns information about which constants are defined vs. controlled by database settings |
| 274 |
* |
| 275 |
* @return array Array with constant names as keys and override info as values |
| 276 |
*/ |
| 277 |
public static function get_constant_override_status() { |
| 278 |
// Return cached result if available |
| 279 |
if ( self::$cached_override_status !== null ) { |
| 280 |
return self::$cached_override_status; |
| 281 |
} |
| 282 |
|
| 283 |
$override_status = []; |
| 284 |
|
| 285 |
foreach ( array_keys( self::$available_constants ) as $constant ) { |
| 286 |
$override_status[ $constant ] = [ |
| 287 |
'is_defined' => defined( $constant ), |
| 288 |
'current_value' => defined( $constant ) ? constant( $constant ) : null, |
| 289 |
'is_overridden' => defined( $constant ), // Same as is_defined for clarity |
| 290 |
]; |
| 291 |
} |
| 292 |
|
| 293 |
self::$cached_override_status = $override_status; |
| 294 |
return $override_status; |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Check if a specific developer feature is enabled |
| 299 |
* |
| 300 |
* @param string $feature The feature constant name |
| 301 |
* @return bool |
| 302 |
*/ |
| 303 |
public static function is_feature_enabled( $feature ) { |
| 304 |
// First check if the constant is defined |
| 305 |
if ( defined( $feature ) ) { |
| 306 |
return constant( $feature ); |
| 307 |
} |
| 308 |
|
| 309 |
// Fall back to saved settings |
| 310 |
$settings = get_option( 'templately_developer_settings', [] ); |
| 311 |
return isset( $settings[ $feature ] ) ? $settings[ $feature ] : false; |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* Get network admin status information |
| 316 |
* |
| 317 |
* @return array |
| 318 |
*/ |
| 319 |
public static function get_network_admin_status() { |
| 320 |
return [ |
| 321 |
'available' => is_multisite() && self::is_developer_mode_enabled(), |
| 322 |
'multisite' => is_multisite(), |
| 323 |
'developer_mode' => self::is_developer_mode_enabled(), |
| 324 |
'network_admin' => is_network_admin(), |
| 325 |
'can_manage_network' => current_user_can( 'manage_network' ), |
| 326 |
]; |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Log debug message if debug logging is enabled |
| 331 |
* |
| 332 |
* @param string $message The message to log |
| 333 |
* @param string $context Optional context for the log |
| 334 |
*/ |
| 335 |
public static function debug_log( $message, $context = 'templately' ) { |
| 336 |
if ( self::is_feature_enabled( 'TEMPLATELY_DEBUG_LOG' ) ) { |
| 337 |
error_log( "[{$context}] {$message}" ); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
/** |
| 342 |
* Check if we should ignore AI animations |
| 343 |
* |
| 344 |
* @return bool |
| 345 |
*/ |
| 346 |
public static function should_ignore_ai_animate() { |
| 347 |
return self::is_feature_enabled( 'TEMPLATELY_IGNORE_AI_ANIMATE' ); |
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* Get HTTP retry count |
| 352 |
* |
| 353 |
* @return int |
| 354 |
*/ |
| 355 |
public static function get_http_retry_count() { |
| 356 |
$count = self::is_feature_enabled( 'TEMPLATELY_HTTP_RETRY' ); |
| 357 |
return is_numeric( $count ) ? max( 1, min( 10, intval( $count ) ) ) : 3; |
| 358 |
} |
| 359 |
} |
| 360 |
|