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