| 1 | <?php |
| 2 | /** |
| 3 | * MS Themes List Table class. |
| 4 | * |
| 5 | * @package WordPress |
| 6 | * @subpackage List_Table |
| 7 | * @since 3.1.0 |
| 8 | */ |
| 9 | class WP_MS_Themes_Table extends WP_List_Table { |
| 10 | |
| 11 | function WP_MS_Themes_Table() { |
| 12 | global $status, $page; |
| 13 | |
| 14 | $default_status = get_user_option( 'themes_last_view' ); |
| 15 | if ( empty( $default_status ) ) |
| 16 | $default_status = 'all'; |
| 17 | $status = isset( $_REQUEST['theme_status'] ) ? $_REQUEST['theme_status'] : $default_status; |
| 18 | if ( !in_array( $status, array( 'all', 'enabled', 'disabled', 'upgrade', 'search' ) ) ) |
| 19 | $status = 'all'; |
| 20 | if ( $status != $default_status && 'search' != $status ) |
| 21 | update_user_meta( get_current_user_id(), 'themes_last_view', $status ); |
| 22 | |
| 23 | $page = $this->get_pagenum(); |
| 24 | |
| 25 | parent::WP_List_Table( array( |
| 26 | 'screen' => 'themes', |
| 27 | 'plural' => 'plugins', // @todo replace with themes and add css |
| 28 | ) ); |
| 29 | } |
| 30 | |
| 31 | function check_permissions() { |
| 32 | if ( is_multisite() ) { |
| 33 | $menu_perms = get_site_option( 'menu_items', array() ); |
| 34 | |
| 35 | if ( empty( $menu_perms['themes'] ) ) { |
| 36 | if ( !is_super_admin() ) |
| 37 | wp_die( __( 'Cheatin’ uh?' ) ); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | if ( !current_user_can('manage_network_themes') ) |
| 42 | wp_die( __( 'You do not have sufficient permissions to manage themes for this site.' ) ); |
| 43 | } |
| 44 | |
| 45 | function prepare_items() { |
| 46 | global $status, $themes, $totals, $page, $orderby, $order, $s; |
| 47 | |
| 48 | wp_reset_vars( array( 'orderby', 'order', 's' ) ); |
| 49 | |
| 50 | $themes = array( |
| 51 | 'all' => apply_filters( 'all_themes', get_themes() ), |
| 52 | 'search' => array(), |
| 53 | 'enabled' => array(), |
| 54 | 'disabled' => array(), |
| 55 | 'upgrade' => array() |
| 56 | ); |
| 57 | |
| 58 | $allowed_themes = get_site_allowed_themes(); |
| 59 | $current = get_site_transient( 'update_themes' ); |
| 60 | |
| 61 | foreach ( (array) $themes['all'] as $key => $theme ) { |
| 62 | if ( array_key_exists( $theme['Template'], $allowed_themes ) ) { |
| 63 | $themes['all'][$key]['enabled'] = true; |
| 64 | $themes['enabled'][$key] = $themes['all'][$key]; |
| 65 | } |
| 66 | else { |
| 67 | $themes['all'][$key]['enabled'] = false; |
| 68 | $themes['disabled'][$key] = $themes['all'][$key]; |
| 69 | } |
| 70 | if ( isset( $current->response[ $theme['Template'] ] ) ) |
| 71 | $themes['upgrade'][$key] = $themes['all'][$key]; |
| 72 | } |
| 73 | |
| 74 | if ( !current_user_can( 'update_themes' ) ) |
| 75 | $themes['upgrade'] = array(); |
| 76 | |
| 77 | if ( $s ) { |
| 78 | $status = 'search'; echo "opopop"; |
| 79 | $themes['search'] = array_filter( $themes['all'], array( $this, '_search_callback' ) ); |
| 80 | } |
| 81 | |
| 82 | $totals = array(); |
| 83 | foreach ( $themes as $type => $list ) |
| 84 | $totals[ $type ] = count( $list ); |
| 85 | |
| 86 | if ( empty( $themes[ $status ] ) && !in_array( $status, array( 'all', 'search' ) ) ) |
| 87 | $status = 'all'; |
| 88 | |
| 89 | $this->items = $themes[ $status ]; |
| 90 | $total_this_page = $totals[ $status ]; |
| 91 | |
| 92 | if ( $orderby ) { |
| 93 | $orderby = ucfirst( $orderby ); |
| 94 | $order = strtoupper( $order ); |
| 95 | |
| 96 | uasort( $this->items, array( $this, '_order_callback' ) ); |
| 97 | } |
| 98 | |
| 99 | $themes_per_page = $this->get_items_per_page( 'themes_per_page', 999 ); |
| 100 | |
| 101 | $start = ( $page - 1 ) * $themes_per_page; |
| 102 | |
| 103 | if ( $total_this_page > $themes_per_page ) |
| 104 | $this->items = array_slice( $this->items, $start, $themes_per_page ); |
| 105 | |
| 106 | $this->set_pagination_args( array( |
| 107 | 'total_items' => $total_this_page, |
| 108 | 'per_page' => $themes_per_page, |
| 109 | ) ); |
| 110 | } |
| 111 | |
| 112 | function _search_callback( $theme ) { |
| 113 | static $term; |
| 114 | if ( is_null( $term ) ) |
| 115 | $term = stripslashes( $_REQUEST['s'] ); |
| 116 | |
| 117 | foreach ( $theme as $key->$theme ) |
| 118 | if ( stripos( $key, $term ) !== false ) |
| 119 | return true; |
| 120 | |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | function _order_callback( $theme_a, $theme_b ) { |
| 125 | global $orderby, $order; |
| 126 | |
| 127 | $a = $theme_a[$orderby]; |
| 128 | $b = $theme_b[$orderby]; |
| 129 | |
| 130 | if ( $a == $b ) |
| 131 | return 0; |
| 132 | |
| 133 | if ( 'DESC' == $order ) |
| 134 | return ( $a < $b ) ? 1 : -1; |
| 135 | else |
| 136 | return ( $a < $b ) ? -1 : 1; |
| 137 | } |
| 138 | |
| 139 | function no_items() { |
| 140 | global $themes; |
| 141 | |
| 142 | if ( !empty( $themes['all'] ) ) |
| 143 | _e( 'No themes found.' ); |
| 144 | else |
| 145 | _e( 'You do not appear to have any themes available at this time.' ); |
| 146 | } |
| 147 | |
| 148 | function get_columns() { |
| 149 | global $status; |
| 150 | |
| 151 | return array( |
| 152 | 'cb' => '<input type="checkbox" />', |
| 153 | 'name' => __( 'Theme' ), |
| 154 | 'description' => __( 'Description' ), |
| 155 | ); |
| 156 | } |
| 157 | |
| 158 | function get_sortable_columns() { |
| 159 | return array( |
| 160 | 'name' => 'name', |
| 161 | ); |
| 162 | } |
| 163 | |
| 164 | function display_tablenav( $which ) { |
| 165 | global $status; |
| 166 | |
| 167 | parent::display_tablenav( $which ); |
| 168 | } |
| 169 | |
| 170 | function get_views() { |
| 171 | global $totals, $status; |
| 172 | |
| 173 | $status_links = array(); |
| 174 | foreach ( $totals as $type => $count ) { |
| 175 | if ( !$count ) |
| 176 | continue; |
| 177 | |
| 178 | switch ( $type ) { |
| 179 | case 'all': |
| 180 | $text = _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $count, 'themes' ); |
| 181 | break; |
| 182 | case 'enabled': |
| 183 | $text = _n( 'Enabled <span class="count">(%s)</span>', 'Enabled <span class="count">(%s)</span>', $count ); |
| 184 | break; |
| 185 | case 'disabled': |
| 186 | $text = _n( 'Disabled <span class="count">(%s)</span>', 'Disabled <span class="count">(%s)</span>', $count ); |
| 187 | break; |
| 188 | case 'upgrade': |
| 189 | $text = _n( 'Upgrade Available <span class="count">(%s)</span>', 'Upgrade Available <span class="count">(%s)</span>', $count ); |
| 190 | break; |
| 191 | case 'search': |
| 192 | $text = _n( 'Search Results <span class="count">(%s)</span>', 'Search Results <span class="count">(%s)</span>', $count ); |
| 193 | break; |
| 194 | } |
| 195 | |
| 196 | $status_links[$type] = sprintf( "<li><a href='%s' %s>%s</a>", |
| 197 | add_query_arg('theme_status', $type, 'themes.php'), |
| 198 | ( $type == $status ) ? ' class="current"' : '', |
| 199 | sprintf( $text, number_format_i18n( $count ) ) |
| 200 | ); |
| 201 | } |
| 202 | |
| 203 | return $status_links; |
| 204 | } |
| 205 | |
| 206 | function get_bulk_actions() { |
| 207 | global $status; |
| 208 | |
| 209 | $actions = array(); |
| 210 | if ( 'enabled' != $status ) |
| 211 | $actions['network-enable-selected'] = __( 'Network Enable' ); |
| 212 | if ( 'disabled' != $status ) |
| 213 | $actions['network-disable-selected'] = __( 'Network Disable' ); |
| 214 | if ( current_user_can( 'update_themes' ) ) |
| 215 | $actions['update-selected'] = __( 'Update' ); |
| 216 | |
| 217 | return $actions; |
| 218 | } |
| 219 | |
| 220 | function bulk_actions( $which ) { |
| 221 | global $status; |
| 222 | parent::bulk_actions( $which ); |
| 223 | } |
| 224 | |
| 225 | function current_action() { |
| 226 | return parent::current_action(); |
| 227 | } |
| 228 | |
| 229 | function display_rows() { |
| 230 | global $status, $page, $s; |
| 231 | |
| 232 | $context = $status; |
| 233 | |
| 234 | foreach ( $this->items as $key => $theme ) { |
| 235 | // preorder |
| 236 | $actions = array( |
| 237 | 'network_enable' => '', |
| 238 | 'network_disable' => '', |
| 239 | 'edit' => '' |
| 240 | ); |
| 241 | |
| 242 | $theme_key = esc_html( $theme['Stylesheet'] ); |
| 243 | |
| 244 | if ( empty( $theme['enabled'] ) ) { |
| 245 | if ( current_user_can( 'manage_network_themes' ) ) |
| 246 | $actions['network_enable'] = '<a href="' . wp_nonce_url('themes.php?action=network-enable&theme=' . $theme_key . '&paged=' . $page . '&s=' . $s, 'enable-theme_' . $theme_key) . '" title="' . __('Enable this theme for all sites in this network') . '" class="edit">' . __('Network Enable') . '</a>'; |
| 247 | } else { |
| 248 | if ( current_user_can( 'manage_network_themes' ) ) |
| 249 | $actions['network_disable'] = '<a href="' . wp_nonce_url('themes.php?action=network-disable&theme=' . $theme_key . '&paged=' . $page . '&s=' . $s, 'disable-theme_' . $theme_key) . '" title="' . __('Disable this theme') . '">' . __('Network Disable') . '</a>'; |
| 250 | } |
| 251 | |
| 252 | /* @todo link to theme editor |
| 253 | if ( current_user_can('edit_themes') ) |
| 254 | $actions['edit'] = '<a href="theme-editor.php?file=' . $theme['Stylesheet Files'][0] . '" title="' . __('Open this theme in the Theme Editor') . '" class="edit">' . __('Edit') . '</a>'; |
| 255 | */ |
| 256 | |
| 257 | $actions = apply_filters( 'theme_action_links', array_filter( $actions ), $theme_key, $theme, $context ); |
| 258 | $actions = apply_filters( "theme_action_links_$theme_key", $actions, $theme_key, $theme, $context ); |
| 259 | |
| 260 | $class = empty( $theme['enabled'] ) ? 'inactive' : 'active'; |
| 261 | $checkbox = "<input type='checkbox' name='checked[]' value='" . esc_attr( $theme_key ) . "' />"; |
| 262 | |
| 263 | $description = '<p>' . $theme['Description'] . '</p>'; |
| 264 | $theme_name = $theme['Name']; |
| 265 | |
| 266 | |
| 267 | $id = sanitize_title( $theme_name ); |
| 268 | |
| 269 | echo " |
| 270 | <tr id='$id' class='$class'> |
| 271 | <th scope='row' class='check-column'>$checkbox</th> |
| 272 | <td class='theme-title'><strong>$theme_name</strong></td> |
| 273 | <td class='desc'>$description</td> |
| 274 | </tr> |
| 275 | <tr class='$class second'> |
| 276 | <td></td> |
| 277 | <td class='theme-title'>"; |
| 278 | |
| 279 | echo $this->row_actions( $actions, true ); |
| 280 | |
| 281 | echo "</td> |
| 282 | <td class='desc'>"; |
| 283 | $theme_meta = array(); |
| 284 | if ( !empty( $theme['Version'] ) ) |
| 285 | $theme_meta[] = sprintf( __( 'Version %s' ), $theme['Version'] ); |
| 286 | if ( !empty( $theme['Author'] ) ) { |
| 287 | $author = $theme['Author']; |
| 288 | if ( !empty( $theme['Author URI'] ) ) |
| 289 | $author = '<a href="' . $theme['Author URI'] . '" title="' . __( 'Visit author homepage' ) . '">' . $theme['Author'] . '</a>'; |
| 290 | $theme_meta[] = sprintf( __( 'By %s' ), $author ); |
| 291 | } |
| 292 | if ( !empty( $theme['Theme URI'] ) ) |
| 293 | $theme_meta[] = '<a href="' . $theme['Theme URI'] . '" title="' . __( 'Visit theme homepage' ) . '">' . __( 'Visit Theme Site' ) . '</a>'; |
| 294 | |
| 295 | $theme_meta = apply_filters( 'theme_row_meta', $theme_meta, $theme_key, $theme, $status ); |
| 296 | echo implode( ' | ', $theme_meta ); |
| 297 | echo "</td> |
| 298 | </tr>\n"; |
| 299 | |
| 300 | do_action( 'after_theme_row', $theme_key, $theme, $status ); |
| 301 | do_action( "after_theme_row_$theme_key", $theme_key, $theme, $status ); |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | ?> |
| 306 | No newline at end of file |