opentools-update-checker.php 8.81 KB
Newer Older
1
<?php
2 3 4 5 6 7 8
/**
 * OpenTools Plugin Update Checker Library
 * 
 * Copyright 2016 Reinhold Kainhofer
 * Extends the plugin-update-checker by Janis Elsts
 * Released under the MIT license. 
 */
9 10 11


// *****************************************************************
12
// * PLUGIN UPDATES (using plugin-update-checker and a self-written update server script)
13 14 15 16 17 18 19 20
// * http://w-shadow.com/blog/2010/09/02/automatic-updates-for-any-plugin/
// *****************************************************************

if (!class_exists('OpenToolsPluginUpdateChecker')):

require 'plugin-update-checker/plugin-update-checker.php';

class OpenToolsPluginUpdateChecker extends PluginUpdateChecker_2_1 {
21 22
	protected $credvars = array();
	protected $ajaxurl = '';
23 24 25 26 27
	public function __construct($metadataUrl, $pluginFile, $slug = '', $checkPeriod = 12, $optionName = '', $muPluginFile = '')
	{
		parent::__construct($metadataUrl, $pluginFile, $slug, $checkPeriod, $optionName, $muPluginFile);
		$this->installOTHooks();
	}
28 29 30
	public function declareCredentials($credential_def) {
		$this->credvars = $credential_def;
	}
31 32 33
	
	protected function installOTHooks() 
	{
34 35
		$this->ajaxurl = is_network_admin()?network_admin_url( 'admin-ajax.php' ): admin_url( 'admin-ajax.php' );
		
36 37 38 39 40 41
		// Append the update credentials to the update server link
		$this->addQueryArgFilter(array($this, 'appendQueryArgsCredentials'));
		
		add_action('admin_print_scripts-plugins.php', array($this, 'addCredentialCheckScripts'));
		add_action('admin_print_styles-plugins.php',  array($this, 'addCredentialCheckStyles'));

42 43 44
// 		add_filter('plugin_row_meta', array($this, 'displayUpdateCredentialsLink'), 9, 2);
		add_filter('plugin_action_links_'.$this->pluginFile, array($this, 'displayUpdateCredentialsLink'), 9, 2);

45 46 47 48 49 50 51
		add_action( 'wp_ajax_getUpdateCredentialsRow',		array( &$this, 'getUpdateCredentialsRow') );
		add_action( 'wp_ajax_submitUpdateCredentials',		array( &$this, 'submitUpdateCredentials') );
	}

	protected function getCredentials($slug) 
	{
		$credentials = array('validated' => FALSE);
52 53 54
		foreach ($this->credvars as $credkey => $credname) {
			$credentials[$credkey] = get_option('otup_credentials_'.$slug.'_'.$credkey);
		}
55 56 57 58
		$credentials['validated']    = get_option('otup_credentials_validated_'.$slug);
		return $credentials;
	}
	
59
	protected function setCredentials($slug, $credentials, $validated = false) 
60
	{
61 62 63
		foreach ($credentials as $credkey => $credvalue) {
			update_option('otup_credentials_'.$slug.'_'.$credkey, $credvalue, false);
		}
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
		update_option('otup_credentials_validated_'.$slug,    $validated,   false);
	}


	public function addCredentialCheckScripts() {
		wp_register_script( 'opentools-updatecheck', plugins_url('assets/js/opentools-updatecheck.js', __FILE__), array('jquery'));
		wp_enqueue_script( 'opentools-updatecheck');
	}
	
	public function addCredentialCheckStyles() {
		wp_register_style( 'opentools-updatecheck', plugins_url('assets/css/opentools-updatecheck.css', __FILE__));
		wp_enqueue_style( 'opentools-updatecheck');
	}
	
	/** Append the ordernumber and order password to the update server URL 
	 */
	public function appendQueryArgsCredentials($queryArgs) {
		$credentials = $this->getCredentials($this->slug);
82 83
		foreach ($credentials as $credkey => $credvalue) {
			$queryArgs[$credkey] = $credvalue;
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
		}
		return $queryArgs;
	}

	/**
	 * Add a "Update Credentials" link to the plugin row in the "Plugins" page. By default,
	 * the new link will appear after the "Visit plugin site" link. 
	 *
	 * You can change the link text by using the "otup_enter_update_credentials-$slug" filter.
	 * Returning an empty string from the filter will disable the link.
	 *
	 * @param array $pluginMeta Array of meta links.
	 * @param string $pluginFile
	 * @return array
	 */
99
	public function displayUpdateCredentialsLink($links, $pluginFile) {
100 101 102 103
		$isRelevant = ($pluginFile == $this->pluginFile)
		              || (!empty($this->muPluginFile) && $pluginFile == $this->muPluginFile);

		if ( $isRelevant && current_user_can('update_plugins') ) {
104
			$credentials = $this->getCredentials($this->slug);
105 106
			$linkText = apply_filters('otup_enter_update_credentials-' . $this->slug, __('Update Credentials', 'oton-updates'));
			if ( !empty($linkText) ) {
107
				$iconyesno = $credentials['validated']?'yes':'no';
108
				$link = sprintf('<a href="#" onClick=\'return showUpdateCredentialsRow(this);\' class="dashicons-before dashicons-'.$iconyesno.' otup_credentials_link_'.$this->slug.'" data-slug="%s" data-nonce="%s" data-ajaxurl="%s" >%s</a>', esc_attr($this->slug), esc_attr(wp_create_nonce( 'otup_enter_update_credentials' )), esc_attr($this->ajaxurl), $linkText);
109
				array_unshift($links, $link);
110 111
			}
		}
112
		return $links;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	}
	
	
	/**
	* If the user has clicked on the "Update Credentials" link, display the input boxes after the plugin row.
	*
	* @param string $file
	* @param array  $plugin_data
	* @return false|void
	*/
	public function getUpdateCredentialsRow() {
		$json = array('row' => '', 'message'=>'Unsuccessful');
		
		$showCredentials = isset($_REQUEST['slug'])
			&& $_REQUEST['slug'] == $this->slug
			&& current_user_can('update_plugins')
			&& check_ajax_referer('otup_enter_update_credentials');
			
		if ( $showCredentials && (is_network_admin() || !is_multisite() )) {
			$slug = $this->slug;
			if ( is_network_admin() ) {
				$active_class = is_plugin_active_for_network( $this->pluginFile ) ? 'active': '';
			} else {
				$active_class = is_plugin_active( $this->pluginFile ) ? 'active' : '';
			}
			
			$current_credentials = $this->getCredentials($slug);
			
141
			$tr = '<tr class="' . $active_class . ' otup_update_credentials" id="' . esc_attr( $slug . '-credentials' ) . '" >';
142 143 144 145 146 147
			$tr .= '<th colspan="3" class="check-column colspanchange">';
			$tr .= '<div class="update-credentials">';
			$tr .= '<div class="update-credentials-message">';
			$tr .= '</div>';
			$tr .= '<div class="update-credentials-form">';
			
148 149 150 151 152 153 154 155 156 157
			foreach ($this->credvars as $credkey => $credname) {
				$tr .= $credname    . " <input type=\"text\" name=\"otup_update_credentials[$slug][$credkey]\" value=\"" . esc_attr($current_credentials[$credkey]) . "\">&nbsp;&nbsp;&nbsp;";
			}

			$tr .= sprintf('<input type="submit" class="button otup_update_credentials_submit" onclick="return submitUpdateCredentials(this);" data-slug="%s" data-nonce="%s" data-ajaxurl="%s" data-credentialvars=\'%s\'>',
				esc_attr($this->slug),
				esc_attr(wp_create_nonce( 'otup_enter_update_credentials_'.$slug )),
				esc_attr($this->ajaxurl),
				esc_attr(json_encode(array_keys($this->credvars)))
			);
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

			$tr .= '</div>';
			$tr .= '</div></th></tr>';
			$json['row'] = $tr;
			$json['message'] = '';
		} else {
			$json['message'] = __("No permissions to modify update credentials", "opentools-updatecheck");
		}
		wp_send_json($json);
	}


	/**
	 * Check the submitted update credentials for correctness and save them
	 *
	 * @return void
	 */
	public function submitUpdateCredentials() {
		$json = array('message' => '', 'success' => FALSE);
		$slug = isset($_REQUEST['slug'])?($_REQUEST['slug']):"INVALIDSLUG";
		
		$submitCredentials = $slug == $this->slug
			&& current_user_can('update_plugins')
			&& check_ajax_referer('otup_enter_update_credentials_'.$slug);
			

		if ( $submitCredentials ) {
185 186 187 188 189 190
			$credentials = array();
			foreach ($this->credvars as $credkey=>$credname) {
				if (isset($_REQUEST[$credkey])) {
					$credentials[$credkey] = $_REQUEST[$credkey];
				}
			}
191 192
			
			$message = "";
193 194
			$validated = $this->checkUpdateCredentials($credentials, $message);
			$this->setCredentials($this->slug, $credentials, $validated);
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
			
			$json['success'] = $validated;
			
			if ($validated) {
				if ( is_network_admin() ) {
					$active_class = is_plugin_active_for_network( $this->pluginFile ) ? 'active': '';
				} else {
					$active_class = is_plugin_active( $this->pluginFile ) ? 'active' : '';
				}

				$json['message'] .= __("Update credentials successfully validated and saved. Automatic updates will be provided.", "opentools-updatecheck");
			} else {
				$json['message'] = $message;
			}
		} else {
			$json['message'] = __("No permissions to modify update credentials", "opentools-updatecheck");
		}
		wp_send_json($json);
	}

215
	public function checkUpdateCredentials($credentials, &$message)
216
	{
217
		$this->setCredentials($this->slug, $credentials);
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
		$success = FALSE;
		$updateinfo = $this->requestInfo(array());
		if ($updateinfo && isset($updateinfo->download_url)) {
			$downloadurl = $updateinfo->download_url;
			$downloadurl .= (parse_url($downloadurl, PHP_URL_QUERY) ? '&' : '?') . 'check_access=1';
		
			$headers = get_headers($downloadurl);
			list($version, $status_code, $msg) = explode(' ',$headers[0], 3);
		
			// Check the HTTP Status code
			$message = $msg;
			$success = ($status_code==200);
		} else {
			$message = __('Unable to access plugin download URL. Please check your credentials.');
		}
		return $success;
	}

};

endif;



// *****************************************************************