← All changes
|
jetpack_vendor/automattic/jetpack-plugins-installer/src/class-automatic-install-skin.php
+161
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,161 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Automatic_Upgrader_Skin extension for Jetpack | |
| 4 | + * | |
| 5 | + * @package jetpack-plugins-installer | |
| 6 | + */ | |
| 7 | + | |
| 8 | +namespace Automattic\Jetpack; | |
| 9 | + | |
| 10 | +use Automatic_Upgrader_Skin; | |
| 11 | +use WP_Error; | |
| 12 | +use WP_Upgrader; | |
| 13 | + | |
| 14 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 15 | + exit( 0 ); | |
| 16 | +} | |
| 17 | + | |
| 18 | +/** | |
| 19 | + * Include required files from wp-admin. | |
| 20 | + */ | |
| 21 | +require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; | |
| 22 | +require_once ABSPATH . 'wp-admin/includes/file.php'; | |
| 23 | + | |
| 24 | +/** | |
| 25 | + * Allows us to capture that the site doesn't have proper file system access. | |
| 26 | + * In order to update the plugin. | |
| 27 | + */ | |
| 28 | +class Automatic_Install_Skin extends Automatic_Upgrader_Skin { | |
| 29 | + /** | |
| 30 | + * Stores the last error key; | |
| 31 | + * | |
| 32 | + * @var string | |
| 33 | + **/ | |
| 34 | + protected $main_error_code = 'install_error'; | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * Stores the last error message. | |
| 38 | + * | |
| 39 | + * @var string | |
| 40 | + **/ | |
| 41 | + protected $main_error_message = 'An unknown error occurred during installation'; | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * Overwrites the set_upgrader to be able to tell if we e ven have the ability to write to the files. | |
| 45 | + * | |
| 46 | + * @param WP_Upgrader $upgrader The upgrader object. | |
| 47 | + */ | |
| 48 | + public function set_upgrader( &$upgrader ) { | |
| 49 | + parent::set_upgrader( $upgrader ); | |
| 50 | + | |
| 51 | + // Check if we even have permission to. | |
| 52 | + $result = $upgrader->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) ); | |
| 53 | + if ( ! $result ) { | |
| 54 | + // set the string here since they are not available just yet. | |
| 55 | + $upgrader->generic_strings(); | |
| 56 | + $this->feedback( 'fs_unavailable' ); | |
| 57 | + } | |
| 58 | + } | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * Overwrites the error function | |
| 62 | + * | |
| 63 | + * @param WP_Error|mixed $error The error object. | |
| 64 | + */ | |
| 65 | + public function error( $error ) { | |
| 66 | + if ( is_wp_error( $error ) ) { | |
| 67 | + $this->feedback( $error ); | |
| 68 | + } | |
| 69 | + } | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * Set the main error code. | |
| 73 | + * | |
| 74 | + * Don't set the process_failed as code since it is not that helpful unless we don't have one already set | |
| 75 | + * | |
| 76 | + * @param string $code The error code. | |
| 77 | + * @return void | |
| 78 | + */ | |
| 79 | + private function set_main_error_code( $code ) { | |
| 80 | + $this->main_error_code = ( 'process_failed' === $code && $this->main_error_code ? $this->main_error_code : $code ); | |
| 81 | + } | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * Set the main error message. | |
| 85 | + * | |
| 86 | + * Don't set the process_failed as message since it is not that helpful unless we don't have one already set | |
| 87 | + * | |
| 88 | + * @param string $message The error message. | |
| 89 | + * @param string $code The error code. | |
| 90 | + * @return void | |
| 91 | + */ | |
| 92 | + private function set_main_error_message( $message, $code ) { | |
| 93 | + $this->main_error_message = ( 'process_failed' === $code && $this->main_error_message ? $this->main_error_message : $message ); | |
| 94 | + } | |
| 95 | + | |
| 96 | + /** | |
| 97 | + * Get the main error code | |
| 98 | + * | |
| 99 | + * @return string | |
| 100 | + */ | |
| 101 | + public function get_main_error_code() { | |
| 102 | + return $this->main_error_code; | |
| 103 | + } | |
| 104 | + | |
| 105 | + /** | |
| 106 | + * Get the main error message | |
| 107 | + * | |
| 108 | + * @return string | |
| 109 | + */ | |
| 110 | + public function get_main_error_message() { | |
| 111 | + return $this->main_error_message; | |
| 112 | + } | |
| 113 | + | |
| 114 | + /** | |
| 115 | + * Overwrites the feedback function | |
| 116 | + * | |
| 117 | + * @param string|array|WP_Error $data Data. | |
| 118 | + * @param mixed ...$args Optional text replacements. | |
| 119 | + */ | |
| 120 | + public function feedback( $data, ...$args ) { | |
| 121 | + | |
| 122 | + $current_error = null; | |
| 123 | + if ( is_wp_error( $data ) ) { | |
| 124 | + $this->set_main_error_code( $data->get_error_code() ); | |
| 125 | + $string = $data->get_error_message(); | |
| 126 | + } elseif ( is_array( $data ) ) { | |
| 127 | + return; | |
| 128 | + } else { | |
| 129 | + $string = $data; | |
| 130 | + } | |
| 131 | + | |
| 132 | + if ( ! empty( $this->upgrader->strings[ $string ] ) ) { | |
| 133 | + $this->set_main_error_code( $string ); | |
| 134 | + | |
| 135 | + $current_error = $string; | |
| 136 | + $string = $this->upgrader->strings[ $string ]; | |
| 137 | + } | |
| 138 | + | |
| 139 | + if ( strpos( $string, '%' ) !== false ) { | |
| 140 | + if ( ! empty( $args ) ) { | |
| 141 | + $string = vsprintf( $string, $args ); | |
| 142 | + } | |
| 143 | + } | |
| 144 | + | |
| 145 | + $string = trim( $string ); | |
| 146 | + $string = wp_kses( | |
| 147 | + $string, | |
| 148 | + array( | |
| 149 | + 'a' => array( | |
| 150 | + 'href' => true, | |
| 151 | + ), | |
| 152 | + 'br' => true, | |
| 153 | + 'em' => true, | |
| 154 | + 'strong' => true, | |
| 155 | + ) | |
| 156 | + ); | |
| 157 | + | |
| 158 | + $this->set_main_error_message( $string, $current_error ); | |
| 159 | + $this->messages[] = $string; | |
| 160 | + } | |
| 161 | +} | |