installation
2 months ago
rest-api
2 months ago
class-wc-wccom-site-installer.php
2 months ago
class-wc-wccom-site.php
2 months ago
class-wc-wccom-site-installer.php
111 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce.com Product Installation. |
| 4 | * |
| 5 | * @package WooCommerce\WCCom |
| 6 | * @since 3.7.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * WC_WCCOM_Site_Installer Class |
| 13 | * |
| 14 | * Contains functionalities to install products via WooCommerce.com helper connection. |
| 15 | */ |
| 16 | class WC_WCCOM_Site_Installer { |
| 17 | |
| 18 | /** |
| 19 | * An instance of the WP_Upgrader class to be used for installation. |
| 20 | * |
| 21 | * @var \WP_Upgrader $wp_upgrader |
| 22 | */ |
| 23 | private static $wp_upgrader; |
| 24 | |
| 25 | |
| 26 | /** |
| 27 | * Get WP.org plugin's main file. |
| 28 | * |
| 29 | * @since 3.7.0 |
| 30 | * @param string $dir Directory name of the plugin. |
| 31 | * @return bool|string |
| 32 | */ |
| 33 | public static function get_wporg_plugin_main_file( $dir ) { |
| 34 | // Ensure that exact dir name is used. |
| 35 | $dir = trailingslashit( $dir ); |
| 36 | |
| 37 | if ( ! function_exists( 'get_plugins' ) ) { |
| 38 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 39 | } |
| 40 | |
| 41 | $plugins = get_plugins(); |
| 42 | foreach ( $plugins as $path => $plugin ) { |
| 43 | if ( 0 === strpos( $path, $dir ) ) { |
| 44 | return $path; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | /** |
| 53 | * Get plugin info |
| 54 | * |
| 55 | * @since 3.9.0 |
| 56 | * @param string $dir Directory name of the plugin. |
| 57 | * @return bool|array |
| 58 | */ |
| 59 | public static function get_plugin_info( $dir ) { |
| 60 | $plugin_folder = basename( $dir ); |
| 61 | |
| 62 | if ( ! function_exists( 'get_plugins' ) ) { |
| 63 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 64 | } |
| 65 | |
| 66 | $plugins = get_plugins(); |
| 67 | |
| 68 | $related_plugins = array_filter( |
| 69 | $plugins, |
| 70 | function( $key ) use ( $plugin_folder ) { |
| 71 | return strpos( $key, $plugin_folder . '/' ) === 0; |
| 72 | }, |
| 73 | ARRAY_FILTER_USE_KEY |
| 74 | ); |
| 75 | |
| 76 | if ( 1 === count( $related_plugins ) ) { |
| 77 | $plugin_key = array_keys( $related_plugins )[0]; |
| 78 | $plugin_data = $plugins[ $plugin_key ]; |
| 79 | return array( |
| 80 | 'name' => $plugin_data['Name'], |
| 81 | 'version' => $plugin_data['Version'], |
| 82 | 'active' => is_plugin_active( $plugin_key ), |
| 83 | ); |
| 84 | } |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get an instance of WP_Upgrader to use for installing plugins. |
| 90 | * |
| 91 | * @return WP_Upgrader |
| 92 | */ |
| 93 | public static function get_wp_upgrader() { |
| 94 | if ( ! empty( self::$wp_upgrader ) ) { |
| 95 | return self::$wp_upgrader; |
| 96 | } |
| 97 | |
| 98 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 99 | require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 100 | require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 101 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 102 | |
| 103 | WP_Filesystem(); |
| 104 | self::$wp_upgrader = new WP_Upgrader( new Automatic_Upgrader_Skin() ); |
| 105 | self::$wp_upgrader->init(); |
| 106 | wp_clean_plugins_cache(); |
| 107 | |
| 108 | return self::$wp_upgrader; |
| 109 | } |
| 110 | } |
| 111 |