self::SLUG ), $args ), admin_url( 'admin.php' ) ); } public static function page_url( $slug, array $args = array() ) { return add_query_arg( array_merge( array( 'page' => $slug ), $args ), admin_url( 'admin.php' ) ); } public static function post_url( $action, array $args = array(), $nonce_action = '' ) { $url = add_query_arg( array_merge( array( 'action' => $action ), $args ), admin_url( 'admin-post.php' ) ); return $nonce_action ? wp_nonce_url( $url, $nonce_action ) : $url; } public static function json( $data ) { return wp_json_encode( $data, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ); } public static function shortcode( $id ) { return '[nitida_table id="' . (int) $id . '"]'; } /** Relative time, e.g. "5 minutes ago". */ public static function ago( $ts ) { $ts = (int) $ts; if ( ! $ts ) { return '-'; } if ( time() - $ts < 60 ) { return __( 'Just now', 'nitida-tables' ); } if ( time() - $ts > 30 * DAY_IN_SECONDS ) { return wp_date( get_option( 'date_format' ), $ts ); } // WordPress core's own phrase, so the whole text comes from one locale. /* translators: %s: time span, for example "5 minutes" */ return sprintf( __( '%s ago' ), human_time_diff( $ts, time() ) ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain, WordPress.WP.I18n.TextDomainMismatch -- Core string. } public static function full_date( $ts ) { return wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), (int) $ts ); } private static function importer_has( $method ) { return class_exists( '\NitidaTables\Importer' ) && method_exists( '\NitidaTables\Importer', $method ); } /** Guard for admin-post handlers: capability and nonce. */ private static function guard( $nonce_action, $cap = '' ) { $cap = '' !== $cap ? $cap : Settings::capability(); if ( ! current_user_can( $cap ) ) { wp_die( esc_html__( 'You do not have permission to do this.', 'nitida-tables' ), '', array( 'response' => 403 ) ); } check_admin_referer( $nonce_action ); } private static function ajax_guard() { if ( ! current_user_can( Settings::capability() ) ) { wp_send_json_error( array( 'error' => __( 'You do not have permission to do this.', 'nitida-tables' ) ), 403 ); } if ( ! check_ajax_referer( 'nitida_tables_admin', '_wpnonce', false ) ) { wp_send_json_error( array( 'error' => __( 'Your session has expired. Reload the page and try again.', 'nitida-tables' ) ), 403 ); } } /** Decoded JSON payload of an ajax request. */ private static function payload() { $raw = isset( $_POST['payload'] ) ? wp_unslash( $_POST['payload'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Verified in ajax_guard(); JSON is decoded and each value is sanitized by the Store. $p = json_decode( (string) $raw, true ); return is_array( $p ) ? $p : array(); } /** Stores a one-time message for the next admin page view. */ private static function flash( $type, $message ) { set_transient( 'nitida_tables_flash_' . get_current_user_id(), array( 'type' => $type, 'message' => $message ), 120 ); } private static function redirect( $url ) { wp_safe_redirect( $url ); exit; } /** * Reads an uploaded file into a 2D array through the importer. * * @param string $key $_FILES key. * @return array|\WP_Error|null Null when no file was uploaded. */ private static function read_upload( $key ) { if ( empty( $_FILES[ $key ] ) || ! is_array( $_FILES[ $key ] ) ) { return null; } // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified by the caller. $f = $_FILES[ $key ]; // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized if ( isset( $f['error'] ) && UPLOAD_ERR_NO_FILE === (int) $f['error'] ) { return null; } if ( isset( $f['error'] ) && in_array( (int) $f['error'], array( UPLOAD_ERR_INI_SIZE, UPLOAD_ERR_FORM_SIZE ), true ) ) { /* translators: %s: upload limit of this server, for example "2 MB" */ return new \WP_Error( 'size', sprintf( __( 'The file is larger than the upload limit of this server (%s).', 'nitida-tables' ), size_format( min( Importer::MAX_BYTES, wp_max_upload_size() ) ) ) ); } if ( empty( $f['tmp_name'] ) || ! isset( $f['error'] ) || UPLOAD_ERR_OK !== (int) $f['error'] || ! is_uploaded_file( $f['tmp_name'] ) ) { return new \WP_Error( 'upload', __( 'The file could not be uploaded.', 'nitida-tables' ) ); } if ( ! empty( $f['size'] ) && (int) $f['size'] > Importer::MAX_BYTES ) { return new \WP_Error( 'size', __( 'The file is larger than 5 MB.', 'nitida-tables' ) ); } $name = isset( $f['name'] ) ? sanitize_file_name( wp_unslash( $f['name'] ) ) : ''; $ext = strtolower( pathinfo( $name, PATHINFO_EXTENSION ) ); if ( ! in_array( $ext, array( 'csv', 'tsv', 'txt', 'xlsx', 'html', 'htm', 'json' ), true ) ) { return new \WP_Error( 'type', __( 'Unsupported file type. Use CSV, TSV, XLSX, HTML or JSON.', 'nitida-tables' ) ); } if ( ! self::importer_has( 'parse_upload' ) ) { return new \WP_Error( 'importer', __( 'The importer is not available.', 'nitida-tables' ) ); } return Importer::parse_upload( $f['tmp_name'], $name ); } /** A table pasted with its formatting (clipboard HTML captured by the paste box). Null when empty. */ private static function read_html( $key ) { $html = isset( $_POST[ $key ] ) ? (string) wp_unslash( $_POST[ $key ] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Verified by the caller; parsed by the importer (links, bold, italic, line breaks only) and sanitized by the Store. if ( '' === trim( $html ) || false === stripos( $html, '