[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-admin/includes/ -> class-wp-importer.php (source)

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


Generated : Sat May 2 08:20:14 2026 Cross-referenced by PHPXref