diff --git a/inc/duplication/data.php b/inc/duplication/data.php index 00439ac5..ccd32308 100644 --- a/inc/duplication/data.php +++ b/inc/duplication/data.php @@ -192,7 +192,7 @@ public static function should_copy_table($table, $table_base_name, $from_site_id * @param int $from_site_id Source site ID. * @param int $to_site_id Target site ID. */ - return (bool) apply_filters('mucd_should_copy_table', $copy, $table, $table_base_name, $from_site_id, $to_site_id); + return (bool) apply_filters('wu_mucd_should_copy_table', $copy, $table, $table_base_name, $from_site_id, $to_site_id); } /** @@ -248,7 +248,7 @@ public static function is_runtime_table($table_base_name) { * * @param array $runtime_tables Runtime-only table base names. */ - $runtime_tables = (array) apply_filters('mucd_runtime_tables_to_ignore', $runtime_tables); + $runtime_tables = (array) apply_filters('wu_mucd_runtime_tables_to_ignore', $runtime_tables); return in_array($table_base_name, $runtime_tables, true); } @@ -274,7 +274,7 @@ private static function should_skip_empty_table($table, $table_base_name, $from_ return false; } - return 0 === self::get_table_row_count($table); + return ! self::table_has_rows($table); } /** @@ -297,7 +297,7 @@ private static function skip_empty_tables($from_site_id, $to_site_id) { * @param int $from_site_id Source site ID. * @param int $to_site_id Target site ID. */ - return (bool) apply_filters('mucd_skip_empty_tables', true, $from_site_id, $to_site_id); + return (bool) apply_filters('wu_mucd_skip_empty_tables', true, $from_site_id, $to_site_id); } /** @@ -330,24 +330,30 @@ private static function is_required_table($table_base_name) { * * @param array $required_tables Required table base names. */ - $required_tables = (array) apply_filters('mucd_required_tables_to_copy', $required_tables); + $required_tables = (array) apply_filters('wu_mucd_required_tables_to_copy', $required_tables); return in_array($table_base_name, $required_tables, true); } /** - * Count rows in a source table. + * Check whether a source table has rows. * * @since 2.5.1 * * @param string $table Full source table name. - * @return int Source table row count. + * @return bool Whether the source table has rows. */ - private static function get_table_row_count($table) { + private static function table_has_rows($table) { + global $wpdb; + + $wpdb->last_error = ''; + $has_rows = self::do_sql_query('SELECT 1 FROM `' . $table . '` LIMIT 1', 'var', false); - $count = self::do_sql_query('SELECT COUNT(*) FROM `' . $table . '`', 'var', false); + if ('' !== $wpdb->last_error) { + return true; + } - return (int) $count; + return null !== $has_rows; } /** diff --git a/tests/WP_Ultimo/Duplication/MUCD_Data_Test.php b/tests/WP_Ultimo/Duplication/MUCD_Data_Test.php index 7b0803ec..25a77305 100644 --- a/tests/WP_Ultimo/Duplication/MUCD_Data_Test.php +++ b/tests/WP_Ultimo/Duplication/MUCD_Data_Test.php @@ -713,7 +713,7 @@ public function test_should_copy_table_filter_can_force_copy() { }; add_filter( - 'mucd_should_copy_table', + 'wu_mucd_should_copy_table', $filter, 10, 3 @@ -724,10 +724,25 @@ public function test_should_copy_table_filter_can_force_copy() { \MUCD_Data::should_copy_table($table, 'actionscheduler_actions', get_current_blog_id(), 123) ); } finally { - remove_filter('mucd_should_copy_table', $filter, 10); + remove_filter('wu_mucd_should_copy_table', $filter, 10); } } + /** + * Test empty-table checks fail open when the row probe errors. + */ + public function test_should_copy_table_keeps_optional_table_when_row_probe_errors() { + global $wpdb; + + $table = $wpdb->get_blog_prefix() . 'wu_missing_optional_fixture'; + + $this->drop_table_selection_fixture($table); + + $this->assertTrue( + \MUCD_Data::should_copy_table($table, 'wu_missing_optional_fixture', get_current_blog_id(), 123) + ); + } + /** * Create a temporary table used by table-selection tests. *