PluginProbe
Site Reviews / trunk
Site Reviews vtrunk
8.3.1 8.3.0 8.2.2 8.2.1 8.2.0 8.1.0 8.0.13 8.0.12 8.0.11 trunk 1.2.2 2.17.1 3.5.4 4.7.0 5.25.1 6.11.8 7.0.10 7.0.11 7.0.12 7.0.13 7.0.14 7.0.15 7.0.16 7.0.17 7.0.18 All 54 releases
site-reviews / plugin / Controllers / RevisionController.php

RevisionController.php in Site Reviews trunk, at plugin/Controllers/RevisionController.php

139 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace GeminiLabs\SiteReviews\Controllers;
4
5 use GeminiLabs\SiteReviews\Database\PostMeta;
6 use GeminiLabs\SiteReviews\Database\ReviewManager;
7 use GeminiLabs\SiteReviews\Defaults\RevisionFieldsDefaults;
8 use GeminiLabs\SiteReviews\Modules\Html\Builder;
9 use GeminiLabs\SiteReviews\Modules\Rating;
10 use GeminiLabs\SiteReviews\Review;
11
12 class RevisionController extends AbstractController
13 {
14 /**
15 * @filter wp_save_post_revision_check_for_changes
16 */
17 public function filterCheckForChanges(bool $performCheck, \WP_Post $lastRevision, \WP_Post $post): bool
18 {
19 return !Review::isReview($post)
20 ? $performCheck
21 : true;
22 }
23
24 /**
25 * @filter wp_save_post_revision_post_has_changed
26 */
27 public function filterReviewHasChanged(bool $hasChanged, \WP_Post $lastRevision, \WP_Post $post): bool
28 {
29 if (!Review::isReview($post)) {
30 return $hasChanged;
31 }
32 $review = glsr(ReviewManager::class)->get($post->ID, true); // bypass the cache
33 $revision = glsr(PostMeta::class)->get($lastRevision->ID, 'review');
34 foreach ($revision as $key => $value) {
35 if ((string) $review->$key !== (string) $value) {
36 return true;
37 }
38 }
39 return $hasChanged;
40 }
41
42 /**
43 * @param array[] $return
44 * @param \WP_Post|false $compareFrom
45 *
46 * @return array[]
47 *
48 * @filter wp_get_revision_ui_diff
49 */
50 public function filterRevisionUiDiff(array $return, $compareFrom, \WP_Post $compareTo): array
51 {
52 if (!Review::isReview($compareTo->post_parent)) {
53 return $return;
54 }
55 $fields = glsr(RevisionFieldsDefaults::class)->defaults();
56 $oldReview = $this->reviewFromRevision($compareFrom);
57 $newReview = $this->reviewFromRevision($compareTo);
58 foreach ($fields as $field => $name) {
59 $old = (string) $oldReview->$field;
60 $new = (string) $newReview->$field;
61 $diff = wp_text_diff($old, $new, [
62 'show_split_view' => true,
63 ]);
64 if ('rating' === $field) {
65 $callback = fn ($matches) => $this->ratingValueForDiff((int) $matches[1]);
66 $diff = preg_replace_callback('|(\d)</td>|', $callback, $diff);
67 }
68 if ($diff) {
69 $return[] = [
70 'diff' => $diff,
71 'id' => $field,
72 'name' => $name,
73 ];
74 }
75 }
76 return $return;
77 }
78
79 /**
80 * @action wp_restore_post_revision
81 */
82 public function restoreRevision(int $reviewId, int $revisionId): void
83 {
84 if (!Review::isReview($reviewId)) {
85 return;
86 }
87 $revision = glsr(PostMeta::class)->get($revisionId, 'review');
88 if (is_array($revision)) {
89 $review = glsr_get_review($reviewId);
90 glsr(ReviewManager::class)->updateRating($reviewId, $revision);
91 glsr()->action('cache/flush', "review_{$reviewId}_revision_restored", $review);
92 }
93 }
94
95 /**
96 * @action _wp_put_post_revision
97 */
98 public function saveRevision(int $revisionId): void
99 {
100 $postId = wp_is_post_revision($revisionId);
101 if (Review::isReview($postId)) {
102 $review = glsr(ReviewManager::class)->get((int) $postId);
103 $revision = glsr(RevisionFieldsDefaults::class)->defaults();
104 foreach ($revision as $field => &$value) {
105 $value = $review->$field;
106 }
107 glsr(PostMeta::class)->set($revisionId, 'review', $revision);
108 }
109 }
110
111 protected function ratingValueForDiff(int $rating): string
112 {
113 $max = Rating::max();
114 $empty = max(0, $max - $rating);
115 $stars = str_repeat('�
116 ', $rating).str_repeat('', $empty);
117 return glsr(Builder::class)->span([
118 'style' => 'font-family:system-ui;font-size:16px;line-height:1.375;',
119 'text' => $stars,
120 ]);
121 }
122
123 /**
124 * @param \WP_Post|false $post
125 */
126 protected function reviewFromRevision($post): Review
127 {
128 if (!is_a($post, \WP_Post::class)) {
129 return new Review([], false);
130 }
131 if (wp_is_post_revision($post->ID)) {
132 return new Review(
133 glsr(PostMeta::class)->get($post->ID, 'review')
134 );
135 }
136 return glsr(ReviewManager::class)->get($post->ID);
137 }
138 }
139