| 1 |
<?php |
| 2 |
namespace BetterLinks\Admin; |
| 3 |
if ( ! defined( 'ABSPATH' ) ) { exit; } |
| 4 |
|
| 5 |
class Cache { |
| 6 |
public static function init() { |
| 7 |
self::write_json_settings(); |
| 8 |
} |
| 9 |
public static function write_json_settings() { |
| 10 |
$betterlinks_links = get_option( BETTERLINKS_LINKS_OPTION_NAME, array() ); |
| 11 |
if ( is_string( $betterlinks_links ) ) { |
| 12 |
$betterlinks_links = json_decode( $betterlinks_links, true ); |
| 13 |
} |
| 14 |
|
| 15 |
// Security: Remove sensitive API keys from JSON cache |
| 16 |
// API keys are stored separately in BETTERLINKS_AI_API_KEYS_OPTION_NAME |
| 17 |
unset( $betterlinks_links['openai_api_key'] ); |
| 18 |
unset( $betterlinks_links['gemini_api_key'] ); |
| 19 |
|
| 20 |
if( !is_dir( BETTERLINKS_UPLOAD_DIR_PATH ) ){ |
| 21 |
wp_mkdir_p(BETTERLINKS_UPLOAD_DIR_PATH); |
| 22 |
} |
| 23 |
file_put_contents( BETTERLINKS_UPLOAD_DIR_PATH . '/settings.json', json_encode( $betterlinks_links ) ); |
| 24 |
return $betterlinks_links; |
| 25 |
} |
| 26 |
|
| 27 |
public static function get_json_settings() { |
| 28 |
if ( file_exists( BETTERLINKS_UPLOAD_DIR_PATH . '/settings.json' ) ) { |
| 29 |
$settings = json_decode( file_get_contents( BETTERLINKS_UPLOAD_DIR_PATH . '/settings.json' ), true ); |
| 30 |
if ( ! empty( $settings ) ) { |
| 31 |
return $settings; |
| 32 |
} |
| 33 |
} |
| 34 |
return self::write_json_settings(); |
| 35 |
} |
| 36 |
} |
| 37 |
|