| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Suppress "error - 0 - No summary was found for this file" on phpdoc generation |
| 5 |
* |
| 6 |
* @package WPDataAccess\Utilities |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WPDataAccess\Utilities { |
| 10 |
|
| 11 |
use WPDataAccess\WPDA; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class WPDA_Export_Json |
| 15 |
* |
| 16 |
* @author Peter Schulz |
| 17 |
* @since 2.0.13 |
| 18 |
*/ |
| 19 |
class WPDA_Export_Json extends WPDA_Export_Formatted { |
| 20 |
|
| 21 |
/** |
| 22 |
* Variable used for loop processing |
| 23 |
* |
| 24 |
* @var bool |
| 25 |
*/ |
| 26 |
protected $first_row = true; |
| 27 |
|
| 28 |
/** |
| 29 |
* File header for JSON export |
| 30 |
* |
| 31 |
* @since 2.0.13 |
| 32 |
*/ |
| 33 |
protected function header() { |
| 34 |
WPDA::sent_header( 'application/json', null, "{$this->table_names}.json" ); |
| 35 |
|
| 36 |
echo '{ "table": "' . esc_attr( $this->table_names ) . '"'; |
| 37 |
echo ', "info": "Generated by WP Data Access"'; |
| 38 |
echo ', "time": "' . esc_attr( gmdate( 'Y-m-d\TH:i:s\Z' ) ) . '"'; |
| 39 |
if ( is_array( $this->rows ) && count( $this->rows ) > 0 ) {//phpcs:ignore - 8.1 proof |
| 40 |
echo ', "columns": ['; |
| 41 |
$first_col = true; |
| 42 |
foreach ( $this->rows[0] as $column_name => $column_value ) { |
| 43 |
if ( $first_col ) { |
| 44 |
$first_col = false; |
| 45 |
} else { |
| 46 |
echo ', '; |
| 47 |
} |
| 48 |
echo '"' . esc_attr( $column_name) . '"'; |
| 49 |
} |
| 50 |
echo ']'; |
| 51 |
} |
| 52 |
echo ', "row_count": ' . esc_attr( $this->row_count ); |
| 53 |
echo ', "rows": ['; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Process one row to be export in JSON format |
| 58 |
* |
| 59 |
* @param array $row |
| 60 |
* |
| 61 |
* @since 2.0.13 |
| 62 |
*/ |
| 63 |
protected function row( $row ) { |
| 64 |
if ( $this->first_row ) { |
| 65 |
$this->first_row = false; |
| 66 |
} else { |
| 67 |
echo ', '; |
| 68 |
} |
| 69 |
echo '['; |
| 70 |
$first_col = true; |
| 71 |
foreach ( $row as $column_name => $column_value ) { |
| 72 |
if ( $first_col ) { |
| 73 |
$first_col = false; |
| 74 |
} else { |
| 75 |
echo ', '; |
| 76 |
} |
| 77 |
|
| 78 |
if ( null === $column_value ) { |
| 79 |
echo 'null'; |
| 80 |
} else { |
| 81 |
$is_string = 'number' === WPDA::get_type( $this->data_types[ $column_name ] ) ? '' : '"'; |
| 82 |
echo $is_string . $column_value . $is_string; // phpcs:ignore WordPress.Security.EscapeOutput |
| 83 |
} |
| 84 |
} |
| 85 |
echo ']'; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* File footer for JSON export |
| 90 |
* |
| 91 |
* @since 2.0.13 |
| 92 |
*/ |
| 93 |
protected function footer() { |
| 94 |
echo ']}'; |
| 95 |
} |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
} |
| 100 |
|