Skip to content

Commit 228206f

Browse files
committed
Merge branch 'url-replace'
2 parents d27b6c9 + 1685b13 commit 228206f

File tree

2 files changed

+148
-18
lines changed

2 files changed

+148
-18
lines changed

includes/class-STL_Settings.php

+27-18
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function admin_enqueue( $hook ) {
4242

4343
public function add_plugin_page(){
4444
if (stl_staging_exists()) {
45-
return;
45+
return;
4646
}
4747
add_menu_page(
4848
__( 'Staging2Live', 'staging2live' ),
@@ -54,14 +54,14 @@ public function add_plugin_page(){
5454
90
5555
);
5656

57-
add_submenu_page(
58-
'staging2live',
59-
'Sync',
60-
'Sync',
61-
'manage_options',
62-
'staging2live',
63-
array( $this, 'create_admin_page' )
64-
);
57+
add_submenu_page(
58+
'staging2live',
59+
'Sync',
60+
'Sync',
61+
'manage_options',
62+
'staging2live',
63+
array( $this, 'create_admin_page' )
64+
);
6565

6666
add_action( 'admin_init', array( $this, 'options_init') );
6767

@@ -194,7 +194,7 @@ public function option_create_staging_button_cb( array $args ){
194194
if ( !empty( $description) )
195195
echo '<p class="description">' . $description . '</p>';
196196

197-
echo '<div id="response"></div>';
197+
echo '<div id="response"></div>';
198198

199199
}
200200

@@ -280,6 +280,8 @@ public static function get_staging_domain(): string {
280280
*/
281281
public function ajax_create_staging() {
282282

283+
global $wpdb;
284+
283285
// check if nonce is valid
284286
if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'stl_nonce' ) ) {
285287
wp_send_json_error( array( 'message' => esc_html__( 'Invalid nonce.', 'staging2live' ) ) );
@@ -295,7 +297,9 @@ public function ajax_create_staging() {
295297
wp_send_json_error( array( 'message' => esc_html__( 'Class is missing. Please contact plugin author.', 'staging2live' ) ) );
296298
}
297299

298-
$staging_name = empty( $this->options_general[ 'staging_name' ] ) ? STL_STAGING_NAME_DEFAULT : $this->options_general[ 'staging_name' ];
300+
$staging_name = empty( $this->options_general[ 'staging_name' ] ) ? STL_STAGING_NAME_DEFAULT : $this->options_general[ 'staging_name' ];
301+
$live_domain = untrailingslashit( STL_General::get_site_url() );
302+
$staging_domain = untrailingslashit( self::get_staging_domain() );
299303

300304
// Start the staging creation process
301305
$database = new STL_Database( $staging_name );
@@ -306,7 +310,15 @@ public function ajax_create_staging() {
306310
wp_send_json_error( array( 'message' => esc_html__( 'Error while duplicating the database.', 'staging2live' ) ) );
307311
}
308312

309-
// 2. Delete previous staging environment
313+
// 2. Replace URL from live to staging
314+
if( ! class_exists( 'STL_URL_Replacer') ) {
315+
include_once STL_PLUGIN_PATH . 'includes/class-STL_URL_Replacer.php';
316+
}
317+
$staging_prefix = $wpdb->prefix . $staging_name;
318+
$replacer = new STL_URL_Replacer();
319+
$replacer->replace_url_in_database( $live_domain, $staging_domain, $staging_prefix );
320+
321+
// 3. Delete previous staging environment
310322
$file_lister = new STL_File_Handling();
311323
$file_lister->delete_staging_files();
312324

@@ -318,14 +330,11 @@ public function ajax_create_staging() {
318330
// Insert file data into the database
319331
$file_lister->insert_files_into_database();
320332
}
321-
322-
// 3. Copy files to the staging environment
333+
// 5. Copy files to the staging environment
323334
$file_lister->copy_files_to_staging();
324-
// 4. Copy files to the staging environment
325335

326-
// 5. Finish and generate URL
327-
$staging_domain = trailingslashit( STL_General::get_site_url() ) . trailingslashit( $staging_name );
328-
wp_send_json_success( array( 'message' => sprintf( esc_html__( 'Staging site successfully created. The URL is %s', 'staging2live' ), '<a href="' . $staging_domain . '" target="_blank">' . $staging_domain . '</a>' ) ) );
336+
// 6. Finish and generate URL
337+
wp_send_json_success( array( 'message' => sprintf( esc_html__( 'Staging site successfully created. The URL is %s', 'staging2live' ), '<a href="' . trailingslashit( $staging_domain ) . '" target="_blank">' . trailingslashit( $staging_domain ) . '</a>' ) ) );
329338
}
330339

331340
}

includes/class-STL_URL_Replacer.php

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
if ( !defined('ABSPATH' ) ) exit;
4+
5+
if ( ! class_exists('STL_URL_Replacer') ) {
6+
class STL_URL_Replacer {
7+
8+
/**
9+
* Replaces URLs in the WordPress database.
10+
*
11+
* @param string $old_url The old URL to be replaced.
12+
* @param string $new_url The new URL to replace with.
13+
*
14+
* @return void
15+
*/
16+
public function replace_url_in_database( string $old_url, string $new_url, string $table_prefix_staging ): void {
17+
18+
global $wpdb;
19+
20+
// Loop through all tables that may contain serialized data
21+
$tables = $wpdb->get_results( "SHOW TABLES LIKE '{$table_prefix_staging}%'", ARRAY_N );
22+
23+
foreach ( $tables as $table ) {
24+
$table_name = $table[0];
25+
26+
// Check if the table has fields that could contain serialized data
27+
$columns = $wpdb->get_results( "DESCRIBE {$table_name}", ARRAY_A );
28+
29+
// Find the filed name of the primary key
30+
foreach ( $columns as $column ) {
31+
if( 'PRI' == $column[ 'Key' ] ) {
32+
$id_field = $column[ 'Field' ];
33+
break;
34+
}
35+
}
36+
37+
if( empty( $id_field ) ) {
38+
continue;
39+
}
40+
41+
foreach ( $columns as $column ) {
42+
$column_name = $column[ 'Field' ];
43+
$column_type = $column[ 'Type' ];
44+
45+
// If it is a TEXT or LONGTEXT field, check its content
46+
if ( strpos( $column_type, 'text' ) !== false ) {
47+
48+
// Fetch the data
49+
$query = $wpdb->prepare( "SELECT `{$column_name}`, `{$id_field}` FROM {$table_name} WHERE `{$column_name}` LIKE %s",
50+
'%' . $wpdb->esc_like( $old_url ) . '%' );
51+
$results = $wpdb->get_results( $query );
52+
53+
foreach ( $results as $row ) {
54+
$original_value = $row->$column_name;
55+
56+
// Check if the value is serialized
57+
if ( is_serialized( $original_value ) ) {
58+
// Unserialize the data
59+
$unserialized_data = unserialize( $original_value );
60+
61+
// Replace old URL in unserialized data
62+
$updated_data = self::replace_url_in_serialized_data( $unserialized_data,
63+
$old_url,
64+
$new_url );
65+
66+
// Re-serialize the updated data
67+
$new_serialized_data = serialize( $updated_data );
68+
69+
// Only update if the data has changed
70+
if ( $new_serialized_data !== $original_value ) {
71+
// Update the serialized data
72+
$wpdb->update(
73+
$table_name,
74+
array( $column_name => $new_serialized_data ),
75+
array( $id_field => $row->$id_field )
76+
);
77+
}
78+
} else {
79+
// If not serialized, perform regular string replacement
80+
$updated_value = str_replace( $old_url, $new_url, $original_value );
81+
82+
// Update if the value has changed
83+
if ( $updated_value !== $original_value ) {
84+
$wpdb->update(
85+
$table_name,
86+
array( $column_name => $updated_value ),
87+
array( $id_field => $row->$id_field )
88+
);
89+
}
90+
}
91+
}
92+
}
93+
}
94+
}
95+
}
96+
97+
/**
98+
* Replaces URLs in serialized data.
99+
*
100+
* @param mixed $data The serialized data.
101+
* @param string $old_url The old URL.
102+
* @param string $new_url The new URL.
103+
*
104+
* @return mixed The data with the replaced URL.
105+
*/
106+
private function replace_url_in_serialized_data( $data, string $old_url, string $new_url ) {
107+
// If the data is an array, loop through and replace URLs
108+
if ( is_array( $data ) ) {
109+
foreach ( $data as $key => $value ) {
110+
$data[ $key ] = self::replace_url_in_serialized_data( $value, $old_url, $new_url );
111+
}
112+
} // If the data is a string, replace the URL
113+
elseif ( is_string( $data ) ) {
114+
$data = str_replace( $old_url, $new_url, $data );
115+
}
116+
117+
return $data;
118+
}
119+
}
120+
121+
}

0 commit comments

Comments
 (0)