PluginProbe
TablePress – Tables in WordPress made easy / 3.4
TablePress – Tables in WordPress made easy v3.4
3.4 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.12 1.14 1.9.2 2.0.4 2.1.7 2.1.8 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3 2.3.1 2.3.2 2.4 2.4.1 2.4.2 2.4.3 All 45 releases
tablepress / classes / class-import-base.php

class-import-base.php in TablePress – Tables in WordPress made easy 3.4, at classes/class-import-base.php

66 lines 1.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * TablePress Table Import Base Class
4 *
5 * @package TablePress
6 * @subpackage Export/Import
7 * @author Tobias Bäthge
8 * @since 2.0.0
9 */
10
11 declare(strict_types=1);
12
13 // Prohibit direct script loading.
14 defined( 'ABSPATH' ) || die( 'No direct script access allowed!' );
15
16 /**
17 * TablePress Table Import Base Class
18 *
19 * @package TablePress
20 * @subpackage Export/Import
21 * @author Tobias Bäthge
22 * @since 2.0.0
23 */
24 abstract class TablePress_Import_Base {
25
26 /**
27 * Makes sure that a passed table array is rectangular with all rows having the same number of columns (the highest one that is found).
28 *
29 * This function uses call by reference to save PHP memory on large arrays.
30 *
31 * @since 1.0.0
32 * @since 2.0.0 The $an_array parameter is handled by reference.
33 *
34 * @param array<int, array<int, mixed>> $an_array Two-dimensional array to be padded.
35 */
36 public function pad_array_to_max_cols( array &$an_array ): void {
37 $max_columns = $this->count_max_columns( $an_array );
38 // Extend the array to at least one column.
39 $max_columns = max( 1, $max_columns );
40 array_walk(
41 $an_array,
42 static function ( array &$row, int $col_idx ) use ( $max_columns ): void {
43 $row = array_pad( $row, $max_columns, '' );
44 },
45 );
46 }
47
48 /**
49 * Get the highest number of columns in the rows.
50 *
51 * @since 1.0.0
52 *
53 * @param array<int, array<int, mixed>> $an_array Two-dimensional array.
54 * @return int Highest number of columns in the rows of the array.
55 */
56 protected function count_max_columns( array $an_array ): int {
57 $max_columns = 0;
58 foreach ( $an_array as $row_idx => $row ) {
59 $num_columns = count( $row );
60 $max_columns = max( $num_columns, $max_columns );
61 }
62 return $max_columns;
63 }
64
65 } // class TablePress_Import_Base
66