Passed
Push — master ( 9a7a6b...7de9ff )
by Paul
03:37
created

ChangeStatus::getStatusLinks()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 0
dl 0
loc 14
ccs 0
cts 14
cp 0
crap 12
rs 9.9
c 0
b 0
f 0
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
		];
29
	}
30
31
	/**
32
	 * @param int $postId
33
	 * @return string
34
	 */
35
	protected function getPostLink( $postId )
36
	{
37
		$title = _draft_or_post_title( $postId );
38
		return glsr( Builder::class )->a( $title, [
39
			'aria-label' => '&#8220;'.esc_attr( $title ).'&#8221; ('.__( 'Edit', 'site-reviews' ).')',
40
			'class' => 'row-title',
41
			'href' => get_edit_post_link( $postId ),
42
		]);
43
	}
44
45
	/**
46
	 * @param int $postId
47
	 * @return string
48
	 */
49
	protected function getPostState( $postId )
50
	{
51
		ob_start();
52
		_post_states( get_post( $postId ));
53
		return ob_get_clean();
54
	}
55
56
	/**
57
	 * @return void|string
58
	 */
59
	protected function getStatusLinks()
60
	{
61
		global $avail_post_stati;
1 ignored issue
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
62
		require_once( ABSPATH.'wp-admin/includes/class-wp-posts-list-table.php' );
63
		$hookName = 'edit-'.Application::POST_TYPE;
64
		set_current_screen( $hookName );
65
		$avail_post_stati = get_available_post_statuses( Application::POST_TYPE );
66
		$table = new \WP_Posts_List_Table( ['screen' => $hookName] );
67
		$views = apply_filters( 'views_'.$hookName, $table->get_views() ); // uses compat get_views()
68
		if( empty( $views ))return;
69
		foreach( $views as $class => $view ) {
70
			$views[$class] = "\t<li class='$class'>$view";
71
		}
72
		return implode( ' |</li>', $views ).'</li>';
73
	}
74
}
75