[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * WP_Importer base class 4 */ 5 class WP_Importer { 6 /** 7 * Class Constructor 8 */ 9 public function __construct() {} 10 11 /** 12 * Returns array with imported permalinks from WordPress database 13 * 14 * @global wpdb $wpdb WordPress database abstraction object. 15 * 16 * @param string $importer_name 17 * @param string $blog_id 18 * @return array 19 */ 20 public function get_imported_posts( $importer_name, $blog_id ) { 21 global $wpdb; 22 23 $hashtable = array(); 24 25 $limit = 100; 26 $offset = 0; 27 28 // Grab all posts in chunks. 29 do { 30 $meta_key = $importer_name . '_' . $blog_id . '_permalink'; 31 $sql = $wpdb->prepare( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = %s LIMIT %d,%d", $meta_key, $offset, $limit ); 32 $results = $wpdb->get_results( $sql ); 33 34 // Increment offset. 35 $offset = ( $limit + $offset ); 36 37 if ( ! empty( $results ) ) { 38 foreach ( $results as $r ) { 39 // Set permalinks into array. 40 $hashtable[ $r->meta_value ] = (int) $r->post_id; 41 } 42 } 43 } while ( count( $results ) == $limit ); 44 45 return $hashtable; 46 } 47 48 /** 49 * Return count of imported permalinks from WordPress database 50 * 51 * @global wpdb $wpdb WordPress database abstraction object. 52 * 53 * @param string $importer_name 54 * @param string $blog_id 55 * @return int 56 */ 57 public function count_imported_posts( $importer_name, $blog_id ) { 58 global $wpdb; 59 60 $count = 0; 61 62 // Get count of permalinks. 63 $meta_key = $importer_name . '_' . $blog_id . '_permalink'; 64 $sql = $wpdb->prepare( "SELECT COUNT( post_id ) AS cnt FROM $wpdb->postmeta WHERE meta_key = %s", $meta_key ); 65 66 $result = $wpdb->get_results( $sql ); 67 68 if ( ! empty( $result ) ) { 69 $count = (int) $result[0]->cnt; 70 } 71 72 return $count; 73 } 74 75 /** 76 * Set array with imported comments from WordPress database 77 * 78 * @global wpdb $wpdb WordPress database abstraction object. 79 * 80 * @param string $blog_id 81 * @return array 82 */ 83 public function get_imported_comments( $blog_id ) { 84 global $wpdb; 85 86 $hashtable = array(); 87 88 $limit = 100; 89 $offset = 0; 90 91 // Grab all comments in chunks. 92 do { 93 $sql = $wpdb->prepare( "SELECT comment_ID, comment_agent FROM $wpdb->comments LIMIT %d,%d", $offset, $limit ); 94 $results = $wpdb->get_results( $sql ); 95 96 // Increment offset. 97 $offset = ( $limit + $offset ); 98 99 if ( ! empty( $results ) ) { 100 foreach ( $results as $r ) { 101 // Explode comment_agent key. 102 list ( $comment_agent_blog_id, $source_comment_id ) = explode( '-', $r->comment_agent ); 103 104 $source_comment_id = (int) $source_comment_id; 105 106 // Check if this comment came from this blog. 107 if ( $blog_id == $comment_agent_blog_id ) { 108 $hashtable[ $source_comment_id ] = (int) $r->comment_ID; 109 } 110 } 111 } 112 } while ( count( $results ) == $limit ); 113 114 return $hashtable; 115 } 116 117 /** 118 * @param int $blog_id 119 * @return int|void 120 */ 121 public function set_blog( $blog_id ) { 122 if ( is_numeric( $blog_id ) ) { 123 $blog_id = (int) $blog_id; 124 } else { 125 $blog = 'http://' . preg_replace( '#^https?://#', '', $blog_id ); 126 $parsed = parse_url( $blog ); 127 if ( ! $parsed || empty( $parsed['host'] ) ) { 128 fwrite( STDERR, "Error: can not determine blog_id from $blog_id\n" ); 129 exit; 130 } 131 if ( empty( $parsed['path'] ) ) { 132 $parsed['path'] = '/'; 133 } 134 $blogs = get_sites( 135 array( 136 'domain' => $parsed['host'], 137 'number' => 1, 138 'path' => $parsed['path'], 139 ) 140 ); 141 if ( ! $blogs ) { 142 fwrite( STDERR, "Error: Could not find blog\n" ); 143 exit; 144 } 145 $blog = array_shift( $blogs ); 146 $blog_id = (int) $blog->blog_id; 147 } 148 149 if ( function_exists( 'is_multisite' ) ) { 150 if ( is_multisite() ) { 151 switch_to_blog( $blog_id ); 152 } 153 } 154 155 return $blog_id; 156 } 157 158 /** 159 * @param int $user_id 160 * @return int|void 161 */ 162 public function set_user( $user_id ) { 163 if ( is_numeric( $user_id ) ) { 164 $user_id = (int) $user_id; 165 } else { 166 $user_id = (int) username_exists( $user_id ); 167 } 168 169 if ( ! $user_id || ! wp_set_current_user( $user_id ) ) { 170 fwrite( STDERR, "Error: can not find user\n" ); 171 exit; 172 } 173 174 return $user_id; 175 } 176 177 /** 178 * Sort by strlen, longest string first 179 * 180 * @param string $a 181 * @param string $b 182 * @return int 183 */ 184 public function cmpr_strlen( $a, $b ) { 185 return strlen( $b ) - strlen( $a ); 186 } 187 188 /** 189 * GET URL 190 * 191 * @param string $url 192 * @param string $username 193 * @param string $password 194 * @param bool $head 195 * @return array 196 */ 197 public function get_page( $url, $username = '', $password = '', $head = false ) { 198 // Increase the timeout. 199 add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) ); 200 201 $headers = array(); 202 $args = array(); 203 if ( true === $head ) { 204 $args['method'] = 'HEAD'; 205 } 206 if ( ! empty( $username ) && ! empty( $password ) ) { 207 $headers['Authorization'] = 'Basic ' . base64_encode( "$username:$password" ); 208 } 209 210 $args['headers'] = $headers; 211 212 return wp_safe_remote_request( $url, $args ); 213 } 214 215 /** 216 * Bump up the request timeout for http requests 217 * 218 * @param int $val 219 * @return int 220 */ 221 public function bump_request_timeout( $val ) { 222 return 60; 223 } 224 225 /** 226 * Check if user has exceeded disk quota 227 * 228 * @return bool 229 */ 230 public function is_user_over_quota() { 231 if ( function_exists( 'upload_is_user_over_quota' ) ) { 232 if ( upload_is_user_over_quota() ) { 233 return true; 234 } 235 } 236 237 return false; 238 } 239 240 /** 241 * Replace newlines, tabs, and multiple spaces with a single space. 242 * 243 * @param string $text 244 * @return string 245 */ 246 public function min_whitespace( $text ) { 247 return preg_replace( '|[\r\n\t ]+|', ' ', $text ); 248 } 249 250 /** 251 * Resets global variables that grow out of control during imports. 252 * 253 * @since 3.0.0 254 * 255 * @global wpdb $wpdb WordPress database abstraction object. 256 * @global int[] $wp_actions 257 */ 258 public function stop_the_insanity() { 259 global $wpdb, $wp_actions; 260 // Or define( 'WP_IMPORTING', true ); 261 $wpdb->queries = array(); 262 // Reset $wp_actions to keep it from growing out of control. 263 $wp_actions = array(); 264 } 265 } 266 267 /** 268 * Returns value of command line params. 269 * Exits when a required param is not set. 270 * 271 * @param string $param 272 * @param bool $required 273 * @return mixed 274 */ 275 function get_cli_args( $param, $required = false ) { 276 $args = $_SERVER['argv']; 277 if ( ! is_array( $args ) ) { 278 $args = array(); 279 } 280 281 $out = array(); 282 283 $last_arg = null; 284 $return = null; 285 286 $il = count( $args ); 287 288 for ( $i = 1, $il; $i < $il; $i++ ) { 289 if ( (bool) preg_match( '/^--(.+)/', $args[ $i ], $match ) ) { 290 $parts = explode( '=', $match[1] ); 291 $key = preg_replace( '/[^a-z0-9]+/', '', $parts[0] ); 292 293 if ( isset( $parts[1] ) ) { 294 $out[ $key ] = $parts[1]; 295 } else { 296 $out[ $key ] = true; 297 } 298 299 $last_arg = $key; 300 } elseif ( (bool) preg_match( '/^-([a-zA-Z0-9]+)/', $args[ $i ], $match ) ) { 301 for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) { 302 $key = $match[1][ $j ]; 303 $out[ $key ] = true; 304 } 305 306 $last_arg = $key; 307 } elseif ( null !== $last_arg ) { 308 $out[ $last_arg ] = $args[ $i ]; 309 } 310 } 311 312 // Check array for specified param. 313 if ( isset( $out[ $param ] ) ) { 314 // Set return value. 315 $return = $out[ $param ]; 316 } 317 318 // Check for missing required param. 319 if ( ! isset( $out[ $param ] ) && $required ) { 320 // Display message and exit. 321 echo "\"$param\" parameter is required but was not specified\n"; 322 exit; 323 } 324 325 return $return; 326 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Nov 21 01:00:03 2024 | Cross-referenced by PHPXref 0.7.1 |