| 1 |
<?php |
| 2 |
/** |
| 3 |
* Updates Table Helper. |
| 4 |
* |
| 5 |
* @package MainWP/Dashboard |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace MainWP\Dashboard; |
| 9 |
|
| 10 |
// Exit if accessed directly. |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Class MainWP_Updates_Table_Helper |
| 17 |
* |
| 18 |
* @package MainWP\Dashboard |
| 19 |
*/ |
| 20 |
class MainWP_Updates_Table_Helper { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR. |
| 21 |
|
| 22 |
/** |
| 23 |
* Protected variable to hold columns info. |
| 24 |
* |
| 25 |
* @var array |
| 26 |
*/ |
| 27 |
protected $columns_info; |
| 28 |
|
| 29 |
/** |
| 30 |
* Public variable to type |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
public $type = 'plugin'; |
| 35 |
|
| 36 |
/** |
| 37 |
* Public variable to show select box on rows. |
| 38 |
* |
| 39 |
* @var string |
| 40 |
*/ |
| 41 |
public $show_select = false; |
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* Protected variable to view per |
| 46 |
* |
| 47 |
* @var string |
| 48 |
*/ |
| 49 |
public $view_per; |
| 50 |
|
| 51 |
/** |
| 52 |
* MainWP_Updates_Table_Helper constructor. |
| 53 |
* |
| 54 |
* Run each time the class is called. |
| 55 |
* |
| 56 |
* @param string $view_per View per value. |
| 57 |
* @param string $type Type of plugin or theme, option, default: 'plugin'. |
| 58 |
* @param array $others others parameters. |
| 59 |
*/ |
| 60 |
public function __construct( $view_per, $type = 'plugin', $others = array() ) { |
| 61 |
$this->type = $type; |
| 62 |
$this->view_per = (int) $view_per; |
| 63 |
$this->show_select = is_array( $others ) && isset( $others['show_select'] ) && $others['show_select'] ? true : false; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Method get_columns() |
| 68 |
* |
| 69 |
* Combine all columns. |
| 70 |
* |
| 71 |
* @return array $columns Array of column names. |
| 72 |
*/ |
| 73 |
public function get_columns() { |
| 74 |
|
| 75 |
$title = ( MAINWP_VIEW_PER_PLUGIN_THEME === $this->view_per || MAINWP_VIEW_PER_GROUP === $this->view_per ) ? esc_html__( 'Website', 'mainwp' ) : ''; |
| 76 |
if ( MAINWP_VIEW_PER_SITE === $this->view_per ) { |
| 77 |
$title = ( 'plugin' === $this->type ) ? esc_html__( 'Plugin', 'mainwp' ) : esc_html__( 'Theme', 'mainwp' ); |
| 78 |
} |
| 79 |
|
| 80 |
$columns = array( |
| 81 |
'site' => esc_html__( 'Website', 'mainwp' ), |
| 82 |
'title' => $title, |
| 83 |
'version' => esc_html__( 'Version', 'mainwp' ), |
| 84 |
'latest' => esc_html__( 'Latest', 'mainwp' ), |
| 85 |
'trusted' => esc_html__( 'Trusted', 'mainwp' ), |
| 86 |
'status' => esc_html__( 'Status', 'mainwp' ), |
| 87 |
'client' => esc_html__( 'Client', 'mainwp' ), |
| 88 |
'action' => '', |
| 89 |
); |
| 90 |
|
| 91 |
if ( MAINWP_VIEW_PER_PLUGIN_THEME !== $this->view_per ) { |
| 92 |
unset( $columns['site'] ); |
| 93 |
unset( $columns['client'] ); |
| 94 |
} else { |
| 95 |
unset( $columns['title'] ); |
| 96 |
} |
| 97 |
if ( MAINWP_VIEW_PER_PLUGIN_THEME === $this->view_per ) { |
| 98 |
unset( $columns['trusted'] ); |
| 99 |
} |
| 100 |
if ( ! \mainwp_current_user_can( 'dashboard', 'manage_clients' ) ) { |
| 101 |
unset( $columns['client'] ); |
| 102 |
} |
| 103 |
return $columns; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Get column info. |
| 108 |
*/ |
| 109 |
protected function get_column_info() { |
| 110 |
if ( isset( $this->columns_info ) ) { |
| 111 |
return $this->columns_info; |
| 112 |
} |
| 113 |
$columns = $this->get_columns(); |
| 114 |
$sortable = $this->get_sortable_columns(); |
| 115 |
$collapsing = $this->get_collapsing_columns(); |
| 116 |
$this->columns_info = apply_filters( 'mainwp_updates_table_columns_header', array( $columns, $sortable, $collapsing ), $this->type, $this->view_per ); |
| 117 |
return $this->columns_info; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Get sortable columns. |
| 122 |
* |
| 123 |
* @return array $sortable_columns Array of sortable column names. |
| 124 |
*/ |
| 125 |
public function get_sortable_columns() { |
| 126 |
return array( |
| 127 |
'title' => true, |
| 128 |
'version' => true, |
| 129 |
'trusted' => true, |
| 130 |
'status' => true, |
| 131 |
); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Get collapsing columns. |
| 136 |
* |
| 137 |
* @return array $collapsing_columns Array of collapsing columns. |
| 138 |
*/ |
| 139 |
public function get_collapsing_columns() { |
| 140 |
return array( |
| 141 |
'version' => true, |
| 142 |
'latest' => true, |
| 143 |
'trusted' => true, |
| 144 |
'status' => true, |
| 145 |
'client' => true, |
| 146 |
'action' => true, |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Echo the column headers. |
| 152 |
* |
| 153 |
* @param bool $top true|false. |
| 154 |
*/ |
| 155 |
public function print_column_headers( $top = true ) { |
| 156 |
|
| 157 |
list( $columns_header, $sortable, $collapsing ) = $this->get_column_info(); |
| 158 |
|
| 159 |
foreach ( $columns_header as $column_key => $column_display_name ) { |
| 160 |
$class = array(); |
| 161 |
if ( ! isset( $sortable[ $column_key ] ) ) { |
| 162 |
$class[] = 'no-sort'; |
| 163 |
} |
| 164 |
if ( isset( $collapsing[ $column_key ] ) ) { |
| 165 |
$class[] = 'two wide collapsing'; |
| 166 |
} |
| 167 |
|
| 168 |
if ( ! empty( $class ) ) { |
| 169 |
$class = "class='" . join( ' ', $class ) . "'"; |
| 170 |
} else { |
| 171 |
$class = ''; |
| 172 |
} |
| 173 |
$column_display_name = apply_filters( 'mainwp_updates_table_header_content', $column_display_name, $column_key, $top, $this ); |
| 174 |
echo "<th $class>$column_display_name</th>"; // phpcs:ignore WordPress.Security.EscapeOutput |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
/** |
| 180 |
* Trusted column. |
| 181 |
* |
| 182 |
* @param mixed $value Value of column. |
| 183 |
*/ |
| 184 |
public function column_trusted( $value ) { |
| 185 |
return MainWP_Manage_Sites_Update_View::get_column_trusted( $value ); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Status column. |
| 190 |
* |
| 191 |
* @param mixed $value Value of column. |
| 192 |
*/ |
| 193 |
public function column_status( $value ) { |
| 194 |
if ( $value ) { |
| 195 |
$label = '<span class="ui tiny basic green label mainwp-768-fluid">Active</span>'; |
| 196 |
} else { |
| 197 |
$label = '<span class="ui tiny basic grey label mainwp-768-fluid">Inactive</span>'; |
| 198 |
} |
| 199 |
return '<td class="mainwp-768-half-width-cell">' . $label . '</td>'; |
| 200 |
} |
| 201 |
/** |
| 202 |
* Title column. |
| 203 |
* |
| 204 |
* @param mixed $value Value of column. |
| 205 |
* @param object $website The website. |
| 206 |
* |
| 207 |
* @return string Column HTML. |
| 208 |
*/ |
| 209 |
public function column_site( $value, $website = null ) { |
| 210 |
if ( true !== $value ) { |
| 211 |
return '<td></td>'; |
| 212 |
} |
| 213 |
|
| 214 |
$current_wpid = MainWP_System_Utility::get_current_wpid(); |
| 215 |
|
| 216 |
$column_content = ''; |
| 217 |
|
| 218 |
if ( empty( $current_wpid ) || $this->show_select ) { |
| 219 |
$column_content .= '<div class="ui child checkbox"> |
| 220 |
<input type="checkbox" name=""> |
| 221 |
</div>'; |
| 222 |
} |
| 223 |
|
| 224 |
if ( empty( $current_wpid ) && ! empty( $website ) ) { |
| 225 |
$column_content .= MainWP_Utility::mainwp_display_site( $website ); |
| 226 |
} |
| 227 |
|
| 228 |
return '<td>' . $column_content . '</td>'; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Title column. |
| 233 |
* |
| 234 |
* @param mixed $value Value of column. |
| 235 |
* |
| 236 |
* @return string Column HTML. |
| 237 |
*/ |
| 238 |
public function column_title( $value ) { |
| 239 |
|
| 240 |
$current_wpid = MainWP_System_Utility::get_current_wpid(); |
| 241 |
|
| 242 |
$column_content = ''; |
| 243 |
|
| 244 |
if ( empty( $current_wpid ) || $this->show_select ) { |
| 245 |
$column_content .= '<div class="ui child checkbox"> |
| 246 |
<input type="checkbox" name=""> |
| 247 |
</div>'; |
| 248 |
} |
| 249 |
|
| 250 |
$column_content .= $value; |
| 251 |
|
| 252 |
return '<td>' . $column_content . '</td>'; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Default column. |
| 257 |
* |
| 258 |
* @param mixed $value Value of column. |
| 259 |
* @param mixed $column_name Name of column. |
| 260 |
* @param array $others others data. |
| 261 |
* |
| 262 |
* @return string Column HTML. |
| 263 |
*/ |
| 264 |
public function column_default( $value, $column_name, $others = array() ) { |
| 265 |
$class = ''; |
| 266 |
if ( 'version' === $column_name || 'latest' === $column_name ) { |
| 267 |
$class = 'mainwp-768-half-width-cell'; |
| 268 |
} |
| 269 |
|
| 270 |
$column_content = $value; |
| 271 |
|
| 272 |
if ( 'latest' === $column_name && ! empty( $others['roll_info'] ) ) { |
| 273 |
$column_content = $others['roll_info'] . ' ' . $column_content; |
| 274 |
} |
| 275 |
|
| 276 |
return '<td class="' . $class . '">' . $column_content . '</td>'; |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Echo columns. |
| 281 |
* |
| 282 |
* @param array $columns Array of columns. |
| 283 |
* @param object $website The website. |
| 284 |
* @param object $others others data. |
| 285 |
* |
| 286 |
* @return array Row columns. |
| 287 |
*/ |
| 288 |
public function render_columns( $columns, $website, $others = array() ) { |
| 289 |
$row_columns = apply_filters( 'mainwp_updates_table_row_columns', $columns, $website, $this->type, $this->view_per ); |
| 290 |
list( $columns_header ) = $this->get_column_info(); |
| 291 |
foreach ( $columns_header as $col => $title ) { |
| 292 |
if ( isset( $row_columns[ $col ] ) ) { |
| 293 |
$value = $row_columns[ $col ]; |
| 294 |
if ( method_exists( $this, 'column_' . $col ) ) { |
| 295 |
echo call_user_func( array( &$this, 'column_' . $col ), $value, $website, $others ); // phpcs:ignore WordPress.Security.EscapeOutput |
| 296 |
} else { |
| 297 |
echo $this->column_default( $value, $col, $others ); // phpcs:ignore WordPress.Security.EscapeOutput |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
return $row_columns; |
| 302 |
} |
| 303 |
} |
| 304 |
|