Skip to content

Commit 5c222fa

Browse files
committed
👌 IMPROVE: Updated plugin serve updates from GitHub instead of wp.org
1 parent 7ea10b2 commit 5c222fa

File tree

116 files changed

+11059
-23
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+11059
-23
lines changed

admin/js/clwc-admin.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
jQuery(document).ready(function($) {
1+
jQuery(function($) {
22
$('.clwc-loyalty-points').on('change', function() {
33
var $input = $(this);
44
var userID = $input.data('user-id');
@@ -22,7 +22,7 @@ jQuery(document).ready(function($) {
2222
$input.css('border-color', 'green');
2323
} else {
2424
$input.css('border-color', 'red');
25-
alert(response.data || 'An error occurred');
25+
alert(response.data.message || 'An error occurred');
2626
}
2727
},
2828
error: function(jqXHR, textStatus, errorThrown) {

customer-loyalty-for-woocommerce.php

+51
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
2121
* Text Domain: customer-loyalty-for-woocommerce
2222
* Domain Path: /languages
23+
* Update URI: https://github.com/deviodigital/customer-loyalty-for-woocommerce/
2324
*/
2425

2526
// If this file is called directly, abort.
@@ -279,3 +280,53 @@ function clwc_redeem_points_callback() {
279280
wp_send_json_success( ['html' => $new_coupon_html, 'updated_points' => $updated_points] );
280281
}
281282
add_action( 'wp_ajax_clwc_redeem_points', 'clwc_redeem_points_callback' );
283+
284+
/**
285+
* AJAX handler to update loyalty points for a specific user.
286+
*
287+
* @since 2.0.0
288+
* @return void
289+
*/
290+
function clwc_update_loyalty_points_callback() {
291+
// Verify the nonce for security.
292+
if ( ! check_ajax_referer( 'clwc_nonce', 'security', false ) ) {
293+
wp_send_json_error( [ 'message' => __( 'Nonce verification failed', 'customer-loyalty-for-woocommerce' ) ] );
294+
return;
295+
}
296+
297+
// Retrieve and sanitize data from the AJAX request.
298+
$user_id = isset( $_POST['user_id'] ) ? absint( $_POST['user_id'] ) : 0;
299+
$new_points = isset( $_POST['points'] ) ? intval( $_POST['points'] ) : 0;
300+
301+
if ( ! $user_id ) {
302+
wp_send_json_error( [ 'message' => __( 'Invalid user ID.', 'customer-loyalty-for-woocommerce' ) ] );
303+
return;
304+
}
305+
306+
// Get current points and calculate the change.
307+
$current_points = (int) get_user_meta( $user_id, 'clwc_loyalty_points', true );
308+
$points_change = $new_points - $current_points;
309+
310+
// Update the user's points.
311+
update_user_meta( $user_id, 'clwc_loyalty_points', $new_points );
312+
313+
// Get the admin username who made the change.
314+
$admin_user = wp_get_current_user();
315+
$admin_name = $admin_user->display_name;
316+
317+
// Log the change in the loyalty log table.
318+
$user_info = get_userdata( $user_id );
319+
$user_name = $user_info->display_name;
320+
$user_email = $user_info->user_email;
321+
$details = sprintf(
322+
esc_html__( '%s adjusted points for %s by %s%d.', 'customer-loyalty-for-woocommerce' ),
323+
$admin_name,
324+
$user_name,
325+
$points_change > 0 ? '+' : '',
326+
$points_change
327+
);
328+
clwc_insert_loyalty_log_entry( $user_id, $user_name, $user_email, $points_change, $details );
329+
330+
wp_send_json_success( [ 'message' => __( 'Points updated successfully.', 'customer-loyalty-for-woocommerce' ) ] );
331+
}
332+
add_action( 'wp_ajax_clwc_update_loyalty_points', 'clwc_update_loyalty_points_callback' );

includes/class-clwc.php

+13
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@
1414
* @since 1.0.0
1515
*/
1616

17+
// Add the Plugin Update Checker.
18+
require 'vendor/plugin-update-checker/plugin-update-checker.php';
19+
use YahnisElsts\PluginUpdateChecker\v5\PucFactory;
20+
21+
$myUpdateChecker = PucFactory::buildUpdateChecker(
22+
'https://github.com/deviodigital/customer-loyalty-fort-woocommerce/',
23+
__FILE__,
24+
'customer-loyalty-for-woocommerce'
25+
);
26+
27+
// Set the branch that contains the stable release.
28+
$myUpdateChecker->setBranch( 'main' );
29+
1730
/**
1831
* The core plugin class.
1932
*

includes/clwc-helper-functions.php

+34-21
Original file line numberDiff line numberDiff line change
@@ -493,32 +493,45 @@ function clwc_earning_points_money_spent() {
493493
}
494494

495495
/**
496-
* Insert a loyalty log entry into the database.
496+
* Insert a loyalty log entry.
497497
*
498-
* @since 2.0.0
498+
* @param int $user_id User ID.
499+
* @param string $name User display name.
500+
* @param string $email User email.
501+
* @param int $points Points awarded or redeemed.
502+
* @param string $details Action details.
499503
*
500-
* @param int $user_id The user ID.
501-
* @param string $name The customer's name.
502-
* @param string $email The customer's email.
503-
* @param int $points The number of points for the action.
504-
* @param string $details Details about the action (e.g., "Points redeemed for discount").
505-
* @return bool|int The inserted row ID on success, or false on failure.
504+
* @return void
506505
*/
507506
function clwc_insert_loyalty_log_entry( $user_id, $name, $email, $points, $details ) {
508507
global $wpdb;
509-
510508
$table_name = $wpdb->prefix . 'clwc_loyalty_log';
511509

512-
// Prepare data and sanitize for database insertion.
513-
$data = [
514-
'user_id' => absint( $user_id ),
515-
'name' => sanitize_text_field( $name ),
516-
'email' => sanitize_email( $email ),
517-
'points' => intval( $points ),
518-
'details' => sanitize_textarea_field( $details ),
519-
'date' => current_time( 'mysql' ),
520-
];
521-
522-
// Insert the data into the table.
523-
return $wpdb->insert( $table_name, $data );
510+
// Attempt to insert the log entry.
511+
$result = $wpdb->insert(
512+
$table_name,
513+
[
514+
'user_id' => $user_id,
515+
'name' => $name,
516+
'email' => $email,
517+
'points' => $points,
518+
'details' => $details,
519+
'date' => current_time( 'mysql' ),
520+
],
521+
[
522+
'%d',
523+
'%s',
524+
'%s',
525+
'%d',
526+
'%s',
527+
'%s',
528+
]
529+
);
530+
531+
// Log any database errors for debugging.
532+
if ( false === $result ) {
533+
error_log( 'Failed to insert log entry: ' . $wpdb->last_error );
534+
} else {
535+
error_log( 'Log entry added for user ID ' . $user_id );
536+
}
524537
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace YahnisElsts\PluginUpdateChecker\v5;
4+
5+
if ( !class_exists(PucFactory::class, false) ):
6+
7+
class PucFactory extends \YahnisElsts\PluginUpdateChecker\v5p4\PucFactory {
8+
}
9+
10+
endif;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace YahnisElsts\PluginUpdateChecker\v5p4;
4+
5+
if ( !class_exists(Autoloader::class, false) ):
6+
7+
class Autoloader {
8+
const DEFAULT_NS_PREFIX = 'YahnisElsts\\PluginUpdateChecker\\';
9+
10+
private $prefix;
11+
private $rootDir;
12+
private $libraryDir;
13+
14+
private $staticMap;
15+
16+
public function __construct() {
17+
$this->rootDir = dirname(__FILE__) . '/';
18+
19+
$namespaceWithSlash = __NAMESPACE__ . '\\';
20+
$this->prefix = $namespaceWithSlash;
21+
22+
$this->libraryDir = $this->rootDir . '../..';
23+
if ( !self::isPhar() ) {
24+
$this->libraryDir = realpath($this->libraryDir);
25+
}
26+
$this->libraryDir = $this->libraryDir . '/';
27+
28+
//Usually, dependencies like Parsedown are in the global namespace,
29+
//but if someone adds a custom namespace to the entire library, they
30+
//will be in the same namespace as this class.
31+
$isCustomNamespace = (
32+
substr($namespaceWithSlash, 0, strlen(self::DEFAULT_NS_PREFIX)) !== self::DEFAULT_NS_PREFIX
33+
);
34+
$libraryPrefix = $isCustomNamespace ? $namespaceWithSlash : '';
35+
36+
$this->staticMap = array(
37+
$libraryPrefix . 'PucReadmeParser' => 'vendor/PucReadmeParser.php',
38+
$libraryPrefix . 'Parsedown' => 'vendor/Parsedown.php',
39+
);
40+
41+
//Add the generic, major-version-only factory class to the static map.
42+
$versionSeparatorPos = strrpos(__NAMESPACE__, '\\v');
43+
if ( $versionSeparatorPos !== false ) {
44+
$versionSegment = substr(__NAMESPACE__, $versionSeparatorPos + 1);
45+
$pointPos = strpos($versionSegment, 'p');
46+
if ( ($pointPos !== false) && ($pointPos > 1) ) {
47+
$majorVersionSegment = substr($versionSegment, 0, $pointPos);
48+
$majorVersionNs = __NAMESPACE__ . '\\' . $majorVersionSegment;
49+
$this->staticMap[$majorVersionNs . '\\PucFactory'] =
50+
'Puc/' . $majorVersionSegment . '/Factory.php';
51+
}
52+
}
53+
54+
spl_autoload_register(array($this, 'autoload'));
55+
}
56+
57+
/**
58+
* Determine if this file is running as part of a Phar archive.
59+
*
60+
* @return bool
61+
*/
62+
private static function isPhar() {
63+
//Check if the current file path starts with "phar://".
64+
static $pharProtocol = 'phar://';
65+
return (substr(__FILE__, 0, strlen($pharProtocol)) === $pharProtocol);
66+
}
67+
68+
public function autoload($className) {
69+
if ( isset($this->staticMap[$className]) && file_exists($this->libraryDir . $this->staticMap[$className]) ) {
70+
include($this->libraryDir . $this->staticMap[$className]);
71+
return;
72+
}
73+
74+
if ( strpos($className, $this->prefix) === 0 ) {
75+
$path = substr($className, strlen($this->prefix));
76+
$path = str_replace(array('_', '\\'), '/', $path);
77+
$path = $this->rootDir . $path . '.php';
78+
79+
if ( file_exists($path) ) {
80+
include $path;
81+
}
82+
}
83+
}
84+
}
85+
86+
endif;

0 commit comments

Comments
 (0)