| 1 | <?php |
|---|
| 2 | // Test the output of Export Building functions |
|---|
| 3 | |
|---|
| 4 | include_once(ABSPATH . 'wp-admin/includes/export.php'); |
|---|
| 5 | |
|---|
| 6 | /** |
|---|
| 7 | * @group export |
|---|
| 8 | */ |
|---|
| 9 | class Test_Export_Helpers extends WP_UnitTestCase { |
|---|
| 10 | |
|---|
| 11 | function test_wxr_authors_list() { |
|---|
| 12 | $editor_id = $this->factory->user->create( array( 'role' => 'editor' ) ); |
|---|
| 13 | $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) ); |
|---|
| 14 | |
|---|
| 15 | $this->factory->post->create( array( 'post_author' => $editor_id ) ); |
|---|
| 16 | $this->factory->post->create( array( 'post_author' => $admin_id, 'post_status' => 'auto-draft' ) ); |
|---|
| 17 | |
|---|
| 18 | ob_start(); |
|---|
| 19 | wxr_authors_list(); |
|---|
| 20 | $list = ob_get_clean(); |
|---|
| 21 | $count = preg_match_all("|<wp:author_id>(.*)</wp:author_id>|", $list, $authors ); |
|---|
| 22 | |
|---|
| 23 | $this->assertEquals( $count, 1 ); |
|---|
| 24 | $this->assertEquals( $authors[1][0], $editor_id ); |
|---|
| 25 | |
|---|
| 26 | } |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | function wxr_authors_list() { |
|---|
| 30 | global $wpdb; |
|---|
| 31 | |
|---|
| 32 | $authors = array(); |
|---|
| 33 | $results = $wpdb->get_results( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_status" ); |
|---|
| 34 | foreach ( (array) $results as $result ) |
|---|
| 35 | $authors[] = get_userdata( $result->post_author ); |
|---|
| 36 | |
|---|
| 37 | $authors = array_filter( $authors ); |
|---|
| 38 | |
|---|
| 39 | foreach ( $authors as $author ) { |
|---|
| 40 | echo "\t<wp:author>"; |
|---|
| 41 | echo '<wp:author_id>' . $author->ID . '</wp:author_id>'; |
|---|
| 42 | echo '<wp:author_login>' . $author->user_login . '</wp:author_login>'; |
|---|
| 43 | echo '<wp:author_email>' . $author->user_email . '</wp:author_email>'; |
|---|
| 44 | echo '<wp:author_display_name>' . wxr_cdata( $author->display_name ) . '</wp:author_display_name>'; |
|---|
| 45 | echo '<wp:author_first_name>' . wxr_cdata( $author->user_firstname ) . '</wp:author_first_name>'; |
|---|
| 46 | echo '<wp:author_last_name>' . wxr_cdata( $author->user_lastname ) . '</wp:author_last_name>'; |
|---|
| 47 | echo "</wp:author>\n"; |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | function wxr_authors_list_fixed() { |
|---|
| 52 | global $wpdb; |
|---|
| 53 | |
|---|
| 54 | $authors = array(); |
|---|
| 55 | $results = $wpdb->get_results( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE post_status != 'auto-draft'" ); |
|---|
| 56 | foreach ( (array) $results as $result ) |
|---|
| 57 | $authors[] = get_userdata( $result->post_author ); |
|---|
| 58 | |
|---|
| 59 | $authors = array_filter( $authors ); |
|---|
| 60 | |
|---|
| 61 | foreach ( $authors as $author ) { |
|---|
| 62 | echo "\t<wp:author>"; |
|---|
| 63 | echo '<wp:author_id>' . $author->ID . '</wp:author_id>'; |
|---|
| 64 | echo '<wp:author_login>' . $author->user_login . '</wp:author_login>'; |
|---|
| 65 | echo '<wp:author_email>' . $author->user_email . '</wp:author_email>'; |
|---|
| 66 | echo '<wp:author_display_name>' . wxr_cdata( $author->display_name ) . '</wp:author_display_name>'; |
|---|
| 67 | echo '<wp:author_first_name>' . wxr_cdata( $author->user_firstname ) . '</wp:author_first_name>'; |
|---|
| 68 | echo '<wp:author_last_name>' . wxr_cdata( $author->user_lastname ) . '</wp:author_last_name>'; |
|---|
| 69 | echo "</wp:author>\n"; |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | function wxr_cdata( $str ) { |
|---|
| 74 | if ( seems_utf8( $str ) == false ) |
|---|
| 75 | $str = utf8_encode( $str ); |
|---|
| 76 | |
|---|
| 77 | // $str = ent2ncr(esc_html($str)); |
|---|
| 78 | $str = '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>'; |
|---|
| 79 | |
|---|
| 80 | return $str; |
|---|
| 81 | } |
|---|