Created
May 8, 2026 11:39
-
-
Save annuman97/8da75b091d7a7f6c23b0139135af78a2 to your computer and use it in GitHub Desktop.
**Gist Name:** `ninja-tables-custom-sql-ajax-chunk-fix.php` **Description:** Fixes Ninja Tables Pro Custom SQL tables loading only the first Ajax chunk by calculating the real SQL row count and setting the missing chunk metadata, scoped only to `raw_sql` Ajax tables.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_filter('ninja_table_rendering_table_vars', function ($tableVars, $tableId) { | |
| if (empty($tableVars['provider']) || $tableVars['provider'] !== 'raw_sql') { | |
| return $tableVars; | |
| } | |
| if (empty($tableVars['render_type']) || $tableVars['render_type'] !== 'ajax_table') { | |
| return $tableVars; | |
| } | |
| $perChunk = function_exists('ninjaTablePerChunk') ? (int) ninjaTablePerChunk($tableId) : 3000; | |
| if ($perChunk < 1) { | |
| return $tableVars; | |
| } | |
| $totalRows = wpmn_ninja_tables_raw_sql_total_rows((int) $tableId); | |
| if ($totalRows > $perChunk) { | |
| $tableVars['chunks'] = (int) ceil($totalRows / $perChunk) - 1; | |
| } | |
| return $tableVars; | |
| }, 20, 2); | |
| function wpmn_ninja_tables_raw_sql_total_rows($tableId) { | |
| if (!$tableId) { | |
| return 0; | |
| } | |
| $sql = get_post_meta($tableId, '_ninja_table_raw_sql_query', true); | |
| if (!is_string($sql) || trim($sql) === '') { | |
| return 0; | |
| } | |
| global $wpdb; | |
| $db = $wpdb; | |
| if (class_exists('\NinjaTablesPro\App\Modules\DataProviders\RawSqlProvider')) { | |
| try { | |
| $provider = new \NinjaTablesPro\App\Modules\DataProviders\RawSqlProvider(); | |
| $db = $provider->getDb($tableId); | |
| } catch (\Throwable $e) { | |
| $db = $wpdb; | |
| } | |
| } | |
| if (!is_object($db) || !method_exists($db, 'get_var')) { | |
| $db = $wpdb; | |
| } | |
| $parsedSql = wpmn_ninja_tables_parse_raw_sql($sql, $tableId, $db); | |
| $parsedSql = trim(rtrim($parsedSql, ";\t\n\r\0\x0B ")); | |
| if ($parsedSql === '') { | |
| return 0; | |
| } | |
| $countSql = 'SELECT COUNT(*) FROM (' . $parsedSql . ') AS ninja_table_raw_sql_count'; | |
| $previousSuppress = null; | |
| if (is_object($db) && method_exists($db, 'suppress_errors')) { | |
| $previousSuppress = $db->suppress_errors(true); | |
| } | |
| $totalRows = $db->get_var($countSql); | |
| if ($previousSuppress !== null && is_object($db) && method_exists($db, 'suppress_errors')) { | |
| $db->suppress_errors($previousSuppress); | |
| } | |
| return max(0, (int) $totalRows); | |
| } | |
| function wpmn_ninja_tables_parse_raw_sql($sql, $tableId, $db) { | |
| $prefix = (is_object($db) && property_exists($db, 'prefix')) ? $db->prefix : ''; | |
| $placeholders = apply_filters('ninja_table_raw_sql_placeholders', [ | |
| '{current_user_id}' => get_current_user_id(), | |
| '{current_date}' => date('Y-m-d'), | |
| '{prefix}' => $prefix, | |
| '{current_date_time}' => date('Y-m-d H:i:s'), | |
| ], $tableId); | |
| $parsedSql = str_replace(array_keys($placeholders), array_values($placeholders), $sql); | |
| $parsedSql = apply_filters('ninja_parse_placeholder', $parsedSql); | |
| return apply_filters('ninja_table_raw_sql_parsed', $parsedSql, $sql, $tableId); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment