CDTClient
1 month ago
admin-notices
1 month ago
buy-url
1 month ago
debug
1 month ago
exceptions
5 years ago
instances
1 month ago
loader
5 years ago
products
1 month ago
repository
1 month ago
rest
1 month ago
site-key
1 month ago
support
1 month ago
template-service
1 month ago
upgrade
1 month ago
utilities
1 month ago
wpml-content-stats
1 month ago
class-installer-dependencies.php
1 month ago
class-installer-theme.php
1 month ago
class-installer-upgrader-skins.php
1 month ago
class-otgs-installer-autoloader.php
5 years ago
class-otgs-installer-cloned-sites-handler.php
1 month ago
class-otgs-installer-debug-info.php
1 month ago
class-otgs-installer-factory.php
1 month ago
class-otgs-installer-filename-hooks.php
1 month ago
class-otgs-installer-icons.php
1 month ago
class-otgs-installer-loader.php
10 months ago
class-otgs-installer-package-product-finder.php
1 month ago
class-otgs-installer-package-product.php
1 month ago
class-otgs-installer-package.php
5 years ago
class-otgs-installer-php-functions.php
1 month ago
class-otgs-installer-plugin-factory.php
1 month ago
class-otgs-installer-plugin-finder.php
1 month ago
class-otgs-installer-plugin.php
1 month ago
class-otgs-installer-plugins-page-notice.php
1 month ago
class-otgs-installer-plugins-update-cache-cleaner.php
5 years ago
class-otgs-installer-settings.php
1 month ago
class-otgs-installer-source-factory.php
5 years ago
class-otgs-installer-source.php
1 month ago
class-otgs-installer-subscription-factory.php
5 years ago
class-otgs-installer-subscription-warning-message.php
1 month ago
class-otgs-installer-subscription.php
1 month ago
class-otgs-installer-wp-components-hooks.php
1 month ago
class-otgs-installer-wp-components-sender.php
1 month ago
class-otgs-installer-wp-components-setting-ajax.php
1 month ago
class-otgs-installer-wp-components-setting-resources.php
1 month ago
class-otgs-installer-wp-components-storage.php
1 month ago
class-otgs-installer-wp-share-local-components-setting-hooks.php
1 month ago
class-otgs-installer-wp-share-local-components-setting.php
1 month ago
class-translation-service-info.php
1 month ago
class-wp-installer-api.php
1 month ago
class-wp-installer-channels.php
1 month ago
class-wp-installer.php
1 month ago
functions-core.php
1 month ago
functions-templates.php
1 month ago
otgs-installer-autoload-classmap.php
10 months ago
class-wp-installer-channels.php
225 lines
| 1 | <?php |
| 2 | |
| 3 | use OTGS\Installer\Settings; |
| 4 | |
| 5 | class WP_Installer_Channels{ |
| 6 | |
| 7 | const CHANNEL_PRODUCTION = 'production'; |
| 8 | const CHANNEL_BETA = 'beta'; |
| 9 | const CHANNEL_DEVELOPMENT = 'development'; |
| 10 | |
| 11 | protected static $_instance = null; |
| 12 | |
| 13 | function __construct() { |
| 14 | add_action( 'init', array( $this, 'init' ), 20 ); |
| 15 | } |
| 16 | |
| 17 | public static function instance() { |
| 18 | |
| 19 | if ( is_null( self::$_instance ) ) { |
| 20 | self::$_instance = new self(); |
| 21 | } |
| 22 | |
| 23 | return self::$_instance; |
| 24 | } |
| 25 | |
| 26 | public static function channel_name_by_id( $id ) { |
| 27 | if ( self::CHANNEL_DEVELOPMENT === $id ) { |
| 28 | $channel = __( 'Development', 'installer' ); |
| 29 | } elseif ( self::CHANNEL_BETA === $id ) { |
| 30 | $channel = __( 'Beta', 'installer' ); |
| 31 | } else { |
| 32 | $channel = __( 'Production', 'installer' ); |
| 33 | } |
| 34 | |
| 35 | return $channel; |
| 36 | } |
| 37 | |
| 38 | public function init(){ |
| 39 | global $pagenow; |
| 40 | |
| 41 | if ( defined( 'DOING_AJAX' ) ) { |
| 42 | add_action( 'wp_ajax_installer_set_channel', array( $this, 'set_channel' ) ); |
| 43 | } |
| 44 | |
| 45 | if ( $pagenow === 'plugin-install.php' && isset( $_GET['tab'] ) && $_GET['tab'] === 'commercial' ) { |
| 46 | wp_enqueue_script( 'installer-channels', WP_Installer()->res_url() . '/res/js/channels.js', array( 'jquery' ), WP_Installer()->version() ); |
| 47 | } |
| 48 | |
| 49 | } |
| 50 | |
| 51 | public function set_channel(){ |
| 52 | $repository_id = sanitize_text_field( $_POST['repository_id'] ); |
| 53 | $channel = sanitize_text_field( $_POST['channel'] ); |
| 54 | $installer = WP_Installer::instance(); |
| 55 | $settings = $installer->get_settings(); |
| 56 | |
| 57 | $response = array(); |
| 58 | if ( wp_verify_nonce( $_POST['nonce'], 'installer_set_channel:' . $repository_id ) ) { |
| 59 | if( isset( $settings['repositories'][$repository_id] ) ){ |
| 60 | $settings['repositories'][$repository_id]['channel'] = $channel; |
| 61 | $settings['repositories'][$repository_id]['no-prompt'] = $_POST['noprompt'] === 'true'; |
| 62 | $installer->save_settings( $settings ); |
| 63 | } |
| 64 | |
| 65 | $installer->refresh_repositories_data(); |
| 66 | |
| 67 | $response['status'] = 'OK'; |
| 68 | } |
| 69 | |
| 70 | echo json_encode( $response ); |
| 71 | exit; |
| 72 | } |
| 73 | |
| 74 | public function get_channel( $repository_id ){ |
| 75 | $channel = self::CHANNEL_PRODUCTION; |
| 76 | $settings = Settings::load_channels(); |
| 77 | if( isset( $settings['repositories'][$repository_id]['channel'] ) ){ |
| 78 | $channel = $settings['repositories'][$repository_id]['channel']; |
| 79 | } |
| 80 | return $channel; |
| 81 | } |
| 82 | |
| 83 | private function get_no_prompt( $repository_id ) { |
| 84 | $settings = WP_Installer()->settings; |
| 85 | |
| 86 | return ! empty( $settings['repositories'][ $repository_id ]['no-prompt'] ); |
| 87 | } |
| 88 | |
| 89 | public function load_channel_selector( $repository_id, $downloads ) { |
| 90 | |
| 91 | $available_channels = $this->get_available_channels( $repository_id ); |
| 92 | |
| 93 | if ( $available_channels ) { |
| 94 | $args = array( |
| 95 | 'can_switch' => $this->can_use_unstable_channels( $downloads ) |
| 96 | || $this->get_channel( $repository_id ) != self::CHANNEL_PRODUCTION, |
| 97 | 'channels' => $available_channels, |
| 98 | 'repository_id' => $repository_id, |
| 99 | 'current_channel' => $this->get_channel( $repository_id ), |
| 100 | 'no_prompt' => $this->get_no_prompt( $repository_id ), |
| 101 | 'nonce' => wp_create_nonce( 'installer_set_channel:' . $repository_id ) |
| 102 | ); |
| 103 | extract( $args ); |
| 104 | include WP_Installer()->plugin_path() . '/templates/channel-selector.php'; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | public function can_use_unstable_channels( $downloads ){ |
| 109 | |
| 110 | $can = true; |
| 111 | foreach( $downloads as $download ){ |
| 112 | $available_version = $download['version']; |
| 113 | $installed_version = WP_Installer()->plugin_is_installed( $download['name'], $download['slug'] ); |
| 114 | if( $installed_version !== false && version_compare( $available_version, $installed_version, '>' ) ){ |
| 115 | $can = false; |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return $can; |
| 121 | } |
| 122 | |
| 123 | public function get_available_channels( $repository_id ) { |
| 124 | |
| 125 | $beta = false; |
| 126 | $dev = false; |
| 127 | |
| 128 | $downloads = WP_Installer()->settings['repositories'][ $repository_id ]['data']['downloads']; |
| 129 | foreach ( $downloads as $type => $download_types ) { |
| 130 | foreach ( $download_types as $download ) { |
| 131 | $extra_channels = isset( $download['extra_channels'] ) ? array_keys( $download['extra_channels'] ) : array(); |
| 132 | if ( ! $beta && in_array( self::CHANNEL_BETA, $extra_channels ) ) { |
| 133 | $beta = true; |
| 134 | } |
| 135 | if ( ! $dev && in_array( self::CHANNEL_DEVELOPMENT, $extra_channels ) ) { |
| 136 | $dev = true; |
| 137 | } |
| 138 | if ( $beta && $dev ) { |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | $channels = array(); |
| 145 | if ( $beta || $dev ) { |
| 146 | $channels[ self::CHANNEL_PRODUCTION ] = self::channel_name_by_id( self::CHANNEL_PRODUCTION ); |
| 147 | if ( $beta ) { |
| 148 | $channels[ self::CHANNEL_BETA ] = self::channel_name_by_id( self::CHANNEL_BETA ); |
| 149 | } |
| 150 | if ( $dev ) { |
| 151 | $channels[ self::CHANNEL_DEVELOPMENT ] = self::channel_name_by_id( self::CHANNEL_DEVELOPMENT ); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return $channels; |
| 156 | } |
| 157 | |
| 158 | public function filter_downloads_by_channel( $repository_id, $downloads ) { |
| 159 | |
| 160 | $current_channel = $this->get_channel( $repository_id ); |
| 161 | |
| 162 | foreach ( $downloads as $type => $type_downloads ) { |
| 163 | foreach ( $type_downloads as $slug => $download ) { |
| 164 | |
| 165 | $override_download = array(); |
| 166 | if ( $current_channel === self::CHANNEL_DEVELOPMENT ) { |
| 167 | if( ! empty( $download['channels']['development'] ) ){ |
| 168 | $override_download = $download['channels']['development']; |
| 169 | $override_download['channel'] = self::CHANNEL_DEVELOPMENT; |
| 170 | }elseif( ! empty( $download['channels']['beta'] ) ){ |
| 171 | $override_download = $download['channels']['beta']; |
| 172 | $override_download['channel'] = self::CHANNEL_BETA; |
| 173 | } |
| 174 | }elseif ( $current_channel === self::CHANNEL_BETA && ! empty( $download['channels']['beta'] ) ) { |
| 175 | $override_download = $download['channels']['beta']; |
| 176 | $override_download['channel'] = self::CHANNEL_BETA; |
| 177 | } |
| 178 | |
| 179 | if ( $override_download ) { |
| 180 | foreach ( $override_download as $key => $value ) { |
| 181 | $downloads[ $type ][ $slug ][ $key ] = $value; |
| 182 | } |
| 183 | } else { |
| 184 | $downloads[ $type ][ $slug ]['channel'] = self::CHANNEL_PRODUCTION; |
| 185 | } |
| 186 | unset ( $downloads[ $type ][ $slug ]['channels'] ); |
| 187 | |
| 188 | $downloads[ $type ][ $slug ]['extra_channels'] = array(); |
| 189 | if( isset( $download['channels'] ) ) { |
| 190 | foreach( $download['channels'] as $channel_id => $channel ){ |
| 191 | $downloads[ $type ][ $slug ]['extra_channels'][$channel_id] = array( |
| 192 | 'version' => $channel['version'] |
| 193 | ); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return $downloads; |
| 201 | } |
| 202 | |
| 203 | public function get_download_source_channel( $version, $repository_id, $download_id, $download_kind ) { |
| 204 | |
| 205 | $version_channel = ''; |
| 206 | $installer_settings = WP_Installer()->get_settings(); |
| 207 | if ( isset( $installer_settings['repositories'][ $repository_id ] ) ) { |
| 208 | $repository_data = $installer_settings['repositories'][ $repository_id ]['data']; |
| 209 | if ( isset( $repository_data['downloads'][ $download_kind ][ $download_id ]['extra_channels'] ) ) { |
| 210 | |
| 211 | foreach ( $repository_data['downloads'][ $download_kind ][ $download_id ]['extra_channels'] as $channel_id => $channel_data ) { |
| 212 | if ( $version === $channel_data['version'] ) { |
| 213 | $version_channel = self::channel_name_by_id( $channel_id ); |
| 214 | break; |
| 215 | } |
| 216 | |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | } |
| 221 | |
| 222 | return $version_channel; |
| 223 | } |
| 224 | } |
| 225 |