| 1 |
<?php |
| 2 |
namespace Templately\Core\Developer; |
| 3 |
|
| 4 |
use Templately\Utils\Base; |
| 5 |
use Templately\Utils\Helper; |
| 6 |
|
| 7 |
/** |
| 8 |
* Developer API Manager |
| 9 |
* |
| 10 |
* Handles API endpoint selection and legacy constant compatibility for developer features. |
| 11 |
* Follows the proper module architecture pattern used by other developer modules. |
| 12 |
* |
| 13 |
* @since 3.3.4 |
| 14 |
*/ |
| 15 |
class ApiManager extends Base { |
| 16 |
|
| 17 |
/** |
| 18 |
* Initialize ApiManager module |
| 19 |
* |
| 20 |
* Handles legacy TEMPLATELY_DEV constant compatibility and sets up hooks |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
// Handle legacy TEMPLATELY_DEV constant compatibility |
| 24 |
$this->handle_legacy_constants(); |
| 25 |
|
| 26 |
// Initialize hooks |
| 27 |
$this->init_hooks(); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Handle legacy TEMPLATELY_DEV constant compatibility |
| 32 |
* |
| 33 |
* Automatically define TEMPLATELY_DEV_API as true if TEMPLATELY_DEV_API is not |
| 34 |
* already defined AND TEMPLATELY_DEV constant is defined and true. |
| 35 |
* This ensures existing setups using the old TEMPLATELY_DEV constant continue to work. |
| 36 |
*/ |
| 37 |
private function handle_legacy_constants() { |
| 38 |
if ( ! defined( 'TEMPLATELY_DEV_API' ) && defined( 'TEMPLATELY_DEV' ) && constant( 'TEMPLATELY_DEV' ) ) { |
| 39 |
define( 'TEMPLATELY_DEV_API', true ); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Initialize hooks |
| 45 |
*/ |
| 46 |
private function init_hooks() { |
| 47 |
add_filter( 'templately_admin_localized_data', [ $this, 'filter_localized_data' ] ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Check if development API should be used |
| 52 |
* |
| 53 |
* Simplified logic: if TEMPLATELY_DEV_API is defined and true, use dev server. |
| 54 |
* Otherwise, use production server. |
| 55 |
* |
| 56 |
* @return bool True if development API should be used |
| 57 |
*/ |
| 58 |
public static function is_dev_api() { |
| 59 |
return defined( 'TEMPLATELY_DEV_API' ) && constant( 'TEMPLATELY_DEV_API' ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Filter localized data to add API connection information |
| 64 |
* |
| 65 |
* @param array $data The localized data array |
| 66 |
* @return array Modified localized data |
| 67 |
*/ |
| 68 |
public function filter_localized_data( $data ) { |
| 69 |
$data['dev_api'] = Helper::is_dev_api(); |
| 70 |
|
| 71 |
return $data; |
| 72 |
} |
| 73 |
} |
| 74 |
|