| 1 |
<?php |
| 2 |
use Linguise\Vendor\Linguise\Script\Core\Management; |
| 3 |
use Linguise\WordPress\Synchronization; |
| 4 |
use Linguise\Vendor\Linguise\Script\Core\Debug; |
| 5 |
|
| 6 |
defined('ABSPATH') || die(''); |
| 7 |
|
| 8 |
/** |
| 9 |
* Sync config when languages changed in WP |
| 10 |
*/ |
| 11 |
add_action('updated_option', function ($option, $old_value, $value) { |
| 12 |
linguiseInitializeConfiguration(); |
| 13 |
$original_data = [$option, $old_value, $value]; |
| 14 |
|
| 15 |
// Not linguise config |
| 16 |
if ($option !== 'linguise_options') { |
| 17 |
return $original_data; |
| 18 |
} |
| 19 |
|
| 20 |
// If the config updated by Api-JS via REST API |
| 21 |
// We don't need to sync |
| 22 |
if (defined('REST_REQUEST') && REST_REQUEST) { |
| 23 |
return $original_data; // @codeCoverageIgnore |
| 24 |
} |
| 25 |
|
| 26 |
try { |
| 27 |
$options = linguiseGetOptions(); |
| 28 |
$synchronization = Synchronization::getInstance(); |
| 29 |
$configs = $synchronization->buildPayload($options); |
| 30 |
$token = $options['token']; |
| 31 |
|
| 32 |
if (!empty($token)) { |
| 33 |
$management = Management::getInstance(); |
| 34 |
$api_url = $synchronization->getApiRoot($options, '/api/sync/domain'); |
| 35 |
$result = $management->pushRemoteSync($configs, $token, $api_url); |
| 36 |
|
| 37 |
if (!$result) { |
| 38 |
Debug::saveError('configuration synchronization to Linguise failed:'. $token); // @codeCoverageIgnore |
| 39 |
} |
| 40 |
} |
| 41 |
} catch (Exception $e) { // @codeCoverageIgnore |
| 42 |
Debug::saveError('configuration synchronization to Linguise failed: ' . $e->getMessage()); // @codeCoverageIgnore |
| 43 |
} |
| 44 |
|
| 45 |
return $original_data; |
| 46 |
}, 10, 3); |
| 47 |
|
| 48 |
/** |
| 49 |
* Handle sync languages request from Linguise API |
| 50 |
* |
| 51 |
* @param WP_REST_Request $request Ret API params |
| 52 |
* |
| 53 |
* @return WP_REST_Response |
| 54 |
*/ |
| 55 |
function linguise_synchronize_update_config($request) |
| 56 |
{ |
| 57 |
$jwt_token = $request->get_header('X-Linguise-Hash'); |
| 58 |
$options = linguiseGetOptions(); |
| 59 |
|
| 60 |
$params = $request->get_json_params(); |
| 61 |
$token = isset($params['token']) ? $params['token'] : null; |
| 62 |
|
| 63 |
$management = Management::getInstance(); |
| 64 |
$synchronization = Synchronization::getInstance(); |
| 65 |
$api_url = $synchronization->getApiRoot($options, '/api/sync/verify'); |
| 66 |
// Verify hash data |
| 67 |
$management->verifyRemoteToken($jwt_token, $api_url); |
| 68 |
|
| 69 |
// Validate token |
| 70 |
if ($token !== $options['token']) { |
| 71 |
return new WP_Error('unauthorized', 'Invalid Token', array( 'status' => 401 )); |
| 72 |
} |
| 73 |
|
| 74 |
try { |
| 75 |
$config = $synchronization->convertparamsToWPOptions($params); |
| 76 |
$merged_config = array_merge($options, $config); |
| 77 |
} catch (Exception $e) { |
| 78 |
return new WP_Error('error', $e->getMessage(), array( 'status' => 500 )); |
| 79 |
} |
| 80 |
|
| 81 |
update_option('linguise_options', $merged_config); |
| 82 |
|
| 83 |
return rest_ensure_response(['message' => 'Config successfully updated']); |
| 84 |
} |
| 85 |
|
| 86 |
add_action('rest_api_init', function () { |
| 87 |
register_rest_route('linguise/v1', '/sync', array( |
| 88 |
'methods' => 'POST', |
| 89 |
'callback' => 'linguise_synchronize_update_config', |
| 90 |
'permission_callback' => '__return_true', |
| 91 |
)); |
| 92 |
}); |
| 93 |
|