function fbrfg_clean_site_instance() { // Remove options foreach (Favicon_By_RealFaviconGenerator_Common::get_options_list() as $opt) { delete_option($opt); } // Remove files $dir = Favicon_By_RealFaviconGenerator_Common::get_files_dir(); if (file_exists($dir)) { Favicon_By_RealFaviconGenerator_Common::remove_directory($dir); } }
public function install_new_favicon() { header("Content-type: application/json"); try { // URL is explicitely decoded to compensate the extra encoding performed while generating the settings page $url = 'https://realfavicongenerator.net' . $_REQUEST['json_result_url']; $result = $this->download_result_json($url); $response = new Favicon_By_RealFaviconGenerator_Api_Response($result); if (!wp_verify_nonce($response->getCustomParameter(), Favicon_By_RealFaviconGenerator_Admin::NONCE_ACTION_NAME)) { // Attack in progress? ?> { "status": "error", "message": "<?php _e('Nonce was not recognized. This case is supposed to happen only in case of XSS attack. If you feel like something is wrong, please <a href=\\"mailto:contact@realfavicongenerator.net\\">contact us</a>.', FBRFG_PLUGIN_SLUG); ?> " } <?php } else { $zip_path = Favicon_By_RealFaviconGenerator_Common::get_tmp_dir(); if (!file_exists($zip_path)) { if (mkdir($zip_path, 0755, true) !== true) { throw new InvalidArgumentException(sprintf(__('Cannot create directory %s to store the favicon package', FBRFG_PLUGIN_SLUG), $zip_path)); } } $response->downloadAndUnpack($zip_path); $this->store_pictures($response); $this->store_preview($response->getPreviewPath()); Favicon_By_RealFaviconGenerator_Common::remove_directory($zip_path); update_option(Favicon_By_RealFaviconGenerator_Common::OPTION_HTML_CODE, $response->getHtmlCode()); $this->set_favicon_configured(true, $response->isFilesInRoot(), $response->getVersion(), $response->getNonInteractiveAPIRequest()); ?> { "status": "success", "preview_url": <?php echo json_encode($this->get_preview_url()); ?> , "favicon_in_root": <?php echo json_encode($this->is_favicon_in_root()); ?> } <?php } } catch (Exception $e) { ?> { "status": "error", "message": <?php echo json_encode($e->getMessage()); ?> } <?php } die; }
public function check_for_updates() { $this->log_info('Checking for updates...'); if (!$this->is_favicon_configured()) { // No favicon so nothing to update $this->log_info('No favicon configured so nothing to update'); return; } $version = $this->get_favicon_current_version(); $this->log_info('Current version is ' . $version); if ($version == NULL) { // No version for some reason. Let's leave. $this->log_info('Current favicon version not available'); return; } $checkUrl = 'https://realfavicongenerator.net/api/versions?since=' . $version; $resp = wp_remote_get($checkUrl); if ($resp == NULL || $resp == false || is_wp_error($resp) || $resp['response'] == NULL || $resp['response']['code'] == NULL || $resp['response']['code'] != 200) { // Error of some kind? Return $this->log_info('Cannot get latest version from RealFaviconGenerator ' . (is_wp_error($resp) ? ': ' . $resp->get_error_message() : '') . ' (URL was ' . $checkUrl . ')'); return; } $json = json_decode($resp['body'], true); if (empty($json)) { $this->log_info('No change since version ' . $version . ' or cannot parse JSON (JSON parsing error code is ' . json_last_error() . ')'); return; } $this->log_info('Update versions database'); $this->set_updates_data($json); // Check update relevancy $manual = false; $automatic = false; foreach ($json as $version) { if ($version['relevance']['manual_update']) { $manual = true; } if ($version['relevance']['automated_update']) { $automatic = true; } } $this->log_info("Automated updates to run: " . ($automatic ? 'true' : 'false')); $this->log_info("Manual updates to run: " . ($manual ? 'true' : 'false')); if ($manual || $automatic && !$this->get_non_interactive_api_request()) { $this->log_info('Manual update, or automatic update but we have no non-interactive request to do it automatically'); // We only note the latest available version. // For example, if we receive version 0.8, 0.9 and 0.10 (in this order), we only note 0.10 $last = $json[count($json) - 1]; $latest_version = $last['version']; // Save the fact that we should update $this->log_info('There is a manual update to go to ' . $latest_version); $this->set_latest_manual_available_update($latest_version); } else { if ($automatic && $this->get_non_interactive_api_request()) { // Automatic update $this->log_info('Automatic update to be performed'); // Do not run it when the update is also manual, because we are going to ask the user to // update anyway. try { $result = $this->run_non_interactive_api_request($this->get_non_interactive_api_request()); $response = new Favicon_By_RealFaviconGenerator_Api_Response($result); $zip_path = Favicon_By_RealFaviconGenerator_Common::get_tmp_dir(); if (!file_exists($zip_path)) { if (mkdir($zip_path, 0755, true) !== true) { throw new InvalidArgumentException(sprintf(__('Cannot create directory %s to store the favicon package', FBRFG_PLUGIN_SLUG), $zip_path)); } } $response->downloadAndUnpack($zip_path); $this->store_pictures($response); $this->store_preview($response->getPreviewPath()); Favicon_By_RealFaviconGenerator_Common::remove_directory($zip_path); update_option(Favicon_By_RealFaviconGenerator_Common::OPTION_HTML_CODE, $response->getHtmlCode()); $version_before_update = $this->get_favicon_current_version(); $this->set_favicon_configured(true, $response->isFilesInRoot(), $response->getVersion()); // Remember about this update $this->log_info('Save the fact that we updated from ' . $version_before_update . ' to ' . $response->getVersion()); $this->set_most_recent_automatic_update($version_before_update, $response->getVersion()); } catch (Exception $e) { $this->log_info('Cannot update favicon automatically: ' . $e->getMessage()); } } } }