Plugin to allow visitor contributions to WordPress posts, wiki style.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

46 lines
1.2 KiB

<?php
$requestBody = file_get_contents('php://input');
$requestJson = json_decode($requestBody, true);
if (empty($requestJson['old']) && empty($requestJson['new'])) {
throw new \Exception('Old text or new text is required.');
}
$jsonFile = __DIR__.'/demos.json';
$demoStorage = json_decode(file_get_contents($jsonFile), true);
if (empty($requestJson['name'])) {
$requestJson['name'] = 'DEMO '.count($demoStorage);
}
$oldText = $requestJson['old'];
$newText = $requestJson['new'];
$name = $requestJson['name'];
$legislativeOverride = !empty($requestJson['legislativeOverride']) ? $requestJson['legislativeOverride'] : null;
$existingDemoIndex = null;
foreach ($demoStorage as $index => $demo) {
if ($demo['name'] === $name) {
$existingDemoIndex = $index;
break;
}
}
if ($existingDemoIndex !== null) {
$demoStorage[$existingDemoIndex]['old'] = $oldText;
$demoStorage[$existingDemoIndex]['new'] = $newText;
} else {
$demoStorage[] = array(
'name' => $name,
'old' => $oldText,
'new' => $newText,
'legislativeOverride' => $legislativeOverride,
);
}
if (false === file_put_contents($jsonFile, json_encode($demoStorage))) {
throw new \Exception("Unable to save to file: $jsonFile");
}