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

Controller::redirectUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\DuplicatePage;
4
5
use GeminiLabs\SiteReviews\Controllers\AbstractController;
6
use GeminiLabs\SiteReviews\Database\ReviewManager;
7
use GeminiLabs\SiteReviews\Helpers\Str;
8
use GeminiLabs\SiteReviews\Review;
9
10
class Controller extends AbstractController
11
{
12
    /**
13
     * @action admin_action_dt_duplicate_post_as_draft
14
     */
15
    public function duplicateReview(): void
16
    {
17
        $nonce = filter_input(INPUT_GET, 'nonce') ?: filter_input(INPUT_POST, 'nonce');
18
        $postId = filter_input(INPUT_GET, 'post') ?: filter_input(INPUT_POST, 'post');
19
        if (!Review::isReview($postId)) {
20
            return; // not a review so don't override
21
        }
22
        if (!wp_verify_nonce(sanitize_text_field($nonce), "dt-duplicate-page-{$postId}")) {
23
            return; // let the integrated plugin handle errors
24
        }
25
        if (!glsr()->can('edit_posts')) {
26
            wp_die(_x('Unauthorized Access.', 'admin-text', 'site-reviews'));
27
        }
28
        if (!$review = glsr(ReviewManager::class)->duplicate((int) $postId)) {
29
            wp_die(_x('Invalid review.', 'admin-text', 'site-reviews'));
30
        }
31
        $options = get_option('duplicate_page_options');
32
        if (!empty($options['duplicate_post_suffix'])) {
33
            wp_update_post([
34
                'ID' => $review->ID,
35
                'post_title' => Str::suffix($review->title, ' -- '.$options['duplicate_post_suffix']),
36
            ]);
37
        }
38
        wp_redirect($this->redirectUrl($review->ID));
39
        exit;
1 ignored issue
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
40
    }
41
42
    protected function redirectUrl(int $postId): string
43
    {
44
        $options = get_option('duplicate_page_options');
45
        $redirect = $options['duplicate_post_redirect'] ?? 'to_list';
46
        if ('to_page' === $redirect) {
47
            $url = admin_url('edit.php?post_type='.glsr()->post_type);
48
        } else {
49
            $url = admin_url('post.php?action=edit&post='.$postId);
50
        }
51
        return sanitize_url($url);
52
    }
53
}
54