| @@ -280,9 +280,13 @@ | ||
| 280 | 280 | 'map' => array( |
| 281 | 281 | 'required' => false, |
| 282 | 282 | 'type' => 'string', |
| 283 | 283 | 'description' => __( 'Map settings - JSON string', 'wp-data-access' ), |
| 284 | - 'sanitize_callback' => 'wp_kses_post', | |
| 284 | + 'sanitize_callback' => function ( $param ) { | |
| 285 | + $sanitized_settings = $this->sanitize_settings( json_decode( (string) $param, true ) ); | |
| 286 | + // Save sanitized JSON as string | |
| 287 | + return json_encode( $sanitized_settings ); | |
| 288 | + }, | |
| 285 | 289 | 'validate_callback' => 'rest_validate_request_arg', |
| 286 | 290 | ), |
| 287 | 291 | 'chart' => array( |
| 288 | 292 | 'required' => false, |
| @@ -514,8 +518,15 @@ | ||
| 514 | 518 | 'permission_callback' => '__return_true', |
| 515 | 519 | 'args' => array( |
| 516 | 520 | 'dbs_source' => $this->get_param( 'dbs' ), |
| 517 | 521 | 'dbs_destination' => $this->get_param( 'dbs' ), |
| 522 | + 'appDbId' => array( | |
| 523 | + 'required' => false, | |
| 524 | + 'type' => 'integer', | |
| 525 | + 'description' => __( 'App Id', 'wp-data-access' ), | |
| 526 | + 'sanitize_callback' => 'absint', | |
| 527 | + 'validate_callback' => 'rest_validate_request_arg', | |
| 528 | + ), | |
| 518 | 529 | ), |
| 519 | 530 | ) ); |
| 520 | 531 | register_rest_route( WPDA_API::WPDA_NAMESPACE, 'app/lang/get', array( |
| 521 | 532 | 'methods' => array('POST'), |
| @@ -1472,8 +1483,68 @@ | ||
| 1472 | 1483 | $renamed = 0; |
| 1473 | 1484 | $debug_mode = 'on' === WPDA::get_option( WPDA::OPTION_PLUGIN_DEBUG ); |
| 1474 | 1485 | $debug = array(); |
| 1475 | 1486 | $errors = array(); |
| 1487 | + if ( $request->get_param( 'appDbId' ) ) { | |
| 1488 | + // Update app only | |
| 1489 | + $appDbId = $request->get_param( 'appDbId' ); | |
| 1490 | + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- plugin table | |
| 1491 | + $sql = $wpdb->prepare( "update `{$wpdb->prefix}wpda_app_container` set `cnt_dbs` = %s where `cnt_dbs` = %s and app_id = %d", array($dbs_destination, $dbs_source, $appDbId) ); | |
| 1492 | + $result = $wpdb->query( $sql ); | |
| 1493 | + // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared | |
| 1494 | + $renamed += $result; | |
| 1495 | + if ( $debug_mode ) { | |
| 1496 | + $debug[] = array( | |
| 1497 | + 'sql' => $sql, | |
| 1498 | + 'result' => $result, | |
| 1499 | + ); | |
| 1500 | + } | |
| 1501 | + if ( '' !== $wpdb->last_error ) { | |
| 1502 | + $errors[] = array( | |
| 1503 | + 'sql' => $sql, | |
| 1504 | + 'error' => $wpdb->last_error, | |
| 1505 | + ); | |
| 1506 | + } | |
| 1507 | + $sql_content = array("update `{$wpdb->prefix}wpda_app_container` set `cnt_table` = replace(`cnt_table`, '\"dbs\":\"%1s\"', '\"dbs\":\"%1s\"') where `cnt_table` like '%\"dbs\":\"%1s\"%' and app_id = %d", "update `{$wpdb->prefix}wpda_app_container` set `cnt_form` = replace(`cnt_form`, '\"dbs\":\"%1s\"', '\"dbs\":\"%1s\"') where `cnt_form` like '%\"dbs\":\"%1s\"%' and app_id = %d"); | |
| 1508 | + foreach ( $sql_content as $sql ) { | |
| 1509 | + // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- plugin table | |
| 1510 | + $result = $wpdb->query( $wpdb->prepare( $sql, array( | |
| 1511 | + $dbs_source, | |
| 1512 | + $dbs_destination, | |
| 1513 | + $dbs_source, | |
| 1514 | + $appDbId | |
| 1515 | + ) ) ); | |
| 1516 | + $renamed += $result; | |
| 1517 | + if ( $debug_mode ) { | |
| 1518 | + $debug[] = array( | |
| 1519 | + 'sql' => $sql, | |
| 1520 | + 'result' => $result, | |
| 1521 | + ); | |
| 1522 | + } | |
| 1523 | + if ( '' !== $wpdb->last_error ) { | |
| 1524 | + $errors[] = array( | |
| 1525 | + 'sql' => $sql, | |
| 1526 | + 'error' => $wpdb->last_error, | |
| 1527 | + ); | |
| 1528 | + } | |
| 1529 | + } | |
| 1530 | + $context = array(); | |
| 1531 | + if ( $debug_mode ) { | |
| 1532 | + $context['debug'] = $debug; | |
| 1533 | + } | |
| 1534 | + if ( 0 < count( $errors ) ) { | |
| 1535 | + $context['errors'] = $errors; | |
| 1536 | + return new \WP_Error('error', 'Failed renaming database', array( | |
| 1537 | + 'status' => 401, | |
| 1538 | + 'context' => $context, | |
| 1539 | + )); | |
| 1540 | + } | |
| 1541 | + return $this->WPDA_Rest_Response( sprintf( | |
| 1542 | + /* translators: %s = number of database substitutions */ | |
| 1543 | + __( 'Successfully renamed %s database occurrences', 'wp-data-access' ), | |
| 1544 | + $renamed | |
| 1545 | + ), null, $context ); | |
| 1546 | + } | |
| 1476 | 1547 | // Rename all occurrences in repository tables and apps |
| 1477 | 1548 | $sqls = array( |
| 1478 | 1549 | "update `{$wpdb->prefix}wpda_publisher` set `pub_schema_name` = %s where `pub_schema_name` = %s", |
| 1479 | 1550 | "update `{$wpdb->prefix}wpda_project_page` set `page_schema_name` = %s where `page_schema_name` = %s", |
| @@ -2220,8 +2291,17 @@ | ||
| 2220 | 2291 | return $this->get_app_container_meta( $app_id, $container ); |
| 2221 | 2292 | } |
| 2222 | 2293 | } |
| 2223 | 2294 | |
| 2295 | + private function escapeUnicodeForExport( $str ) { | |
| 2296 | + if ( empty( $str ) ) { | |
| 2297 | + return $str; | |
| 2298 | + } | |
| 2299 | + return preg_replace_callback( '/\\\\(u[0-9a-fA-F]{4})/', function ( $matches ) { | |
| 2300 | + return '\\\\' . $matches[1]; | |
| 2301 | + }, $str ); | |
| 2302 | + } | |
| 2303 | + | |
| 2224 | 2304 | private function do_app_export_app( $app_id, $main_app_id ) { |
| 2225 | 2305 | global $wpdb; |
| 2226 | 2306 | $quotes = function ( $value ) { |
| 2227 | 2307 | return str_replace( array( |
| @@ -2231,9 +2311,10 @@ | ||
| 2231 | 2311 | "\\t", |
| 2232 | 2312 | "\\\\n", |
| 2233 | 2313 | "\\n", |
| 2234 | 2314 | "\\r\\n", |
| 2235 | - "\\r" | |
| 2315 | + "\\r", | |
| 2316 | + "\\d" | |
| 2236 | 2317 | ), array( |
| 2237 | 2318 | "''", |
| 2238 | 2319 | '\\\\"', |
| 2239 | 2320 | "\\\\\\t", |
| @@ -2240,9 +2321,10 @@ | ||
| 2240 | 2321 | "\\\\t", |
| 2241 | 2322 | "\\\\\\n", |
| 2242 | 2323 | "\\\\n", |
| 2243 | 2324 | "\\\\r\\\\n", |
| 2244 | - "\\\\r" | |
| 2325 | + "\\\\r", | |
| 2326 | + "\\\\\\\\\\d" | |
| 2245 | 2327 | ), $value ); |
| 2246 | 2328 | }; |
| 2247 | 2329 | $app = WPDA_App_Model::get_by_id( $app_id ); |
| 2248 | 2330 | $app_settings = ( null === $app[0]['app_settings'] ? 'null' : "{$quotes( $app[0]['app_settings'] )}" ); |
| @@ -2289,8 +2371,11 @@ | ||
| 2289 | 2371 | // Replace default WordPress database with conversion string |
| 2290 | 2372 | $cnt_dbs = ( $wpdb->dbname === $container['cnt_dbs'] ? '{wp_schema}' : "{$quotes( $container['cnt_dbs'] )}" ); |
| 2291 | 2373 | $cnt_table = str_replace( "\"dbs\":\"{$wpdb->dbname}\"", "\"dbs\":\"{wp_schema}\"", $cnt_table ); |
| 2292 | 2374 | $cnt_form = str_replace( "\"dbs\":\"{$wpdb->dbname}\"", "\"dbs\":\"{wp_schema}\"", $cnt_form ); |
| 2375 | + // Replace Unicode characters | |
| 2376 | + $cnt_table = $this->escapeUnicodeForExport( $cnt_table ); | |
| 2377 | + $cnt_form = $this->escapeUnicodeForExport( $cnt_form ); | |
| 2293 | 2378 | // phpcs:ignore PluginCheck.CodeAnalysis.Heredoc.NotAllowed |
| 2294 | 2379 | $containers_sql .= <<<SQL |
| 2295 | 2380 | # Import app container |
| 2296 | 2381 | insert into `{wp_prefix}wpda_app_container` |