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' => '“'.esc_attr( $title ).'” ('.__( '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; |
|
|
|
|
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
|
|
|
|
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state