-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpaybutton.php
More file actions
118 lines (102 loc) · 4.75 KB
/
paybutton.php
File metadata and controls
118 lines (102 loc) · 4.75 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
<?php
/**
* Plugin Name: PayButton
* Description: Monetize your content with configurable no-signup paywalls.
* Version: 6.0.0
* Author: PayButton
* Author URI: https://github.com/PayButton/wordpress-plugin
* License: MIT
* License URI: https://opensource.org/licenses/MIT
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Defines plugin directory constants:
* - PAYBUTTON_PLUGIN_DIR: Absolute filesystem path to the plugin's directory.
* - PAYBUTTON_PLUGIN_URL: Web-accessible URL to the plugin's directory.
*
* These constants allow easy and consistent referencing of plugin files (such as assets and templates)
* without hardcoding paths.
*/
define( 'PAYBUTTON_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'PAYBUTTON_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
// NEW: Declare HPOS Compatibility for WooCommerce
// This must run on 'before_woocommerce_init' to ensure WooCommerce knows we support custom order tables.
add_action( 'before_woocommerce_init', function() {
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
}
} );
// Include required class files.
require_once PAYBUTTON_PLUGIN_DIR . 'includes/class-paybutton-activator.php';
require_once PAYBUTTON_PLUGIN_DIR . 'includes/class-paybutton-deactivator.php';
require_once PAYBUTTON_PLUGIN_DIR . 'includes/class-paybutton-admin.php';
require_once PAYBUTTON_PLUGIN_DIR . 'includes/class-paybutton-public.php';
require_once PAYBUTTON_PLUGIN_DIR . 'includes/class-paybutton-transactions.php';
require_once PAYBUTTON_PLUGIN_DIR . 'includes/class-paybutton-ajax.php';
require_once PAYBUTTON_PLUGIN_DIR . 'includes/class-paybutton-state.php';
// --- NEW: Include WooCommerce Gateway ---
// We load this file, but the class inside it initiates itself safely on 'plugins_loaded'
if ( file_exists( PAYBUTTON_PLUGIN_DIR . 'includes/woocommerce/class-wc-gateway-paybutton.php' ) ) {
require_once PAYBUTTON_PLUGIN_DIR . 'includes/woocommerce/class-wc-gateway-paybutton.php';
}
/**
* Registers the plugin's activation and deactivation hooks.
*
* - When the plugin is activated, the static method 'activate' of the PayButton_Activator
* class is called to perform setup tasks (like creating custom database table for unlocked contents).
*
* - When the plugin is deactivated, the static method 'deactivate' of the PayButton_Deactivator
* class is called to perform any necessary cleanup.
*/
register_activation_hook( __FILE__, array( 'PayButton_Activator', 'activate' ) );
register_deactivation_hook( __FILE__, array( 'PayButton_Deactivator', 'deactivate' ) );
// Initialize plugin functionality.
add_action( 'plugins_loaded', function() {
// Make sure tables (including any newly added ones) exist after upgrades.
if ( class_exists( 'PayButton_Activator' ) ) {
PayButton_Activator::create_tables();
}
// Initialize admin functionality if in admin area.
if ( is_admin() ) {
new PayButton_Admin();
}
// Initialize public-facing functionality.
new PayButton_Public();
// Initialize AJAX handlers.
new PayButton_AJAX();
}, 1); // Use a priority to ensure this runs before other actions that might depend on session data.
// 1. Register the Gateway
add_filter( 'woocommerce_payment_gateways', 'paybutton_add_gateway_class' );
function paybutton_add_gateway_class( $gateways ) {
if ( class_exists( 'WC_Gateway_PayButton' ) ) {
$gateways[] = 'WC_Gateway_PayButton';
}
return $gateways;
}
// 2. Register Gutenberg Block Support
add_action( 'woocommerce_blocks_payment_method_type_registration', 'paybutton_register_blocks_support' );
function paybutton_register_blocks_support( \Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry ) {
// Ensure WooCommerce Blocks class exists
if ( ! class_exists( 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType' ) ) {
return;
}
$block_support_file = PAYBUTTON_PLUGIN_DIR . 'includes/woocommerce/class-paybutton-blocks-support.php';
if ( file_exists( $block_support_file ) ) {
require_once $block_support_file;
if ( class_exists( 'WC_PayButton_Blocks_Support' ) ) {
$payment_method_registry->register( new WC_PayButton_Blocks_Support() );
}
}
}
add_action('admin_init', function() {
if (get_option('paybutton_activation_redirect', false)) {
delete_option('paybutton_activation_redirect');
// Prevent redirect during bulk plugin activation
if (!isset($_GET['activate-multi'])) {
wp_safe_redirect(admin_url('admin.php?page=paybutton-paywall'));
exit;
}
}
});