' . esc_html__( 'Upgrade to Pro', 'tablewright' ) . '', 'edit_posts', 'tablewright-pro', array( $this, 'page_pro' ) ); } add_submenu_page( $parent, __( 'More from us', 'tablewright' ), __( 'More tools', 'tablewright' ), 'manage_options', 'tablewright-more', array( $this, 'page_more' ) ); } /** Where "Get Pro" buttons point (TABLEWRIGHT_PRO_URL, a Studio redirect; filter tablewright_pro_url to change it). */ public static function pro_url() { $url = defined( 'TABLEWRIGHT_PRO_URL' ) ? TABLEWRIGHT_PRO_URL : ''; return (string) apply_filters( 'tablewright_pro_url', $url ); } public static function pro_features() { return array( __( 'Accordion, compare-columns, hide-columns and pinned-column layouts for phones', 'tablewright' ), __( 'Separate layouts for tablets and phones', 'tablewright' ), __( 'Button and image cells', 'tablewright' ), __( '2,800+ Font Awesome icons, including brand logos', 'tablewright' ), __( 'Pagination and a header that stays visible while scrolling', 'tablewright' ), __( 'Save your own styles and templates, plus 4 extra styles and 2 extra templates', 'tablewright' ), ); } /** Our other products, services and newsletter (links only; see Tablewright_More_Tools). */ public function page_more() { ?>

' . esc_html__( 'Settings', 'tablewright' ) . '' ); if ( ! Tablewright_Data::is_pro() && self::pro_url() ) { $links[] = '' . esc_html__( 'Get Pro', 'tablewright' ) . ''; } return $links; } public function redirect_core_screens() { global $typenow; // phpcs:disable WordPress.Security.NonceVerification $post_id = isset( $_GET['post'] ) ? absint( wp_unslash( $_GET['post'] ) ) : 0; // Only decides where to redirect; the editor checks access. $type = $post_id ? get_post_type( $post_id ) : $typenow; // phpcs:enable if ( Tablewright_Plugin::CPT !== $type ) { return; } wp_safe_redirect( $post_id ? admin_url( 'admin.php?page=tablewright-editor&table=' . $post_id ) : admin_url( 'admin.php?page=tablewright-new' ) ); exit; } public function edit_link( $link, $post_id ) { if ( Tablewright_Plugin::CPT === get_post_type( $post_id ) ) { return admin_url( 'admin.php?page=tablewright-editor&table=' . (int) $post_id ); } return $link; } public function columns( $cols ) { $new = array(); foreach ( $cols as $k => $v ) { $new[ $k ] = $v; if ( 'title' === $k ) { $new['tablewright_shortcode'] = __( 'Shortcode', 'tablewright' ); $new['tablewright_size'] = __( 'Size', 'tablewright' ); } } return $new; } public function column_content( $col, $post_id ) { if ( 'tablewright_shortcode' === $col ) { echo '[tablewright id="' . (int) $post_id . '"]'; } elseif ( 'tablewright_size' === $col ) { $d = Tablewright_Data::get( $post_id ); if ( $d ) { /* translators: 1: rows, 2: columns */ printf( esc_html__( '%1$d rows × %2$d columns', 'tablewright' ), count( $d['cells'] ), count( $d['cells'][0] ) ); } } } public function row_actions( $actions, $post ) { if ( Tablewright_Plugin::CPT !== $post->post_type ) { return $actions; } unset( $actions['inline hide-if-no-js'] ); $url = wp_nonce_url( admin_url( 'admin.php?action=tablewright_duplicate&post=' . $post->ID ), 'tablewright_dup_' . $post->ID ); $actions['tablewright_dup'] = '' . esc_html__( 'Duplicate', 'tablewright' ) . ''; return $actions; } public function duplicate() { $id = isset( $_GET['post'] ) ? absint( wp_unslash( $_GET['post'] ) ) : 0; check_admin_referer( 'tablewright_dup_' . $id ); if ( ! $id || Tablewright_Plugin::CPT !== get_post_type( $id ) || ! current_user_can( 'edit_post', $id ) || ! current_user_can( 'edit_posts' ) ) { wp_die( esc_html__( 'You are not allowed to duplicate this table.', 'tablewright' ) ); } $src = get_post( $id ); $new = wp_insert_post( array( 'post_type' => Tablewright_Plugin::CPT, 'post_status' => 'publish', /* translators: %s: table title */ 'post_title' => sprintf( __( '%s (copy)', 'tablewright' ), $src->post_title ), ) ); if ( $new && ! is_wp_error( $new ) ) { Tablewright_Data::save( $new, Tablewright_Data::get( $id ) ); } wp_safe_redirect( admin_url( 'edit.php?post_type=' . Tablewright_Plugin::CPT ) ); exit; } /* ------------------------------------------------------------------ */ /* Assets */ /* ------------------------------------------------------------------ */ public function assets() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only picks which admin page's scripts to load; nothing is changed. $page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : ''; if ( 'tablewright-more' === $page ) { Tablewright_More_Tools::enqueue(); return; } if ( ! in_array( $page, array( 'tablewright-editor', 'tablewright-new', 'tablewright-pro' ), true ) ) { return; } wp_enqueue_style( 'tablewright-admin', TABLEWRIGHT_URL . 'assets/admin/editor.css', array(), TABLEWRIGHT_VERSION ); wp_enqueue_style( 'tablewright-frontend' ); if ( 'tablewright-new' === $page ) { // Template previews: their CSS goes with the admin stylesheet (the cards are printed by page_new()). foreach ( Tablewright_Data::builtin_templates() as $tpl ) { $tuid = 'twr-tpl-' . sanitize_html_class( $tpl['id'] ); wp_add_inline_style( 'tablewright-admin', Tablewright_Render::esc_css( Tablewright_Render::table_css( $tuid, Tablewright_Data::sanitize_table( $tpl['data'] ) ) ) ); } wp_enqueue_script( 'tablewright-new', TABLEWRIGHT_URL . 'assets/admin/new-table.js', array(), TABLEWRIGHT_VERSION, true ); wp_localize_script( 'tablewright-new', 'TABLEWRIGHT_NEW', array( 'ajax' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'tablewright_editor' ), ) ); } if ( 'tablewright-editor' !== $page ) { return; } wp_enqueue_media(); wp_enqueue_style( 'tablewright-pickr', TABLEWRIGHT_URL . 'assets/vendor/pickr/nano.min.css', array(), '1.10.2' ); wp_enqueue_script( 'tablewright-pickr', TABLEWRIGHT_URL . 'assets/vendor/pickr/pickr.min.js', array(), '1.10.2', true ); wp_enqueue_script( 'tablewright-editor', TABLEWRIGHT_URL . 'assets/admin/editor.js', array( 'tablewright-pickr' ), TABLEWRIGHT_VERSION, true ); $id = self::requested_table(); $data = $id ? Tablewright_Data::get( $id ) : null; $post = $data ? get_post( $id ) : null; if ( ! $data ) { $id = 0; } wp_localize_script( 'tablewright-editor', 'TABLEWRIGHT_EDITOR', array( 'ajax' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'tablewright_editor' ), 'id' => $id, 'title' => $post ? $post->post_title : '', 'data' => $data, 'defaults' => Tablewright_Data::default_settings(), 'presets' => Tablewright_Data::builtin_presets(), 'fontsUrl' => TABLEWRIGHT_URL . 'assets/data/google-fonts.json?v=' . TABLEWRIGHT_VERSION, 'iconsUrl' => TABLEWRIGHT_URL . 'assets/data/icons-lucide.json?v=' . TABLEWRIGHT_VERSION, 'pro' => Tablewright_Data::is_pro(), 'proUrl' => self::pro_url() ? self::pro_url() : admin_url( 'admin.php?page=tablewright-pro' ), 'layouts' => Tablewright_Data::layouts(), 'systemFonts' => Tablewright_Fonts::system_fonts(), 'listUrl' => admin_url( 'edit.php?post_type=' . Tablewright_Plugin::CPT ), 'breakpoint' => (int) Tablewright_Plugin::get_settings()['breakpoint'], 'breakpointT' => (int) Tablewright_Plugin::get_settings()['breakpoint_t'], 'fontMode' => Tablewright_Plugin::get_settings()['font_mode'], ) ); } /* ------------------------------------------------------------------ */ /* Pages */ /* ------------------------------------------------------------------ */ public function page_new() { $templates = Tablewright_Data::builtin_templates(); ?>

Pro

' . esc_html__( 'Table not found', 'tablewright' ) . '

' . esc_html__( 'Create a new table', 'tablewright' ) . '

'; return; } echo '

' . esc_html__( 'Loading editor…', 'tablewright' ) . '

'; } /* ------------------------------------------------------------------ */ /* Settings */ /* ------------------------------------------------------------------ */ public function register_settings() { register_setting( 'tablewright_settings', 'tablewright_settings', array( 'sanitize_callback' => function ( $in ) { $d = Tablewright_Plugin::default_global_settings(); return array( 'font_mode' => isset( $in['font_mode'] ) && in_array( $in['font_mode'], array( 'google', 'local', 'off' ), true ) ? $in['font_mode'] : $d['font_mode'], 'default_preset' => isset( $in['default_preset'] ) ? sanitize_key( $in['default_preset'] ) : $d['default_preset'], 'breakpoint' => isset( $in['breakpoint'] ) ? max( 240, min( 2000, (int) $in['breakpoint'] ) ) : $d['breakpoint'], 'breakpoint_t' => isset( $in['breakpoint_t'] ) ? max( 320, min( 3000, (int) $in['breakpoint_t'] ) ) : $d['breakpoint_t'], 'share_stats' => empty( $in['share_stats'] ) ? 0 : 1, ); }, ) ); } public function page_settings() { $s = Tablewright_Plugin::get_settings(); $presets = Tablewright_Data::builtin_presets(); ?>


guard(); $title = $this->text_param( 'title' ); $tpl = sanitize_key( $this->text_param( 'tpl' ) ); $data = null; $name = ''; if ( $tpl ) { foreach ( Tablewright_Data::builtin_templates() as $t ) { if ( $t['id'] === $tpl ) { $data = $t['data']; $name = $t['name']; } } } $paste = $this->json_param( 'paste' ); if ( ! $data && $paste ) { $data = $this->table_from_rows( $paste, '1' === $this->text_param( 'paste_head' ) ); if ( ! $data ) { wp_send_json_error( __( 'No rows were found in the pasted data.', 'tablewright' ) ); } $name = __( 'Pasted table', 'tablewright' ); } if ( ! $data ) { $data = Tablewright_Data::blank( (int) $this->text_param( 'rows' ), (int) $this->text_param( 'cols' ) ); } $id = wp_insert_post( array( 'post_type' => Tablewright_Plugin::CPT, 'post_status' => 'publish', 'post_title' => $title ? $title : ( $name ? $name : __( 'Untitled table', 'tablewright' ) ), ), true ); if ( is_wp_error( $id ) ) { wp_send_json_error( $id->get_error_message() ); } Tablewright_Data::save( $id, $data ); wp_send_json_success( array( 'id' => $id, 'url' => admin_url( 'admin.php?page=tablewright-editor&table=' . $id ) ) ); } /** Table data from pasted rows (plain text cells), styled like a new blank table. */ private function table_from_rows( $rows, $head ) { $rows = array_slice( array_values( array_filter( (array) $rows, 'is_array' ) ), 0, Tablewright_Data::MAX_ROWS ); if ( ! $rows ) { return null; } $ncols = 1; foreach ( $rows as $r ) { $ncols = max( $ncols, count( $r ) ); } $ncols = min( Tablewright_Data::MAX_COLS, $ncols ); $cells = array(); foreach ( $rows as $r ) { $r = array_values( $r ); $row = array(); for ( $c = 0; $c < $ncols; $c++ ) { $v = isset( $r[ $c ] ) && is_scalar( $r[ $c ] ) ? (string) $r[ $c ] : ''; $row[] = Tablewright_Data::cell( esc_html( trim( substr( $v, 0, 5000 ) ) ) ); } $cells[] = $row; } $base = Tablewright_Data::blank( 1, 1 ); $base['settings']['header_rows'] = $head ? 1 : 0; return array( 'settings' => $base['settings'], 'cells' => $cells, ); } public function ajax_save() { $this->guard(); $id = (int) $this->text_param( 'id' ); if ( ! $id || Tablewright_Plugin::CPT !== get_post_type( $id ) || ! current_user_can( 'edit_post', $id ) ) { wp_send_json_error( __( 'Table not found.', 'tablewright' ) ); } $data = $this->json_param( 'data' ); if ( ! $data || empty( $data['cells'] ) ) { wp_send_json_error( __( 'The table data was empty or corrupted, so nothing was saved.', 'tablewright' ) ); } $title = $this->text_param( 'title' ); if ( $title ) { wp_update_post( array( 'ID' => $id, 'post_title' => $title ) ); } Tablewright_Data::save( $id, $data ); wp_send_json_success(); } /** * Server-rendered preview of unsaved table data, for the editor's tablet and phone views. * Returns a complete HTML document for an iframe. */ public function ajax_preview() { $this->guard(); $data = $this->json_param( 'data' ); if ( ! $data || empty( $data['cells'] ) ) { wp_die( esc_html__( 'Nothing to preview.', 'tablewright' ) ); } $data = Tablewright_Data::sanitize_table( $data ); $html = Tablewright_Render::render_data( $data, 'twr-preview' ); header( 'Content-Type: text/html; charset=utf-8' ); echo ''; wp_add_inline_style( 'tablewright-frontend', 'html{background:#fff}body{margin:0;padding:16px;font:16px/1.5 system-ui,-apple-system,"Segoe UI",Roboto,sans-serif;color:#1f2933}' ); wp_print_styles(); echo '' . $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Built by Tablewright_Render from sanitized table data. wp_print_scripts(); // Links in the preview must not navigate the preview frame away. wp_print_inline_script_tag( 'document.addEventListener("click",function(e){var a=e.target.closest("a");if(a)e.preventDefault();});' ); echo ''; exit; } }