web/wp-admin/import/opml.php
changeset 136 bde1974c263b
equal deleted inserted replaced
135:53cff4b4a802 136:bde1974c263b
       
     1 <?php
       
     2 /**
       
     3  * Links Import Administration Panel.
       
     4  *
       
     5  * @copyright 2002 Mike Little <mike@zed1.com>
       
     6  * @author Mike Little <mike@zed1.com>
       
     7  * @package WordPress
       
     8  * @subpackage Administration
       
     9  */
       
    10 
       
    11 /** Load WordPress Administration Bootstrap */
       
    12 $parent_file = 'tools.php';
       
    13 $submenu_file = 'import.php';
       
    14 $title = __('Import Blogroll');
       
    15 
       
    16 class OPML_Import {
       
    17 
       
    18 	function dispatch() {
       
    19 		global $wpdb, $user_ID;
       
    20 $step = $_POST['step'];
       
    21 if (!$step) $step = 0;
       
    22 ?>
       
    23 <?php
       
    24 switch ($step) {
       
    25 	case 0: {
       
    26 		include_once('admin-header.php');
       
    27 		if ( !current_user_can('manage_links') )
       
    28 			wp_die(__('Cheatin&#8217; uh?'));
       
    29 
       
    30 		$opmltype = 'blogrolling'; // default.
       
    31 ?>
       
    32 
       
    33 <div class="wrap">
       
    34 <?php screen_icon(); ?>
       
    35 <h2><?php _e('Import your blogroll from another system') ?> </h2>
       
    36 <form enctype="multipart/form-data" action="admin.php?import=opml" method="post" name="blogroll">
       
    37 <?php wp_nonce_field('import-bookmarks') ?>
       
    38 
       
    39 <p><?php _e('If a program or website you use allows you to export your links or subscriptions as OPML you may import them here.'); ?></p>
       
    40 <div style="width: 70%; margin: auto; height: 8em;">
       
    41 <input type="hidden" name="step" value="1" />
       
    42 <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
       
    43 <div style="width: 48%;" class="alignleft">
       
    44 <h3><label for="opml_url"><?php _e('Specify an OPML URL:'); ?></label></h3>
       
    45 <input type="text" name="opml_url" id="opml_url" size="50" class="code" style="width: 90%;" value="http://" />
       
    46 </div>
       
    47 
       
    48 <div style="width: 48%;" class="alignleft">
       
    49 <h3><label for="userfile"><?php _e('Or choose from your local disk:'); ?></label></h3>
       
    50 <input id="userfile" name="userfile" type="file" size="30" />
       
    51 </div>
       
    52 
       
    53 </div>
       
    54 
       
    55 <p style="clear: both; margin-top: 1em;"><label for="cat_id"><?php _e('Now select a category you want to put these links in.') ?></label><br />
       
    56 <?php _e('Category:') ?> <select name="cat_id" id="cat_id">
       
    57 <?php
       
    58 $categories = get_terms('link_category', 'get=all');
       
    59 foreach ($categories as $category) {
       
    60 ?>
       
    61 <option value="<?php echo $category->term_id; ?>"><?php echo esc_html(apply_filters('link_category', $category->name)); ?></option>
       
    62 <?php
       
    63 } // end foreach
       
    64 ?>
       
    65 </select></p>
       
    66 
       
    67 <p class="submit"><input type="submit" name="submit" value="<?php esc_attr_e('Import OPML File') ?>" /></p>
       
    68 </form>
       
    69 
       
    70 </div>
       
    71 <?php
       
    72 		break;
       
    73 	} // end case 0
       
    74 
       
    75 	case 1: {
       
    76 		check_admin_referer('import-bookmarks');
       
    77 
       
    78 		include_once('admin-header.php');
       
    79 		if ( !current_user_can('manage_links') )
       
    80 			wp_die(__('Cheatin&#8217; uh?'));
       
    81 ?>
       
    82 <div class="wrap">
       
    83 
       
    84 <h2><?php _e('Importing...') ?></h2>
       
    85 <?php
       
    86 		$cat_id = abs( (int) $_POST['cat_id'] );
       
    87 		if ( $cat_id < 1 )
       
    88 			$cat_id  = 1;
       
    89 
       
    90 		$opml_url = $_POST['opml_url'];
       
    91 		if ( isset($opml_url) && $opml_url != '' && $opml_url != 'http://' ) {
       
    92 			$blogrolling = true;
       
    93 		} else { // try to get the upload file.
       
    94 			$overrides = array('test_form' => false, 'test_type' => false);
       
    95 			$_FILES['userfile']['name'] .= '.txt';
       
    96 			$file = wp_handle_upload($_FILES['userfile'], $overrides);
       
    97 
       
    98 			if ( isset($file['error']) )
       
    99 				wp_die($file['error']);
       
   100 
       
   101 			$url = $file['url'];
       
   102 			$opml_url = $file['file'];
       
   103 			$blogrolling = false;
       
   104 		}
       
   105 
       
   106 		global $opml, $updated_timestamp, $all_links, $map, $names, $urls, $targets, $descriptions, $feeds;
       
   107 		if ( isset($opml_url) && $opml_url != '' ) {
       
   108 			if ( $blogrolling === true ) {
       
   109 				$opml = wp_remote_fopen($opml_url);
       
   110 			} else {
       
   111 				$opml = file_get_contents($opml_url);
       
   112 			}
       
   113 
       
   114 			/** Load OPML Parser */
       
   115 			include_once('link-parse-opml.php');
       
   116 
       
   117 			$link_count = count($names);
       
   118 			for ( $i = 0; $i < $link_count; $i++ ) {
       
   119 				if ('Last' == substr($titles[$i], 0, 4))
       
   120 					$titles[$i] = '';
       
   121 				if ( 'http' == substr($titles[$i], 0, 4) )
       
   122 					$titles[$i] = '';
       
   123 				$link = array( 'link_url' => $urls[$i], 'link_name' => $wpdb->escape($names[$i]), 'link_category' => array($cat_id), 'link_description' => $wpdb->escape($descriptions[$i]), 'link_owner' => $user_ID, 'link_rss' => $feeds[$i]);
       
   124 				wp_insert_link($link);
       
   125 				echo sprintf('<p>'.__('Inserted <strong>%s</strong>').'</p>', $names[$i]);
       
   126 			}
       
   127 ?>
       
   128 
       
   129 <p><?php printf(__('Inserted %1$d links into category %2$s. All done! Go <a href="%3$s">manage those links</a>.'), $link_count, $cat_id, 'link-manager.php') ?></p>
       
   130 
       
   131 <?php
       
   132 } // end if got url
       
   133 else
       
   134 {
       
   135 	echo "<p>" . __("You need to supply your OPML url. Press back on your browser and try again") . "</p>\n";
       
   136 } // end else
       
   137 
       
   138 if ( ! $blogrolling )
       
   139 	do_action( 'wp_delete_file', $opml_url);
       
   140 	@unlink($opml_url);
       
   141 ?>
       
   142 </div>
       
   143 <?php
       
   144 		break;
       
   145 	} // end case 1
       
   146 } // end switch
       
   147 	}
       
   148 
       
   149 	function OPML_Import() {}
       
   150 }
       
   151 
       
   152 $opml_importer = new OPML_Import();
       
   153 
       
   154 register_importer('opml', __('Blogroll'), __('Import links in OPML format.'), array(&$opml_importer, 'dispatch'));
       
   155 
       
   156 ?>