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


Generated : Tue Jul 21 08:20:16 2026 Cross-referenced by PHPXref