| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | <?php |
| 2 | -/* | |
| 2 | +/** | |
| 3 | 3 | +=====================================================================+ |
| 4 | 4 | | ____ _ ____ __ _ _ | |
| 5 | 5 | | / ___|___ __| | ___ | _ \ _ __ ___ / _(_) | ___ _ __ | |
| 6 | 6 | | | | / _ \ / _` |/ _ \ | |_) | '__/ _ \| |_| | |/ _ \ '__| | |
| @@ -6,13 +6,15 @@ | ||
| 6 | 6 | | | | / _ \ / _` |/ _ \ | |_) | '__/ _ \| |_| | |/ _ \ '__| | |
| 7 | 7 | | | |__| (_) | (_| | __/ | __/| | | (_) | _| | | __/ | | |
| 8 | 8 | | \____\___/ \__,_|\___| |_| |_| \___/|_| |_|_|\___|_| | |
| 9 | 9 | | | |
| 10 | - | (c) Jerome Bruandet ~ https://code-profiler.com/ | | |
| 10 | + | (c) Jerome Bruandet ~ https://nintechnet.com/codeprofiler/ | | |
| 11 | 11 | +=====================================================================+ |
| 12 | 12 | */ |
| 13 | 13 | |
| 14 | -if (! defined( 'ABSPATH' ) ) { die( 'Forbidden' ); } | |
| 14 | +if (! defined('ABSPATH') ) { | |
| 15 | + die('Forbidden'); | |
| 16 | +} | |
| 15 | 17 | |
| 16 | 18 | // ===================================================================== |
| 17 | 19 | |
| 18 | 20 | class CodeProfiler_Table_Profiles extends WP_List_Table { |
| @@ -25,42 +27,52 @@ | ||
| 25 | 27 | 'summary', |
| 26 | 28 | 'composer' |
| 27 | 29 | ]; |
| 28 | 30 | private $abspath; |
| 31 | + private $row_count = 0; | |
| 29 | 32 | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * Initialize | |
| 36 | + */ | |
| 30 | 37 | function __construct() { |
| 31 | 38 | |
| 32 | - $this->abspath = rtrim( ABSPATH, '/\\' ); | |
| 39 | + $this->abspath = rtrim( ABSPATH, '/\\'); | |
| 33 | 40 | |
| 34 | - parent::__construct( array( | |
| 35 | - 'singular' => esc_html__( 'profile', 'code-profiler' ), | |
| 36 | - 'plural' => esc_html__( 'profiles', 'code-profiler' ), | |
| 41 | + parent::__construct( [ | |
| 42 | + 'singular' => esc_html__('profile', 'code-profiler'), | |
| 43 | + 'plural' => esc_html__('profiles', 'code-profiler'), | |
| 37 | 44 | 'ajax' => false |
| 38 | - )); | |
| 45 | + ] ); | |
| 39 | 46 | } |
| 40 | 47 | |
| 41 | - /* | |
| 48 | + | |
| 49 | + /** | |
| 42 | 50 | * Empty list |
| 43 | 51 | */ |
| 44 | 52 | function no_items() { |
| 45 | - esc_html_e( 'No profile found.', 'code-profiler' ); | |
| 53 | + esc_html_e('No profile found.', 'code-profiler'); | |
| 54 | + $this->is_empty = 1; | |
| 46 | 55 | } |
| 47 | 56 | |
| 48 | - /* | |
| 57 | + | |
| 58 | + /** | |
| 49 | 59 | * Default |
| 50 | 60 | */ |
| 51 | 61 | function column_default( $item, $column_name ) { |
| 52 | 62 | |
| 53 | - if ( $item[ $column_name ] == '-' ) { | |
| 63 | + if ( $item[ $column_name ] == '-') { | |
| 54 | 64 | // Old profiles (CP <=1.2) must not call number_format() |
| 55 | 65 | return $item[ $column_name ]; |
| 56 | 66 | } |
| 57 | 67 | switch( $column_name ) { |
| 58 | - case 'profile': | |
| 59 | - return $item[ $column_name ]; | |
| 60 | - break; | |
| 61 | 68 | case 'date': |
| 62 | - return date('Y/m/d \@ H:i', $item[ $column_name ] ); | |
| 69 | + return sprintf( | |
| 70 | + /* translators: This is already translated in WordPress core */ | |
| 71 | + __('%1$s at %2$s'), | |
| 72 | + date('Y/m/d', $item[ $column_name ] ) .'<br />', | |
| 73 | + date('g:i a', $item[ $column_name ] ) | |
| 74 | + ); | |
| 63 | 75 | break; |
| 64 | 76 | case 'time': |
| 65 | 77 | return $item[ $column_name ] .' s'; |
| 66 | 78 | break; |
| @@ -76,28 +88,30 @@ | ||
| 76 | 88 | return ''; |
| 77 | 89 | } |
| 78 | 90 | } |
| 79 | 91 | |
| 80 | - /* | |
| 92 | + | |
| 93 | + /** | |
| 81 | 94 | * Sortable columns |
| 82 | 95 | */ |
| 83 | 96 | function get_sortable_columns() { |
| 84 | - return array( | |
| 85 | - 'profile' => array( 'profile', true ), | |
| 86 | - 'date' => array( 'date', true ), | |
| 87 | - 'items' => array( 'items', true ), | |
| 88 | - 'time' => array( 'time', true ), | |
| 89 | - 'mem' => array( 'mem', true ), | |
| 90 | - 'io' => array( 'io', true ), | |
| 91 | - 'queries' => array( 'queries', true ) | |
| 92 | - ); | |
| 97 | + return [ | |
| 98 | + 'profile' => ['profile', true ], | |
| 99 | + 'date' => ['date', true ], | |
| 100 | + 'items' => ['items', true ], | |
| 101 | + 'time' => ['time', true ], | |
| 102 | + 'mem' => ['mem', true ], | |
| 103 | + 'io' => ['io', true ], | |
| 104 | + 'queries' => ['queries', true ] | |
| 105 | + ]; | |
| 93 | 106 | } |
| 94 | 107 | |
| 95 | - /* | |
| 108 | + | |
| 109 | + /** | |
| 96 | 110 | * Columns |
| 97 | 111 | */ |
| 98 | 112 | function get_columns(){ |
| 99 | - return array( | |
| 113 | + return [ | |
| 100 | 114 | 'cb' => '<input type="checkbox" />', |
| 101 | 115 | 'profile' => esc_html__( 'Profile', 'code-profiler' ), |
| 102 | 116 | 'date' => esc_html__( 'Date', 'code-profiler' ), |
| 103 | 117 | 'items' => esc_html__( 'Items', 'code-profiler' ), |
| @@ -104,12 +118,13 @@ | ||
| 104 | 118 | 'time' => esc_html__( 'Time', 'code-profiler' ), |
| 105 | 119 | 'mem' => esc_html__( 'Memory', 'code-profiler' ), |
| 106 | 120 | 'io' => esc_html__( 'File I/O', 'code-profiler' ), |
| 107 | 121 | 'queries' => esc_html__( 'SQL', 'code-profiler' ) |
| 108 | - ); | |
| 122 | + ]; | |
| 109 | 123 | } |
| 110 | 124 | |
| 111 | - /* | |
| 125 | + | |
| 126 | + /** | |
| 112 | 127 | * Sorting |
| 113 | 128 | */ |
| 114 | 129 | function usort_reorder( $a, $b ) { |
| 115 | 130 | // Sort by date by default |
| @@ -115,12 +130,13 @@ | ||
| 115 | 130 | // Sort by date by default |
| 116 | 131 | $orderby = (! empty( $_GET['orderby'] ) ) ? sanitize_key( $_GET['orderby'] ) : 'ID'; |
| 117 | 132 | $order = (! empty( $_GET['order'] ) ) ? sanitize_key( $_GET['order'] ) : 'asc'; |
| 118 | 133 | $result = $this->cmp_num_or_string( $a[$orderby], $b[$orderby] ); |
| 119 | - return ( $order === 'asc' ) ? $result : -$result; | |
| 134 | + return ( $order === 'asc') ? $result : -$result; | |
| 120 | 135 | } |
| 121 | 136 | |
| 122 | - /* | |
| 137 | + | |
| 138 | + /** | |
| 123 | 139 | * Sort string and numeric values differently |
| 124 | 140 | */ |
| 125 | 141 | function cmp_num_or_string( $a, $b ) { |
| 126 | 142 | if ( is_numeric( $a ) && is_numeric( $b ) ) { |
| @@ -130,13 +146,15 @@ | ||
| 130 | 146 | } |
| 131 | 147 | } |
| 132 | 148 | |
| 133 | 149 | |
| 134 | - /* | |
| 150 | + /** | |
| 135 | 151 | * Row action links |
| 136 | 152 | */ |
| 137 | 153 | function column_profile( $item ) { |
| 138 | 154 | |
| 155 | + $this->row_count++; | |
| 156 | + | |
| 139 | 157 | // Keep sorting order for the "delete" action link |
| 140 | 158 | $orderby = (! empty( $_GET['orderby'] ) ) ? sanitize_key( $_GET['orderby'] ) : 'ID'; |
| 141 | 159 | $order = (! empty( $_GET['order'] ) ) ? sanitize_key( $_GET['order'] ) : 'asc'; |
| 142 | 160 | |
| @@ -142,30 +160,64 @@ | ||
| 142 | 160 | |
| 143 | 161 | $actions = array( |
| 144 | 162 | 'view' => sprintf( |
| 145 | 163 | '<a href="?page=code-profiler&cptab=profiles_list&action=%s&id=%s§ion=1">%s</a>', |
| 146 | - 'view_profile', esc_attr( $item['ID'] ), esc_attr__('View') | |
| 164 | + 'view_profile', | |
| 165 | + esc_attr( $item['ID'] ), | |
| 166 | + esc_html__('View') | |
| 147 | 167 | ), |
| 168 | + 'edit' => sprintf( | |
| 169 | + '<a style="cursor:pointer" onClick="cpjs_toggle_name(\'%s\')">%s</a>', | |
| 170 | + esc_attr( $this->row_count ), | |
| 171 | + esc_html__('Quick Edit') | |
| 172 | + ), | |
| 148 | 173 | 'delete' => sprintf( |
| 149 | 174 | '<a href="?page=code-profiler&cptab=profiles_list&action=%s&profiles[]=%s&_wpnonce=%s&orderby=%s&order=%s" '. |
| 150 | 175 | 'onclick="return cpjs_delete_profile();">%s</a>', |
| 151 | - 'delete_profiles', esc_attr( $item['ID'] ), | |
| 152 | - wp_create_nonce( 'bulk-'. $this->_args['plural'] ), $orderby, $order, esc_attr__('Delete') | |
| 176 | + 'delete_profiles', | |
| 177 | + esc_attr( $item['ID'] ), | |
| 178 | + wp_create_nonce('bulk-'. $this->_args['plural'] ), | |
| 179 | + $orderby, | |
| 180 | + $order, | |
| 181 | + esc_html__('Delete') | |
| 153 | 182 | ) |
| 154 | 183 | ); |
| 155 | - return sprintf( '%1$s %2$s', $item['profile'], $this->row_actions( $actions ) ); | |
| 184 | + | |
| 185 | + $profile_name = sprintf( | |
| 186 | + '<div id="profile_name_%1$s">%2$s</div>'. | |
| 187 | + '<div id="profile_div_%1$s" style="display:none">'. | |
| 188 | + '<input type="text" id="edit-%1$s" name="edit_%3$s" value="" maxlength="100" />'. | |
| 189 | + '<p>'. | |
| 190 | + '<input type="button" class="button button-primary button-small" value="%4$s" onClick="cpjs_edit_name(\'%3$s\', \'%1$s\', \'%6$s\', \'%1$s\')"/>'. | |
| 191 | + ' '. | |
| 192 | + '<input type="button" class="button button-secondary button-small" value="%5$s" onClick="cpjs_toggle_name(\'%1$s\')"/>'. | |
| 193 | + ' '. | |
| 194 | + '<span id="profile_spinner_%1$s" class="spinner" style="float:none"></span>'. | |
| 195 | + '</p>'. | |
| 196 | + '</div>', | |
| 197 | + esc_attr( $this->row_count ), | |
| 198 | + esc_html( $item['profile'] ), | |
| 199 | + esc_attr( $item['ID'] ), | |
| 200 | + esc_attr__('Update'), | |
| 201 | + esc_attr__('Cancel'), | |
| 202 | + wp_create_nonce('rename-profile') | |
| 203 | + ); | |
| 204 | + | |
| 205 | + return sprintf('%1$s %2$s', $profile_name, $this->row_actions( $actions ) ); | |
| 156 | 206 | } |
| 157 | 207 | |
| 158 | - /* | |
| 208 | + | |
| 209 | + /** | |
| 159 | 210 | * Bulk action menu (delete) |
| 160 | 211 | */ |
| 161 | 212 | function get_bulk_actions() { |
| 162 | - return array( | |
| 213 | + return [ | |
| 163 | 214 | 'delete_profiles' => esc_html__('Delete') |
| 164 | - ); | |
| 215 | + ]; | |
| 165 | 216 | } |
| 166 | 217 | |
| 167 | - /* | |
| 218 | + | |
| 219 | + /** | |
| 168 | 220 | * Checkboxes |
| 169 | 221 | */ |
| 170 | 222 | function column_cb($item) { |
| 171 | 223 | return sprintf( |
| @@ -173,9 +225,10 @@ | ||
| 173 | 225 | esc_attr( $item['ID'] ) |
| 174 | 226 | ); |
| 175 | 227 | } |
| 176 | 228 | |
| 177 | - /* | |
| 229 | + | |
| 230 | + /** | |
| 178 | 231 | * Prepare to display profiles |
| 179 | 232 | */ |
| 180 | 233 | function prepare_items() { |
| 181 | 234 | $columns = $this->get_columns(); |
| @@ -184,14 +237,14 @@ | ||
| 184 | 237 | $this->_column_headers = array( $columns, $hidden, $sortable ); |
| 185 | 238 | |
| 186 | 239 | // Fetch our data |
| 187 | 240 | $profile = $this->fetch_profiles(); |
| 188 | - usort( $profile, array( &$this, 'usort_reorder' ) ); | |
| 241 | + usort( $profile, array( &$this, 'usort_reorder') ); | |
| 189 | 242 | |
| 190 | 243 | $per_page = 30; |
| 191 | 244 | |
| 192 | 245 | // If we just delete some profiles, we go back to page #1: |
| 193 | - if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'delete_profiles' ) { | |
| 246 | + if ( isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'delete_profiles') { | |
| 194 | 247 | $current_page = 1; |
| 195 | 248 | } else { |
| 196 | 249 | $current_page = $this->get_pagenum(); |
| 197 | 250 | } |
| @@ -204,21 +257,22 @@ | ||
| 204 | 257 | 'per_page' => $per_page |
| 205 | 258 | )); |
| 206 | 259 | } |
| 207 | 260 | |
| 208 | - /* | |
| 261 | + | |
| 262 | + /** | |
| 209 | 263 | * Retrieve all profiles |
| 210 | 264 | */ |
| 211 | 265 | function fetch_profiles() { |
| 212 | 266 | |
| 213 | 267 | $profiles = []; |
| 214 | - $glob = glob( CODE_PROFILER_UPLOAD_DIR .'/*.slugs.profile' ); | |
| 268 | + $glob = code_profiler_glob( CODE_PROFILER_UPLOAD_DIR, '\.slugs\.profile$', true ); | |
| 215 | 269 | |
| 216 | 270 | if ( is_array( $glob ) ) { |
| 217 | 271 | $count = 0; |
| 218 | 272 | foreach( $glob as $path ) { |
| 219 | 273 | $file = basename( $path ); |
| 220 | - if ( preg_match( '`^(\d{10}\.\d+)\.(.+?)\.(?:slugs)\.profile$`', $file, $match ) ) { | |
| 274 | + if ( preg_match('`^(\d{10}\.\d+)\.(.+?)\.(?:slugs)\.profile$`', $file, $match ) ) { | |
| 221 | 275 | |
| 222 | 276 | $error = 0; $fsize = 0; |
| 223 | 277 | // Make sure we have all profile files |
| 224 | 278 | foreach( $this->default_files as $pname ) { |
| @@ -288,15 +342,19 @@ | ||
| 288 | 342 | return $profiles; |
| 289 | 343 | } |
| 290 | 344 | |
| 291 | 345 | |
| 292 | - | |
| 293 | - /* | |
| 346 | + /** | |
| 294 | 347 | * Search a profile file for a string. |
| 295 | 348 | */ |
| 296 | 349 | private function search_profile_file( $file, $string ) { |
| 297 | 350 | |
| 298 | - $fh = fopen( $file, 'rb' ); | |
| 351 | + // Search in the filename too | |
| 352 | + if ( stripos( $file, $string ) !== false ) { | |
| 353 | + return true; | |
| 354 | + } | |
| 355 | + | |
| 356 | + $fh = fopen( $file, 'rb'); | |
| 299 | 357 | if ( $fh === false ) { |
| 300 | 358 | return false; |
| 301 | 359 | } |
| 302 | 360 | while(! feof( $fh ) ) { |
| @@ -301,9 +359,9 @@ | ||
| 301 | 359 | } |
| 302 | 360 | while(! feof( $fh ) ) { |
| 303 | 361 | $line = fgets( $fh ); |
| 304 | 362 | // Remove the ABSPATH, we don't include it in the search |
| 305 | - $line = ltrim( str_replace( $this->abspath, '', $line ), '\\/' ); | |
| 363 | + $line = ltrim( str_replace( $this->abspath, '', $line ), '\\/'); | |
| 306 | 364 | if ( stripos( $line, $string ) !== false ) { |
| 307 | 365 | fclose( $fh ); |
| 308 | 366 | return true; |
| 309 | 367 | } |
| @@ -313,9 +371,9 @@ | ||
| 313 | 371 | return false; |
| 314 | 372 | } |
| 315 | 373 | |
| 316 | 374 | |
| 317 | - /* | |
| 375 | + /** | |
| 318 | 376 | * Delete one or more profiles. |
| 319 | 377 | */ |
| 320 | 378 | public function delete_profiles( $profiles ) { |
| 321 | 379 | |