@@ -351,67 +351,97 @@ class VersionControl extends WireData implements Module, ConfigurableModule {
351351 // If Page has no ID, it's currently being added.
352352 $page_id = $page->id ?: 0;
353353
354+ // Check if we should read this page.
355+ if (!$this->shouldReadPageData($page, $field, $force)) return;
356+
357+ foreach ($page->template->fields as $page_field) {
358+
359+ // Check if we should read this field.
360+ if (!$this->shouldReadFieldData($page, $field, $page_field, $force)) continue;
361+
362+ // Get data from the Page object.
363+ $data = $page->get($page_field->name);
364+
365+ // Continue only if either the page in question exists (i.e. old field was cleared)
366+ // or page is new and field has value.
367+ if ($page->id || !is_null($data) && $data != "") {
368+ if (!isset($this->page_data[$page_id])) {
369+ $this->page_data[$page_id] = [];
370+ }
371+ $data_array = [
372+ 'data' => $data,
373+ ];
374+ if ($data instanceof Pagefiles) {
375+ // Note: originally 'sort' value of each item was used instead of a custom
376+ // counter, but that's not always available when working over the API.
377+ $count = 0;
378+ foreach ($data as $item) {
379+ $data_item = [
380+ 'original_filename' => $item->filename,
381+ 'filename' => hash_file('sha1', $item->filename) . "." . $item->basename,
382+ 'description' => $item->description,
383+ 'modified' => $item->modified,
384+ 'created' => $item->created,
385+ ];
386+ if ($page_field->useTags) {
387+ $data_item['tags'] = $item->tags;
388+ }
389+ $data_array[$count.'.data'] = json_encode($data_item);
390+ ++$count;
391+ }
392+ if (empty($data_array)) {
393+ $data_array['0.data'] = null;
394+ }
395+ } else if ($data instanceof LanguagesPageFieldValue) {
396+ foreach ($data->getChanges() as $key) {
397+ if ($key == 'data') continue;
398+ $data_array[$key] = $data->getLanguageValue(str_replace('data', '', $key));
399+ }
400+ }
401+ $this->page_data[$page_id][$page_field->id] = $data_array;
402+ }
403+ }
404+ }
405+
406+ /**
407+ * Check if page data should be read
408+ *
409+ * @param Page $page
410+ * @param Field|null $field
411+ * @param bool $force
412+ * @return bool
413+ */
414+ protected function ___shouldReadPageData(Page $page, ?Field $field, bool $force) {
415+
416+ if ($force) return true;
417+
354418 // Check if tracking values has been enabled for the template of current page, or (in the
355419 // case of Repeater pages) template of the containing page.
356420 $template = $page->template;
357421 if ($page instanceof RepeaterPage) {
358422 $template = $page->getForPage()->template;
359423 }
360- if (!$force && !$this->isEnabledTemplate($template)) return;
361-
362- if ($force || $page->isChanged()) {
363- foreach ($page->template->fields as $page_field) {
364-
365- // Check if we should skip this field.
366- if ($field && $page_field->id != $field->id) continue;
367- if (!$force && !$page->isChanged($page_field->name)) continue;
368- if (!$page->id && $page_field->type instanceof FieldtypeFile) continue;
369- if (!in_array($page_field->type, $this->compatible_fieldtypes)) continue;
370- if (!empty($this->enabled_fields) && !in_array($page_field->id, $this->enabled_fields)) continue;
371-
372- // Get data from the Page object.
373- $data = $page->get($page_field->name);
374-
375- // Continue only if either the page in question exists (i.e. old field was cleared)
376- // or page is new and field has value.
377- if ($page->id || !is_null($data) && $data != "") {
378- if (!isset($this->page_data[$page_id])) {
379- $this->page_data[$page_id] = [];
380- }
381- $data_array = [
382- 'data' => $data,
383- ];
384- if ($data instanceof Pagefiles) {
385- // Note: originally 'sort' value of each item was used instead of a custom
386- // counter, but that's not always available when working over the API.
387- $count = 0;
388- foreach ($data as $item) {
389- $data_item = [
390- 'original_filename' => $item->filename,
391- 'filename' => hash_file('sha1', $item->filename) . "." . $item->basename,
392- 'description' => $item->description,
393- 'modified' => $item->modified,
394- 'created' => $item->created,
395- ];
396- if ($page_field->useTags) {
397- $data_item['tags'] = $item->tags;
398- }
399- $data_array[$count.'.data'] = json_encode($data_item);
400- ++$count;
401- }
402- if (empty($data_array)) {
403- $data_array['0.data'] = null;
404- }
405- } else if ($data instanceof LanguagesPageFieldValue) {
406- foreach ($data->getChanges() as $key) {
407- if ($key == 'data') continue;
408- $data_array[$key] = $data->getLanguageValue(str_replace('data', '', $key));
409- }
410- }
411- $this->page_data[$page_id][$page_field->id] = $data_array;
412- }
413- }
414- }
424+ if (!$this->isEnabledTemplate($template)) return false;
425+
426+ return $page->isChanged();
427+ }
428+
429+ /**
430+ * Check if field data should be read
431+ *
432+ * @param Page $page
433+ * @param Field $page_field
434+ * @param Field|null $field
435+ * @param bool $force
436+ * @return bool
437+ */
438+ protected function ___shouldReadFieldData(Page $page, ?Field $field, Field $page_field, bool $force) {
439+ if ($field && $page_field->id != $field->id) return false;
440+ if (!$force && !$page->isChanged($page_field->name)) return false;
441+ if (!$page->id && $page_field->type instanceof FieldtypeFile) return false;
442+ if (!in_array($page_field->type, $this->compatible_fieldtypes)) return false;
443+ if (!empty($this->enabled_fields) && !in_array($page_field->id, $this->enabled_fields)) return false;
444+ return true;
415445 }
416446
417447 /**
0 commit comments