Skip to content

Commit 141f2b5

Browse files
committed
chore: cleanup
1 parent 2d444d1 commit 141f2b5

File tree

2 files changed

+123
-110
lines changed

2 files changed

+123
-110
lines changed

inc/media_rename/attachment_db_renamer.php

+14-12
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Optml_Attachment_Db_Renamer {
1717
*
1818
* @var array
1919
*/
20-
private $skip_tables = [];
20+
private $skip_tables = ['users', 'terms', 'term_relationships', 'term_taxonomy'];
2121

2222
/**
2323
* Columns to skip during replacement
@@ -33,18 +33,11 @@ class Optml_Attachment_Db_Renamer {
3333
*/
3434
private $handle_image_sizes = false;
3535

36-
/**
37-
* Skip sizes
38-
*
39-
* @var bool
40-
*/
41-
private $skip_sizes = false;
42-
4336
/**
4437
* Constructor
4538
*/
4639
public function __construct( $skip_sizes = false ) {
47-
$this->skip_sizes = $skip_sizes;
40+
$this->handle_image_sizes = ! $skip_sizes;
4841
}
4942

5043
/**
@@ -60,8 +53,13 @@ public function replace( $old_url, $new_url ) {
6053
return 0;
6154
}
6255

63-
// Always handle both image sizes and scaled variations
64-
$this->handle_image_sizes = true;
56+
if( empty($old_url) || empty($new_url) ) {
57+
return 0;
58+
}
59+
60+
if( ! is_string($old_url) || ! is_string($new_url) ) {
61+
return 0;
62+
}
6563

6664
$tables = $this->get_tables();
6765
$total_replacements = 0;
@@ -394,6 +392,10 @@ private function php_handle_column( $table, $column, $primary_keys, $old_url, $n
394392
* @return string The processed value
395393
*/
396394
private function replace_urls_in_value( $value, $old_url, $new_url ) {
395+
var_dump([
396+
$value,
397+
$old_url,
398+
]);
397399
// Check if the value is serialized
398400
if ( $this->is_serialized( $value ) ) {
399401
$unserialized = @unserialize( $value );
@@ -458,7 +460,7 @@ private function replace_image_urls( $content, $old_url, $new_url ) {
458460
$content = str_replace( $json_old_url, $json_new_url, $content );
459461

460462
// If we have a file with extension, handle variations
461-
if ( ! empty( $old_ext ) && ! $this->skip_sizes ) {
463+
if ( ! empty( $old_ext ) && $this->handle_image_sizes ) {
462464
$old_dir = dirname( $old_path );
463465
$new_dir = dirname( $new_path );
464466

tests/media_rename/test-db-renamer.php

+109-98
Original file line numberDiff line numberDiff line change
@@ -15,133 +15,144 @@ class Test_Attachment_Db_Renamer extends WP_UnitTestCase {
1515
*/
1616
private $attachment_id;
1717

18+
/**
19+
* @var Optml_Attachment_Db_Renamer
20+
*/
21+
private $replacer;
22+
23+
private $replace_method;
24+
1825
/**
1926
* Setup test
2027
*/
2128
public function setUp(): void {
2229
parent::setUp();
23-
$this->attachment_id = $this->create_attachment_get_id(OPTML_PATH . 'tests/assets/sample-test.jpg');
30+
$this->attachment_id = $this->create_attachment_get_id( OPTML_PATH . 'tests/assets/sample-test.jpg' );
31+
$this->replacer = new Optml_Attachment_Db_Renamer();
32+
33+
$this->replace_method = new ReflectionMethod( 'Optml_Attachment_Db_Renamer', 'replace_urls_in_value' );
34+
$this->replace_method->setAccessible( true );
2435
}
2536

2637
/**
2738
* Clean up after each test
2839
*/
2940
public function tearDown(): void {
30-
if ($this->attachment_id) {
31-
$this->delete_attachment($this->attachment_id);
41+
if ( $this->attachment_id ) {
42+
$this->delete_attachment( $this->attachment_id );
3243
}
3344
parent::tearDown();
3445
}
3546

3647
/**
37-
* Test basic URL replacement
48+
* Test replace method with valid URLs
3849
*/
39-
public function test_basic_url_replacement() {
40-
$attachment = new Optml_Attachment_Model($this->attachment_id);
41-
$old_url = $attachment->get_main_url();
42-
$new_url = str_replace('sample-test', 'new-test-image', $old_url);
50+
public function test_replace() {
51+
$attachment = new Optml_Attachment_Model( $this->attachment_id );
52+
$old_url = $attachment->get_main_url();
53+
$new_url = str_replace( 'sample-test', 'new-test-image', $old_url );
4354

44-
$replacer = new Optml_Attachment_Db_Renamer();
45-
$count = $replacer->replace($old_url, $new_url);
46-
47-
$this->assertGreaterThan(0, $count);
55+
$count = $this->replacer->replace( $old_url, $new_url );
56+
$this->assertGreaterThan( 0, $count );
4857
}
4958

5059
/**
51-
* Test replacement with same URLs
60+
* Test replace method with identical URLs
5261
*/
53-
public function test_same_url_replacement() {
54-
$attachment = new Optml_Attachment_Model($this->attachment_id);
55-
$url = $attachment->get_main_url();
56-
57-
$replacer = new Optml_Attachment_Db_Renamer();
58-
$count = $replacer->replace($url, $url);
62+
public function test_replace_with_identical_urls() {
63+
$attachment = new Optml_Attachment_Model( $this->attachment_id );
64+
$url = $attachment->get_main_url();
5965

60-
$this->assertEquals(0, $count);
66+
$count = $this->replacer->replace( $url, $url );
67+
$this->assertEquals( 0, $count );
6168
}
6269

6370
/**
64-
* Test replacement with image sizes
71+
* Test replace method with empty URLs
6572
*/
66-
// public function test_image_sizes_replacement() {
67-
// $attachment = new Optml_Attachment_Model($this->attachment_id);
68-
// $old_url = $attachment->get_guid();
69-
// $new_url = str_replace('sample-test', 'new-test-image', $old_url);
70-
//
71-
// // Create a post with image sizes
72-
// $post_id = $this->factory->post->create();
73-
// $content = sprintf(
74-
// '<img src="%s" class="wp-image-%d" />',
75-
// $old_url,
76-
// $this->attachment_id
77-
// );
78-
// wp_update_post(['ID' => $post_id, 'post_content' => $content]);
79-
//
80-
// $replacer = new Optml_Attachment_Db_Renamer();
81-
// $count = $replacer->replace($old_url, $new_url);
82-
//
83-
// $this->assertGreaterThan(0, $count);
84-
//
85-
// $updated_post = get_post($post_id);
86-
// $this->assertStringContainsString($new_url, $updated_post->post_content);
87-
// }
73+
public function test_replace_with_empty_urls() {
74+
$count = $this->replacer->replace( '', '' );
75+
$this->assertEquals( 0, $count );
8876

89-
/**
90-
* Test replacement with scaled images
91-
*/
92-
// public function test_scaled_image_replacement() {
93-
// $scaled_attachment = $this->create_attachment_get_id(OPTML_PATH . 'tests/assets/3000x3000.jpg');
94-
// $attachment = new Optml_Attachment_Model($scaled_attachment);
95-
// $old_url = $attachment->get_guid();
96-
// $new_url = str_replace('3000x3000', 'new-scaled-image', $old_url);
97-
//
98-
// // Create a post with scaled image
99-
// $post_id = $this->factory->post->create();
100-
// $content = sprintf(
101-
// '<img src="%s" class="wp-image-%d" />',
102-
// $old_url,
103-
// $scaled_attachment
104-
// );
105-
// wp_update_post(['ID' => $post_id, 'post_content' => $content]);
106-
//
107-
// $replacer = new Optml_Attachment_Db_Renamer();
108-
// $count = $replacer->replace($old_url, $new_url);
109-
//
110-
// $this->assertGreaterThan(0, $count);
111-
//
112-
// $updated_post = get_post($post_id);
113-
// $this->assertStringContainsString($new_url, $updated_post->post_content);
114-
//
115-
// $this->delete_attachment($scaled_attachment);
116-
// }
77+
$count = $this->replacer->replace( 'http://example.com', '' );
78+
$this->assertEquals( 0, $count );
79+
80+
$count = $this->replacer->replace( '', 'http://example.com' );
81+
$this->assertEquals( 0, $count );
82+
}
11783

11884
/**
119-
* Test replacement with serialized data
85+
* Test replace method with null URLs
12086
*/
121-
// public function test_serialized_data_replacement() {
122-
// $attachment = new Optml_Attachment_Model($this->attachment_id);
123-
// $old_url = $attachment->get_guid();
124-
// $new_url = str_replace('sample-test', 'new-test-image', $old_url);
125-
//
126-
// // Create a post with serialized data
127-
// $post_id = $this->factory->post->create();
128-
// $meta_data = [
129-
// 'image_url' => $old_url,
130-
// 'sizes' => [
131-
// 'thumbnail' => str_replace('.jpg', '-150x150.jpg', $old_url),
132-
// 'medium' => str_replace('.jpg', '-300x300.jpg', $old_url)
133-
// ]
134-
// ];
135-
// update_post_meta($post_id, 'test_meta', $meta_data);
136-
//
137-
// $replacer = new Optml_Attachment_Db_Renamer();
138-
// $count = $replacer->replace($old_url, $new_url);
139-
//
140-
// $this->assertGreaterThan(0, $count);
141-
//
142-
// $updated_meta = get_post_meta($post_id, 'test_meta', true);
143-
// $this->assertEquals($new_url, $updated_meta['image_url']);
144-
// $this->assertStringContainsString($new_url, $updated_meta['sizes']['thumbnail']);
145-
// $this->assertStringContainsString($new_url, $updated_meta['sizes']['medium']);
146-
// }
87+
public function test_replace_with_null_urls() {
88+
$count = $this->replacer->replace( null, null );
89+
$this->assertEquals( 0, $count );
90+
91+
$count = $this->replacer->replace( 'http://example.com', null );
92+
$this->assertEquals( 0, $count );
93+
94+
$count = $this->replacer->replace( null, 'http://example.com' );
95+
$this->assertEquals( 0, $count );
96+
}
97+
98+
public function test_simple_replacement() {
99+
$value = 'http://example.com';
100+
$old_url = 'http://example.com';
101+
$new_url = 'http://example.org';
102+
103+
$replaced = $this->replace_method->invoke( $this->replacer, $value, $old_url, $new_url );
104+
$this->assertEquals( $new_url, $replaced );
105+
}
106+
107+
public function test_multiple_replacement() {
108+
$value = 'http://example.com http://example.com http://example.com';
109+
$old_url = 'http://example.com';
110+
$new_url = 'http://example.org';
111+
112+
$replaced = $this->replace_method->invoke( $this->replacer, $value, $old_url, $new_url );
113+
$this->assertStringNotContainsString( $old_url, $replaced );
114+
$this->assertStringContainsString( $new_url, $replaced );
115+
}
116+
117+
public function test_replacing_scaled_urls() {
118+
$value = "<img src=\"http://example.com/wp-content/uploads/2020/01/image-150x150.jpg\" /><img src=\"http://example.com/wp-content/uploads/2020/01/image-300x300.jpg\" /><img src=\"http://example.com/wp-content/uploads/2020/01/image-scaled.jpg\" /><img src=\"http://example.com/wp-content/uploads/2020/01/image.jpg\" />";
119+
120+
$old_url = 'http://example.com/wp-content/uploads/2020/01/image.jpg';
121+
$new_url = 'http://example.com/wp-content/uploads/2020/01/new-url.jpg';
122+
123+
$replaced = $this->replace_method->invoke( $this->replacer, $value, $old_url, $new_url );
124+
125+
$this->assertStringNotContainsString( $old_url, $replaced );
126+
$this->assertStringContainsString( $new_url, $replaced );
127+
128+
$this->assertStringContainsString( 'http://example.com/wp-content/uploads/2020/01/new-url-150x150.jpg', $replaced );
129+
$this->assertStringContainsString( 'http://example.com/wp-content/uploads/2020/01/new-url-300x300.jpg', $replaced );
130+
$this->assertStringContainsString( 'http://example.com/wp-content/uploads/2020/01/new-url-scaled.jpg', $replaced );
131+
}
132+
133+
public function test_replacing_serialized_content() {
134+
$value = 'a:2:{s:5:"image";s:63:"http://example.com/wp-content/uploads/2020/01/image.jpg";s:5:"thumb";s:63:"http://example.com/wp-content/uploads/2020/01/thumb.jpg";}';
135+
136+
$old_url = 'http://example.com/wp-content/uploads/2020/01/image.jpg';
137+
$new_url = 'http://example.com/wp-content/uploads/2020/01/new-url.jpg';
138+
139+
$replaced = $this->replace_method->invoke( $this->replacer, $value, $old_url, $new_url );
140+
141+
$this->assertStringNotContainsString( $old_url, $replaced );
142+
$this->assertStringContainsString( $new_url, $replaced );
143+
$this->assertStringContainsString( 'thumb.jpg', $replaced );
144+
}
145+
146+
public function test_replacing_json_content() {
147+
$value = '{"image":"http://example.com/wp-content/uploads/2020/01/image.jpg","thumb":"http://example.com/wp-content/uploads/2020/01/thumb.jpg"}';
148+
149+
$old_url = 'http://example.com/wp-content/uploads/2020/01/image.jpg';
150+
$new_url = 'http://example.com/wp-content/uploads/2020/01/new-url.jpg';
151+
152+
$replaced = $this->replace_method->invoke( $this->replacer, $value, $old_url, $new_url );
153+
154+
$this->assertStringNotContainsString( $old_url, $replaced );
155+
$this->assertStringContainsString( $new_url, $replaced );
156+
$this->assertStringContainsString( 'thumb.jpg', $replaced );
157+
}
147158
}

0 commit comments

Comments
 (0)