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