Plugin.php
183 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | final class AC_Notice_Plugin { |
| 8 | |
| 9 | /** |
| 10 | * @var string |
| 11 | */ |
| 12 | private $plugin_basename; |
| 13 | |
| 14 | /** |
| 15 | * @var string |
| 16 | */ |
| 17 | private $message; |
| 18 | |
| 19 | /** |
| 20 | * @var string |
| 21 | */ |
| 22 | private $class; |
| 23 | |
| 24 | /** |
| 25 | * @var string |
| 26 | */ |
| 27 | private $icon; |
| 28 | |
| 29 | /** |
| 30 | * @param string $plugin_basename |
| 31 | */ |
| 32 | public function __construct( $plugin_basename ) { |
| 33 | $this->plugin_basename = $plugin_basename; |
| 34 | $this->set_type( 'warning' ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Check if the plugin has an update available |
| 39 | * |
| 40 | * @return bool |
| 41 | */ |
| 42 | private function update_available() { |
| 43 | $current = get_site_transient( 'update_plugins' ); |
| 44 | |
| 45 | return isset( $current->response[ $this->plugin_basename ] ); |
| 46 | } |
| 47 | |
| 48 | public function hook_notice() { |
| 49 | add_action( 'after_plugin_row_' . $this->plugin_basename, array( $this, 'display_notice' ), 11 ); |
| 50 | } |
| 51 | |
| 52 | public function display_notice() { |
| 53 | $class = ''; |
| 54 | |
| 55 | if ( is_plugin_active( $this->plugin_basename ) ) { |
| 56 | $class .= ' active'; |
| 57 | |
| 58 | if ( $this->update_available() ) { |
| 59 | $class .= ' update'; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | ?> |
| 64 | |
| 65 | <style> |
| 66 | .plugins tr[data-plugin='<?php echo $this->plugin_basename; ?>'] th, |
| 67 | .plugins tr[data-plugin='<?php echo $this->plugin_basename; ?>'] td { |
| 68 | box-shadow: none; |
| 69 | } |
| 70 | |
| 71 | <?php if ( $this->icon ) : ?> |
| 72 | .plugins tr[data-plugin='<?php echo $this->plugin_basename; ?>'] .update-message p:before { |
| 73 | content: "<?php echo $this->icon ?>"; |
| 74 | } |
| 75 | |
| 76 | <?php endif; ?> |
| 77 | </style> |
| 78 | |
| 79 | <tr class="plugin-update-tr <?php echo esc_attr( $class ); ?>" data-slug="<?php echo esc_attr( basename( $this->plugin_basename ) ); ?>" data-plugin="<?php echo esc_attr( $this->plugin_basename ); ?>"> |
| 80 | <td colspan="3" class="plugin-update colspanchange"> |
| 81 | <div class="update-message notice inline <?php echo esc_attr( $this->class ); ?>"> |
| 82 | <p><?php echo $this->message; ?></p> |
| 83 | </div> |
| 84 | </td> |
| 85 | </tr> |
| 86 | |
| 87 | <?php |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Set the message of this notice. Only links allowed, other HTML is escaped |
| 92 | * |
| 93 | * @param string $message |
| 94 | * |
| 95 | * @return $this |
| 96 | */ |
| 97 | public function set_message( $message ) { |
| 98 | $this->message = wp_kses( $message, array( |
| 99 | 'strong' => array(), |
| 100 | 'br' => array(), |
| 101 | 'a' => array( |
| 102 | 'class' => true, |
| 103 | 'data' => true, |
| 104 | 'href' => true, |
| 105 | 'id' => true, |
| 106 | 'title' => true, |
| 107 | ), |
| 108 | ) ); |
| 109 | |
| 110 | return $this; |
| 111 | } |
| 112 | |
| 113 | private function get_predefined_type( $type ) { |
| 114 | $mapping = array( |
| 115 | 'warning' => 'notice-warning|\f348', |
| 116 | 'error' => 'notice-error|\f534', |
| 117 | 'success' => 'updated-message notice-success|\f147', |
| 118 | 'update' => 'notice-warning|\f463', |
| 119 | ); |
| 120 | |
| 121 | if ( array_key_exists( $type, $mapping ) ) { |
| 122 | $parts = explode( '|', $mapping[ $type ] ); |
| 123 | |
| 124 | return (object) array( |
| 125 | 'class' => $parts[0] . ' notice-alt', |
| 126 | 'icon' => $parts[1], |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | public function set_type( $type ) { |
| 134 | $type = $this->get_predefined_type( $type ); |
| 135 | |
| 136 | if ( $type ) { |
| 137 | $this->set_class( $type->class ); |
| 138 | $this->set_icon( $type->icon ); |
| 139 | } |
| 140 | |
| 141 | return $this; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Set the color-scheme of the notice |
| 146 | * |
| 147 | * @param string $class |
| 148 | * |
| 149 | * @return $this |
| 150 | */ |
| 151 | public function set_class( $class ) { |
| 152 | $type = $this->get_predefined_type( $class ); |
| 153 | |
| 154 | if ( $type ) { |
| 155 | $class = $type->class; |
| 156 | } |
| 157 | |
| 158 | $this->class = $class; |
| 159 | |
| 160 | return $this; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Set the icon of the notice. Defaults to 'update' |
| 165 | * |
| 166 | * @param string $icon |
| 167 | * |
| 168 | * @return $this |
| 169 | */ |
| 170 | public function set_icon( $icon ) { |
| 171 | $type = $this->get_predefined_type( $icon ); |
| 172 | |
| 173 | if ( $type ) { |
| 174 | $icon = $type->icon; |
| 175 | } |
| 176 | |
| 177 | $this->icon = $icon; |
| 178 | |
| 179 | return $this; |
| 180 | } |
| 181 | |
| 182 | } |
| 183 |