Plugin.php
2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace AC\Message;
use AC\Message;
use AC\View;
class Plugin extends Message {
/**
* @var string
*/
protected $plugin_basename;
/**
* @var string
*/
protected $icon;
/**
* @param string $message
* @param string $plugin_basename
* @param string $type
*/
public function __construct( $message, $plugin_basename, $type = null ) {
if ( null === $type ) {
$type = self::WARNING;
}
parent::__construct( $message, $type );
$this->plugin_basename = $plugin_basename;
$this->icon = $this->get_icon_by_current_type();
}
public function register() {
add_action( 'after_plugin_row_' . $this->plugin_basename, [ $this, 'display' ], 11 );
}
public function render() {
switch ( $this->type ) {
case self::SUCCESS :
$class = 'updated-message notice-success';
break;
case self::INFO :
$class = self::WARNING;
break;
default:
$class = $this->type;
}
$is_plugin_active = is_multisite() && is_network_admin()
? is_plugin_active_for_network( $this->plugin_basename )
: is_plugin_active( $this->plugin_basename );
$status = $is_plugin_active
? 'active'
: 'inactive';
$data = [
'plugin_basename' => $this->plugin_basename,
'icon' => $this->icon,
'class' => $class,
'message' => $this->message,
'type' => $this->type,
'status' => $status,
];
$view = new View( $data );
$view->set_template( 'message/plugin' );
return $view->render();
}
/**
* @return string
*/
protected function get_icon_by_current_type() {
$mapping = [
self::SUCCESS => '\f147', // yes
self::WARNING => '\f348', // info
self::ERROR => '\f534', // warning
self::INFO => '\f14c', // info outline
];
if ( ! isset( $mapping[ $this->type ] ) ) {
return false;
}
return $mapping[ $this->type ];
}
/**
* Set the icon of this notice
*
* @param string $icon
*
* @return $this
*/
public function set_icon( $icon ) {
$this->icon = $icon;
return $this;
}
}