]> _ Git - physioassist-wordpress.git/blob
706135b9380811f5333b5dc27f585da87c37a6dc
[physioassist-wordpress.git] /
1 <?php
2
3 class OTGS_Installer_Fetch_Subscription {
4
5         private $package_source_factory;
6         private $plugin_finder;
7         private $repositories;
8         private $logger;
9         private $log_factory;
10
11         public function __construct(
12                 OTGS_Installer_Source_Factory $package_source_factory,
13                 OTGS_Installer_Plugin_Finder $plugin_finder,
14                 OTGS_Installer_Repositories $repositories,
15                 OTGS_Installer_Logger $logger,
16                 OTGS_Installer_Log_Factory $log_factory
17         ) {
18                 $this->package_source_factory = $package_source_factory;
19                 $this->plugin_finder          = $plugin_finder;
20                 $this->repositories           = $repositories;
21                 $this->logger                 = $logger;
22                 $this->log_factory            = $log_factory;
23         }
24
25         /**
26          * @param string $repository_id
27          * @param string $site_key
28          * @param string $source
29          *
30          * @return bool|stdClass
31          * @throws OTGS_Installer_Fetch_Subscription_Exception
32          */
33         public function get( $repository_id, $site_key, $source ) {
34                 if ( ! $repository_id || ! $site_key || ! $source ) {
35                         throw new OTGS_Installer_Fetch_Subscription_Exception( 'Repository, site key and source are required fields.' );
36                 }
37
38                 $subscription_data = false;
39
40                 $args['body'] = array(
41                         'action'   => 'site_key_validation',
42                         'site_key' => $site_key,
43                         'site_url' => $this->get_installer_site_url( $repository_id ),
44                         'source'   => $source
45                 );
46
47                 if ( $repository_id === 'wpml' ) {
48                         $args['body']['using_icl']    = function_exists( 'wpml_site_uses_icl' ) && wpml_site_uses_icl();
49                         $args['body']['wpml_version'] = defined( 'ICL_SITEPRESS_VERSION' ) ? ICL_SITEPRESS_VERSION : '';
50                 }
51
52                 $args['body']['installer_version'] = WP_INSTALLER_VERSION;
53                 $args['body']['theme']             = wp_get_theme()->get( 'Name' );
54                 $args['body']['site_name']         = get_bloginfo( 'name' );
55                 $args['body']['repository_id']     = $repository_id;
56                 $args['body']['versions']          = $this->get_local_product_versions();
57                 $args['timeout']                   = 45;
58
59                 $package_source = $this->package_source_factory->create()->get();
60
61                 // Add extra parameters for custom Installer packages
62                 if ( $package_source ) {
63                         $extra = $this->get_extra_url_parameters( $package_source );
64                         if ( ! empty( $extra['repository'] ) && $extra['repository'] == $repository_id ) {
65                                 unset( $extra['repository'] );
66                                 foreach ( $extra as $key => $val ) {
67                                         $args['body'][ $key ] = $val;
68                                 }
69                         }
70                 }
71
72                 $repository = $this->repositories->get( $repository_id );
73
74                 $valid_response = null;
75                 $valid_body     = null;
76                 $api_url        = null;
77
78                 foreach ( array( $repository->get_api_url(), $repository->get_api_url( false ) ) as $api_url ) {
79                         $valid_response = false;
80                         $valid_body     = false;
81
82                         $response = wp_remote_post(
83                                 $api_url,
84                                 apply_filters( 'installer_fetch_subscription_data_request', $args )
85                         );
86
87                         if ( is_wp_error( $response ) ) {
88                                 continue;
89                         }
90
91                         $valid_response = true;
92
93                         $body = trim( wp_remote_retrieve_body( $response ) );
94
95                         if ( ! $body || ! is_serialized( $body ) ) {
96                                 continue;
97                         }
98
99                         $valid_body = true;
100
101                         break;
102                 }
103
104                 $this->logger->add_api_log( "POST {$api_url}" );
105                 $this->logger->add_api_log( $args );
106
107                 $this->logger->add_log( "POST {$api_url} - fetch subscription data" );
108
109                 if ( $valid_response ) {
110                         if ( $valid_body ) {
111                                 $data = unserialize( $body );
112                                 $this->logger->add_api_log( $data );
113
114                                 if ( isset( $data->subscription_data ) && $data->subscription_data ) {
115                                         $subscription_data = $data->subscription_data;
116                                 } else {
117                                         $this->store_log( $args, $api_url, isset( $data->error ) ? $data->error : '' );
118                                 }
119
120                                 do_action( 'installer_fetched_subscription_data', $data, $repository_id );
121                         } else {
122                                 if ( is_wp_error( $response ) ) {
123                                         $response_message = $response->get_error_message();
124                                 } else {
125                                         $response_message = wp_remote_retrieve_response_message( $response );
126                                 }
127                                 $this->store_log( $args, $api_url, $response_message );
128                                 $this->logger->add_api_log( $body );
129                         }
130
131                 } else {
132                         $this->store_log( $args, $api_url, $response->get_error_message() );
133                         $this->logger->add_api_log( $response );
134                         throw new OTGS_Installer_Fetch_Subscription_Exception( $response->get_error_message() );
135                 }
136
137                 return $subscription_data;
138         }
139
140         private function store_log( $args, $request_url, $response ) {
141                 $log = $this->log_factory->create();
142                 $log->set_request_args( $args )
143                     ->set_request_url( $request_url )
144                     ->set_response( $response )
145                     ->set_component( OTGS_Installer_Logger_Storage::COMPONENT_SUBSCRIPTION );
146
147                 $this->logger->save_log( $log );
148         }
149
150         /**
151          * @return array
152          */
153         private function get_local_product_versions() {
154                 $installed_plugins = $this->plugin_finder->get_otgs_installed_plugins();
155                 $versions          = array();
156
157                 foreach ( $installed_plugins as $plugin ) {
158                         $versions[ $plugin->get_slug() ] = $plugin->get_installed_version();
159                 }
160
161                 return $versions;
162         }
163
164         /**
165          * @param string $source
166          *
167          * @return array
168          */
169         private function get_extra_url_parameters( $source ) {
170                 if ( $source ) {
171                         $parameters = $source;
172                 }
173
174                 $parameters['installer_version'] = WP_INSTALLER_VERSION;
175                 $parameters['theme']             = wp_get_theme()->get( 'Name' );
176                 $parameters['site_name']         = get_bloginfo( 'name' );
177
178                 return $parameters;
179         }
180
181         /**
182          * @param bool $repository_id
183          *
184          * @return string
185          */
186         private function get_installer_site_url( $repository_id = false ) {
187                 global $current_site;
188
189                 $site_url = get_site_url();
190
191                 if ( $repository_id && is_multisite() && $this->repositories->get_all() ) {
192                         $network_settings = maybe_unserialize( get_site_option( 'wp_installer_network' ) );
193
194                         if ( isset( $network_settings[ $repository_id ] ) ) {
195                                 $site_url = get_site_url( $current_site->blog_id );
196                         }
197
198                 }
199
200                 $filtered_site_url = filter_var( apply_filters( 'otgs_installer_site_url', $site_url ), FILTER_SANITIZE_URL );
201
202                 return $filtered_site_url ? $filtered_site_url : $site_url;
203         }
204 }