class-plugin-installer-skin.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. /**
  3. * Upgrader API: Plugin_Installer_Skin class
  4. *
  5. * @package WordPress
  6. * @subpackage Upgrader
  7. * @since 4.6.0
  8. */
  9. /**
  10. * Plugin Installer Skin for WordPress Plugin Installer.
  11. *
  12. * @since 2.8.0
  13. * @since 4.6.0 Moved to its own file from wp-admin/includes/class-wp-upgrader-skins.php.
  14. *
  15. * @see WP_Upgrader_Skin
  16. */
  17. class Plugin_Installer_Skin extends WP_Upgrader_Skin {
  18. public $api;
  19. public $type;
  20. public $url;
  21. public $overwrite;
  22. private $is_downgrading = false;
  23. /**
  24. * @param array $args
  25. */
  26. public function __construct( $args = array() ) {
  27. $defaults = array(
  28. 'type' => 'web',
  29. 'url' => '',
  30. 'plugin' => '',
  31. 'nonce' => '',
  32. 'title' => '',
  33. 'overwrite' => '',
  34. );
  35. $args = wp_parse_args( $args, $defaults );
  36. $this->type = $args['type'];
  37. $this->url = $args['url'];
  38. $this->api = isset( $args['api'] ) ? $args['api'] : array();
  39. $this->overwrite = $args['overwrite'];
  40. parent::__construct( $args );
  41. }
  42. /**
  43. * Action to perform before installing a plugin.
  44. *
  45. * @since 2.8.0
  46. */
  47. public function before() {
  48. if ( ! empty( $this->api ) ) {
  49. $this->upgrader->strings['process_success'] = sprintf(
  50. $this->upgrader->strings['process_success_specific'],
  51. $this->api->name,
  52. $this->api->version
  53. );
  54. }
  55. }
  56. /**
  57. * Hides the `process_failed` error when updating a plugin by uploading a zip file.
  58. *
  59. * @since 5.5.0
  60. *
  61. * @param WP_Error $wp_error WP_Error object.
  62. * @return bool
  63. */
  64. public function hide_process_failed( $wp_error ) {
  65. if (
  66. 'upload' === $this->type &&
  67. '' === $this->overwrite &&
  68. $wp_error->get_error_code() === 'folder_exists'
  69. ) {
  70. return true;
  71. }
  72. return false;
  73. }
  74. /**
  75. * Action to perform following a plugin install.
  76. *
  77. * @since 2.8.0
  78. */
  79. public function after() {
  80. // Check if the plugin can be overwritten and output the HTML.
  81. if ( $this->do_overwrite() ) {
  82. return;
  83. }
  84. $plugin_file = $this->upgrader->plugin_info();
  85. $install_actions = array();
  86. $from = isset( $_GET['from'] ) ? wp_unslash( $_GET['from'] ) : 'plugins';
  87. if ( 'import' === $from ) {
  88. $install_actions['activate_plugin'] = sprintf(
  89. '<a class="button button-primary" href="%s" target="_parent">%s</a>',
  90. wp_nonce_url( 'plugins.php?action=activate&amp;from=import&amp;plugin=' . urlencode( $plugin_file ), 'activate-plugin_' . $plugin_file ),
  91. __( 'Activate Plugin &amp; Run Importer' )
  92. );
  93. } elseif ( 'press-this' === $from ) {
  94. $install_actions['activate_plugin'] = sprintf(
  95. '<a class="button button-primary" href="%s" target="_parent">%s</a>',
  96. wp_nonce_url( 'plugins.php?action=activate&amp;from=press-this&amp;plugin=' . urlencode( $plugin_file ), 'activate-plugin_' . $plugin_file ),
  97. __( 'Activate Plugin &amp; Go to Press This' )
  98. );
  99. } else {
  100. $install_actions['activate_plugin'] = sprintf(
  101. '<a class="button button-primary" href="%s" target="_parent">%s</a>',
  102. wp_nonce_url( 'plugins.php?action=activate&amp;plugin=' . urlencode( $plugin_file ), 'activate-plugin_' . $plugin_file ),
  103. __( 'Activate Plugin' )
  104. );
  105. }
  106. if ( is_multisite() && current_user_can( 'manage_network_plugins' ) ) {
  107. $install_actions['network_activate'] = sprintf(
  108. '<a class="button button-primary" href="%s" target="_parent">%s</a>',
  109. wp_nonce_url( 'plugins.php?action=activate&amp;networkwide=1&amp;plugin=' . urlencode( $plugin_file ), 'activate-plugin_' . $plugin_file ),
  110. __( 'Network Activate' )
  111. );
  112. unset( $install_actions['activate_plugin'] );
  113. }
  114. if ( 'import' === $from ) {
  115. $install_actions['importers_page'] = sprintf(
  116. '<a href="%s" target="_parent">%s</a>',
  117. admin_url( 'import.php' ),
  118. __( 'Go to Importers' )
  119. );
  120. } elseif ( 'web' === $this->type ) {
  121. $install_actions['plugins_page'] = sprintf(
  122. '<a href="%s" target="_parent">%s</a>',
  123. self_admin_url( 'plugin-install.php' ),
  124. __( 'Go to Plugin Installer' )
  125. );
  126. } elseif ( 'upload' === $this->type && 'plugins' === $from ) {
  127. $install_actions['plugins_page'] = sprintf(
  128. '<a href="%s">%s</a>',
  129. self_admin_url( 'plugin-install.php' ),
  130. __( 'Go to Plugin Installer' )
  131. );
  132. } else {
  133. $install_actions['plugins_page'] = sprintf(
  134. '<a href="%s" target="_parent">%s</a>',
  135. self_admin_url( 'plugins.php' ),
  136. __( 'Go to Plugins page' )
  137. );
  138. }
  139. if ( ! $this->result || is_wp_error( $this->result ) ) {
  140. unset( $install_actions['activate_plugin'], $install_actions['network_activate'] );
  141. } elseif ( ! current_user_can( 'activate_plugin', $plugin_file ) || is_plugin_active( $plugin_file ) ) {
  142. unset( $install_actions['activate_plugin'] );
  143. }
  144. /**
  145. * Filters the list of action links available following a single plugin installation.
  146. *
  147. * @since 2.7.0
  148. *
  149. * @param string[] $install_actions Array of plugin action links.
  150. * @param object $api Object containing WordPress.org API plugin data. Empty
  151. * for non-API installs, such as when a plugin is installed
  152. * via upload.
  153. * @param string $plugin_file Path to the plugin file relative to the plugins directory.
  154. */
  155. $install_actions = apply_filters( 'install_plugin_complete_actions', $install_actions, $this->api, $plugin_file );
  156. if ( ! empty( $install_actions ) ) {
  157. $this->feedback( implode( ' ', (array) $install_actions ) );
  158. }
  159. }
  160. /**
  161. * Check if the plugin can be overwritten and output the HTML for overwriting a plugin on upload.
  162. *
  163. * @since 5.5.0
  164. *
  165. * @return bool Whether the plugin can be overwritten and HTML was outputted.
  166. */
  167. private function do_overwrite() {
  168. if ( 'upload' !== $this->type || ! is_wp_error( $this->result ) || 'folder_exists' !== $this->result->get_error_code() ) {
  169. return false;
  170. }
  171. $folder = $this->result->get_error_data( 'folder_exists' );
  172. $folder = ltrim( substr( $folder, strlen( WP_PLUGIN_DIR ) ), '/' );
  173. $current_plugin_data = false;
  174. $all_plugins = get_plugins();
  175. foreach ( $all_plugins as $plugin => $plugin_data ) {
  176. if ( strrpos( $plugin, $folder ) !== 0 ) {
  177. continue;
  178. }
  179. $current_plugin_data = $plugin_data;
  180. }
  181. $new_plugin_data = $this->upgrader->new_plugin_data;
  182. if ( ! $current_plugin_data || ! $new_plugin_data ) {
  183. return false;
  184. }
  185. echo '<h2 class="update-from-upload-heading">' . esc_html__( 'This plugin is already installed.' ) . '</h2>';
  186. $this->is_downgrading = version_compare( $current_plugin_data['Version'], $new_plugin_data['Version'], '>' );
  187. $rows = array(
  188. 'Name' => __( 'Plugin name' ),
  189. 'Version' => __( 'Version' ),
  190. 'Author' => __( 'Author' ),
  191. 'RequiresWP' => __( 'Required WordPress version' ),
  192. 'RequiresPHP' => __( 'Required PHP version' ),
  193. );
  194. $table = '<table class="update-from-upload-comparison"><tbody>';
  195. $table .= '<tr><th></th><th>' . esc_html_x( 'Current', 'plugin' ) . '</th>';
  196. $table .= '<th>' . esc_html_x( 'Uploaded', 'plugin' ) . '</th></tr>';
  197. $is_same_plugin = true; // Let's consider only these rows.
  198. foreach ( $rows as $field => $label ) {
  199. $old_value = ! empty( $current_plugin_data[ $field ] ) ? (string) $current_plugin_data[ $field ] : '-';
  200. $new_value = ! empty( $new_plugin_data[ $field ] ) ? (string) $new_plugin_data[ $field ] : '-';
  201. $is_same_plugin = $is_same_plugin && ( $old_value === $new_value );
  202. $diff_field = ( 'Version' !== $field && $new_value !== $old_value );
  203. $diff_version = ( 'Version' === $field && $this->is_downgrading );
  204. $table .= '<tr><td class="name-label">' . $label . '</td><td>' . wp_strip_all_tags( $old_value ) . '</td>';
  205. $table .= ( $diff_field || $diff_version ) ? '<td class="warning">' : '<td>';
  206. $table .= wp_strip_all_tags( $new_value ) . '</td></tr>';
  207. }
  208. $table .= '</tbody></table>';
  209. /**
  210. * Filters the compare table output for overwriting a plugin package on upload.
  211. *
  212. * @since 5.5.0
  213. *
  214. * @param string $table The output table with Name, Version, Author, RequiresWP, and RequiresPHP info.
  215. * @param array $current_plugin_data Array with current plugin data.
  216. * @param array $new_plugin_data Array with uploaded plugin data.
  217. */
  218. echo apply_filters( 'install_plugin_overwrite_comparison', $table, $current_plugin_data, $new_plugin_data );
  219. $install_actions = array();
  220. $can_update = true;
  221. $blocked_message = '<p>' . esc_html__( 'The plugin cannot be updated due to the following:' ) . '</p>';
  222. $blocked_message .= '<ul class="ul-disc">';
  223. $requires_php = isset( $new_plugin_data['RequiresPHP'] ) ? $new_plugin_data['RequiresPHP'] : null;
  224. $requires_wp = isset( $new_plugin_data['RequiresWP'] ) ? $new_plugin_data['RequiresWP'] : null;
  225. if ( ! is_php_version_compatible( $requires_php ) ) {
  226. $error = sprintf(
  227. /* translators: 1: Current PHP version, 2: Version required by the uploaded plugin. */
  228. __( 'The PHP version on your server is %1$s, however the uploaded plugin requires %2$s.' ),
  229. PHP_VERSION,
  230. $requires_php
  231. );
  232. $blocked_message .= '<li>' . esc_html( $error ) . '</li>';
  233. $can_update = false;
  234. }
  235. if ( ! is_wp_version_compatible( $requires_wp ) ) {
  236. $error = sprintf(
  237. /* translators: 1: Current WordPress version, 2: Version required by the uploaded plugin. */
  238. __( 'Your WordPress version is %1$s, however the uploaded plugin requires %2$s.' ),
  239. get_bloginfo( 'version' ),
  240. $requires_wp
  241. );
  242. $blocked_message .= '<li>' . esc_html( $error ) . '</li>';
  243. $can_update = false;
  244. }
  245. $blocked_message .= '</ul>';
  246. if ( $can_update ) {
  247. if ( $this->is_downgrading ) {
  248. $warning = sprintf(
  249. /* translators: %s: Documentation URL. */
  250. __( 'You are uploading an older version of a current plugin. You can continue to install the older version, but be sure to <a href="%s">back up your database and files</a> first.' ),
  251. __( 'https://wordpress.org/support/article/wordpress-backups/' )
  252. );
  253. } else {
  254. $warning = sprintf(
  255. /* translators: %s: Documentation URL. */
  256. __( 'You are updating a plugin. Be sure to <a href="%s">back up your database and files</a> first.' ),
  257. __( 'https://wordpress.org/support/article/wordpress-backups/' )
  258. );
  259. }
  260. echo '<p class="update-from-upload-notice">' . $warning . '</p>';
  261. $overwrite = $this->is_downgrading ? 'downgrade-plugin' : 'update-plugin';
  262. $install_actions['overwrite_plugin'] = sprintf(
  263. '<a class="button button-primary update-from-upload-overwrite" href="%s" target="_parent">%s</a>',
  264. wp_nonce_url( add_query_arg( 'overwrite', $overwrite, $this->url ), 'plugin-upload' ),
  265. _x( 'Replace current with uploaded', 'plugin' )
  266. );
  267. } else {
  268. echo $blocked_message;
  269. }
  270. $cancel_url = add_query_arg( 'action', 'upload-plugin-cancel-overwrite', $this->url );
  271. $install_actions['plugins_page'] = sprintf(
  272. '<a class="button" href="%s">%s</a>',
  273. wp_nonce_url( $cancel_url, 'plugin-upload-cancel-overwrite' ),
  274. __( 'Cancel and go back' )
  275. );
  276. /**
  277. * Filters the list of action links available following a single plugin installation failure
  278. * when overwriting is allowed.
  279. *
  280. * @since 5.5.0
  281. *
  282. * @param string[] $install_actions Array of plugin action links.
  283. * @param object $api Object containing WordPress.org API plugin data.
  284. * @param array $new_plugin_data Array with uploaded plugin data.
  285. */
  286. $install_actions = apply_filters( 'install_plugin_overwrite_actions', $install_actions, $this->api, $new_plugin_data );
  287. if ( ! empty( $install_actions ) ) {
  288. printf(
  289. '<p class="update-from-upload-expired hidden">%s</p>',
  290. __( 'The uploaded file has expired. Please go back and upload it again.' )
  291. );
  292. echo '<p class="update-from-upload-actions">' . implode( ' ', (array) $install_actions ) . '</p>';
  293. }
  294. return true;
  295. }
  296. }