Commit aa67969d authored by Reinhold Kainhofer's avatar Reinhold Kainhofer

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
/**
* 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 = {
type: "POST",
url: ajax_updatecheck.ajax_url,
data: {
url: ajaxurl,
data: {
action: 'getUpdateCredentialsRow',
slug: slug,
_ajax_nonce: nonce
......@@ -21,22 +24,29 @@ var showUpdateCredentialsRow = function (btn, slug, nonce) {
};
var submitUpdateCredentials = function(btn) {
var tr = jQuery(btn).closest('tr');
var slug = jQuery(tr).data("slug");
var nonce = jQuery(tr).data("nonce");
var order_number = jQuery(tr).find("input[name='otup_update_credentials["+slug+"][order_number]']").val();
var order_pass = jQuery(tr).find("input[name='otup_update_credentials["+slug+"][order_pass]']").val();
var ajaxurl = jQuery(btn).data('ajaxurl');
var slug = jQuery(btn).data("slug");
var nonce = jQuery(btn).data("nonce");
// the credentialvars data field contains a json-encoded array of variables!
var credentialvars = jQuery(btn).data("credentialvars");
var ajaxargs = {
type: "POST",
url: ajax_updatecheck.ajax_url,
data: {
var tr = jQuery(btn).closest('tr');
var data = {
action: 'submitUpdateCredentials',
slug: slug,
_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 ) {
if (json['success']) {
jQuery(tr).find('div.update-credentials-message').html(json['message']);
......@@ -60,7 +70,3 @@ var submitUpdateCredentials = function(btn) {
jQuery.ajax(ajaxargs);
return false;
}
jQuery( function ( $ ) {
$('input.otup_update_credentials_submit').click(submitUpdateCredentials);
});
<?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/
// *****************************************************************
......@@ -11,47 +18,49 @@ if (!class_exists('OpenToolsPluginUpdateChecker')):
require 'plugin-update-checker/plugin-update-checker.php';
class OpenToolsPluginUpdateChecker extends PluginUpdateChecker_2_1 {
protected $credvars = array();
protected $ajaxurl = '';
public function __construct($metadataUrl, $pluginFile, $slug = '', $checkPeriod = 12, $optionName = '', $muPluginFile = '')
{
parent::__construct($metadataUrl, $pluginFile, $slug, $checkPeriod, $optionName, $muPluginFile);
$this->installOTHooks();
}
public function declareCredentials($credential_def) {
$this->credvars = $credential_def;
}
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
$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'), 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_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);
foreach ($this->credvars as $credkey => $credname) {
$credentials[$credkey] = get_option('otup_credentials_'.$slug.'_'.$credkey);
}
$credentials['validated'] = get_option('otup_credentials_validated_'.$slug);
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);
update_option('otup_credentials_order_pass_'.$slug, $order_pass, false);
foreach ($credentials as $credkey => $credvalue) {
update_option('otup_credentials_'.$slug.'_'.$credkey, $credvalue, false);
}
update_option('otup_credentials_validated_'.$slug, $validated, false);
}
......@@ -59,17 +68,6 @@ class OpenToolsPluginUpdateChecker extends PluginUpdateChecker_2_1 {
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() {
......@@ -81,11 +79,8 @@ class OpenToolsPluginUpdateChecker extends PluginUpdateChecker_2_1 {
*/
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'];
foreach ($credentials as $credkey => $credvalue) {
$queryArgs[$credkey] = $credvalue;
}
return $queryArgs;
}
......@@ -105,13 +100,12 @@ class OpenToolsPluginUpdateChecker extends PluginUpdateChecker_2_1 {
$isRelevant = ($pluginFile == $this->pluginFile)
|| (!empty($this->muPluginFile) && $pluginFile == $this->muPluginFile);
if ( $isRelevant && current_user_can('update_plugins') ) {
$credentials = $this->getCredentials($this->slug);
$linkText = apply_filters('otup_enter_update_credentials-' . $this->slug, __('Update Credentials', 'oton-updates'));
if ( !empty($linkText) ) {
$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);
}
}
......@@ -144,17 +138,23 @@ class OpenToolsPluginUpdateChecker extends PluginUpdateChecker_2_1 {
$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 = '<tr class="' . $active_class . ' otup_update_credentials" id="' . esc_attr( $slug . '-credentials' ) . '" >';
$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);" >';
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)))
);
$tr .= '</div>';
$tr .= '</div></th></tr>';
......@@ -182,12 +182,16 @@ class OpenToolsPluginUpdateChecker extends PluginUpdateChecker_2_1 {
if ( $submitCredentials ) {
$ordernumber = $_REQUEST['order_number'];
$orderpass= $_REQUEST['order_pass'];
$credentials = array();
foreach ($this->credvars as $credkey=>$credname) {
if (isset($_REQUEST[$credkey])) {
$credentials[$credkey] = $_REQUEST[$credkey];
}
}
$message = "";
$validated = $this->checkUpdateCredentials($ordernumber, $orderpass, $message);
$this->setCredentials($this->slug, $ordernumber, $orderpass, $validated);
$validated = $this->checkUpdateCredentials($credentials, $message);
$this->setCredentials($this->slug, $credentials, $validated);
$json['success'] = $validated;
......@@ -208,9 +212,9 @@ class OpenToolsPluginUpdateChecker extends PluginUpdateChecker_2_1 {
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;
$updateinfo = $this->requestInfo(array());
if ($updateinfo && isset($updateinfo->download_url)) {
......
......@@ -31,6 +31,10 @@ $myUpdateChecker = new OpenToolsPluginUpdateChecker(
__FILE__,
'woocommerce-advanced-ordernumbers'
);
$myUpdateChecker->declareCredentials(array(
'order_number' => __('Order Number:'),
'order_pass' => __('Order Password:'),
));
// $myUpdateChecker->checkForUpdates();
// *****************************************************************
......@@ -42,7 +46,7 @@ function otaon_is_wc_active() {
}
return
in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) )
||
||
is_plugin_active_for_network( 'woocommerce/woocommerce.php' );
}
......@@ -57,7 +61,6 @@ if ( otaon_is_wc_active() ) {
if (!class_exists("OpenToolsOrdernumbersBasic"))
require_once( plugin_dir_path( __FILE__ ) . '/ordernumbers_woocommerce_basic.php');
// instantiate the plugin class
if (class_exists("OpenToolsOrdernumbers")) {
$ordernumber_plugin = new OpenToolsOrdernumbers(plugin_basename(__FILE__));
......@@ -65,5 +68,4 @@ if ( otaon_is_wc_active() ) {
$ordernumber_plugin = new OpenToolsOrdernumbersBasic(plugin_basename(__FILE__));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment