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