opentools-update-checker.php 8.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
<?php


// *****************************************************************
// * PLUGIN UPDATES (using plugin-update-checker and a self-written update server script
// * 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 {
	public function __construct($metadataUrl, $pluginFile, $slug = '', $checkPeriod = 12, $optionName = '', $muPluginFile = '')
	{
		parent::__construct($metadataUrl, $pluginFile, $slug, $checkPeriod, $optionName, $muPluginFile);
		$this->installOTHooks();
	}
	
	protected function installOTHooks() 
	{
		// 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'));


		add_filter('plugin_row_meta', array($this, 'displayUpdateCredentialsLink'), 10, 2);

		add_action( 'wp_ajax_getUpdateCredentialsRow',		array( &$this, 'getUpdateCredentialsRow') );
		add_action( 'wp_ajax_submitUpdateCredentials',		array( &$this, 'submitUpdateCredentials') );
		
		
// 		add_action('after_plugin_row_'.$this->pluginFile, array($this, 'displayUpdateCredentialsRow'), 10, 2);
// 		add_action('admin_init', array($this, 'checkSubmittedUpdateCredentials'));
// 		add_action('all_admin_notices', array($this, 'displayCredentialsCheckResult'));
	}

	protected function getCredentials($slug) 
	{
		$credentials = array('validated' => FALSE);
		$credentials['order_number'] = get_option('otup_credentials_order_number_'.$slug);
		$credentials['order_pass']   = get_option('otup_credentials_order_pass_'.$slug);
		$credentials['validated']    = get_option('otup_credentials_validated_'.$slug);
		return $credentials;
	}
	
	protected function setCredentials($slug, $order_number, $order_pass, $validated = false) 
	{
		update_option('otup_credentials_order_number_'.$slug, $order_number,false);
		update_option('otup_credentials_order_pass_'.$slug,   $order_pass,  false);
		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');
		
		// Handle the translations:
		// Check for MS dashboard
		if( is_network_admin() )
			$url = network_admin_url( 'admin-ajax.php' );
		else
			$url = admin_url( 'admin-ajax.php' );
		$localizations = array( 'ajax_url' => $url );

		// in JavaScript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value
		wp_localize_script( 'opentools-updatecheck', 'ajax_updatecheck', $localizations );
	}
	
	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);
		if (isset($credentials['order_number'])) {
			$queryArgs['order_number'] = $credentials['order_number'];
		}
		if (isset($credentials['order_pass'])) {
			$queryArgs['order_pass'] = $credentials['order_pass'];
		}
		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
	 */
	public function displayUpdateCredentialsLink($pluginMeta, $pluginFile) {
		$isRelevant = ($pluginFile == $this->pluginFile)
		              || (!empty($this->muPluginFile) && $pluginFile == $this->muPluginFile);

		if ( $isRelevant && current_user_can('update_plugins') ) {
			$linkText = apply_filters('otup_enter_update_credentials-' . $this->slug, __('Update Credentials', 'oton-updates'));
			if ( !empty($linkText) ) {
				$pluginMeta[] = sprintf('<a href="#" onClick=\'return showUpdateCredentialsRow(this, "%s", "%s");\' >%s</a>', esc_attr($this->slug), esc_attr(wp_create_nonce( 'otup_enter_update_credentials' )), $linkText);
			}
		}
		return $pluginMeta;
	}
	
	
	/**
	* 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);
			
			// TODO: Remove the arrows icon from the field:
			$tr = '<tr class="' . $active_class . ' otup_update_credentials" id="' . esc_attr( $slug . '-credentials' ) . '" data-slug="' . esc_attr( $slug ) . '" data-nonce="' . esc_attr( wp_create_nonce( 'otup_enter_update_credentials_'.$slug ) ) . '">';
			$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">';
			
			$tr .= __('Order Number:')   . " <input type=\"text\" name=\"otup_update_credentials[$slug][order_number]\" value=\"" . esc_attr($current_credentials['order_number']) . "\">&nbsp;&nbsp;&nbsp;&nbsp;";
			$tr .= __('Order Password:') . " <input type=\"text\" name=\"otup_update_credentials[$slug][order_pass]\"   value=\"" . esc_attr($current_credentials['order_pass']) .   "\">&nbsp;&nbsp;&nbsp;&nbsp;";
			$tr .= '<input type="submit" class="button otup_update_credentials_submit" onclick="return submitUpdateCredentials(this);" >';

			$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 ) {
			$ordernumber = $_REQUEST['order_number'];
			$orderpass= $_REQUEST['order_pass'];
			
			$message = "";
			$validated = $this->checkUpdateCredentials($ordernumber, $orderpass, $message);
			$this->setCredentials($this->slug, $ordernumber, $orderpass, $validated);
			
			$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);
	}

	public function checkUpdateCredentials($ordernumber, $orderpass, &$message)
	{
		$this->setCredentials($this->slug, $ordernumber, $orderpass);
		$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;



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