Passed
Branch master (2fcc75)
by Paul
11:46
created

ChangeStatus::getStatusLinks()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 3
Bugs 0 Features 2
Metric Value
cc 3
eloc 12
c 3
b 0
f 2
nc 3
nop 0
dl 0
loc 16
ccs 0
cts 16
cp 0
crap 12
rs 9.8666
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Handlers;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Commands\ChangeStatus as Command;
7
use GeminiLabs\SiteReviews\Modules\Html\Builder;
8
9
class ChangeStatus
10
{
11
    /**
12
     * @return array
13
     */
14
    public function handle(Command $command)
15
    {
16
        $postId = wp_update_post([
17
            'ID' => $command->id,
18
            'post_status' => $command->status,
19
        ]);
20
        if (is_wp_error($postId)) {
21
            glsr_log()->error($postId->get_error_message());
22
            return [];
23
        }
24
        return [
25
            'class' => 'status-'.$command->status,
26
            'counts' => $this->getStatusLinks(),
27
            'link' => $this->getPostLink($postId).$this->getPostState($postId),
28
            'pending' => wp_count_posts(Application::POST_TYPE, 'readable')->pending,
29
        ];
30
    }
31
32
    /**
33
     * @param int $postId
34
     * @return string
35
     */
36
    protected function getPostLink($postId)
37
    {
38
        $title = _draft_or_post_title($postId);
39
        return glsr(Builder::class)->a($title, [
40
            'aria-label' => '&#8220;'.esc_attr($title).'&#8221; ('.__('Edit', 'site-reviews').')',
41
            'class' => 'row-title',
42
            'href' => get_edit_post_link($postId),
43
        ]);
44
    }
45
46
    /**
47
     * @param int $postId
48
     * @return string
49
     */
50
    protected function getPostState($postId)
51
    {
52
        ob_start();
53
        _post_states(get_post($postId));
54
        return ob_get_clean();
55
    }
56
57
    /**
58
     * @return void|string
59
     */
60
    protected function getStatusLinks()
61
    {
62
        global $avail_post_stati;
63
        require_once ABSPATH.'wp-admin/includes/class-wp-posts-list-table.php';
64
        $hookName = 'edit-'.Application::POST_TYPE;
65
        set_current_screen($hookName);
66
        $avail_post_stati = get_available_post_statuses(Application::POST_TYPE);
67
        $table = new \WP_Posts_List_Table(['screen' => $hookName]);
68
        $views = apply_filters('views_'.$hookName, $table->get_views()); // uses compat get_views()
69
        if (empty($views)) {
70
            return;
71
        }
72
        foreach ($views as $class => $view) {
73
            $views[$class] = "\t<li class='$class'>$view";
74
        }
75
        return implode(' |</li>', $views).'</li>';
76
    }
77
}
78