Test Failed
Push — develop ( 514ddb...0ee1ea )
by Paul
09:58
created

Controller::duplicateReview()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 14
ccs 0
cts 7
cp 0
rs 9.9332
cc 4
nc 4
nop 2
crap 20
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\DuplicatePost;
4
5
use GeminiLabs\SiteReviews\Controllers\AbstractController;
6
use GeminiLabs\SiteReviews\Database\CountManager;
7
use GeminiLabs\SiteReviews\Database\ReviewManager;
8
use GeminiLabs\SiteReviews\Helpers\Arr;
9
use GeminiLabs\SiteReviews\Review;
10
11
class Controller extends AbstractController
12
{
13
    /**
14
     * @param int|\WP_Error $newPostId
15
     * @param \WP_Post      $post
16
     *
17
     * @action duplicate_post_post_copy
18
     */
19
    public function duplicateReview($newPostId, $post): void
20
    {
21
        if (!Review::isReview($post)) {
22
            return;
23
        }
24
        if (is_wp_error($newPostId)) {
25
            return;
26
        }
27
        $review = glsr_get_review($post->ID);
28
        if ($review->isValid()) {
29
            glsr(ReviewManager::class)->createFromPost((int) $newPostId, $review->toArray());
30
            delete_post_meta($newPostId, '_submitted');
1 ignored issue
show
Bug introduced by
It seems like $newPostId can also be of type WP_Error; however, parameter $post_id of delete_post_meta() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
            delete_post_meta(/** @scrutinizer ignore-type */ $newPostId, '_submitted');
Loading history...
31
            delete_post_meta($newPostId, '_submitted_hash');
32
            update_post_meta($newPostId, '_duplicated_from', $post->ID);
1 ignored issue
show
Bug introduced by
It seems like $newPostId can also be of type WP_Error; however, parameter $post_id of update_post_meta() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
            update_post_meta(/** @scrutinizer ignore-type */ $newPostId, '_duplicated_from', $post->ID);
Loading history...
33
        }
34
    }
35
36
    /**
37
     * @param string[] $actions
38
     *
39
     * @filter bulk_actions-edit-{Application::POST_TYPE}
40
     */
41
    public function filterBulkActions($actions): array
42
    {
43
        $actions = Arr::consolidate($actions);
44
        unset($actions['duplicate_post_bulk_rewrite_republish']);
45
        return $actions;
46
    }
47
48
    /**
49
     * @param string[] $metaKeys
50
     *
51
     * @filter duplicate_post_excludelist_filter
52
     */
53
    public function filterExcludedMetaKeys($metaKeys): array
54
    {
55
        $metaKeys[] = CountManager::META_AVERAGE;
56
        $metaKeys[] = CountManager::META_RANKING;
57
        $metaKeys[] = CountManager::META_REVIEWS;
58
        return $metaKeys;
59
    }
60
61
    /**
62
     * @param string[] $actions
63
     * @param \WP_Post $post
64
     *
65
     * @filter post_row_actions
66
     */
67
    public function filterRowActions($actions, $post): array
68
    {
69
        $actions = Arr::consolidate($actions);
70
        if (Review::isReview($post)) {
71
            unset($actions['rewrite']);
72
        }
73
        return $actions;
74
    }
75
76
    /**
77
     * @param \WP_Post|null $post
78
     *
79
     * @action post_submitbox_start
80
     */
81
    public function removeRewriteEditorLink($post): void
82
    {
83
        if (!Review::isReview($post)) {
84
            return;
85
        }
86
        global $wp_filter;
87
        $callbacks = Arr::get($wp_filter, 'post_submitbox_start.callbacks.10', []);
88
        foreach ($callbacks as $key => $value) {
89
            if (str_ends_with($key, 'add_rewrite_and_republish_post_button')) {
90
                remove_action('post_submitbox_start', Arr::get($value, 'function'), 10);
91
            }
92
        }
93
    }
94
}
95