GIF94;
| Path : /var/www/wordpress/wp-content/plugins/oxymade/includes/ |
| Current File : /var/www/wordpress/wp-content/plugins/oxymade/includes/json-manager.php |
<?php
namespace OxyMade\JSONManager;
use function Breakdance\Variables\saveVariables;
/**
* Manage JSON data for OxyMade variables and selectors
*/
class JSONManager {
/**
* Get the JSON data for variables and selectors from static files
*
* @return array|false JSON data or false on error
*/
public static function get_json_data() {
// Get variables from static file
$variables_json = self::get_variables_json();
$selectors_json = self::get_selectors_json();
if (!$variables_json || !$selectors_json) {
return false;
}
$variables = json_decode($variables_json, true);
$selectors = json_decode($selectors_json, true);
// Create the JSON structure
return [
'variables' => $variables,
'selectors' => $selectors,
'version' => '1.0.0',
'generated_at' => \current_time('mysql'),
'oxymade_version' => OXYMADE_VERSION
];
}
/**
* Get variables JSON from static file
*
* @return string|false JSON string or false on failure
*/
public static function get_variables_json() {
$json_file = OXYMADE_PLUGIN_DIR . 'data/variables.json';
if (file_exists($json_file)) {
return file_get_contents($json_file);
}
return false;
}
/**
* Get selectors JSON from static file
*
* @return string|false JSON string or false on failure
*/
public static function get_selectors_json() {
$json_file = OXYMADE_PLUGIN_DIR . 'data/selectors.json';
if (file_exists($json_file)) {
return file_get_contents($json_file);
}
return false;
}
/**
* Import variables and selectors from JSON data
*
* @param array $json_data JSON data array
* @param string $mode Sync mode: 'add_new', 'update', or 'replace'
* @return array Import results
*/
public static function import_from_json($json_data, $mode = 'add_new') {
if (!$json_data) {
return [
'success' => false,
'message' => 'No JSON data provided'
];
}
// Validate the JSON structure
if (!isset($json_data['variables']) || !isset($json_data['selectors'])) {
return [
'success' => false,
'message' => 'Invalid JSON structure. Missing variables or selectors.'
];
}
// Import variables
$variables_result = self::import_variables($json_data['variables'], $mode);
// Import selectors
$selectors_result = self::import_selectors($json_data['selectors'], $mode);
if ($variables_result['success'] && $selectors_result['success']) {
return [
'success' => true,
'message' => 'Variables and selectors imported successfully!',
'variables_count' => count($json_data['variables']),
'selectors_count' => count($json_data['selectors'])
];
} else {
$error_message = 'Failed to import: ';
if (!$variables_result['success']) {
$error_message .= 'Variables: ' . $variables_result['message'] . ' ';
}
if (!$selectors_result['success']) {
$error_message .= 'Selectors: ' . $selectors_result['message'] . ' ';
}
return [
'success' => false,
'message' => $error_message
];
}
}
/**
* Import variables to Oxygen
*
* @param array $variables Variables to import
* @param string $mode Sync mode
* @return array Import result
*/
private static function import_variables($variables, $mode) {
// Check if Oxygen is active
if (!function_exists('\\Breakdance\\Data\\get_global_option')) {
return [
'success' => false,
'message' => 'Oxygen is not active'
];
}
// Get existing variables
$current_variables_data = \Breakdance\Data\get_global_option('variables_json_string');
if ($current_variables_data) {
// Check if it's already an array or needs to be decoded
if (is_array($current_variables_data)) {
$current_variables = $current_variables_data;
} else {
$current_variables = json_decode($current_variables_data, true);
if (json_last_error() !== JSON_ERROR_NONE) {
$current_variables = [];
}
}
} else {
$current_variables = [];
}
// Handle different modes
switch ($mode) {
case 'add_new':
// Add only new variables
$existing_variable_map = [];
foreach ($current_variables as $variable) {
if (isset($variable['cssVariableName'])) {
$existing_variable_map[$variable['cssVariableName']] = true;
}
}
$variables_added = false;
foreach ($variables as $variable) {
if (!isset($existing_variable_map[$variable['cssVariableName']])) {
$current_variables[] = $variable;
$variables_added = true;
}
}
if (!$variables_added) {
return [
'success' => true,
'message' => 'No new variables to add'
];
}
break;
case 'update':
// Update existing and add new
$existing_variable_map = [];
foreach ($current_variables as $index => $variable) {
if (isset($variable['cssVariableName'])) {
$existing_variable_map[$variable['cssVariableName']] = $index;
}
}
foreach ($variables as $variable) {
if (isset($existing_variable_map[$variable['cssVariableName']])) {
$current_variables[$existing_variable_map[$variable['cssVariableName']]] = $variable;
} else {
$current_variables[] = $variable;
}
}
break;
case 'replace':
// Replace all OxyMade variables
$new_variable_map = [];
foreach ($variables as $variable) {
if (isset($variable['cssVariableName'])) {
$new_variable_map[$variable['cssVariableName']] = true;
}
}
$filtered_variables = [];
foreach ($current_variables as $variable) {
if (isset($variable['cssVariableName']) && isset($new_variable_map[$variable['cssVariableName']])) {
continue;
}
$filtered_variables[] = $variable;
}
$current_variables = array_merge($filtered_variables, $variables);
break;
}
// Use Oxygen's saveVariables function exactly as designed
$collections = array_values(array_unique(array_column($current_variables, 'collection')));
$variables_data = json_encode([
'variables' => $current_variables,
'collections' => $collections
]);
// Use Oxygen's saveVariables function - it will handle option name mapping
try {
saveVariables($variables_data);
// In Oxygen mode, also save to the oxygen-specific option names
if (defined('BREAKDANCE_MODE') && \BREAKDANCE_MODE === 'oxygen') {
\Breakdance\Data\set_global_option('oxygen_variables_json_string', $current_variables);
\Breakdance\Data\set_global_option('oxygen_variables_collections_json_string', $collections);
}
} catch (\Exception $e) {
return [
'success' => false,
'error' => 'Failed to save variables: ' . $e->getMessage()
];
} catch (\Error $e) {
return [
'success' => false,
'error' => 'Failed to save variables: ' . $e->getMessage()
];
}
// Mark as synced
\update_option('oxymade_variables_synced', true);
return [
'success' => true,
'message' => 'Variables imported successfully'
];
}
/**
* Import selectors to Oxygen
*
* @param array $selectors Selectors to import
* @param string $mode Sync mode
* @return array Import result
*/
private static function import_selectors($selectors, $mode) {
// Check if Oxygen is active
if (!function_exists('\\Breakdance\\Data\\get_global_option')) {
return [
'success' => false,
'message' => 'Oxygen is not active'
];
}
// Get existing selectors
$current_selectors_json = \Breakdance\Data\get_global_option('oxy_selectors_json_string');
if ($current_selectors_json) {
$current_selectors = json_decode($current_selectors_json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
$current_selectors = [];
}
} else {
$current_selectors = [];
}
// Handle different modes (similar to variables)
switch ($mode) {
case 'add_new':
// Add only new selectors
$existing_selector_map = [];
foreach ($current_selectors as $selector) {
if (isset($selector['name'])) {
$existing_selector_map[$selector['name']] = true;
}
}
$selectors_added = false;
foreach ($selectors as $selector) {
if (!isset($existing_selector_map[$selector['name']])) {
$current_selectors[] = $selector;
$selectors_added = true;
}
}
if (!$selectors_added) {
return [
'success' => true,
'message' => 'No new selectors to add'
];
}
break;
case 'update':
// Update existing and add new
$existing_selector_map = [];
foreach ($current_selectors as $index => $selector) {
if (isset($selector['name'])) {
$existing_selector_map[$selector['name']] = $index;
}
}
foreach ($selectors as $selector) {
if (isset($existing_selector_map[$selector['name']])) {
$current_selectors[$existing_selector_map[$selector['name']]] = $selector;
} else {
$current_selectors[] = $selector;
}
}
break;
case 'replace':
// Replace all OxyMade selectors
$new_selector_map = [];
foreach ($selectors as $selector) {
if (isset($selector['name'])) {
$new_selector_map[$selector['name']] = true;
}
}
$filtered_selectors = [];
foreach ($current_selectors as $selector) {
if (isset($selector['name']) && isset($new_selector_map[$selector['name']])) {
continue;
}
$filtered_selectors[] = $selector;
}
$current_selectors = array_merge($filtered_selectors, $selectors);
break;
}
// Save selectors
$merged_selectors_json = json_encode($current_selectors);
\Breakdance\Data\set_global_option('oxy_selectors_json_string', $merged_selectors_json);
return [
'success' => true,
'message' => 'Selectors imported successfully'
];
}
}