-
Notifications
You must be signed in to change notification settings - Fork 0
/
woocommerce-regenerate-and-notify.php
executable file
·89 lines (74 loc) · 2.88 KB
/
woocommerce-regenerate-and-notify.php
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
<?php
/**
* WooCommerce Regenerate & Notify
*
* @package Woo_Regenerate_And_Notify
*
* @wordpress-plugin
* Plugin Name: WooCommerce Regenerate & Notify
* Plugin URI: https://github.com/bdaley/woocommerce-regenerate-and-notify
* Description: A WooCommerce extension that regenerates download permissions (including expiration date) and notifies the customer.
* Version: 1.0.0
* Author: Brian Daley
* Author URI: https://bdaley.com
* License: MIT
* License URI: https://opensource.org/licenses/MIT
* Text Domain: woo-regenerate-and-notify
* Domain Path: /languages
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
// Composer dependencies.
require_once dirname( __FILE__ ) . '/vendor/autoload.php';
// This plugin is hosted on GitHub, so we use a custom ppdate checker.
$wran_update_checker = Puc_v4_Factory::buildUpdateChecker(
'https://github.com/bdaley/woocommerce-regenerate-and-notify/',
__FILE__,
'woocommerce-regenerate-and-notify'
);
/**
* Adds the new action to the order action meta box.
*
* @param array $actions List of actions in the metabox.
* @return array $actions Amended list of actions.
*/
function woo_regenerate_and_notify_custom_order_action( $actions ) {
// Remove the woocommerce option to regenerate to avoid confusion.
unset( $actions['regenerate_download_permissions'] );
// Add our new action (executed below).
$actions['woo_regenerate_and_notify'] = __( 'Regenerate permissions & send link to customer', 'woo-regenerate-and-notify' );
return $actions;
}
// Register the function above.
add_filter( 'woocommerce_order_actions', 'woo_regenerate_and_notify_custom_order_action' );
/**
* Executes regeneration, expiry reset, order note, and notification to user.
*
* @param object $order The order WC_Order object.
* @return void
*/
function woo_regenerate_and_notify( $order ) {
// First, we reset the download counter to its original value.
$data_store = WC_Data_Store::load( 'customer-download' );
$data_store->delete_by_order_id( $order->ID );
wc_downloadable_product_permissions( $order->ID, true );
// Retrieve Download(s) and completely remove the access expiration.
// At some point, we may want to restore the default, but we'll turn off the time limit for now.
$downloads = $data_store->get_downloads( array( 'order_id' => $order->ID ) );
if ( is_array( $downloads ) ) {
foreach ( $downloads as $download ) {
$download->set_access_expires( null );
$download->save();
}
}
// All restored. Store note and resend an updated invoice to the customer.
$order->add_order_note(
__( "We've reset your download permissions. Please try downloading again.", 'woo-regenerate-and-notify' ),
true,
true
);
}
// Register the function above.
add_action( 'woocommerce_order_action_woo_regenerate_and_notify', 'woo_regenerate_and_notify' );