/wp-content/plugins/wpremote/wprp.plugins.php

https://github.com/sharpmachine/whiteantelopestudio.com · PHP · 303 lines · 176 code · 84 blank · 43 comment · 56 complexity · 159cf0f0b20107df1e63af9b4d631465 MD5 · raw file

  1. <?php
  2. /**
  3. * Return an array of installed plugins
  4. *
  5. * @return array
  6. */
  7. function _wprp_get_plugins() {
  8. require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
  9. // Get all plugins
  10. $plugins = get_plugins();
  11. // Get the list of active plugins
  12. $active = get_option( 'active_plugins', array() );
  13. // Delete the transient so wp_update_plugins can get fresh data
  14. if ( function_exists( 'get_site_transient' ) )
  15. delete_site_transient( 'update_plugins' );
  16. else
  17. delete_transient( 'update_plugins' );
  18. // Force a plugin update check
  19. wp_update_plugins();
  20. // Different versions of wp store the updates in different places
  21. // TODO can we depreciate
  22. if( function_exists( 'get_site_transient' ) && $transient = get_site_transient( 'update_plugins' ) )
  23. $current = $transient;
  24. elseif( $transient = get_transient( 'update_plugins' ) )
  25. $current = $transient;
  26. else
  27. $current = get_option( 'update_plugins' );
  28. // Premium plugins that have adopted the ManageWP API report new plugins by this filter
  29. $manage_wp_updates = apply_filters( 'mwp_premium_update_notification', array() );
  30. foreach ( (array) $plugins as $plugin_file => $plugin ) {
  31. if ( is_plugin_active( $plugin_file ) )
  32. $plugins[$plugin_file]['active'] = true;
  33. else
  34. $plugins[$plugin_file]['active'] = false;
  35. $manage_wp_plugin_update = false;
  36. foreach( $manage_wp_updates as $manage_wp_update ) {
  37. if ( ! empty( $manage_wp_update['Name'] ) && $plugin['Name'] == $manage_wp_update['Name'] )
  38. $manage_wp_plugin_update = $manage_wp_update;
  39. }
  40. if ( $manage_wp_plugin_update ) {
  41. $plugins[$plugin_file]['latest_version'] = $manage_wp_plugin_update['new_version'];
  42. } else if ( isset( $current->response[$plugin_file] ) ) {
  43. $plugins[$plugin_file]['latest_version'] = $current->response[$plugin_file]->new_version;
  44. $plugins[$plugin_file]['latest_package'] = $current->response[$plugin_file]->package;
  45. $plugins[$plugin_file]['slug'] = $current->response[$plugin_file]->slug;
  46. } else {
  47. $plugins[$plugin_file]['latest_version'] = $plugin['Version'];
  48. }
  49. }
  50. return $plugins;
  51. }
  52. /**
  53. * Update a plugin
  54. *
  55. * @access private
  56. * @param mixed $plugin
  57. * @return array
  58. */
  59. function _wprp_update_plugin( $plugin_file, $args ) {
  60. global $wprp_zip_update;
  61. if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
  62. return new WP_Error( 'disallow-file-mods', __( "File modification is disabled with the DISALLOW_FILE_MODS constant.", 'wpremote' ) );
  63. include_once ( ABSPATH . 'wp-admin/includes/admin.php' );
  64. require_once ( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
  65. require_once WPRP_PLUGIN_PATH . 'inc/class-wprp-plugin-upgrader-skin.php';
  66. // check for filesystem access
  67. if ( ! _wpr_check_filesystem_access() )
  68. return new WP_Error( 'filesystem-not-writable', __( 'The filesystem is not writable with the supplied credentials', 'wpremote' ) );
  69. $is_active = is_plugin_active( $plugin_file );
  70. $is_active_network = is_plugin_active_for_network( $plugin_file );
  71. foreach( get_plugins() as $path => $maybe_plugin ) {
  72. if ( $path == $plugin_file ) {
  73. $plugin = $maybe_plugin;
  74. break;
  75. }
  76. }
  77. // Permit specifying a zip URL to update the plugin with
  78. if ( ! empty( $args['zip_url'] ) ) {
  79. $zip_url = $args['zip_url'];
  80. } else {
  81. // Check to see if this is a premium plugin that supports the ManageWP implementation
  82. $manage_wp_updates = apply_filters( 'mwp_premium_perform_update', array() );
  83. $manage_wp_plugin_update = false;
  84. foreach( $manage_wp_updates as $manage_wp_update ) {
  85. if ( ! empty( $manage_wp_update['Name'] )
  86. && $plugin['Name'] == $manage_wp_update['Name']
  87. && ! empty( $manage_wp_update['url'] ) ) {
  88. $zip_url = $manage_wp_update['url'];
  89. break;
  90. }
  91. }
  92. }
  93. $skin = new WPRP_Plugin_Upgrader_Skin();
  94. $upgrader = new Plugin_Upgrader( $skin );
  95. // Fake out the plugin upgrader with our package url
  96. if ( ! empty( $zip_url ) ) {
  97. $wprp_zip_update = array(
  98. 'plugin_file' => $plugin_file,
  99. 'package' => $zip_url,
  100. );
  101. add_filter( 'pre_site_transient_update_plugins', '_wprp_forcably_filter_update_plugins' );
  102. } else {
  103. wp_update_plugins();
  104. }
  105. // Do the upgrade
  106. ob_start();
  107. $result = $upgrader->upgrade( $plugin_file );
  108. $data = ob_get_contents();
  109. ob_clean();
  110. if ( $manage_wp_plugin_update )
  111. remove_filter( 'pre_site_transient_update_plugins', '_wprp_forcably_filter_update_plugins' );
  112. if ( ! empty( $skin->error ) )
  113. return new WP_Error( 'plugin-upgrader-skin', $upgrader->strings[$skin->error] );
  114. else if ( is_wp_error( $result ) )
  115. return $result;
  116. else if ( ( ! $result && ! is_null( $result ) ) || $data )
  117. return new WP_Error( 'plugin-update', __( 'Unknown error updating plugin.', 'wpremote' ) );
  118. // If the plugin was activited, we have to re-activate it
  119. // but if activate_plugin() fatals, then we'll just have to return 500
  120. if ( $is_active )
  121. activate_plugin( $plugin_file, '', $is_active_network, true );
  122. return array( 'status' => 'success' );
  123. }
  124. /**
  125. * Filter `update_plugins` to produce a response it will understand
  126. * so we can have the Upgrader skin handle the update
  127. */
  128. function _wprp_forcably_filter_update_plugins() {
  129. global $wprp_zip_update;
  130. $current = new stdClass;
  131. $current->response = array();
  132. $plugin_file = $wprp_zip_update['plugin_file'];
  133. $current->response[$plugin_file] = new stdClass;
  134. $current->response[$plugin_file]->package = $wprp_zip_update['package'];
  135. return $current;
  136. }
  137. /**
  138. * Install a plugin on this site
  139. */
  140. function _wprp_install_plugin( $plugin, $args = array() ) {
  141. if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
  142. return new WP_Error( 'disallow-file-mods', __( "File modification is disabled with the DISALLOW_FILE_MODS constant.", 'wpremote' ) );
  143. include_once ABSPATH . 'wp-admin/includes/admin.php';
  144. include_once ABSPATH . 'wp-admin/includes/upgrade.php';
  145. include_once ABSPATH . 'wp-includes/update.php';
  146. require_once ( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
  147. require_once WPRP_PLUGIN_PATH . 'inc/class-wprp-plugin-upgrader-skin.php';
  148. // Access the plugins_api() helper function
  149. include_once ABSPATH . 'wp-admin/includes/plugin-install.php';
  150. $api_args = array(
  151. 'slug' => $plugin,
  152. 'fields' => array( 'sections' => false )
  153. );
  154. $api = plugins_api( 'plugin_information', $api_args );
  155. if ( is_wp_error( $api ) )
  156. return $api;
  157. $skin = new WPRP_Plugin_Upgrader_Skin();
  158. $upgrader = new Plugin_Upgrader( $skin );
  159. // The best way to get a download link for a specific version :(
  160. // Fortunately, we can depend on a relatively consistent naming pattern
  161. if ( ! empty( $args['version'] ) && 'stable' != $args['version'] )
  162. $api->download_link = str_replace( $api->version . '.zip', $args['version'] . '.zip', $api->download_link );
  163. $result = $upgrader->install( $api->download_link );
  164. if ( is_wp_error( $result ) )
  165. return $result;
  166. else if ( ! $result )
  167. return new WP_Error( 'plugin-install', __( 'Unknown error installing plugin.', 'wpremote' ) );
  168. return array( 'status' => 'success' );
  169. }
  170. function _wprp_activate_plugin( $plugin ) {
  171. include_once ABSPATH . 'wp-admin/includes/plugin.php';
  172. $result = activate_plugin( $plugin );
  173. if ( is_wp_error( $result ) )
  174. return $result;
  175. return array( 'status' => 'success' );
  176. }
  177. /**
  178. * Deactivate a plugin on this site.
  179. */
  180. function _wprp_deactivate_plugin( $plugin ) {
  181. include_once ABSPATH . 'wp-admin/includes/plugin.php';
  182. if ( is_plugin_active( $plugin ) )
  183. deactivate_plugins( $plugin );
  184. return array( 'status' => 'success' );
  185. }
  186. /**
  187. * Uninstall a plugin on this site.
  188. */
  189. function _wprp_uninstall_plugin( $plugin ) {
  190. global $wp_filesystem;
  191. if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
  192. return new WP_Error( 'disallow-file-mods', __( "File modification is disabled with the DISALLOW_FILE_MODS constant.", 'wpremote' ) );
  193. include_once ABSPATH . 'wp-admin/includes/admin.php';
  194. include_once ABSPATH . 'wp-admin/includes/upgrade.php';
  195. include_once ABSPATH . 'wp-includes/update.php';
  196. if ( ! _wpr_check_filesystem_access() || ! WP_Filesystem() )
  197. return new WP_Error( 'filesystem-not-writable', __( 'The filesystem is not writable with the supplied credentials', 'wpremote' ) );
  198. $plugins_dir = $wp_filesystem->wp_plugins_dir();
  199. if ( empty( $plugins_dir ) )
  200. return new WP_Error( 'missing-plugin-dir', __( 'Unable to locate WordPress Plugin directory.' , 'wpremote' ) );
  201. $plugins_dir = trailingslashit( $plugins_dir );
  202. if ( is_uninstallable_plugin( $plugin ) )
  203. uninstall_plugin( $plugin );
  204. $this_plugin_dir = trailingslashit( dirname( $plugins_dir . $plugin ) );
  205. // If plugin is in its own directory, recursively delete the directory.
  206. if ( strpos( $plugin, '/' ) && $this_plugin_dir != $plugins_dir ) //base check on if plugin includes directory separator AND that it's not the root plugin folder
  207. $deleted = $wp_filesystem->delete( $this_plugin_dir, true );
  208. else
  209. $deleted = $wp_filesystem->delete( $plugins_dir . $plugin );
  210. if ( $deleted ) {
  211. if ( $current = get_site_transient('update_plugins') ) {
  212. unset( $current->response[$plugin] );
  213. set_site_transient('update_plugins', $current);
  214. }
  215. return array( 'status' => 'success' );
  216. } else {
  217. return new WP_Error( 'plugin-uninstall', __( 'Plugin uninstalled, but not deleted.', 'wpremote' ) );
  218. }
  219. }