format
3 months ago
helpers
5 months ago
models
5 months ago
webp
3 months ago
class.backup.php
5 months ago
class.custom-folders.php
5 months ago
class.folder-image.php
5 months ago
class.folder.php
5 months ago
class.folders-list-table.php
5 months ago
class.gallery-nextgen.php
5 months ago
class.image-nextgen.php
5 months ago
class.image-statistic-folders.php
5 months ago
class.image-statistic-nextgen.php
5 months ago
class.wpcli-optimize.php
5 months ago
index.php
5 months ago
class.folders-list-table.php
281 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class WRIO_Folders_List_Table |
| 5 | */ |
| 6 | class WRIO_Folders_List_Table extends WP_List_Table { |
| 7 | |
| 8 | public function __construct() { |
| 9 | // Set parent defaults. |
| 10 | parent::__construct( |
| 11 | [ |
| 12 | 'singular' => 'media', // Singular name of the listed records. |
| 13 | 'plural' => 'media', // Plural name of the listed records. |
| 14 | 'ajax' => false, // Does this table support ajax? |
| 15 | ] |
| 16 | ); |
| 17 | } |
| 18 | |
| 19 | public function get_columns() { |
| 20 | $columns = [ |
| 21 | 'cb' => '<input type="checkbox" />', // Render a checkbox instead of text. |
| 22 | 'preview' => _x( 'Preview', 'Column label', 'robin-image-optimizer' ), |
| 23 | 'file' => _x( 'File path', 'Column label', 'robin-image-optimizer' ), |
| 24 | 'folder' => _x( 'Folder', 'Column label', 'robin-image-optimizer' ), |
| 25 | 'status' => _x( 'Status', 'Column label', 'robin-image-optimizer' ), |
| 26 | 'optimization' => _x( 'Optimization', 'Column label', 'robin-image-optimizer' ), |
| 27 | ]; |
| 28 | |
| 29 | return $columns; |
| 30 | } |
| 31 | |
| 32 | public function prepare_items() { |
| 33 | global $wpdb; |
| 34 | |
| 35 | $per_page = 20; |
| 36 | |
| 37 | $hidden = []; |
| 38 | $columns = $this->get_columns(); |
| 39 | $sortable = $this->get_sortable_columns(); |
| 40 | $this->_column_headers = [ $columns, $hidden, $sortable ]; |
| 41 | |
| 42 | $current_page = $this->get_pagenum(); |
| 43 | $offset = ( $current_page - 1 ) * $per_page; |
| 44 | |
| 45 | $folder_uid = WRIO_Plugin::app()->request->get( 'folder-filter', null, true ); |
| 46 | $status = WRIO_Plugin::app()->request->get( 'status-filter', null, true ); |
| 47 | |
| 48 | if ( ! in_array( $status, [ 'success', 'unoptimized', 'error' ] ) ) { |
| 49 | $status = null; |
| 50 | } |
| 51 | |
| 52 | $db_table = RIO_Process_Queue::table_name(); |
| 53 | |
| 54 | $sql_filter = ''; |
| 55 | |
| 56 | if ( ! empty( $status ) ) { |
| 57 | $sql_filter .= " AND result_status = '" . esc_sql( $status ) . "' "; |
| 58 | } |
| 59 | |
| 60 | if ( ! empty( $folder_uid ) ) { |
| 61 | $sql_filter .= " AND item_hash_alternative = '" . esc_sql( $folder_uid ) . "' "; |
| 62 | } |
| 63 | |
| 64 | $total_items = $wpdb->get_var( |
| 65 | " |
| 66 | SELECT COUNT(*) |
| 67 | FROM {$db_table} |
| 68 | WHERE item_type = 'cf_image' {$sql_filter}" |
| 69 | ); |
| 70 | |
| 71 | $rows = $wpdb->get_results( |
| 72 | $wpdb->prepare( |
| 73 | " |
| 74 | SELECT * |
| 75 | FROM {$db_table} |
| 76 | WHERE item_type = 'cf_image' {$sql_filter} |
| 77 | ORDER BY id DESC |
| 78 | LIMIT %d |
| 79 | OFFSET %d", |
| 80 | $per_page, |
| 81 | $offset |
| 82 | ) |
| 83 | ); |
| 84 | |
| 85 | if ( empty( $rows ) ) { |
| 86 | $this->items = []; |
| 87 | |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | foreach ( (array) $rows as $key => $row ) { |
| 92 | $rows[ $key ] = new RIO_Process_Queue( $row ); |
| 93 | } |
| 94 | |
| 95 | $this->items = $rows; |
| 96 | |
| 97 | $this->set_pagination_args( |
| 98 | [ |
| 99 | 'total_items' => $total_items, // WE have to calculate the total number of items. |
| 100 | 'per_page' => $per_page, // WE have to determine how many items to show on a page. |
| 101 | 'total_pages' => ceil( $total_items / $per_page ), // WE have to calculate the total number of pages. |
| 102 | ] |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | public function display() { |
| 108 | $cf = WRIO_Custom_Folders::get_instance(); |
| 109 | $folders = $cf->getFolders(); |
| 110 | |
| 111 | $folder_uid = WRIO_Plugin::app()->request->get( 'folder-filter', null, true ); |
| 112 | $status = WRIO_Plugin::app()->request->get( 'status-filter', null, true ); |
| 113 | |
| 114 | if ( ! in_array( $status, [ 'success', 'unoptimized', 'error' ] ) ) { |
| 115 | $status = null; |
| 116 | } |
| 117 | |
| 118 | $optimized_count = RIO_Process_Queue::count_by_type_status( 'cf_image', 'success' ); |
| 119 | $unoptimized_count = RIO_Process_Queue::count_by_type_status( 'cf_image', 'unoptimized' ); |
| 120 | $error_count = RIO_Process_Queue::count_by_type_status( 'cf_image', 'error' ); |
| 121 | |
| 122 | ?> |
| 123 | <div class="wrap wriop-files-list"> |
| 124 | <h1><?php echo esc_html( get_admin_page_title() ); ?></h1> |
| 125 | <form method="get" id="wriop-files-list-form" action="<?php echo admin_url( 'upload.php?page=rio-custom-media' ); ?>"> |
| 126 | <input type="hidden" name="page" value="rio-custom-media"/> |
| 127 | <div class="wp-filter"> |
| 128 | <div class="filter-items"> |
| 129 | <label for="folder-filter" class="screen-reader-text"><?php _e( 'Filter by folder', 'robin-image-optimizer' ); ?></label> |
| 130 | <select class="folder-filters" name="folder-filter" id="folder-filter"> |
| 131 | <option value="" selected="selected"><?php _e( 'All Folders', 'robin-image-optimizer' ); ?></option> |
| 132 | <?php if ( ! empty( $folders ) ) : ?> |
| 133 | <?php foreach ( (array) $folders as $folder ) : ?> |
| 134 | <option <?php selected( $folder_uid, $folder->get( 'uid' ) ); ?> value="<?php echo esc_attr( $folder->get( 'uid' ) ); ?>"><?php echo esc_attr( $folder->get( 'path' ) ); ?> |
| 135 | (<?php echo esc_attr( $folder->get( 'files_count' ) ); ?>) |
| 136 | </option> |
| 137 | <?php endforeach; ?> |
| 138 | <?php endif; ?> |
| 139 | </select> |
| 140 | <label for="status-filter" class="screen-reader-text"><?php _e( 'Filter by status', 'robin-image-optimizer' ); ?></label> |
| 141 | <select class="folder-filters" name="status-filter" id="status-filter"> |
| 142 | <option value="" selected="selected"><?php _e( 'All Media Files', 'robin-image-optimizer' ); ?></option> |
| 143 | <option <?php selected( $status, 'success' ); ?> value="success"><?php _e( 'Optimized', 'robin-image-optimizer' ); ?> |
| 144 | (<?php echo esc_attr( $optimized_count ); ?>) |
| 145 | </option> |
| 146 | <option <?php selected( $status, 'unoptimized' ); ?> value="unoptimized"><?php _e( 'Unoptimized', 'robin-image-optimizer' ); ?> |
| 147 | (<?php echo esc_attr( $unoptimized_count ); ?>) |
| 148 | </option> |
| 149 | <option <?php selected( $status, 'error' ); ?> value="error"><?php _e( 'Errors', 'robin-image-optimizer' ); ?> |
| 150 | (<?php echo esc_attr( $error_count ); ?>) |
| 151 | </option> |
| 152 | </select> |
| 153 | <input type="submit" id="folders-query-submit" class="button" value="Filter"> |
| 154 | </div> |
| 155 | </div> |
| 156 | <?php parent::display(); ?> |
| 157 | </form> |
| 158 | </div> |
| 159 | <?php |
| 160 | } |
| 161 | |
| 162 | protected function get_sortable_columns() { |
| 163 | $sortable_columns = []; |
| 164 | |
| 165 | return $sortable_columns; |
| 166 | } |
| 167 | |
| 168 | protected function get_bulk_actions() { |
| 169 | $actions = []; |
| 170 | |
| 171 | return $actions; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * @param RIO_Process_Queue $item |
| 176 | * |
| 177 | * @return string |
| 178 | */ |
| 179 | protected function column_cb( $item ) { |
| 180 | return sprintf( |
| 181 | '<input type="checkbox" name="%1$s[]" value="%2$s" />', |
| 182 | $this->_args['singular'], // Let's simply repurpose the table's singular label ("movie"). |
| 183 | $item->get_id() // The value of the checkbox should be the record's ID. |
| 184 | ); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * @param RIO_Process_Queue $item |
| 189 | */ |
| 190 | protected function column_preview( $item ) { |
| 191 | |
| 192 | /** @var WRIO_CF_Image_Extra_Data $extra_data */ |
| 193 | $extra_data = $item->get_extra_data(); |
| 194 | |
| 195 | if ( ! empty( $extra_data ) ) { |
| 196 | $file_relative_path = wp_normalize_path( $extra_data->get_file_path() ); |
| 197 | $image_url = home_url( $file_relative_path ); |
| 198 | |
| 199 | printf( |
| 200 | ' |
| 201 | <span class="media-icon image-icon"> |
| 202 | <a href="%s"><img src="%s" class="attachment-60x60 size-60x60" alt="" width="60" height="60"></a> |
| 203 | </span>', |
| 204 | esc_url( $image_url ), |
| 205 | esc_url( $image_url ) |
| 206 | ); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * @param RIO_Process_Queue $item |
| 212 | */ |
| 213 | protected function column_file( $item ) { |
| 214 | |
| 215 | /** @var WRIO_CF_Image_Extra_Data $extra_data */ |
| 216 | $extra_data = $item->get_extra_data(); |
| 217 | |
| 218 | if ( ! empty( $extra_data ) ) { |
| 219 | $file_relative_path = wp_normalize_path( $extra_data->get_file_path() ); |
| 220 | $file_name = wp_basename( $file_relative_path ); |
| 221 | $image_url = home_url( $file_relative_path ); |
| 222 | |
| 223 | printf( |
| 224 | ' |
| 225 | <p class="filename"> |
| 226 | <a href="%s">%s</a> |
| 227 | </p>', |
| 228 | esc_url( $image_url ), |
| 229 | $file_name |
| 230 | ); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * @param RIO_Process_Queue $item |
| 236 | */ |
| 237 | protected function column_folder( $item ) { |
| 238 | |
| 239 | /** @var WRIO_CF_Image_Extra_Data $extra_data */ |
| 240 | $extra_data = $item->get_extra_data(); |
| 241 | |
| 242 | if ( ! empty( $extra_data ) ) { |
| 243 | $file_relative_path = wp_normalize_path( $extra_data->get_file_path() ); |
| 244 | $file_name = wp_basename( $file_relative_path ); |
| 245 | $folder = str_replace( $file_name, '', $file_relative_path ); |
| 246 | |
| 247 | printf( '<code>%s</code > ', $folder ); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * @param RIO_Process_Queue $item |
| 253 | */ |
| 254 | protected function column_status( $item ) { |
| 255 | $statuses = [ |
| 256 | 'success' => __( 'Success', 'robin-image-optimizer' ), |
| 257 | 'error' => __( 'Error', 'robin-image-optimizer' ), |
| 258 | 'processing' => __( 'Processing', 'robin-image-optimizer' ), |
| 259 | 'unoptimized' => __( 'Unoptimized', 'robin-image-optimizer' ), |
| 260 | 'skip' => __( 'Skipped', 'robin-image-optimizer' ), |
| 261 | ]; |
| 262 | if ( isset( $statuses[ $item->get_result_status() ] ) ) { |
| 263 | echo esc_attr( $statuses[ $item->get_result_status() ] ); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * @param RIO_Process_Queue $item |
| 269 | */ |
| 270 | protected function column_optimization( $item ) { |
| 271 | $cf = WRIO_Custom_Folders::get_instance(); |
| 272 | echo $cf->getMediaColumnContent( $item->get_id() ); |
| 273 | } |
| 274 | |
| 275 | protected function process_bulk_action() { |
| 276 | // Detect when a bulk action is being triggered. |
| 277 | if ( 'delete' === $this->current_action() ) { |
| 278 | wp_die( 'Items deleted(or they would be if we had items to delete)! ' ); |
| 279 | } |
| 280 | } |
| 281 | } |