| @@ -6,221 +6,298 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace MainWP\Dashboard; |
| 9 | 9 | |
| 10 | +// Exit if accessed directly. | |
| 11 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 12 | + exit; | |
| 13 | +} | |
| 14 | + | |
| 10 | 15 | /** |
| 11 | 16 | * Class MainWP_Updates_Table_Helper |
| 12 | 17 | * |
| 13 | 18 | * @package MainWP\Dashboard |
| 14 | 19 | */ |
| 15 | -class MainWP_Updates_Table_Helper { | |
| 20 | +class MainWP_Updates_Table_Helper { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR. | |
| 16 | 21 | |
| 17 | - /** | |
| 18 | - * Protected variable to hold columns info. | |
| 19 | - * | |
| 20 | - * @var array | |
| 21 | - */ | |
| 22 | - protected $columns_info; | |
| 22 | + /** | |
| 23 | + * Protected variable to hold columns info. | |
| 24 | + * | |
| 25 | + * @var array | |
| 26 | + */ | |
| 27 | + protected $columns_info; | |
| 23 | 28 | |
| 24 | - /** | |
| 25 | - * Public variable to type | |
| 26 | - * | |
| 27 | - * @var string | |
| 28 | - */ | |
| 29 | - public $type = 'plugin'; | |
| 29 | + /** | |
| 30 | + * Public variable to type | |
| 31 | + * | |
| 32 | + * @var string | |
| 33 | + */ | |
| 34 | + public $type = 'plugin'; | |
| 30 | 35 | |
| 31 | - /** | |
| 32 | - * Public variable to show select box on rows. | |
| 33 | - * | |
| 34 | - * @var string | |
| 35 | - */ | |
| 36 | - public $show_select = false; | |
| 36 | + /** | |
| 37 | + * Public variable to show select box on rows. | |
| 38 | + * | |
| 39 | + * @var string | |
| 40 | + */ | |
| 41 | + public $show_select = false; | |
| 37 | 42 | |
| 38 | 43 | |
| 39 | - /** | |
| 40 | - * Protected variable to view per | |
| 41 | - * | |
| 42 | - * @var string | |
| 43 | - */ | |
| 44 | - public $view_per; | |
| 44 | + /** | |
| 45 | + * Protected variable to view per | |
| 46 | + * | |
| 47 | + * @var string | |
| 48 | + */ | |
| 49 | + public $view_per; | |
| 45 | 50 | |
| 46 | - /** | |
| 47 | - * MainWP_Updates_Table_Helper constructor. | |
| 48 | - * | |
| 49 | - * Run each time the class is called. | |
| 50 | - * | |
| 51 | - * @param string $view_per View per value. | |
| 52 | - * @param string $type Type of plugin or theme, option, default: 'plugin'. | |
| 53 | - * @param array $others others parameters. | |
| 54 | - */ | |
| 55 | - public function __construct( $view_per, $type = 'plugin', $others = array() ) { | |
| 56 | - $this->type = $type; | |
| 57 | - $this->view_per = (int) $view_per; | |
| 58 | - $this->show_select = is_array( $others ) && isset( $others['show_select'] ) && $others['show_select'] ? true : false; | |
| 59 | - } | |
| 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 | + } | |
| 60 | 65 | |
| 61 | - /** | |
| 62 | - * Method get_columns() | |
| 63 | - * | |
| 64 | - * Combine all columns. | |
| 65 | - * | |
| 66 | - * @return array $columns Array of column names. | |
| 67 | - */ | |
| 68 | - public function get_columns() { | |
| 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() { | |
| 69 | 74 | |
| 70 | - $title = ( MAINWP_VIEW_PER_PLUGIN_THEME === $this->view_per || MAINWP_VIEW_PER_GROUP === $this->view_per ) ? esc_html__( 'Website', 'mainwp' ) : ''; | |
| 71 | - if ( MAINWP_VIEW_PER_SITE === $this->view_per ) { | |
| 72 | - $title = ( 'plugin' === $this->type ) ? esc_html__( 'Plugin', 'mainwp' ) : esc_html__( 'Theme', 'mainwp' ); | |
| 73 | - } | |
| 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 | + } | |
| 74 | 79 | |
| 75 | - $columns = array( | |
| 76 | - 'title' => $title, | |
| 77 | - 'login' => '<i class="sign in alternate icon"></i>', | |
| 78 | - 'version' => esc_html__( 'Version', 'mainwp' ), | |
| 79 | - 'latest' => esc_html__( 'Latest', 'mainwp' ), | |
| 80 | - 'trusted' => esc_html__( 'Trusted', 'mainwp' ), | |
| 81 | - 'status' => esc_html__( 'Status', 'mainwp' ), | |
| 82 | - 'client' => esc_html__( 'Client', 'mainwp' ), | |
| 83 | - 'action' => '', | |
| 84 | - ); | |
| 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 | + ); | |
| 85 | 90 | |
| 86 | - if ( MAINWP_VIEW_PER_PLUGIN_THEME !== $this->view_per ) { | |
| 87 | - unset( $columns['login'] ); | |
| 88 | - unset( $columns['client'] ); | |
| 89 | - } | |
| 90 | - if ( MAINWP_VIEW_PER_PLUGIN_THEME === $this->view_per ) { | |
| 91 | - unset( $columns['trusted'] ); | |
| 92 | - } | |
| 93 | - return $columns; | |
| 94 | - } | |
| 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 | + } | |
| 95 | 105 | |
| 96 | - /** | |
| 97 | - * Get column info. | |
| 98 | - */ | |
| 99 | - protected function get_column_info() { | |
| 100 | - if ( isset( $this->columns_info ) ) { | |
| 101 | - return $this->columns_info; | |
| 102 | - } | |
| 103 | - $columns = $this->get_columns(); | |
| 104 | - $sortable = $this->get_sortable_columns(); | |
| 105 | - $this->columns_info = apply_filters( 'mainwp_updates_table_columns_header', array( $columns, $sortable ), $this->type, $this->view_per ); | |
| 106 | - return $this->columns_info; | |
| 107 | - } | |
| 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 | + } | |
| 108 | 119 | |
| 109 | - /** | |
| 110 | - * Get sortable columns. | |
| 111 | - * | |
| 112 | - * @return array $sortable_columns Array of sortable column names. | |
| 113 | - */ | |
| 114 | - public function get_sortable_columns() { | |
| 115 | - $sortable_columns = array( | |
| 116 | - 'title' => true, | |
| 117 | - 'version' => true, | |
| 118 | - 'trusted' => true, | |
| 119 | - 'status' => true, | |
| 120 | - ); | |
| 121 | - return $sortable_columns; | |
| 122 | - } | |
| 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 | + } | |
| 123 | 133 | |
| 124 | - /** | |
| 125 | - * Echo the column headers. | |
| 126 | - * | |
| 127 | - * @param bool $top true|false. | |
| 128 | - */ | |
| 129 | - public function print_column_headers( $top = true ) { | |
| 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 | + } | |
| 130 | 149 | |
| 131 | - list( $columns_header, $sortable ) = $this->get_column_info(); | |
| 150 | + /** | |
| 151 | + * Echo the column headers. | |
| 152 | + * | |
| 153 | + * @param bool $top true|false. | |
| 154 | + */ | |
| 155 | + public function print_column_headers( $top = true ) { | |
| 132 | 156 | |
| 133 | - foreach ( $columns_header as $column_key => $column_display_name ) { | |
| 134 | - $class = array(); | |
| 135 | - if ( ! isset( $sortable[ $column_key ] ) ) { | |
| 136 | - $class[] = 'no-sort'; | |
| 137 | - } | |
| 157 | + list( $columns_header, $sortable, $collapsing ) = $this->get_column_info(); | |
| 138 | 158 | |
| 139 | - if ( ! empty( $class ) ) { | |
| 140 | - $class = "class='" . join( ' ', $class ) . "'"; | |
| 141 | - } else { | |
| 142 | - $class = ''; | |
| 143 | - } | |
| 144 | - $column_display_name = apply_filters( 'mainwp_updates_table_header_content', $column_display_name, $column_key, $top, $this ); | |
| 145 | - echo "<th $class>$column_display_name</th>"; // phpcs:ignore WordPress.Security.EscapeOutput | |
| 146 | - } | |
| 147 | - } | |
| 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 | + } | |
| 148 | 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 | + } | |
| 149 | 177 | |
| 150 | - /** | |
| 151 | - * Trusted column. | |
| 152 | - * | |
| 153 | - * @param mixed $value Value of column. | |
| 154 | - */ | |
| 155 | - public function column_trusted( $value ) { | |
| 156 | - if ( $value ) { | |
| 157 | - $label = '<span class="ui tiny basic green label mainwp-768-fluid">Trusted</span>'; | |
| 158 | - } else { | |
| 159 | - $label = '<span class="ui tiny basic grey label mainwp-768-fluid">Not Trusted</span>'; | |
| 160 | - } | |
| 161 | - return '<td class="mainwp-768-half-width-cell">' . $label . '</td>'; | |
| 162 | - } | |
| 163 | 178 | |
| 164 | - /** | |
| 165 | - * Status column. | |
| 166 | - * | |
| 167 | - * @param mixed $value Value of column. | |
| 168 | - */ | |
| 169 | - public function column_status( $value ) { | |
| 170 | - if ( $value ) { | |
| 171 | - $label = '<span class="ui tiny basic green label mainwp-768-fluid">Active</span>'; | |
| 172 | - } else { | |
| 173 | - $label = '<span class="ui tiny basic grey label mainwp-768-fluid">Inactive</span>'; | |
| 174 | - } | |
| 175 | - return '<td class="mainwp-768-half-width-cell">' . $label . '</td>'; | |
| 176 | - } | |
| 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 | + } | |
| 177 | 187 | |
| 178 | - /** | |
| 179 | - * Default column. | |
| 180 | - * | |
| 181 | - * @param mixed $value Value of column. | |
| 182 | - * @param mixed $column_name Name of column. | |
| 183 | - */ | |
| 184 | - public function column_default( $value, $column_name ) { | |
| 185 | - $current_wpid = MainWP_System_Utility::get_current_wpid(); | |
| 186 | - $class = ''; | |
| 187 | - if ( 'version' === $column_name || 'latest' === $column_name ) { | |
| 188 | - $class = 'mainwp-768-half-width-cell'; | |
| 189 | - } | |
| 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 | + } | |
| 190 | 213 | |
| 191 | - $column_content = ''; | |
| 192 | - if ( 'title' === $column_name && ( empty( $current_wpid ) || $this->show_select ) ) { | |
| 193 | - $column_content .= '<div class="ui child checkbox"> | |
| 194 | - <input type="checkbox" name=""> | |
| 195 | - </div>'; | |
| 196 | - } | |
| 197 | - $column_content .= $value; | |
| 214 | + $current_wpid = MainWP_System_Utility::get_current_wpid(); | |
| 198 | 215 | |
| 199 | - $col = '<td class="' . $class . '">' . $column_content . '</td>'; | |
| 200 | - return $col; | |
| 201 | - } | |
| 216 | + $column_content = ''; | |
| 202 | 217 | |
| 203 | - /** | |
| 204 | - * Echo columns. | |
| 205 | - * | |
| 206 | - * @param array $columns Array of columns. | |
| 207 | - * @param object $website The website. | |
| 208 | - * | |
| 209 | - * @return array Row columns. | |
| 210 | - */ | |
| 211 | - public function render_columns( $columns, $website ) { | |
| 212 | - $row_columns = apply_filters( 'mainwp_updates_table_row_columns', $columns, $website, $this->type, $this->view_per ); | |
| 213 | - list( $columns_header ) = $this->get_column_info(); | |
| 214 | - foreach ( $columns_header as $col => $title ) { | |
| 215 | - if ( isset( $row_columns[ $col ] ) ) { | |
| 216 | - $value = $row_columns[ $col ]; | |
| 217 | - if ( method_exists( $this, 'column_' . $col ) ) { | |
| 218 | - echo call_user_func( array( &$this, 'column_' . $col ), $value ); // phpcs:ignore WordPress.Security.EscapeOutput | |
| 219 | - } else { | |
| 220 | - echo $this->column_default( $value, $col ); // phpcs:ignore WordPress.Security.EscapeOutput | |
| 221 | - } | |
| 222 | - } | |
| 223 | - } | |
| 224 | - return $row_columns; | |
| 225 | - } | |
| 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 | + } | |
| 226 | 303 | } |