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.
 
 
 
 
 
 

47 lines
1.2 KiB

  1. <?php
  2. $requestBody = file_get_contents('php://input');
  3. $requestJson = json_decode($requestBody, true);
  4. if (empty($requestJson['old']) && empty($requestJson['new'])) {
  5. throw new \Exception('Old text or new text is required.');
  6. }
  7. $jsonFile = __DIR__.'/demos.json';
  8. $demoStorage = json_decode(file_get_contents($jsonFile), true);
  9. if (empty($requestJson['name'])) {
  10. $requestJson['name'] = 'DEMO '.count($demoStorage);
  11. }
  12. $oldText = $requestJson['old'];
  13. $newText = $requestJson['new'];
  14. $name = $requestJson['name'];
  15. $legislativeOverride = !empty($requestJson['legislativeOverride']) ? $requestJson['legislativeOverride'] : null;
  16. $existingDemoIndex = null;
  17. foreach ($demoStorage as $index => $demo) {
  18. if ($demo['name'] === $name) {
  19. $existingDemoIndex = $index;
  20. break;
  21. }
  22. }
  23. if ($existingDemoIndex !== null) {
  24. $demoStorage[$existingDemoIndex]['old'] = $oldText;
  25. $demoStorage[$existingDemoIndex]['new'] = $newText;
  26. } else {
  27. $demoStorage[] = array(
  28. 'name' => $name,
  29. 'old' => $oldText,
  30. 'new' => $newText,
  31. 'legislativeOverride' => $legislativeOverride,
  32. );
  33. }
  34. if (false === file_put_contents($jsonFile, json_encode($demoStorage))) {
  35. throw new \Exception("Unable to save to file: $jsonFile");
  36. }