home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-admin / includes / class-wp-ajax-upgrader-skin.php < prev    next >
Encoding:
PHP Script  |  2017-07-26  |  3.0 KB  |  127 lines

  1. <?php
  2. /**
  3.  * Upgrader API: WP_Ajax_Upgrader_Skin class
  4.  *
  5.  * @package WordPress
  6.  * @subpackage Upgrader
  7.  * @since 4.6.0
  8.  */
  9.  
  10. /**
  11.  * Upgrader Skin for Ajax WordPress upgrades.
  12.  *
  13.  * This skin is designed to be used for Ajax updates.
  14.  *
  15.  * @since 4.6.0
  16.  *
  17.  * @see Automatic_Upgrader_Skin
  18.  */
  19. class WP_Ajax_Upgrader_Skin extends Automatic_Upgrader_Skin {
  20.  
  21.     /**
  22.      * Holds the WP_Error object.
  23.      *
  24.      * @since 4.6.0
  25.      * @var null|WP_Error
  26.      */
  27.     protected $errors = null;
  28.  
  29.     /**
  30.      * Constructor.
  31.      *
  32.      * @since 4.6.0
  33.      *
  34.      * @param array $args Options for the upgrader, see WP_Upgrader_Skin::__construct().
  35.      */
  36.     public function __construct( $args = array() ) {
  37.         parent::__construct( $args );
  38.  
  39.         $this->errors = new WP_Error();
  40.     }
  41.  
  42.     /**
  43.      * Retrieves the list of errors.
  44.      *
  45.      * @since 4.6.0
  46.      *
  47.      * @return WP_Error Errors during an upgrade.
  48.      */
  49.     public function get_errors() {
  50.         return $this->errors;
  51.     }
  52.  
  53.     /**
  54.      * Retrieves a string for error messages.
  55.      *
  56.      * @since 4.6.0
  57.      *
  58.      * @return string Error messages during an upgrade.
  59.      */
  60.     public function get_error_messages() {
  61.         $messages = array();
  62.  
  63.         foreach ( $this->errors->get_error_codes() as $error_code ) {
  64.             if ( $this->errors->get_error_data( $error_code ) && is_string( $this->errors->get_error_data( $error_code ) ) ) {
  65.                 $messages[] = $this->errors->get_error_message( $error_code ) . ' ' . esc_html( strip_tags( $this->errors->get_error_data( $error_code ) ) );
  66.             } else {
  67.                 $messages[] = $this->errors->get_error_message( $error_code );
  68.             }
  69.         }
  70.  
  71.         return implode( ', ', $messages );
  72.     }
  73.  
  74.     /**
  75.      * Stores a log entry for an error.
  76.      *
  77.      * @since 4.6.0
  78.      *
  79.      * @param string|WP_Error $errors Errors.
  80.      */
  81.     public function error( $errors ) {
  82.         if ( is_string( $errors ) ) {
  83.             $string = $errors;
  84.             if ( ! empty( $this->upgrader->strings[ $string ] ) ) {
  85.                 $string = $this->upgrader->strings[ $string ];
  86.             }
  87.  
  88.             if ( false !== strpos( $string, '%' ) ) {
  89.                 $args = func_get_args();
  90.                 $args = array_splice( $args, 1 );
  91.                 if ( ! empty( $args ) ) {
  92.                     $string = vsprintf( $string, $args );
  93.                 }
  94.             }
  95.  
  96.             // Count existing errors to generate an unique error code.
  97.             $errors_count = count( $this->errors->get_error_codes() );
  98.             $this->errors->add( 'unknown_upgrade_error_' . $errors_count + 1 , $string );
  99.         } elseif ( is_wp_error( $errors ) ) {
  100.             foreach ( $errors->get_error_codes() as $error_code ) {
  101.                 $this->errors->add( $error_code, $errors->get_error_message( $error_code ), $errors->get_error_data( $error_code ) );
  102.             }
  103.         }
  104.  
  105.         $args = func_get_args();
  106.         call_user_func_array( array( $this, 'parent::error' ), $args );
  107.     }
  108.  
  109.     /**
  110.      * Stores a log entry.
  111.      *
  112.      * @since 4.6.0
  113.      *
  114.      * @param string|array|WP_Error $data Log entry data.
  115.      */
  116.     public function feedback( $data ) {
  117.         if ( is_wp_error( $data ) ) {
  118.             foreach ( $data->get_error_codes() as $error_code ) {
  119.                 $this->errors->add( $error_code, $data->get_error_message( $error_code ), $data->get_error_data( $error_code ) );
  120.             }
  121.         }
  122.  
  123.         $args = func_get_args();
  124.         call_user_func_array( array( $this, 'parent::feedback' ), $args );
  125.     }
  126. }
  127.