-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfree-digital-downloads.php
More file actions
434 lines (383 loc) · 17.1 KB
/
Copy pathfree-digital-downloads.php
File metadata and controls
434 lines (383 loc) · 17.1 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<?php
/**
* Plugin Name: Free Digital Downloads
* Plugin URI: https://aiordienow.com/products/free-digital-downloads
* Description: Sell digital products, manage software licenses, and deliver downloads — completely free. No upsells, no premium tiers. Everything EDD charges $500/year for, yours at no cost.
* Version: 1.0.0
* Author: AI Or Die Now
* Author URI: https://aiordienow.com
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: free-digital-downloads
* Domain Path: /languages
* Requires at least: 6.0
* Requires PHP: 7.4
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Plugin constants.
define( 'FDD_VERSION', '1.0.0' );
define( 'FDD_FILE', __FILE__ );
define( 'FDD_DIR', plugin_dir_path( __FILE__ ) );
define( 'FDD_URL', plugin_dir_url( __FILE__ ) );
define( 'FDD_BASENAME', plugin_basename( __FILE__ ) );
/**
* Main plugin class — singleton.
*/
final class Free_Digital_Downloads {
private static $instance = null;
public static function instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
$this->load_dependencies();
$this->init_hooks();
}
private function load_dependencies() {
// Core.
require_once FDD_DIR . 'includes/post-types.php';
require_once FDD_DIR . 'includes/taxonomies.php';
require_once FDD_DIR . 'includes/meta-boxes.php';
// Cart & Checkout.
require_once FDD_DIR . 'includes/cart/cart.php';
require_once FDD_DIR . 'includes/checkout/checkout.php';
require_once FDD_DIR . 'includes/checkout/payments.php';
// Downloads & Licensing.
require_once FDD_DIR . 'includes/downloads/download-handler.php';
require_once FDD_DIR . 'includes/downloads/file-protection.php';
require_once FDD_DIR . 'includes/licensing/license-manager.php';
require_once FDD_DIR . 'includes/licensing/update-server.php';
// Discounts.
require_once FDD_DIR . 'includes/discounts/discount-manager.php';
// Emails.
require_once FDD_DIR . 'includes/emails/email-handler.php';
// Reports.
require_once FDD_DIR . 'includes/reports/reports.php';
// Reviews.
require_once FDD_DIR . 'includes/reviews/reviews.php';
// REST API.
require_once FDD_DIR . 'includes/rest-api/endpoints.php';
// Integrations (email services).
require_once FDD_DIR . 'includes/integrations/integrations.php';
// Admin.
if ( is_admin() ) {
require_once FDD_DIR . 'includes/admin/admin-pages.php';
require_once FDD_DIR . 'includes/admin/settings.php';
}
}
private function init_hooks() {
add_action( 'init', array( $this, 'load_textdomain' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin' ) );
// Product display template.
add_filter( 'the_content', array( $this, 'product_content_template' ) );
}
public static function activate() {
// These files must be loaded here because the activation hook fires
// before plugins_loaded, so load_dependencies() hasn't run yet.
require_once FDD_DIR . 'includes/post-types.php';
require_once FDD_DIR . 'includes/taxonomies.php';
require_once FDD_DIR . 'includes/downloads/file-protection.php';
FDD_Post_Types::register();
FDD_Taxonomies::register();
FDD_DB::create_tables();
FDD_Pages::create_pages();
FDD_File_Protection::protect_uploads();
flush_rewrite_rules();
}
public static function deactivate() {
flush_rewrite_rules();
}
public function load_textdomain() {
// WordPress 4.6+ auto-loads translations for plugins hosted on wordpress.org.
// This is kept for backwards compatibility and custom translation loading.
// phpcs:ignore PluginCheck.CodeAnalysis.DiscouragedFunctions.load_plugin_textdomainFound
load_plugin_textdomain( 'free-digital-downloads', false, dirname( FDD_BASENAME ) . '/languages' );
}
public function enqueue_frontend() {
// Only load on FDD-relevant pages.
$is_fdd_page = is_singular( 'fdd_product' )
|| is_post_type_archive( 'fdd_product' )
|| is_tax( 'fdd_category' )
|| is_tax( 'fdd_tag' )
|| is_page( get_option( 'fdd_cart_page' ) )
|| is_page( get_option( 'fdd_checkout_page' ) )
|| is_page( get_option( 'fdd_receipt_page' ) )
|| is_page( get_option( 'fdd_account_page' ) );
if ( ! $is_fdd_page ) {
return;
}
wp_enqueue_style( 'fdd-frontend', FDD_URL . 'assets/css/frontend.css', array(), FDD_VERSION );
wp_enqueue_script( 'fdd-frontend', FDD_URL . 'assets/js/frontend.js', array(), FDD_VERSION, array( 'in_footer' => true, 'strategy' => 'defer' ) );
wp_localize_script( 'fdd-frontend', 'fdd_data', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'fdd_nonce' ),
'cart_url' => get_permalink( get_option( 'fdd_cart_page' ) ),
) );
// Stripe on checkout page only.
if ( is_page( get_option( 'fdd_checkout_page' ) ) ) {
$stripe_key = get_option( 'fdd_stripe_publishable_key', '' );
if ( $stripe_key ) {
// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Stripe.js is an external library with no version parameter.
wp_enqueue_script( 'stripe-js', 'https://js.stripe.com/v3/', array(), null, true );
wp_enqueue_script( 'fdd-checkout', FDD_URL . 'assets/js/checkout.js', array( 'stripe-js', 'fdd-frontend' ), FDD_VERSION, true );
wp_localize_script( 'fdd-checkout', 'fdd_checkout', array(
'stripe_key' => $stripe_key,
'ajax_url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'fdd_nonce' ),
) );
}
}
}
public function enqueue_admin() {
$screen = get_current_screen();
if ( ! $screen ) {
return;
}
if ( strpos( $screen->id, 'free-digital-downloads' ) === false && $screen->post_type !== 'fdd_product' ) {
return;
}
wp_enqueue_style( 'fdd-admin', FDD_URL . 'assets/css/admin.css', array(), FDD_VERSION );
wp_enqueue_script( 'fdd-admin', FDD_URL . 'assets/js/admin.js', array( 'jquery' ), FDD_VERSION, true );
wp_enqueue_media();
}
/**
* Append product details (price, cart, reviews) to single product content.
*/
public function product_content_template( $content ) {
if ( ! is_singular( 'fdd_product' ) || ! in_the_loop() || ! is_main_query() ) {
return $content;
}
global $post;
$product_id = $post->ID;
$price = get_post_meta( $product_id, '_fdd_price', true );
$sale_price = get_post_meta( $product_id, '_fdd_sale_price', true );
$files = get_post_meta( $product_id, '_fdd_files', true );
$licensing = get_post_meta( $product_id, '_fdd_licensing_enabled', true );
ob_start();
echo wp_kses_post( $content );
?>
<div class="fdd-product-details">
<div class="fdd-product-pricing">
<?php if ( $sale_price && (float) $sale_price > 0 ) : ?>
<span class="fdd-price-original"><s><?php echo esc_html( fdd_format_price( (float) $price ) ); ?></s></span>
<span class="fdd-price-current"><?php echo esc_html( fdd_format_price( (float) $sale_price ) ); ?></span>
<?php elseif ( $price && (float) $price > 0 ) : ?>
<span class="fdd-price-current"><?php echo esc_html( fdd_format_price( (float) $price ) ); ?></span>
<?php else : ?>
<span class="fdd-price-free"><?php esc_html_e( 'Free', 'free-digital-downloads' ); ?></span>
<?php endif; ?>
</div>
<button class="fdd-add-to-cart button" data-product-id="<?php echo esc_attr( $product_id ); ?>">
<?php
if ( ! $price || (float) $price <= 0 ) {
esc_html_e( 'Download Free', 'free-digital-downloads' );
} else {
esc_html_e( 'Add to Cart', 'free-digital-downloads' );
}
?>
</button>
<?php if ( $licensing ) : ?>
<p class="fdd-licensing-note"><?php esc_html_e( 'Includes a software license key for updates and support.', 'free-digital-downloads' ); ?></p>
<?php endif; ?>
<?php if ( is_array( $files ) && ! empty( $files ) ) : ?>
<div class="fdd-file-list">
<h3><?php esc_html_e( 'Included Files', 'free-digital-downloads' ); ?></h3>
<ul>
<?php foreach ( $files as $file ) : ?>
<li><?php echo esc_html( $file['name'] ); ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
</div>
<?php
return ob_get_clean();
}
}
/**
* Database table creation.
*/
class FDD_DB {
public static function create_tables() {
global $wpdb;
$charset = $wpdb->get_charset_collate();
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
// Orders.
$orders = $wpdb->prefix . 'fdd_orders';
dbDelta( "CREATE TABLE $orders (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
order_key varchar(64) NOT NULL DEFAULT '',
customer_email varchar(255) NOT NULL,
customer_name varchar(255) DEFAULT '',
payment_gateway varchar(50) DEFAULT 'stripe',
transaction_id varchar(255) DEFAULT '',
status varchar(20) DEFAULT 'pending',
subtotal decimal(10,2) DEFAULT 0,
discount decimal(10,2) DEFAULT 0,
total decimal(10,2) DEFAULT 0,
currency varchar(3) DEFAULT 'USD',
discount_code varchar(100) DEFAULT '',
ip_address varchar(45) DEFAULT '',
user_id bigint(20) unsigned DEFAULT 0,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
updated_at datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY idx_email (customer_email),
KEY idx_status (status),
KEY idx_order_key (order_key),
KEY idx_created (created_at)
) $charset;" );
// Order items.
$items = $wpdb->prefix . 'fdd_order_items';
dbDelta( "CREATE TABLE $items (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
order_id bigint(20) unsigned NOT NULL,
product_id bigint(20) unsigned NOT NULL,
price_name varchar(255) DEFAULT '',
price decimal(10,2) DEFAULT 0,
quantity int(11) DEFAULT 1,
PRIMARY KEY (id),
KEY idx_order (order_id),
KEY idx_product (product_id)
) $charset;" );
// Download log.
$downloads = $wpdb->prefix . 'fdd_download_log';
dbDelta( "CREATE TABLE $downloads (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
order_id bigint(20) unsigned DEFAULT 0,
product_id bigint(20) unsigned NOT NULL,
file_id varchar(255) DEFAULT '',
customer_email varchar(255) NOT NULL,
ip_address varchar(45) DEFAULT '',
user_agent text,
downloaded_at datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY idx_order (order_id),
KEY idx_product (product_id),
KEY idx_email (customer_email)
) $charset;" );
// License keys.
$licenses = $wpdb->prefix . 'fdd_licenses';
dbDelta( "CREATE TABLE $licenses (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
license_key varchar(255) NOT NULL,
order_id bigint(20) unsigned NOT NULL,
product_id bigint(20) unsigned NOT NULL,
customer_email varchar(255) NOT NULL,
status varchar(20) DEFAULT 'active',
activation_limit int(11) DEFAULT 1,
activation_count int(11) DEFAULT 0,
expires_at datetime DEFAULT NULL,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY idx_key (license_key),
KEY idx_product (product_id),
KEY idx_email (customer_email),
KEY idx_status (status)
) $charset;" );
// License activations.
$activations = $wpdb->prefix . 'fdd_activations';
dbDelta( "CREATE TABLE $activations (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
license_id bigint(20) unsigned NOT NULL,
site_url varchar(500) NOT NULL,
activated_at datetime DEFAULT CURRENT_TIMESTAMP,
deactivated_at datetime DEFAULT NULL,
PRIMARY KEY (id),
KEY idx_license (license_id)
) $charset;" );
// Discount codes.
$discounts = $wpdb->prefix . 'fdd_discounts';
dbDelta( "CREATE TABLE $discounts (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
code varchar(100) NOT NULL,
name varchar(255) DEFAULT '',
type varchar(20) DEFAULT 'percent',
amount decimal(10,2) DEFAULT 0,
max_uses int(11) DEFAULT 0,
use_count int(11) DEFAULT 0,
min_total decimal(10,2) DEFAULT 0,
product_ids text,
starts_at datetime DEFAULT NULL,
expires_at datetime DEFAULT NULL,
status varchar(20) DEFAULT 'active',
created_at datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY idx_code (code),
KEY idx_status (status)
) $charset;" );
// Email captures (for free downloads).
$emails = $wpdb->prefix . 'fdd_email_captures';
dbDelta( "CREATE TABLE $emails (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
email varchar(255) NOT NULL,
product_id bigint(20) unsigned NOT NULL,
ip_address varchar(45) DEFAULT '',
download_count int(11) DEFAULT 0,
created_at datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY idx_email (email),
KEY idx_product (product_id),
UNIQUE KEY unique_email_product (email, product_id)
) $charset;" );
update_option( 'fdd_db_version', FDD_VERSION );
}
}
/**
* Auto-create required pages on activation.
*/
class FDD_Pages {
public static function create_pages() {
$pages = array(
'fdd_cart_page' => array( 'title' => 'Cart', 'content' => '<!-- wp:shortcode -->[fdd_cart]<!-- /wp:shortcode -->' ),
'fdd_checkout_page' => array( 'title' => 'Checkout', 'content' => '<!-- wp:shortcode -->[fdd_checkout]<!-- /wp:shortcode -->' ),
'fdd_receipt_page' => array( 'title' => 'Purchase Confirmation', 'content' => '<!-- wp:shortcode -->[fdd_receipt]<!-- /wp:shortcode -->' ),
'fdd_account_page' => array( 'title' => 'Your Downloads', 'content' => '<!-- wp:shortcode -->[fdd_account]<!-- /wp:shortcode -->' ),
);
foreach ( $pages as $option_key => $page ) {
$existing = get_option( $option_key );
if ( $existing && get_post_status( $existing ) === 'publish' ) {
continue;
}
$page_id = wp_insert_post( array(
'post_title' => $page['title'],
'post_content' => $page['content'],
'post_status' => 'publish',
'post_type' => 'page',
) );
if ( $page_id && ! is_wp_error( $page_id ) ) {
update_option( $option_key, $page_id );
}
}
}
}
// Activation and deactivation hooks must be registered at file load time.
register_activation_hook( FDD_FILE, array( 'Free_Digital_Downloads', 'activate' ) );
register_deactivation_hook( FDD_FILE, array( 'Free_Digital_Downloads', 'deactivate' ) );
/**
* Format a price with the correct currency symbol.
*/
function fdd_format_price( $amount ) {
$currency = strtolower( get_option( 'fdd_currency', 'usd' ) );
$symbols = array( 'usd' => '$', 'eur' => "\xe2\x82\xac", 'gbp' => "\xc2\xa3" );
$symbol = $symbols[ $currency ] ?? '$';
return $symbol . number_format( (float) $amount, 2 );
}
/**
* Get the currency symbol.
*/
function fdd_currency_symbol() {
$currency = strtolower( get_option( 'fdd_currency', 'usd' ) );
$symbols = array( 'usd' => '$', 'eur' => "\xe2\x82\xac", 'gbp' => "\xc2\xa3" );
return $symbols[ $currency ] ?? '$';
}
// Boot the plugin.
function fdd_init() {
return Free_Digital_Downloads::instance();
}
add_action( 'plugins_loaded', 'fdd_init' );