[ 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  #[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( $url, $username = '', $password = '', $head = false ) {
 199          // Increase the timeout.
 200          add_filter( 'http_request_timeout', array( $this, 'bump_request_timeout' ) );
 201  
 202          $headers = array();
 203          $args    = array();
 204          if ( true === $head ) {
 205              $args['method'] = 'HEAD';
 206          }
 207          if ( ! empty( $username ) && ! empty( $password ) ) {
 208              $headers['Authorization'] = 'Basic ' . base64_encode( "$username:$password" );
 209          }
 210  
 211          $args['headers'] = $headers;
 212  
 213          return wp_safe_remote_request( $url, $args );
 214      }
 215  
 216      /**
 217       * Bumps up the request timeout for http requests.
 218       *
 219       * @param int $val
 220       * @return int
 221       */
 222  	public function bump_request_timeout( $val ) {
 223          return 60;
 224      }
 225  
 226      /**
 227       * Checks if user has exceeded disk quota.
 228       *
 229       * @return bool
 230       */
 231  	public function is_user_over_quota() {
 232          if ( function_exists( 'upload_is_user_over_quota' ) ) {
 233              if ( upload_is_user_over_quota() ) {
 234                  return true;
 235              }
 236          }
 237  
 238          return false;
 239      }
 240  
 241      /**
 242       * Replaces newlines, tabs, and multiple spaces with a single space.
 243       *
 244       * @param string $text
 245       * @return string
 246       */
 247  	public function min_whitespace( $text ) {
 248          return preg_replace( '|[\r\n\t ]+|', ' ', $text );
 249      }
 250  
 251      /**
 252       * Resets global variables that grow out of control during imports.
 253       *
 254       * @since 3.0.0
 255       *
 256       * @global wpdb  $wpdb       WordPress database abstraction object.
 257       * @global int[] $wp_actions
 258       */
 259  	public function stop_the_insanity() {
 260          global $wpdb, $wp_actions;
 261          // Or define( 'WP_IMPORTING', true );
 262          $wpdb->queries = array();
 263          // Reset $wp_actions to keep it from growing out of control.
 264          $wp_actions = array();
 265      }
 266  }
 267  
 268  /**
 269   * Returns value of command line params.
 270   * Exits when a required param is not set.
 271   *
 272   * @param string $param
 273   * @param bool   $required
 274   * @return mixed
 275   */
 276  function get_cli_args( $param, $required = false ) {
 277      $args = $_SERVER['argv'];
 278      if ( ! is_array( $args ) ) {
 279          $args = array();
 280      }
 281  
 282      $out = array();
 283  
 284      $last_arg = null;
 285      $return   = null;
 286  
 287      $il = count( $args );
 288  
 289      for ( $i = 1, $il; $i < $il; $i++ ) {
 290          if ( (bool) preg_match( '/^--(.+)/', $args[ $i ], $match ) ) {
 291              $parts = explode( '=', $match[1] );
 292              $key   = preg_replace( '/[^a-z0-9]+/', '', $parts[0] );
 293  
 294              if ( isset( $parts[1] ) ) {
 295                  $out[ $key ] = $parts[1];
 296              } else {
 297                  $out[ $key ] = true;
 298              }
 299  
 300              $last_arg = $key;
 301          } elseif ( (bool) preg_match( '/^-([a-zA-Z0-9]+)/', $args[ $i ], $match ) ) {
 302              for ( $j = 0, $jl = strlen( $match[1] ); $j < $jl; $j++ ) {
 303                  $key         = $match[1][ $j ];
 304                  $out[ $key ] = true;
 305              }
 306  
 307              $last_arg = $key;
 308          } elseif ( null !== $last_arg ) {
 309              $out[ $last_arg ] = $args[ $i ];
 310          }
 311      }
 312  
 313      // Check array for specified param.
 314      if ( isset( $out[ $param ] ) ) {
 315          // Set return value.
 316          $return = $out[ $param ];
 317      }
 318  
 319      // Check for missing required param.
 320      if ( ! isset( $out[ $param ] ) && $required ) {
 321          // Display message and exit.
 322          echo "\"$param\" parameter is required but was not specified\n";
 323          exit;
 324      }
 325  
 326      return $return;
 327  }


Generated : Sat Dec 21 08:20:01 2024 Cross-referenced by PHPXref