auto_backoff = isset($options['auto_backoff']) ? $options['auto_backoff'] : true; $this->debug = isset($options['debug']) ? $options['debug'] : false; $this->require_login = isset($options['require_login']) ? $options['require_login'] : true; $this->interval_hours = isset($options['interval_hours']) ? $options['interval_hours'] : 24; $this->relative_plugin_file = $relative_plugin_file; $this->slug = dirname($relative_plugin_file); $this->url = trailingslashit($mothership).'?muid='.$muid; $this->muid = $muid; $this->ourdir = dirname(__FILE__); // This needs to exactly match PluginUpdateChecker's view $this->plugin_file = trailingslashit(WP_PLUGIN_DIR).$relative_plugin_file; if (!file_exists($this->plugin_file)) throw new Exception("Plugin file not found: ".$this->plugin_file); add_action('wp_ajax_udmupdater_ajax', array($this, 'udmupdater_ajax')); // Prevent updates from wordpress.org showing in all circumstances. Run with lower than default priority, to allow later processes to add something. add_filter('site_transient_update_plugins', array($this, 'site_transient_update_plugins'), 9); $this->udmupdaterl10n = array( 'duplicate_site_id' => __('This site was previously using a licence also used by another WordPress install with a different URL (which most likely originates from one of the sites being created by duplicating the other) - this has resulted in the licence being disconnected from this site', 'udmupdater') ); // Expiry notices add_action(is_multisite() ? 'network_admin_menu' : 'admin_menu', array($this, 'admin_menu')); $this->option_name = $this->slug.'_updater_options'; if (did_action('init')) { $this->load_text_domain(); } else { add_action('init', array($this, 'load_text_domain')); } $this->get_puc_updates_checker(); add_action("after_plugin_row_$relative_plugin_file", array($this, 'after_plugin_row'), 10, 2 ); add_action('load-plugins.php', array($this, 'load_plugins_php')); add_action('core_upgrade_preamble', array($this, 'core_upgrade_preamble')); /** * Maintain compatibility on all versions between WordPress and UDM, specifically since WordPress 5.5 * Due to the new WP's auto-updates interface in WordPress version 5.5, we need to maintain the auto update compatibility on all versions of WordPress and UDM */ $udm_options = $this->get_option($this->option_name); if (empty($udm_options['wp55_option_migrated'])) { $this->replace_auto_update_option(); } include(ABSPATH.WPINC.'/version.php'); if (version_compare($wp_version, '5.5', '<')) { add_filter('auto_update_plugin', array($this, 'auto_update_plugin'), 20, 2); } add_action('wp_loaded', array($this, 'maybe_force_checking_for_updates')); } /** * Whether to manually force checking for updates by inspecting a specified slug submitted as a query argument (force_udm_check) and build restriction where plugin can only be force checked 2 times a day to prevent abuse * * @return Void */ public function maybe_force_checking_for_updates() { if (!isset($_REQUEST['force_udm_check']) || '' === $_REQUEST['force_udm_check']) return; $slug = sanitize_text_field($_REQUEST['force_udm_check']); if ($this->slug !== $slug) return; header('Content-type: application/json'); $restriction = $this->get_option('udm_manual_check_for_updates_restriction'); if (!is_array($restriction)) $restriction = array(); $time_now = time(); $wp_current_day = $time_now - ($time_now % 86400); if (!isset($restriction[$slug]) || !isset($restriction[$slug]['current_day']) || !is_numeric($restriction[$slug]['current_day']) || !isset($restriction[$slug]['count']) || !is_numeric($restriction[$slug]['count']) || $restriction[$slug]['current_day'] > $wp_current_day || $wp_current_day - $restriction[$slug]['current_day'] > 60*60*24) { // If the restriction option doesn't exist, doesn't have a value, has expired, has invalid data type, one of the required variables doesn't exist, or current day doesn't match with what's in the data then set a new one $restriction[$slug] = array( 'current_day' => $wp_current_day, 'count' => 1 ); } elseif ($wp_current_day == $restriction[$slug]['current_day'] && (int) $restriction[$slug]['count'] < 2) { $restriction[$slug]['count'] = 2; } else { echo json_encode(array( 'code' => 'force_checking_limit_exceeded', 'data' => $restriction[$slug]['count'], )); exit; } $this->update_option('udm_manual_check_for_updates_restriction', $restriction); $this->plug_updatechecker->checkForUpdates(); echo json_encode(array( 'code' => 'success', 'data' => $restriction[$slug]['count'], )); exit; } /** * Generate 'site_host_path' option containing the current site URL, which is used for a cloned site checking purpose in future * * @return Boolean True if site_host_path option existed and has just been updated due to having different site address compared to the currently running site, false if either site_host_path didn't exist and has just been added to the database or site_host_path exists and it has the same site address with the currently running site, or if the site URL couldn't be parsed. */ protected function update_site_host_option() { $udm_options = $this->get_option($this->option_name); $site_url = parse_url(network_site_url()); if (false === $site_url) return false; $site_host_path = isset($site_url['host']) ? $site_url['host'] : ''; $site_host_path .= isset($site_url['path']) ? $site_url['path'] : ''; // if no site_host_path option is set in the database, then add a new one as it will be used for a cloned site checking if (!isset($udm_options['site_host_path'])) { $udm_options['site_host_path'] = $site_host_path; $this->update_option($this->option_name, $udm_options); } elseif (strtolower($udm_options['site_host_path']) !== strtolower($site_host_path)) { $udm_options['site_host_path'] = $site_host_path; $this->update_option($this->option_name, $udm_options); return true; } return false; } /** * Potentially disconnect a cloned site by unsetting the 'email' option and resetting PUC update cache */ protected function potentially_disconnect_cloned_site() { // If it's a cloned site, other plugins that use the same site ID may have still connected. Just unset the email and clear the PUC update cache so that when the users reconnect they get the warning message about the duplicate site ID issue if ($this->update_site_host_option()) { $udm_options = $this->get_option($this->option_name); if (isset($udm_options['email'])) { // Since we're not disconnecting the plugin from the updates server, doing this on cloned sites won't make the entitlement inactive, the user will be able to connect again without getting any issue even the updates server finds the plugin is still connected (active) unset($udm_options['email']); if ($this->plug_updatechecker) $this->plug_updatechecker->resetUpdateState(); $this->update_option($this->option_name, $udm_options); } } } /** * Loading of translations */ public function load_text_domain() { $domain = 'udmupdater'; $locale = apply_filters('udmupdater_locale', (is_admin() && function_exists('get_user_locale')) ? get_user_locale() : get_locale(), $domain, $this); $mo_file = realpath(dirname(__FILE__).'/languages').'/'.$domain.'-'.$locale.'.mo'; if (file_exists($mo_file)) load_textdomain($domain, $mo_file); } /** * Set up the updates checker object. * * If successful, the object will be available as $this->plug_updatechecker */ public function get_puc_updates_checker() { if (!empty($this->plug_updatechecker)) return; // Over-ride update mechanism for the plugin $puc_dir = $this->get_puc_dir(); if (!is_readable($puc_dir.'/plugin-update-checker.php') && !class_exists('YahnisElsts\PluginUpdateChecker\v5\PucFactory')) return; $options = $this->get_option($this->option_name); $email = isset($options['email']) ? $options['email'] : ''; if (!$email && $this->require_login) return; // Load the file even if the PucFactory class is already around, as this may get us a later version / avoid a really old + incompatible one if (file_exists($puc_dir.'/plugin-update-checker.php')) include_once($puc_dir.'/plugin-update-checker.php'); if ($this->auto_backoff) add_filter('puc_check_now-'.$this->slug, array($this, 'puc_check_now'), 10, 3); add_filter('puc_retain_fields-'.$this->slug, array($this, 'puc_retain_fields')); // add_filter('puc_request_info_options-'.$this->slug, array($this, 'puc_request_info_options')); if (class_exists('YahnisElsts\PluginUpdateChecker\v5\PucFactory')) { $this->plug_updatechecker = YahnisElsts\PluginUpdateChecker\v5\PucFactory::buildUpdateChecker($this->url, WP_PLUGIN_DIR.'/'.$this->relative_plugin_file, $this->slug, $this->interval_hours); $this->plug_updatechecker->addQueryArgFilter(array($this, 'updater_queryargs_plugin')); if ($this->debug) $this->plug_updatechecker->debugMode = true; } } /** * Get the directory for the PUC component * * @return String */ private function get_puc_dir() { return file_exists($this->ourdir.'/vendor/yahnis-elsts/plugin-update-checker') ? $this->ourdir.'/vendor/yahnis-elsts/plugin-update-checker' : ( file_exists(dirname(dirname($this->ourdir)).'/yahnis-elsts/plugin-update-checker') ? dirname(dirname($this->ourdir)).'/yahnis-elsts/plugin-update-checker' : $this->ourdir.'/puc'); } /** * WordPress filter - decision on whether to update a plugin * * @param Boolean $update * @param Object $item * * @return Boolean */ public function auto_update_plugin($update, $item) { if (!isset($item->slug) || $item->slug != $this->slug || !$this->allow_auto_updates) return $update; $option_auto_update_settings = (array) get_site_option('auto_update_plugins', array()); $update = in_array($item->plugin, $option_auto_update_settings, true); if ($this->debug) error_log("udm_updater: ".$this->slug." auto update decision: ".$update); return $update; } /** * Process admin-ajax.php requests */ public function udmupdater_ajax() { if (empty($_REQUEST['nonce']) || empty($_REQUEST['subaction']) || !wp_verify_nonce($_REQUEST['nonce'], 'udmupdater-ajax-nonce')) die('Security check.'); // Make sure this request is meant for us if (empty($_REQUEST['userid']) || empty($_REQUEST['slug']) || $this->muid != $_REQUEST['userid'] || $_REQUEST['slug'] != $this->slug) return; $this->update_site_host_option(); if ('connect' == $_REQUEST['subaction'] && current_user_can('update_plugins')) { $this->connect(); die; } elseif ('disconnect' == $_REQUEST['subaction'] && current_user_can('update_plugins')) { $this->disconnect(); die(); } elseif ('dismissexpiry' == $_REQUEST['subaction']) { $option = $this->get_option($this->option_name); if (!is_array($option)) $option = array(); $option['dismissed_until'] = time() + 28*86400; $this->update_option($this->option_name, $option); } elseif ('change_auto_update' == $_REQUEST['subaction']) { $auto_update = empty($_REQUEST['auto_update']) ? false : true; if ($auto_update) { $this->enable_automatic_updates(); } else { $this->disable_automatic_updates(); } echo json_encode(array( 'code' => $auto_update ? 'active' : 'inactive', )); } die; } /** * WordPress action admin_menu */ public function admin_menu() { global $pagenow; // Do we want to display a notice about the upcoming or past expiry of their subscription? if (!empty($this->plug_updatechecker) && !empty($this->plug_updatechecker->optionName) && current_user_can('update_plugins')) { #(!is_multisite() && 'options-general.php' == $pagenow) || (is_multisite() && 'settings.php' == $pagenow) || if ('plugins.php' == $pagenow || 'update-core.php' == $pagenow || (('options-general.php' == $pagenow || 'admin.php' == $pagenow) && !empty($_REQUEST['page']) && $this->slug == $_REQUEST['page'])) { $do_expiry_check = true; $dismiss = ''; } elseif (is_admin()) { $options = $this->get_option($this->option_name); $dismissed_until = empty($options['dismissed_until']) ? 0 : $options['dismissed_until']; if ($dismissed_until <= time()) { $do_expiry_check = true; $dismiss = '
'; } } } $oval = is_object($this->plug_updatechecker) ? get_site_option($this->plug_updatechecker->optionName, null) : null; $updateskey = 'x-spm-expiry'; $supportkey = 'x-spm-support-expiry'; $subscription_active = 'x-spm-subscription-active'; $yourversionkey = 'x-spm-yourversion-tested'; $plugin_title = htmlspecialchars($this->get_plugin_data('Name')); $please_renew = __('please renew', 'udmupdater'); if (is_object($oval) && !empty($oval->update) && is_object($oval->update) && !empty($oval->update->homepage)) { $plugin_title = ''.$plugin_title.''; $please_renew = ''.$please_renew.''; } if (is_object($oval) && !empty($oval->update) && is_object($oval->update) && !empty($oval->update->$yourversionkey) && current_user_can('update_plugins') && true == apply_filters('udmanager_showcompatnotice', true, $this->slug) && (!defined('UDMANAGER_DISABLECOMPATNOTICE') || true != UDMANAGER_DISABLECOMPATNOTICE)) { // Prevent false-positives if (file_exists(dirname($this->plugin_file).'/readme.txt') && $fp = fopen(dirname($this->plugin_file).'/readme.txt', 'r')) { $file_data = fread($fp, 1024); if (preg_match("/Tested up to: (\d+\.\d+).*(\r|\n)/", $file_data, $matches)) { $readme_says = $matches[1]; } fclose($fp); } global $wp_version; include(ABSPATH.WPINC.'/version.php'); $compare_wp_version = (preg_match('/^(\d+\.\d+)\..*$/', $wp_version, $wmatches)) ? $wmatches[1] : $wp_version; $compare_tested_version = $oval->update->$yourversionkey; if (!empty($readme_says) && version_compare($readme_says, $compare_tested_version, '>')) $compare_tested_version = $readme_says; #$compare_tested_version = (preg_match('/^(\d+\.\d+)\.*$/', $oval->update->$yourversionkey, $wmatches)) ? $wmatches[1] : $oval->update->$yourversionkey; if (version_compare($compare_wp_version, $compare_tested_version, '>')) { $this->admin_notices['yourversiontested'] = ''.__('Warning', 'udmupdater').': '.sprintf(__('The installed version of %s has not been tested on your version of WordPress (%s).', 'udmupdater'), $plugin_title, $wp_version).' '.sprintf(__('It has been tested up to version %s.', 'udmupdater'), $compare_tested_version).' '.__('You should update to make sure that you have a version that has been tested for compatibility.', 'udmupdater'); } } if (!empty($do_expiry_check) && is_object($oval) && !empty($oval->update) && is_object($oval->update) && !empty($oval->update->$updateskey)) { if (preg_match('/(^|)expired_?(\d+)?(,|$)/', $oval->update->$updateskey, $matches)) { if (empty($matches[2])) { $this->admin_notices['updatesexpired'] = sprintf(__('Your paid access to %s updates for this site has expired. You will no longer receive updates.', 'udmupdater'), $plugin_title).' '.sprintf(__('To regain access to updates (including future features and compatibility with future WordPress releases) and support, %s.', 'udmupdater'), $please_renew).$dismiss; } else { $this->admin_notices['updatesexpired'] = sprintf(__('Your paid access to %s updates for %s add-ons on this site has expired.', 'udmupdater'), $plugin_title, $matches[2]).' '.sprintf(__('To regain access to updates (including future features and compatibility with future WordPress releases) and support, %s.', $please_renew)).$dismiss; } } // If the licence is expiring soon but they still have an active subscription then we don't want to show the notice. $subscription_active = empty($oval->update->$subscription_active) ? false : $oval->update->$subscription_active; $subscription_status = apply_filters('udmupdater_subscription_active', $subscription_active); if (empty($subscription_status)) { if (preg_match('/(^|,)soonpartial_(\d+)_(\d+)($|,)/', $oval->update->$updateskey, $matches)) { $this->admin_notices['updatesexpiringsoon'] = sprintf(__('Your paid access to %s updates for %s of the %s add-ons on this site will soon expire.', 'udmupdater'), $plugin_title, $matches[2], $matches[3]).' '.sprintf(__('To retain your access, and maintain access to updates (including future features and compatibility with future WordPress releases) and support, %s.', 'udmupdater'), $please_renew).$dismiss; } elseif (preg_match('/(^|,)soon($|,)/', $oval->update->$updateskey)) { $this->admin_notices['updatesexpiringsoon'] = sprintf(__('Your paid access to %s updates for this site will soon expire.', 'udmupdater'), $plugin_title).' '.sprintf(__('To retain your access, and maintain access to updates (including future features and compatibility with future WordPress releases) and support, %s.', 'udmupdater'), $please_renew).''.$dismiss; } } } elseif (!empty($do_expiry_check) && is_object($oval) && !empty($oval->update) && is_object($oval->update) && !empty($oval->update->$supportkey)) { if ('expired' == $oval->update->$supportkey) { $this->admin_notices['supportexpired'] = sprintf(__('Your paid access to %s support has expired.','udmupdater'), $plugin_title).' '.sprintf(__('To regain your access, %s.', 'udmupdater'), $please_renew).$dismiss; } elseif ('soon' == $oval->update->$supportkey) { $this->admin_notices['supportsoonexpiring'] = sprintf(__('Your paid access to %s support will soon expire.','udmupdater'), $plugin_title).' '.sprintf(__('To maintain your access to support, %s.', 'udmupdater'), $please_renew).$dismiss; } } add_action('all_admin_notices', array($this, 'admin_notices')); // Refresh, if specifically requested if (('options-general.php' == $pagenow) || (is_multisite() && 'settings.php' == $pagenow) && isset($_GET['udm_refresh'])) { if ($this->plug_updatechecker) $this->plug_updatechecker->checkForUpdates(); } } /** * WordPress filter puc_retain_fields-(slug) * * @param Array $f * * @return Array */ public function puc_retain_fields($f) { if (!is_array($f)) return $f; if (!in_array('x-spm-yourversion-tested', $f)) $f[] = 'x-spm-yourversion-tested'; if (!in_array('x-spm-expiry', $f)) $f[] = 'x-spm-expiry'; if (!in_array('x-spm-support-expiry', $f)) $f[] = 'x-spm-support-expiry'; if (!in_array('x-spm-subscription-active', $f)) $f[] = 'x-spm-subscription-active'; return $f; } /** * WordPress action admin_notices */ public function admin_notices() { foreach ($this->admin_notices as $key => $notice) { $notice = ''.$notice.''; if (is_numeric($key)) { $this->show_admin_warning($notice); } else { $this->show_admin_warning($notice, 'error'); } } } /** * WordPress action core_upgrade_preamble */ public function core_upgrade_preamble() { if (!current_user_can('update_plugins')) return; $this->potentially_disconnect_cloned_site(); if (!$this->is_connected()) $this->admin_notice_not_connected(); } /** * WordPress action load-plugins.php */ public function load_plugins_php() { if (!current_user_can('update_plugins')) return; $this->potentially_disconnect_cloned_site(); $this->add_admin_notice_if_not_connected(); } /** * Returns the state, as to whether we already have a connection or not * * @return Boolean */ protected function is_connected() { $option = $this->get_option($this->option_name); return empty($option['email']) ? false : true; } /** * Add an admin notice, depending on whether we are currently connected or not */ protected function add_admin_notice_if_not_connected() { if ($this->is_connected()) return; add_action('all_admin_notices', array($this, 'admin_notice_not_connected')); } /** * Output the contents of an admin notice */ public function admin_notice_not_connected() { echo '