| 1 |
<?php |
| 2 |
/** |
| 3 |
* Friends import |
| 4 |
* |
| 5 |
* This contains the import functions. |
| 6 |
* |
| 7 |
* @package Friends |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Friends; |
| 11 |
|
| 12 |
/** |
| 13 |
* This is the class for the import part of the Friends Plugin. |
| 14 |
* |
| 15 |
* @since 3.0 |
| 16 |
* |
| 17 |
* @package Friends |
| 18 |
* @author Alex Kirk |
| 19 |
*/ |
| 20 |
class Import { |
| 21 |
|
| 22 |
private static function get_feed_from_opml_node( $friend ) { |
| 23 |
$xml_url = (string) $friend['xmlUrl']; |
| 24 |
if ( ! $xml_url ) { |
| 25 |
return null; |
| 26 |
} |
| 27 |
|
| 28 |
$username = (string) $friend['text']; |
| 29 |
if ( ! $username ) { |
| 30 |
$username = (string) $friend['title']; |
| 31 |
} |
| 32 |
if ( ! $username ) { |
| 33 |
$username = (string) $friend['htmlUrl']; |
| 34 |
$username = preg_replace( '/^https?:\/\//', '', $username ); |
| 35 |
} |
| 36 |
if ( ! $username ) { |
| 37 |
$username = (string) $xml_url; |
| 38 |
$username = preg_replace( '/^https?:\/\//', '', $username ); |
| 39 |
} |
| 40 |
|
| 41 |
$user_login = User::sanitize_username( $username ); |
| 42 |
$feed = User_Feed::get_by_url( $xml_url ); |
| 43 |
if ( $feed instanceof User_Feed ) { |
| 44 |
return $feed; |
| 45 |
} |
| 46 |
|
| 47 |
$friend_user = User::get_by_username( $user_login ); |
| 48 |
if ( ! $friend_user || is_wp_error( $friend_user ) ) { |
| 49 |
$friend_user = Subscription::create( |
| 50 |
$user_login, |
| 51 |
'subscription', |
| 52 |
(string) $friend['htmlUrl'], |
| 53 |
(string) $friend['text'] |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
if ( ! $friend_user instanceof User ) { |
| 58 |
return null; |
| 59 |
} |
| 60 |
|
| 61 |
$feed = $friend_user->save_feed( |
| 62 |
$xml_url, |
| 63 |
array( |
| 64 |
'active' => true, |
| 65 |
'mime-type' => 'atom' === (string) $friend['type'] ? 'application/atom+xml' : 'application/rss+xml', |
| 66 |
) |
| 67 |
); |
| 68 |
|
| 69 |
if ( ! $feed instanceof User_Feed ) { |
| 70 |
if ( is_wp_error( $feed ) && apply_filters( 'friends_debug', false ) ) { |
| 71 |
wp_trigger_error( __FUNCTION__, $feed->get_error_message() ); |
| 72 |
|
| 73 |
} |
| 74 |
return null; |
| 75 |
} |
| 76 |
|
| 77 |
return $feed; |
| 78 |
} |
| 79 |
|
| 80 |
private static function recurse_into_opml( $feeds, $friend ) { |
| 81 |
$xml_url = (string) $friend['xmlUrl']; |
| 82 |
if ( $xml_url ) { |
| 83 |
$feed = self::get_feed_from_opml_node( $friend ); |
| 84 |
if ( $feed instanceof User_Feed ) { |
| 85 |
$friend_user = $feed->get_friend_user(); |
| 86 |
|
| 87 |
if ( ! isset( $feeds[ $friend_user->user_login ] ) ) { |
| 88 |
$feeds[ $friend_user->user_login ] = array(); |
| 89 |
} |
| 90 |
$feeds[ $friend_user->user_login ][] = $feed; |
| 91 |
} |
| 92 |
return $feeds; |
| 93 |
} |
| 94 |
|
| 95 |
foreach ( $friend->outline as $child ) { |
| 96 |
$feeds = self::recurse_into_opml( $feeds, $child ); |
| 97 |
} |
| 98 |
|
| 99 |
return $feeds; |
| 100 |
} |
| 101 |
|
| 102 |
public static function opml( $opml ) { |
| 103 |
$opml = simplexml_load_string( $opml ); |
| 104 |
if ( ! $opml ) { |
| 105 |
return new \WP_Error( 'friends_import_opml_error', __( 'Failed to parse OPML.', 'friends' ) ); |
| 106 |
} |
| 107 |
|
| 108 |
$feeds = self::recurse_into_opml( array(), $opml->body ); |
| 109 |
|
| 110 |
return $feeds; |
| 111 |
} |
| 112 |
} |
| 113 |
|