@@ -387,56 +387,105 @@ public function test_block_attachments_with_fallback() {
387
387
}
388
388
389
389
/**
390
- * Saves an attachment.
391
- *
392
- * @param string $file The file name to create attachment object for.
393
- * @param int $parent_id ID of the post to attach the file to.
390
+ * Test get_media_from_blocks adds alt text to existing images.
394
391
*
395
- * @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure.
392
+ * @covers ::get_media_from_blocks
396
393
*/
397
- public function create_upload_object ( $ file , $ parent_id = 0 ) {
398
- if ( ! class_exists ( 'WP_Filesystem_Direct ' ) ) {
399
- require ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php ' ;
400
- require ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php ' ;
401
- }
394
+ public function test_get_media_from_blocks_adds_alt_text_to_existing_images () {
395
+ $ post_id = self ::factory ()->post ->create (
396
+ array (
397
+ 'post_content ' => '<!-- wp:image {"id":123} --><figure class="wp-block-image"><img src="test.jpg" alt="Test alt text" /></figure><!-- /wp:image --> ' ,
398
+ )
399
+ );
400
+ $ post = get_post ( $ post_id );
402
401
403
- $ dest = dirname ( $ file ) . DIRECTORY_SEPARATOR . 'test-temp.jpg ' ;
404
- $ fs = new \WP_Filesystem_Direct ( array () );
405
- $ fs ->copy ( $ file , $ dest );
402
+ $ transformer = new Post ( $ post );
403
+ $ media = array (
404
+ 'image ' => array (
405
+ array (
406
+ 'id ' => 123 ,
407
+ 'alt ' => '' ,
408
+ ),
409
+ ),
410
+ 'audio ' => array (),
411
+ 'video ' => array (),
412
+ );
406
413
407
- $ file = $ dest ;
414
+ $ reflection = new \ReflectionClass ( Post::class );
415
+ $ method = $ reflection ->getMethod ( 'get_media_from_blocks ' );
416
+ $ method ->setAccessible ( true );
408
417
409
- $ file_array = array (
410
- 'name ' => wp_basename ( $ file ),
411
- 'tmp_name ' => $ file ,
418
+ $ blocks = parse_blocks ( $ post ->post_content );
419
+ $ result = $ method ->invoke ( $ transformer , $ blocks , $ media );
420
+
421
+ $ this ->assertSame ( 'Test alt text ' , $ result ['image ' ][0 ]['alt ' ] );
422
+ $ this ->assertSame ( 123 , $ result ['image ' ][0 ]['id ' ] );
423
+ }
424
+
425
+ /**
426
+ * Test get_media_from_blocks adds new image when none exist.
427
+ *
428
+ * @covers ::get_media_from_blocks
429
+ */
430
+ public function test_get_media_from_blocks_adds_new_image () {
431
+ $ post_id = self ::factory ()->post ->create (
432
+ array (
433
+ 'post_content ' => '<!-- wp:image {"id":123} --><figure class="wp-block-image"><img src="test.jpg" alt="Test alt text" /></figure><!-- /wp:image --> ' ,
434
+ )
412
435
);
436
+ $ post = get_post ( $ post_id );
413
437
414
- $ upload = wp_handle_sideload ( $ file_array , array ( 'test_form ' => false ) );
438
+ $ transformer = new Post ( $ post );
439
+ $ media = array (
440
+ 'image ' => array (),
441
+ 'audio ' => array (),
442
+ 'video ' => array (),
443
+ );
415
444
416
- $ type = '' ;
417
- if ( ! empty ( $ upload ['type ' ] ) ) {
418
- $ type = $ upload ['type ' ];
419
- } else {
420
- $ mime = wp_check_filetype ( $ upload ['file ' ] );
421
- if ( $ mime ) {
422
- $ type = $ mime ['type ' ];
423
- }
424
- }
445
+ $ reflection = new \ReflectionClass ( Post::class );
446
+ $ method = $ reflection ->getMethod ( 'get_media_from_blocks ' );
447
+ $ method ->setAccessible ( true );
425
448
426
- $ attachment = array (
427
- 'post_title ' => wp_basename ( $ upload ['file ' ] ),
428
- 'post_content ' => '' ,
429
- 'post_type ' => 'attachment ' ,
430
- 'post_parent ' => $ parent_id ,
431
- 'post_mime_type ' => $ type ,
432
- 'guid ' => $ upload ['url ' ],
449
+ $ blocks = parse_blocks ( $ post ->post_content );
450
+ $ result = $ method ->invoke ( $ transformer , $ blocks , $ media );
451
+
452
+ $ this ->assertCount ( 1 , $ result ['image ' ] );
453
+ $ this ->assertSame ( 123 , $ result ['image ' ][0 ]['id ' ] );
454
+ $ this ->assertSame ( 'Test alt text ' , $ result ['image ' ][0 ]['alt ' ] );
455
+ }
456
+
457
+ /**
458
+ * Test get_media_from_blocks handles multiple blocks correctly.
459
+ *
460
+ * @covers ::get_media_from_blocks
461
+ */
462
+ public function test_get_media_from_blocks_handles_multiple_blocks () {
463
+ $ post_id = self ::factory ()->post ->create (
464
+ array (
465
+ 'post_content ' => '<!-- wp:image {"id":123} --><figure class="wp-block-image"><img src="test1.jpg" alt="Test alt 1" /></figure><!-- /wp:image --><!-- wp:image {"id":456} --><figure class="wp-block-image"><img src="test2.jpg" alt="Test alt 2" /></figure><!-- /wp:image --> ' ,
466
+ )
433
467
);
468
+ $ post = get_post ( $ post_id );
434
469
435
- // Save the data.
436
- $ id = wp_insert_attachment ( $ attachment , $ upload ['file ' ], $ parent_id );
437
- wp_update_attachment_metadata ( $ id , @wp_generate_attachment_metadata ( $ id , $ upload ['file ' ] ) ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
470
+ $ transformer = new Post ( $ post );
471
+ $ media = array (
472
+ 'image ' => array (),
473
+ 'audio ' => array (),
474
+ 'video ' => array (),
475
+ );
438
476
439
- return $ id ;
477
+ $ reflection = new \ReflectionClass ( Post::class );
478
+ $ method = $ reflection ->getMethod ( 'get_media_from_blocks ' );
479
+ $ method ->setAccessible ( true );
480
+
481
+ $ blocks = parse_blocks ( $ post ->post_content );
482
+ $ result = $ method ->invoke ( $ transformer , $ blocks , $ media );
483
+
484
+ $ this ->assertCount ( 2 , $ result ['image ' ] );
485
+ $ this ->assertSame ( 123 , $ result ['image ' ][0 ]['id ' ] );
486
+ $ this ->assertSame ( 'Test alt 1 ' , $ result ['image ' ][0 ]['alt ' ] );
487
+ $ this ->assertSame ( 456 , $ result ['image ' ][1 ]['id ' ] );
488
+ $ this ->assertSame ( 'Test alt 2 ' , $ result ['image ' ][1 ]['alt ' ] );
440
489
}
441
490
442
491
/**
@@ -457,7 +506,7 @@ public function test_get_icon() {
457
506
$ attachment_id = $ this ->create_upload_object ( dirname ( __DIR__ , 2 ) . '/assets/test.jpg ' );
458
507
459
508
// Set up reflection method.
460
- $ reflection = new ReflectionClass ( Post::class );
509
+ $ reflection = new \ ReflectionClass ( Post::class );
461
510
$ method = $ reflection ->getMethod ( 'get_icon ' );
462
511
$ method ->setAccessible ( true );
463
512
@@ -513,4 +562,56 @@ public function test_get_icon() {
513
562
wp_delete_post ( $ post_id , true );
514
563
wp_delete_attachment ( $ attachment_id , true );
515
564
}
565
+
566
+ /**
567
+ * Saves an attachment.
568
+ *
569
+ * @param string $file The file name to create attachment object for.
570
+ * @param int $parent_id ID of the post to attach the file to.
571
+ * @return int|\WP_Error The attachment ID on success. The value 0 or WP_Error on failure.
572
+ */
573
+ public function create_upload_object ( $ file , $ parent_id = 0 ) {
574
+ if ( ! class_exists ( 'WP_Filesystem_Direct ' ) ) {
575
+ require ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php ' ;
576
+ require ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php ' ;
577
+ }
578
+
579
+ $ dest = dirname ( $ file ) . DIRECTORY_SEPARATOR . 'test-temp.jpg ' ;
580
+ $ fs = new \WP_Filesystem_Direct ( array () );
581
+ $ fs ->copy ( $ file , $ dest );
582
+
583
+ $ file = $ dest ;
584
+
585
+ $ file_array = array (
586
+ 'name ' => wp_basename ( $ file ),
587
+ 'tmp_name ' => $ file ,
588
+ );
589
+
590
+ $ upload = wp_handle_sideload ( $ file_array , array ( 'test_form ' => false ) );
591
+
592
+ $ type = '' ;
593
+ if ( ! empty ( $ upload ['type ' ] ) ) {
594
+ $ type = $ upload ['type ' ];
595
+ } else {
596
+ $ mime = wp_check_filetype ( $ upload ['file ' ] );
597
+ if ( $ mime ) {
598
+ $ type = $ mime ['type ' ];
599
+ }
600
+ }
601
+
602
+ $ attachment = array (
603
+ 'post_title ' => wp_basename ( $ upload ['file ' ] ),
604
+ 'post_content ' => '' ,
605
+ 'post_type ' => 'attachment ' ,
606
+ 'post_parent ' => $ parent_id ,
607
+ 'post_mime_type ' => $ type ,
608
+ 'guid ' => $ upload ['url ' ],
609
+ );
610
+
611
+ // Save the data.
612
+ $ id = wp_insert_attachment ( $ attachment , $ upload ['file ' ], $ parent_id );
613
+ wp_update_attachment_metadata ( $ id , @wp_generate_attachment_metadata ( $ id , $ upload ['file ' ] ) ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
614
+
615
+ return $ id ;
616
+ }
516
617
}
0 commit comments