class.jetpack-automatic-install-skin.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  3. include_once ABSPATH . 'wp-admin/includes/file.php';
  4. /**
  5. * Allows us to capture that the site doesn't have proper file system access.
  6. * In order to update the plugin.
  7. */
  8. class Jetpack_Automatic_Install_Skin extends Automatic_Upgrader_Skin {
  9. /**
  10. * Stores the last error key;
  11. **/
  12. protected $main_error_code = 'install_error';
  13. /**
  14. * Stores the last error message.
  15. **/
  16. protected $main_error_message = 'An unknown error occurred during installation';
  17. /**
  18. * Overwrites the set_upgrader to be able to tell if we e ven have the ability to write to the files.
  19. *
  20. * @param WP_Upgrader $upgrader
  21. *
  22. */
  23. public function set_upgrader( &$upgrader ) {
  24. parent::set_upgrader( $upgrader );
  25. // Check if we even have permission to.
  26. $result = $upgrader->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) );
  27. if ( ! $result ) {
  28. // set the string here since they are not available just yet
  29. $upgrader->generic_strings();
  30. $this->feedback( 'fs_unavailable' );
  31. }
  32. }
  33. /**
  34. * Overwrites the error function
  35. */
  36. public function error( $error ) {
  37. if ( is_wp_error( $error ) ) {
  38. $this->feedback( $error );
  39. }
  40. }
  41. private function set_main_error_code( $code ) {
  42. // Don't set the process_failed as code since it is not that helpful unless we don't have one already set.
  43. $this->main_error_code = ( $code === 'process_failed' && $this->main_error_code ? $this->main_error_code : $code );
  44. }
  45. private function set_main_error_message( $message, $code ) {
  46. // Don't set the process_failed as message since it is not that helpful unless we don't have one already set.
  47. $this->main_error_message = ( $code === 'process_failed' && $this->main_error_code ? $this->main_error_code : $message );
  48. }
  49. public function get_main_error_code() {
  50. return $this->main_error_code;
  51. }
  52. public function get_main_error_message() {
  53. return $this->main_error_message;
  54. }
  55. /**
  56. * Overwrites the feedback function
  57. */
  58. public function feedback( $data ) {
  59. $current_error = null;
  60. if ( is_wp_error( $data ) ) {
  61. $this->set_main_error_code( $data->get_error_code() );
  62. $string = $data->get_error_message();
  63. } elseif ( is_array( $data ) ) {
  64. return;
  65. } else {
  66. $string = $data;
  67. }
  68. if ( ! empty( $this->upgrader->strings[$string] ) ) {
  69. $this->set_main_error_code( $string );
  70. $current_error = $string;
  71. $string = $this->upgrader->strings[$string];
  72. }
  73. if ( strpos( $string, '%' ) !== false ) {
  74. $args = func_get_args();
  75. $args = array_splice( $args, 1 );
  76. if ( ! empty( $args ) ) {
  77. $string = vsprintf( $string, $args );
  78. }
  79. }
  80. $string = trim( $string );
  81. $string = wp_kses(
  82. $string, array(
  83. 'a' => array(
  84. 'href' => true
  85. ),
  86. 'br' => true,
  87. 'em' => true,
  88. 'strong' => true,
  89. )
  90. );
  91. $this->set_main_error_message( $string, $current_error );
  92. $this->messages[] = $string;
  93. }
  94. }