', Kses::allowed() );
}
private static function notices() {
$key = 'nitida_tables_flash_' . get_current_user_id();
$f = get_transient( $key );
if ( is_array( $f ) && ! empty( $f['message'] ) ) {
delete_transient( $key );
self::notice( 'err' === $f['type'] ? 'err' : 'ok', (string) $f['message'] );
}
}
private static function can_edit_check() {
if ( ! current_user_can( Settings::capability() ) ) {
wp_die( esc_html__( 'You do not have permission to access this page.', 'nitida-tables' ), '', array( 'response' => 403 ) );
}
}
/* ---------------------------------------------------------------------
* Tables page: list and editor
* ------------------------------------------------------------------ */
public static function render_tables_page() {
self::can_edit_check();
$action = isset( $_GET['action'] ) ? sanitize_key( wp_unslash( $_GET['action'] ) ) : 'list'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
self::open( 'edit' === $action ? 'tt-wrap-edit' : '' );
if ( 'edit' === $action ) {
self::screen_edit( isset( $_GET['id'] ) ? absint( $_GET['id'] ) : 0 ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
} else {
self::screen_list();
}
self::close();
}
private static function screen_list() {
ListScreen::render( \Closure::fromCallable( array( __CLASS__, 'head' ) ) );
}
/* ---------------------------------------------------------------------
* Add new
* ------------------------------------------------------------------ */
public static function render_new_page() {
self::can_edit_check();
self::open( 'tt-new' );
self::head( __( 'Add new table', 'nitida-tables' ), self::button( __( 'All tables', 'nitida-tables' ), self::url() ) );
NewScreen::render();
self::close();
}
public static function handle_create() {
self::guard( 'nitida_tables_create' );
$name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified in self::guard().
$rows = isset( $_POST['rows'] ) ? max( 1, min( 500, absint( $_POST['rows'] ) ) ) : 5; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified in self::guard().
$cols = isset( $_POST['cols'] ) ? max( 1, min( 50, absint( $_POST['cols'] ) ) ) : 4; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified in self::guard().
$data = self::read_html( 'tt_html' );
if ( null === $data ) { $data = self::read_text( 'tt_text' ); }
if ( is_wp_error( $data ) ) {
self::flash( 'err', $data->get_error_message() );
self::redirect( self::page_url( 'nitida-tables-new' ) );
}
if ( null === $data ) {
$data = array();
for ( $r = 0; $r < $rows; $r++ ) {
$row = array();
for ( $c = 0; $c < $cols; $c++ ) {
/* translators: %d: column number */
$row[] = 0 === $r ? sprintf( __( 'Column %d', 'nitida-tables' ), $c + 1 ) : '';
}
$data[] = $row;
}
}
if ( '' === $name ) { $name = __( 'New table', 'nitida-tables' ); }
$id = Store::create( $name, $data );
if ( ! $id ) {
self::flash( 'err', __( 'The table could not be created.', 'nitida-tables' ) );
self::redirect( self::page_url( 'nitida-tables-new' ) );
}
self::flash( 'ok', __( 'Table created.', 'nitida-tables' ) );
self::redirect( self::url( array( 'action' => 'edit', 'id' => $id ) ) );
}
/* ---------------------------------------------------------------------
* Copy, trash, export, restore revision
* ------------------------------------------------------------------ */
public static function handle_copy() {
$id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Verified in guard().
self::guard( 'nitida_tables_copy_' . $id );
$new = Store::copy( $id );
if ( $new ) {
self::flash( 'ok', __( 'Table duplicated.', 'nitida-tables' ) );
self::redirect( self::url( array( 'action' => 'edit', 'id' => $new ) ) );
}
self::flash( 'err', __( 'The table could not be duplicated.', 'nitida-tables' ) );
self::redirect( self::url() );
}
public static function handle_trash() {
$id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Verified in guard().
self::guard( 'nitida_tables_trash_' . $id );
if ( Store::delete( $id ) ) {
self::flash( 'ok', __( 'Table moved to the trash.', 'nitida-tables' ) );
} else {
self::flash( 'err', __( 'The table could not be moved to the trash.', 'nitida-tables' ) );
}
self::redirect( self::url() );
}
/** Bulk actions from the All tables screen: download a JSON backup of the selection, or move it to the trash. */
public static function handle_bulk() {
self::guard( 'nitida_tables_bulk' );
$ids = isset( $_POST['ids'] ) ? array_values( array_filter( array_map( 'absint', (array) wp_unslash( $_POST['ids'] ) ) ) ) : array(); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Verified in guard(); each value is cast with absint().
$op = isset( $_POST['bulk_op'] ) ? sanitize_key( wp_unslash( $_POST['bulk_op'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified in guard().
if ( ! $ids ) {
self::flash( 'err', __( 'Select at least one table first.', 'nitida-tables' ) );
self::redirect( self::url() );
}
if ( 'download' === $op ) {
ListScreen::bulk_download( $ids );
}
if ( 'trash' === $op ) {
$n = ListScreen::bulk_trash( $ids );
/* translators: %d: number of tables */
self::flash( $n ? 'ok' : 'err', $n ? sprintf( _n( '%d table moved to the trash.', '%d tables moved to the trash.', $n, 'nitida-tables' ), $n ) : __( 'The tables could not be moved to the trash.', 'nitida-tables' ) );
}
self::redirect( self::url() );
}
public static function handle_export() {
$id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Verified in guard().
$format = ( isset( $_GET['format'] ) && 'json' === sanitize_key( wp_unslash( $_GET['format'] ) ) ) ? 'json' : 'csv'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
self::guard( 'nitida_tables_export_' . $id );
$t = Store::get( $id );
if ( ! $t ) { wp_die( esc_html__( 'Table not found.', 'nitida-tables' ), '', array( 'response' => 404 ) ); }
$method = 'json' === $format ? 'json_build' : 'csv_build';
if ( ! self::importer_has( $method ) ) { wp_die( esc_html__( 'The exporter is not available.', 'nitida-tables' ), '', array( 'response' => 500 ) ); }
nocache_headers();
if ( 'json' === $format ) {
header( 'Content-Type: application/json; charset=utf-8' );
header( 'Content-Disposition: attachment; filename="table-' . (int) $id . '.json"' );
echo Importer::json_build( $t['data'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- File download, not HTML.
} else {
header( 'Content-Type: text/csv; charset=utf-8' );
header( 'Content-Disposition: attachment; filename="table-' . (int) $id . '.csv"' );
echo "\xEF\xBB\xBF" . Importer::csv_build( $t['data'], ',' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- File download, not HTML.
}
exit;
}
public static function handle_restore_revision() {
$id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Verified in guard().
$rev = isset( $_GET['rev'] ) ? absint( $_GET['rev'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
self::guard( 'nitida_tables_restore_' . $id . '_' . $rev );
$t = Store::get( $id );
$r = $rev ? get_post( $rev ) : null;
if ( ! $t || ! $r || 'revision' !== $r->post_type || (int) $r->post_parent !== (int) $t['post_id'] ) {
wp_die( esc_html__( 'This revision does not belong to this table.', 'nitida-tables' ), '', array( 'response' => 400 ) );
}
$ok = Store::without_content_filters( function () use ( $rev ) { return wp_restore_post_revision( $rev ); } );
if ( $ok ) {
do_action( 'nitida_tables_saved', $id, Store::get( $id ) );
self::flash( 'ok', __( 'Revision restored. The cell data was rolled back.', 'nitida-tables' ) );
} else {
self::flash( 'err', __( 'The revision could not be restored.', 'nitida-tables' ) );
}
self::redirect( self::url( array( 'action' => 'edit', 'id' => $id ) ) );
}
/* ---------------------------------------------------------------------
* AJAX: save, preview, parse
* ------------------------------------------------------------------ */
public static function ajax_save() {
self::ajax_guard();
$p = self::payload();
$id = isset( $p['id'] ) ? absint( $p['id'] ) : 0;
if ( ! $id || ! isset( $p['data'] ) || ! is_array( $p['data'] ) ) {
wp_send_json_error( array( 'error' => __( 'Invalid request.', 'nitida-tables' ) ), 400 );
}
$patch = array( 'data' => $p['data'] );
foreach ( array( 'name', 'description' ) as $k ) {
if ( isset( $p[ $k ] ) && is_scalar( $p[ $k ] ) ) { $patch[ $k ] = (string) $p[ $k ]; }
}
foreach ( array( 'visibility', 'options' ) as $k ) {
if ( isset( $p[ $k ] ) && is_array( $p[ $k ] ) ) { $patch[ $k ] = $p[ $k ]; }
}
$expected = isset( $p['expected_modified'] ) ? (int) $p['expected_modified'] : null;
/**
* Short-circuits a save from the editor, for example to send the change for review (Nitida Tables Pro).
* Return null to save normally, or an array with 'message' to stop here without changing the table.
*
* @param null|array $pre Result.
* @param int $id Table number.
* @param array $patch Changes (data, visibility, options, name, description).
* @param int|null $expected Modified time the editor started from.
*/
$pre = apply_filters( 'nitida_tables_pre_save', null, $id, $patch, $expected );
if ( is_array( $pre ) ) {
$cur = Store::get( $id );
wp_send_json_success( array( 'modified' => $cur ? (int) $cur['modified'] : 0, 'notice' => isset( $pre['message'] ) ? (string) $pre['message'] : '' ) );
}
$res = Store::save( $id, $patch, $expected );
if ( ! empty( $res['ok'] ) ) {
wp_send_json_success( array( 'modified' => (int) $res['modified'] ) );
}
wp_send_json_error( array(
'error' => (string) $res['error'],
'conflict' => null !== $expected && (int) $res['modified'] && (int) $res['modified'] !== $expected,
'modified' => (int) $res['modified'],
) );
}
/** Lifetime of a stored preview (the frame may reload while the editor is open). */
const PREVIEW_TTL = 600;
/**
* Stores a rendered preview (table markup + extra CSS) under a random token and returns the URL of the preview
* document for the sandboxed iframe. The document itself is printed by handle_preview() with the script and style
* API; nothing is written into the page as a string.
*
* @param string $table_html Table markup from the renderer (already escaped).
* @param string $cap Capability the viewer needs.
* @return string
*/
private static function preview_url( $table_html, $cap ) {
$token = wp_generate_password( 32, false );
// One stored preview per user: a new preview replaces the previous one instead of adding rows.
set_transient( 'nitida_tables_pv_' . get_current_user_id(), array( 'token' => $token, 'html' => $table_html, 'css' => Renderer::settings_css(), 'cap' => $cap ), self::PREVIEW_TTL );
return add_query_arg(
array( 'action' => 'nitida_tables_preview', 'token' => $token, '_wpnonce' => wp_create_nonce( 'nitida_tables_admin' ) ),
admin_url( 'admin-post.php' )
);
}
/**
* Prints the preview document (admin-post): the front-end CSS and JS plus a small height reporter, registered as
* ordinary assets and printed with wp_print_styles()/wp_print_scripts(). No wp_head(), so no admin bar.
*/
public static function handle_preview() {
$token = isset( $_GET['token'] ) ? preg_replace( '/[^A-Za-z0-9]/', '', wp_unslash( $_GET['token'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Nonce checked below; the token is reduced to alphanumerics.
$pv = get_current_user_id() ? get_transient( 'nitida_tables_pv_' . get_current_user_id() ) : false;
if ( '' === $token || ! is_array( $pv ) || ! isset( $pv['html'], $pv['cap'], $pv['token'] ) || ! hash_equals( (string) $pv['token'], $token ) || ! current_user_can( (string) $pv['cap'] ) ) {
wp_die( esc_html__( 'You do not have permission to do this.', 'nitida-tables' ), '', array( 'response' => 403 ) );
}
check_admin_referer( 'nitida_tables_admin' );
wp_register_style( 'nitida-tables-preview-frame', NITIDA_TABLES_URL . 'assets/admin/preview-frame.css', array(), NITIDA_TABLES_VERSION );
wp_register_style( 'nitida-tables-preview', NITIDA_TABLES_URL . 'assets/css/frontend.css', array( 'nitida-tables-preview-frame' ), NITIDA_TABLES_VERSION );
if ( ! empty( $pv['css'] ) ) { wp_add_inline_style( 'nitida-tables-preview', (string) $pv['css'] ); }
wp_register_script( 'nitida-tables-preview', NITIDA_TABLES_URL . 'assets/js/frontend.js', array(), NITIDA_TABLES_VERSION, array( 'in_footer' => true ) );
wp_register_script( 'nitida-tables-preview-frame', NITIDA_TABLES_URL . 'assets/admin/preview-frame.js', array( 'nitida-tables-preview' ), NITIDA_TABLES_VERSION, array( 'in_footer' => true ) );
nocache_headers();
header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
echo '';
wp_print_styles( 'nitida-tables-preview' );
echo '';
echo $pv['html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Table markup escaped by the renderer.
wp_print_scripts( 'nitida-tables-preview-frame' );
echo '';
exit;
}
public static function ajax_preview() {
self::ajax_guard();
$p = self::payload();
$id = isset( $p['id'] ) ? absint( $p['id'] ) : 0;
$base = $id ? Store::get( $id ) : null;
if ( ! $base || ! isset( $p['data'] ) || ! is_array( $p['data'] ) ) {
wp_send_json_error( array( 'error' => __( 'Invalid request.', 'nitida-tables' ) ), 400 );
}
$data = Store::clean_cells( Store::normalize_data( $p['data'] ) );
$t = $base;
$t['data'] = $data;
$t['visibility'] = Store::normalize_visibility( isset( $p['visibility'] ) ? $p['visibility'] : null, count( $data ), count( $data[0] ) );
$t['options'] = Store::normalize_options( isset( $p['options'] ) && is_array( $p['options'] ) ? array_merge( $base['options'], $p['options'] ) : $base['options'], count( $data[0] ) );
if ( isset( $p['name'] ) && is_scalar( $p['name'] ) ) { $t['name'] = (string) $p['name']; }
wp_send_json_success( array( 'url' => self::preview_url( Renderer::render_table( $t, array( 'context' => 'preview' ) ), Settings::capability() ) ) );
}
/** Parses an uploaded file or pasted text for the editor ("Replace data"). */
public static function ajax_parse() {
self::ajax_guard();
$data = self::read_upload( 'file' );
if ( null === $data ) { $data = self::read_text( 'text' ); }
if ( null === $data ) {
wp_send_json_error( array( 'error' => __( 'The file is empty or could not be read.', 'nitida-tables' ) ), 400 );
}
if ( is_wp_error( $data ) ) {
wp_send_json_error( array( 'error' => $data->get_error_message() ), 400 );
}
wp_send_json_success( array( 'data' => Store::clean_cells( Store::normalize_data( $data ) ) ) );
}
/* ---------------------------------------------------------------------
* Import screen
* ------------------------------------------------------------------ */
public static function render_import_page() {
self::can_edit_check();
self::open( 'tt-import' );
self::head( __( 'Import', 'nitida-tables' ), self::button( __( 'All tables', 'nitida-tables' ), self::url() ) );
$key = 'nitida_tables_import_result_' . get_current_user_id();
if ( isset( $_GET['done'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$res = get_transient( $key );
delete_transient( $key );
if ( is_array( $res ) ) { self::tablepress_result( $res ); }
}
echo '';
$tables = self::importer_has( 'tablepress_tables' ) ? (array) Importer::tablepress_tables() : array();
if ( $tables ) {
echo '
' . esc_html__( 'Import from TablePress', 'nitida-tables' ) . '
';
echo '
' . esc_html__( 'Data is copied and TablePress is not touched. Table numbers stay the same, so existing [table id=N] shortcodes can keep working.', 'nitida-tables' ) . '
';
echo '
' . esc_html__( 'ID', 'nitida-tables' ) . '
' . esc_html__( 'Name', 'nitida-tables' ) . '
' . esc_html__( 'Size', 'nitida-tables' ) . '
' . esc_html__( 'Status', 'nitida-tables' ) . '
';
foreach ( $tables as $tp ) {
/* translators: 1: number of rows, 2: number of columns */
$size = sprintf( __( '%1$d rows, %2$d columns', 'nitida-tables' ), (int) $tp['rows'], (int) $tp['cols'] );
echo '
' . esc_html__( 'Click the cell where the link should go first.', 'nitida-tables' ) . '
';
echo '';
echo '';
echo '';
echo '';
echo '
';
echo '
';
echo '
' . esc_html__( 'Start a cell with = for a formula, such as =SUM(B2:B10). Select cells and choose Merge to join them.', 'nitida-tables' ) . '
';
echo '
';
echo '' . esc_html__( 'Tab: next cell. Enter: cell below. Shift+Enter: new line in a cell. Esc: leave the grid. Ctrl/Cmd+S: save.', 'nitida-tables' ) . '