-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwoocommerce-payments-subscriptions-migration.php
174 lines (136 loc) · 5.25 KB
/
woocommerce-payments-subscriptions-migration.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
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
<?php
/*
Plugin Name: WooCommerce Payments Subscriptions Migration
Description: Migrates stripe subscriptions to WooCommerce Payments via CSV file.
Version: 1.0
Author: Marcel Schmitz
Text Domain: wcpay-subscriptions-migration
Domain Path: /languages
Author URI: https://app.codeable.io/tasks/new?preferredContractor=22877
License: GPLv2
*/
if ( ! class_exists( 'wcpay_subscriptions_migration' ) ) {
class wcpay_subscriptions_migration {
private static $instance = null;
private static $data = array();
public static function get_instance() {
if ( self::$instance == null ) {
self::$instance = new wcpay_subscriptions_migration();
}
return self::$instance;
}
public function __construct() {
// Include required files
add_action( 'plugins_loaded', array( $this, 'includes' ), 12 );
// Register extensions
add_action( 'admin_enqueue_scripts', array( $this, 'register_extensions' ) );
// Add plugin internationalization
add_action( 'init', array( $this, 'load_textdomain' ) );
// Register blocks that lack a registration php script.
add_action( 'init', array( $this, 'register_blocks' ) );
}
// Show notice if WooCommerce is not active
public function woocommerce_error_activation_notice() {
$notice = __( 'You need WooCommerce active in order to use WooCommerce Payments Subscriptions Migration.', 'wcpay-subscriptions-migration' );
echo "<div class='error'><p><strong>$notice</strong></p></div>";
}
// Show notice if WCPay is not active
public function wcpay_error_activation_notice() {
$notice = __( 'You need WooCommerce Payments active in order to use WooCommerce Payments Subscriptions Migration.', 'wcpay-subscriptions-migration' );
echo "<div class='error'><p><strong>$notice</strong></p></div>";
}
// Include needed files and classes
public function includes() {
if ( ! class_exists( 'WooCommerce' ) ) {
add_action( 'admin_notices', array( $this, 'woocommerce_error_activation_notice' ) );
} elseif ( ! class_exists( 'WC_Payments' ) ) {
add_action( 'admin_notices', array( $this, 'wcpay_error_activation_notice' ) );
} else {
include_once WCPSM_DIR_PATH . 'includes/api/class-rest.php';
include_once WCPSM_DIR_PATH . 'includes/api/class-rest-subscription.php';
include_once WCPSM_DIR_PATH . 'includes/api/class-rest-payment-method.php';
WCPSM_Rest::get_instance();
if ( is_admin() ) {
include_once WCPSM_DIR_PATH . 'includes/admin/token-migrate.php';
include_once WCPSM_DIR_PATH . 'includes/admin/admin.php';
include_once WCPSM_DIR_PATH . 'includes/admin/migrate.php';
include_once WCPSM_DIR_PATH . 'includes/class-settings.php';
new WCPSM_Admin();
new WCPSM_Settings();
}
}
}
// Load plugin textdomain
public function load_textdomain() {
load_plugin_textdomain( 'wcpay-subscriptions-migration', false, dirname( WCPSM_PLUGIN_BASENAME ) . '/languages' );
}
/**
* Searches for block.json files in the blocks directory and registers them.
*
* @return void
*/
public function register_blocks() {
$blocks_directory = plugin_dir_path( __FILE__ ) . 'build/blocks/';
if ( ! is_dir( $blocks_directory ) ) {
return;
}
$iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $blocks_directory ) );
$blocks = array();
foreach ( $iterator as $file ) {
$path = $file->getPath();
if ( "$path/" !== $blocks_directory ) {
if ( ! isset( $blocks[ $path ] ) ) {
$blocks[ $path ] = '';
}
}
}
foreach ( $blocks as $block_path => $block_file ) {
if ( ! $block_file ) {
$block_path = str_replace( 'src', 'build', $block_path );
if ( file_exists( "$block_path/block.json" ) ) {
$args = apply_filters( 'sd_scaffold_plugin_filter_block_args', array(), $block_path );
register_block_type( $block_path, $args );
}
}
}
}
/**
* Searches for block.json files in the blocks directory and registers them.
*
* @return void
*/
public function register_extensions() {
$script = 'index';
$name = 'migration-app';
$path = WCPSM_DIR_PATH;
$asset_file = include WCPSM_DIR_PATH . '/build-extensions/' . $script . '.asset.php';
wp_register_script( $name, WCPSM_DIR_URL . 'build-extensions/' . $script . '.js', $asset_file['dependencies'], $asset_file['version'], true );
wp_enqueue_script( $name );
$rest = WCPSM_Rest::get_instance();
$endpoints = $rest->get_api_endpoints();
// Localize
wp_localize_script(
$name,
'wcpsm_migration_data',
array(
'nonce' => wp_create_nonce( 'wp_rest' ),
'endpoints' => $endpoints,
)
);
wp_register_style( $name, WCPSM_DIR_URL . 'build-extensions/index.css', array(), $asset_file['version'] );
wp_enqueue_style( $name );
}
}
}
if ( class_exists( 'wcpay_subscriptions_migration' ) ) {
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
define( 'WCPSM_DIR_PATH', plugin_dir_path( __FILE__ ) );
define( 'WCPSM_DIR_URL', plugin_dir_url( __FILE__ ) );
define( 'WCPSM_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
define( 'WCPSM_PLUGIN_FILE', __FILE__ );
define( 'WCPSM_PLUGIN_VERSION', '1.0' );
define( 'WCPSM_PLUGIN_API_BASE', 'wcpsm-api' );
wcpay_subscriptions_migration::get_instance();
}