Skip to content
Snippets Groups Projects
Commit aa67969d authored by Reinhold Kainhofer's avatar Reinhold Kainhofer
Browse files

Move all credential check data into html data- attributes so that the JS will...

Move all credential check data into html data- attributes so that the JS will also work with multiple plugins. The AJAX calls will simply retrieve the data from the button's attributes, including the ajax url and the list of possible credentials
parent 08141960
Branches
No related tags found
No related merge requests found
/** /**
* Ordernumber Admin JS * OpenTools Update Check Admin JS
*/ */
var showUpdateCredentialsRow = function (btn, slug, nonce) { var showUpdateCredentialsRow = function (btn) {
var ajaxurl = jQuery(btn).data('ajaxurl');
var slug = jQuery(btn).data("slug");
var nonce = jQuery(btn).data("nonce");
var ajaxargs = { var ajaxargs = {
type: "POST", type: "POST",
url: ajax_updatecheck.ajax_url, url: ajaxurl,
data: { data: {
action: 'getUpdateCredentialsRow', action: 'getUpdateCredentialsRow',
slug: slug, slug: slug,
_ajax_nonce: nonce _ajax_nonce: nonce
...@@ -21,22 +24,29 @@ var showUpdateCredentialsRow = function (btn, slug, nonce) { ...@@ -21,22 +24,29 @@ var showUpdateCredentialsRow = function (btn, slug, nonce) {
}; };
var submitUpdateCredentials = function(btn) { var submitUpdateCredentials = function(btn) {
var tr = jQuery(btn).closest('tr'); var ajaxurl = jQuery(btn).data('ajaxurl');
var slug = jQuery(tr).data("slug"); var slug = jQuery(btn).data("slug");
var nonce = jQuery(tr).data("nonce"); var nonce = jQuery(btn).data("nonce");
var order_number = jQuery(tr).find("input[name='otup_update_credentials["+slug+"][order_number]']").val(); // the credentialvars data field contains a json-encoded array of variables!
var order_pass = jQuery(tr).find("input[name='otup_update_credentials["+slug+"][order_pass]']").val(); var credentialvars = jQuery(btn).data("credentialvars");
var ajaxargs = { var tr = jQuery(btn).closest('tr');
type: "POST", var data = {
url: ajax_updatecheck.ajax_url,
data: {
action: 'submitUpdateCredentials', action: 'submitUpdateCredentials',
slug: slug, slug: slug,
_ajax_nonce: nonce, _ajax_nonce: nonce,
order_number: order_number, };
order_pass: order_pass
}, var index;
for (index = 0; index < credentialvars.length; index++) {
var credname = credentialvars[index];
data[credname] = jQuery(tr).find("input[name='otup_update_credentials["+slug+"]["+credname+"]']").val();
}
var ajaxargs = {
type: "POST",
url: ajaxurl,
data: data,
success: function ( json ) { success: function ( json ) {
if (json['success']) { if (json['success']) {
jQuery(tr).find('div.update-credentials-message').html(json['message']); jQuery(tr).find('div.update-credentials-message').html(json['message']);
...@@ -60,7 +70,3 @@ var submitUpdateCredentials = function(btn) { ...@@ -60,7 +70,3 @@ var submitUpdateCredentials = function(btn) {
jQuery.ajax(ajaxargs); jQuery.ajax(ajaxargs);
return false; return false;
} }
jQuery( function ( $ ) {
$('input.otup_update_credentials_submit').click(submitUpdateCredentials);
});
<?php <?php
/**
* OpenTools Plugin Update Checker Library
*
* Copyright 2016 Reinhold Kainhofer
* Extends the plugin-update-checker by Janis Elsts
* Released under the MIT license.
*/
// ***************************************************************** // *****************************************************************
// * PLUGIN UPDATES (using plugin-update-checker and a self-written update server script // * 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/ // * http://w-shadow.com/blog/2010/09/02/automatic-updates-for-any-plugin/
// ***************************************************************** // *****************************************************************
...@@ -11,47 +18,49 @@ if (!class_exists('OpenToolsPluginUpdateChecker')): ...@@ -11,47 +18,49 @@ if (!class_exists('OpenToolsPluginUpdateChecker')):
require 'plugin-update-checker/plugin-update-checker.php'; require 'plugin-update-checker/plugin-update-checker.php';
class OpenToolsPluginUpdateChecker extends PluginUpdateChecker_2_1 { class OpenToolsPluginUpdateChecker extends PluginUpdateChecker_2_1 {
protected $credvars = array();
protected $ajaxurl = '';
public function __construct($metadataUrl, $pluginFile, $slug = '', $checkPeriod = 12, $optionName = '', $muPluginFile = '') public function __construct($metadataUrl, $pluginFile, $slug = '', $checkPeriod = 12, $optionName = '', $muPluginFile = '')
{ {
parent::__construct($metadataUrl, $pluginFile, $slug, $checkPeriod, $optionName, $muPluginFile); parent::__construct($metadataUrl, $pluginFile, $slug, $checkPeriod, $optionName, $muPluginFile);
$this->installOTHooks(); $this->installOTHooks();
} }
public function declareCredentials($credential_def) {
$this->credvars = $credential_def;
}
protected function installOTHooks() protected function installOTHooks()
{ {
$this->ajaxurl = is_network_admin()?network_admin_url( 'admin-ajax.php' ): admin_url( 'admin-ajax.php' );
// Append the update credentials to the update server link // Append the update credentials to the update server link
$this->addQueryArgFilter(array($this, 'appendQueryArgsCredentials')); $this->addQueryArgFilter(array($this, 'appendQueryArgsCredentials'));
add_action('admin_print_scripts-plugins.php', array($this, 'addCredentialCheckScripts')); add_action('admin_print_scripts-plugins.php', array($this, 'addCredentialCheckScripts'));
add_action('admin_print_styles-plugins.php', array($this, 'addCredentialCheckStyles')); add_action('admin_print_styles-plugins.php', array($this, 'addCredentialCheckStyles'));
// add_filter('plugin_row_meta', array($this, 'displayUpdateCredentialsLink'), 9, 2); // add_filter('plugin_row_meta', array($this, 'displayUpdateCredentialsLink'), 9, 2);
add_filter('plugin_action_links_'.$this->pluginFile, array($this, 'displayUpdateCredentialsLink'), 9, 2); add_filter('plugin_action_links_'.$this->pluginFile, array($this, 'displayUpdateCredentialsLink'), 9, 2);
add_action( 'wp_ajax_getUpdateCredentialsRow', array( &$this, 'getUpdateCredentialsRow') ); add_action( 'wp_ajax_getUpdateCredentialsRow', array( &$this, 'getUpdateCredentialsRow') );
add_action( 'wp_ajax_submitUpdateCredentials', array( &$this, 'submitUpdateCredentials') ); 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) protected function getCredentials($slug)
{ {
$credentials = array('validated' => FALSE); $credentials = array('validated' => FALSE);
$credentials['order_number'] = get_option('otup_credentials_order_number_'.$slug); foreach ($this->credvars as $credkey => $credname) {
$credentials['order_pass'] = get_option('otup_credentials_order_pass_'.$slug); $credentials[$credkey] = get_option('otup_credentials_'.$slug.'_'.$credkey);
}
$credentials['validated'] = get_option('otup_credentials_validated_'.$slug); $credentials['validated'] = get_option('otup_credentials_validated_'.$slug);
return $credentials; return $credentials;
} }
protected function setCredentials($slug, $order_number, $order_pass, $validated = false) protected function setCredentials($slug, $credentials, $validated = false)
{ {
update_option('otup_credentials_order_number_'.$slug, $order_number,false); foreach ($credentials as $credkey => $credvalue) {
update_option('otup_credentials_order_pass_'.$slug, $order_pass, false); update_option('otup_credentials_'.$slug.'_'.$credkey, $credvalue, false);
}
update_option('otup_credentials_validated_'.$slug, $validated, false); update_option('otup_credentials_validated_'.$slug, $validated, false);
} }
...@@ -59,17 +68,6 @@ class OpenToolsPluginUpdateChecker extends PluginUpdateChecker_2_1 { ...@@ -59,17 +68,6 @@ class OpenToolsPluginUpdateChecker extends PluginUpdateChecker_2_1 {
public function addCredentialCheckScripts() { public function addCredentialCheckScripts() {
wp_register_script( 'opentools-updatecheck', plugins_url('assets/js/opentools-updatecheck.js', __FILE__), array('jquery')); wp_register_script( 'opentools-updatecheck', plugins_url('assets/js/opentools-updatecheck.js', __FILE__), array('jquery'));
wp_enqueue_script( 'opentools-updatecheck'); 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() { public function addCredentialCheckStyles() {
...@@ -81,11 +79,8 @@ class OpenToolsPluginUpdateChecker extends PluginUpdateChecker_2_1 { ...@@ -81,11 +79,8 @@ class OpenToolsPluginUpdateChecker extends PluginUpdateChecker_2_1 {
*/ */
public function appendQueryArgsCredentials($queryArgs) { public function appendQueryArgsCredentials($queryArgs) {
$credentials = $this->getCredentials($this->slug); $credentials = $this->getCredentials($this->slug);
if (isset($credentials['order_number'])) { foreach ($credentials as $credkey => $credvalue) {
$queryArgs['order_number'] = $credentials['order_number']; $queryArgs[$credkey] = $credvalue;
}
if (isset($credentials['order_pass'])) {
$queryArgs['order_pass'] = $credentials['order_pass'];
} }
return $queryArgs; return $queryArgs;
} }
...@@ -105,13 +100,12 @@ class OpenToolsPluginUpdateChecker extends PluginUpdateChecker_2_1 { ...@@ -105,13 +100,12 @@ class OpenToolsPluginUpdateChecker extends PluginUpdateChecker_2_1 {
$isRelevant = ($pluginFile == $this->pluginFile) $isRelevant = ($pluginFile == $this->pluginFile)
|| (!empty($this->muPluginFile) && $pluginFile == $this->muPluginFile); || (!empty($this->muPluginFile) && $pluginFile == $this->muPluginFile);
if ( $isRelevant && current_user_can('update_plugins') ) { if ( $isRelevant && current_user_can('update_plugins') ) {
$credentials = $this->getCredentials($this->slug); $credentials = $this->getCredentials($this->slug);
$linkText = apply_filters('otup_enter_update_credentials-' . $this->slug, __('Update Credentials', 'oton-updates')); $linkText = apply_filters('otup_enter_update_credentials-' . $this->slug, __('Update Credentials', 'oton-updates'));
if ( !empty($linkText) ) { if ( !empty($linkText) ) {
$iconyesno = $credentials['validated']?'yes':'no'; $iconyesno = $credentials['validated']?'yes':'no';
$link = sprintf('<a href="#" onClick=\'return showUpdateCredentialsRow(this, "%s", "%s");\' class="dashicons-before dashicons-'.$iconyesno.' otup_credentials_link_'.$this->slug.'">%s</a>', esc_attr($this->slug), esc_attr(wp_create_nonce( 'otup_enter_update_credentials' )), $linkText); $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);
array_unshift($links, $link); array_unshift($links, $link);
} }
} }
...@@ -144,17 +138,23 @@ class OpenToolsPluginUpdateChecker extends PluginUpdateChecker_2_1 { ...@@ -144,17 +138,23 @@ class OpenToolsPluginUpdateChecker extends PluginUpdateChecker_2_1 {
$current_credentials = $this->getCredentials($slug); $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' ) . '" >';
$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 .= '<th colspan="3" class="check-column colspanchange">';
$tr .= '<div class="update-credentials">'; $tr .= '<div class="update-credentials">';
$tr .= '<div class="update-credentials-message">'; $tr .= '<div class="update-credentials-message">';
$tr .= '</div>'; $tr .= '</div>';
$tr .= '<div class="update-credentials-form">'; $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;"; foreach ($this->credvars as $credkey => $credname) {
$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 .= $credname . " <input type=\"text\" name=\"otup_update_credentials[$slug][$credkey]\" value=\"" . esc_attr($current_credentials[$credkey]) . "\">&nbsp;&nbsp;&nbsp;";
$tr .= '<input type="submit" class="button otup_update_credentials_submit" onclick="return submitUpdateCredentials(this);" >'; }
$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)))
);
$tr .= '</div>'; $tr .= '</div>';
$tr .= '</div></th></tr>'; $tr .= '</div></th></tr>';
...@@ -182,12 +182,16 @@ class OpenToolsPluginUpdateChecker extends PluginUpdateChecker_2_1 { ...@@ -182,12 +182,16 @@ class OpenToolsPluginUpdateChecker extends PluginUpdateChecker_2_1 {
if ( $submitCredentials ) { if ( $submitCredentials ) {
$ordernumber = $_REQUEST['order_number']; $credentials = array();
$orderpass= $_REQUEST['order_pass']; foreach ($this->credvars as $credkey=>$credname) {
if (isset($_REQUEST[$credkey])) {
$credentials[$credkey] = $_REQUEST[$credkey];
}
}
$message = ""; $message = "";
$validated = $this->checkUpdateCredentials($ordernumber, $orderpass, $message); $validated = $this->checkUpdateCredentials($credentials, $message);
$this->setCredentials($this->slug, $ordernumber, $orderpass, $validated); $this->setCredentials($this->slug, $credentials, $validated);
$json['success'] = $validated; $json['success'] = $validated;
...@@ -208,9 +212,9 @@ class OpenToolsPluginUpdateChecker extends PluginUpdateChecker_2_1 { ...@@ -208,9 +212,9 @@ class OpenToolsPluginUpdateChecker extends PluginUpdateChecker_2_1 {
wp_send_json($json); wp_send_json($json);
} }
public function checkUpdateCredentials($ordernumber, $orderpass, &$message) public function checkUpdateCredentials($credentials, &$message)
{ {
$this->setCredentials($this->slug, $ordernumber, $orderpass); $this->setCredentials($this->slug, $credentials);
$success = FALSE; $success = FALSE;
$updateinfo = $this->requestInfo(array()); $updateinfo = $this->requestInfo(array());
if ($updateinfo && isset($updateinfo->download_url)) { if ($updateinfo && isset($updateinfo->download_url)) {
......
...@@ -31,6 +31,10 @@ $myUpdateChecker = new OpenToolsPluginUpdateChecker( ...@@ -31,6 +31,10 @@ $myUpdateChecker = new OpenToolsPluginUpdateChecker(
__FILE__, __FILE__,
'woocommerce-advanced-ordernumbers' 'woocommerce-advanced-ordernumbers'
); );
$myUpdateChecker->declareCredentials(array(
'order_number' => __('Order Number:'),
'order_pass' => __('Order Password:'),
));
// $myUpdateChecker->checkForUpdates(); // $myUpdateChecker->checkForUpdates();
// ***************************************************************** // *****************************************************************
...@@ -42,7 +46,7 @@ function otaon_is_wc_active() { ...@@ -42,7 +46,7 @@ function otaon_is_wc_active() {
} }
return return
in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) )
|| ||
is_plugin_active_for_network( 'woocommerce/woocommerce.php' ); is_plugin_active_for_network( 'woocommerce/woocommerce.php' );
} }
...@@ -57,7 +61,6 @@ if ( otaon_is_wc_active() ) { ...@@ -57,7 +61,6 @@ if ( otaon_is_wc_active() ) {
if (!class_exists("OpenToolsOrdernumbersBasic")) if (!class_exists("OpenToolsOrdernumbersBasic"))
require_once( plugin_dir_path( __FILE__ ) . '/ordernumbers_woocommerce_basic.php'); require_once( plugin_dir_path( __FILE__ ) . '/ordernumbers_woocommerce_basic.php');
// instantiate the plugin class // instantiate the plugin class
if (class_exists("OpenToolsOrdernumbers")) { if (class_exists("OpenToolsOrdernumbers")) {
$ordernumber_plugin = new OpenToolsOrdernumbers(plugin_basename(__FILE__)); $ordernumber_plugin = new OpenToolsOrdernumbers(plugin_basename(__FILE__));
...@@ -65,5 +68,4 @@ if ( otaon_is_wc_active() ) { ...@@ -65,5 +68,4 @@ if ( otaon_is_wc_active() ) {
$ordernumber_plugin = new OpenToolsOrdernumbersBasic(plugin_basename(__FILE__)); $ordernumber_plugin = new OpenToolsOrdernumbersBasic(plugin_basename(__FILE__));
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment