PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 1.5.0
Subscriptions for WooCommerce with Stripe Recurring Payments v1.5.0
2.0.0 1.11.2 1.11.1 1.11.0 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.6 1.9.5 trunk 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 All 61 releases
subscription / includes / Admin / Integrations.php

Integrations.php in Subscriptions for WooCommerce with Stripe Recurring Payments 1.5.0, at includes/Admin/Integrations.php

343 lines 9.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SpringDevs\Subscription\Admin;
4
5 // Exit if accessed directly.
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit;
8 }
9
10 /**
11 * Integrations class
12 *
13 * @package SpringDevs\Subscription\Admin
14 */
15 class Integrations {
16 /**
17 * Integrations list.
18 *
19 * @var array
20 */
21 protected $integrations = [];
22
23 /**
24 * Initialize the class
25 */
26 public function __construct() {
27 // Initialize.
28 add_action( 'init', [ $this, 'init' ], 10 );
29
30 // Admin menu (sidebar).
31 add_action( 'admin_menu', array( $this, 'register_admin_menu' ), 20 );
32
33 // WP Subscription navbar.
34 add_filter( 'wp_subscription_admin_header_menu_items', [ $this, 'add_integrations_menu_item' ], 10, 2 );
35
36 // Enqueue integrations scripts.
37 // add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_integrations_scripts' ] );
38
39 // Integrations AJAX handler.
40 // add_action( 'admin_ajax_integrations_handler', [ $this,'integrations_handler_callback' ] );
41 // add_action( 'admin_ajax_nopriv_integrations_handler', [ $this,'integrations_handler_callback' ] );
42 }
43
44 /**
45 * Initialize the integrations.
46 */
47 public function init() {
48 // Set integrations.
49 $this->integrations = $this->get_integrations();
50 }
51
52 /**
53 * Register submenu under `subscriptions` menu.
54 *
55 * @return void
56 */
57 public function register_admin_menu() {
58 $parent_slug = 'wp-subscription';
59 add_submenu_page(
60 $parent_slug,
61 __( 'Integrations', 'wp_subscription' ),
62 __( 'Integrations', 'wp_subscription' ),
63 'manage_options',
64 'wp-subscription-integrations',
65 [ $this, 'render_integrations_page' ],
66 40
67 );
68 }
69
70 /**
71 * Add Integrations link to the WP Subscription admin header menu.
72 *
73 * @param array $menu_items Array of menu items.
74 * @param string $current Current active menu item slug.
75 */
76 public function add_integrations_menu_item( $menu_items, $current ) {
77 $menu_items[] = [
78 'slug' => 'wp-subscription-integrations',
79 'label' => __( 'Integrations', 'wp_subscription' ),
80 'url' => admin_url( 'admin.php?page=wp-subscription-integrations' ),
81 ];
82 return $menu_items;
83 }
84
85 /**
86 * Enqueue scripts for integrations page.
87 */
88 public function enqueue_integrations_scripts() {
89 wp_enqueue_script( 'wp-subs-integrations', WP_SUBSCRIPTION_ASSETS . '/js/integration_settings.js', [ 'jquery' ], WP_SUBSCRIPTION_VERSION, true );
90
91 wp_localize_script(
92 'wp-subs-integrations',
93 'wpSubsIntegrations',
94 array(
95 'nonce' => wp_create_nonce( 'wp_subs_integrations_nonce' ),
96 'ajax_url' => admin_url( 'admin-ajax.php' ),
97 )
98 );
99 }
100
101 /**
102 * Handle AJAX request for integrations.
103 */
104 public function integrations_handler_callback() {
105 check_ajax_referer( 'wp_subs_integrations_nonce', 'nonce' );
106
107 $action_callback = ! empty( $_POST['action_callback'] ) ? sanitize_text_field( wp_unslash( $_POST['action_callback'] ) ) : '';
108
109 dd( '🔽 action_callback', $action_callback );
110 }
111
112 /**
113 * Check if a payment gateway is installed and active.
114 *
115 * @param string $gateway_id Gateway ID.
116 * @return bool
117 */
118 protected function is_gateway_installed( $gateway_id ) {
119 $installed_gateways = WC()->payment_gateways()->payment_gateways();
120 return isset( $installed_gateways[ $gateway_id ] );
121 }
122
123 /**
124 * Check if a payment gateway is enabled.
125 *
126 * @param string $gateway_id Gateway ID.
127 * @return bool
128 */
129 protected function is_gateway_enabled( $gateway_id ) {
130 $installed_gateways = WC()->payment_gateways()->payment_gateways();
131 if ( ! isset( $installed_gateways[ $gateway_id ] ) ) {
132 return false;
133 }
134 return $installed_gateways[ $gateway_id ]->is_available();
135 }
136
137
138
139 /**
140 * Check if a gateway is installed by plugin file.
141 *
142 * @param string $plugin_file Plugin file path.
143 * @return bool
144 */
145 protected function is_plugin_installed( $plugin_file ) {
146 if ( ! function_exists( 'get_plugins' ) ) {
147 require_once ABSPATH . 'wp-admin/includes/plugin.php';
148 }
149 $plugins = get_plugins();
150 return isset( $plugins[ $plugin_file ] );
151 }
152
153 /**
154 * Check if a gateway plugin is active.
155 *
156 * @param string $plugin_file Plugin file path.
157 * @return bool
158 */
159 protected function is_plugin_active( $plugin_file ) {
160 return is_plugin_active( $plugin_file );
161 }
162
163 /**
164 * Check if a payment gateway is enabled.
165 *
166 * @param string $gateway_id Gateway ID.
167 */
168 public function is_payment_gateway_enabled( $gateway_id ) {
169 $gateways = WC()->payment_gateways->get_available_payment_gateways();
170 return isset( $gateways[ $gateway_id ] );
171 }
172
173 /**
174 * Get the list of integrations.
175 *
176 * @return array
177 */
178 protected function get_integrations(): array {
179 $integrations = [
180 [
181 'title' => 'PayPal for WP Subscription',
182 'description' => 'Accept subscription payments via PayPal.',
183 'icon_url' => WP_SUBSCRIPTION_ASSETS . '/images/paypal.svg',
184 'is_installed' => 'on' === get_option( 'wp_subs_paypal_integration_enabled', 'off' ),
185 'is_active' => $this->is_gateway_enabled( 'wp_subscription_paypal' ),
186 'supports_recurring' => true,
187 'actions' => [
188 // [
189 // 'action' => 'install',
190 // 'label' => 'Install Now',
191 // 'type' => 'function',
192 // 'function' => 'wpSubsInstallPaypalIntegration()',
193 // ],
194 [
195 'action' => 'settings',
196 'label' => 'Settings',
197 'type' => 'link',
198 'url' => admin_url( 'admin.php?page=wc-settings&tab=checkout&section=wp_subscription_paypal' ),
199 ],
200 // [
201 // 'action' => 'uninstall',
202 // 'label' => 'Uninstall',
203 // 'type' => 'function',
204 // 'function' => 'wpSubsUninstallPaypalIntegration()',
205 // 'class' => 'button button-primary wp-subs-button-danger',
206 // ],
207 ],
208 ],
209 [
210 'title' => 'Stripe',
211 'description' => 'Process subscription payments securely with Stripe.',
212 'icon_url' => 'https://ps.w.org/woocommerce-gateway-stripe/assets/icon-256x256.png',
213 'is_installed' => class_exists( 'WC_Stripe' ),
214 'is_active' => $this->is_gateway_enabled( 'stripe' ),
215 'supports_recurring' => true,
216 'actions' => [
217 [
218 'action' => 'install',
219 'label' => 'Install Now',
220 'type' => 'link',
221 'url' => admin_url( 'plugin-install.php?s=WooCommerce%2520Stripe%2520Payment%2520Gateway&tab=search&type=term' ),
222 ],
223 [
224 'action' => 'settings',
225 'label' => 'Settings',
226 'type' => 'link',
227 'url' => admin_url( 'admin.php?page=wc-settings&tab=checkout&section=stripe&panel=settings' ),
228 ],
229 ],
230 ],
231 [
232 'title' => 'Paddle',
233 'description' => 'Process subscription payments securely with Paddle.',
234 'icon_url' => WP_SUBSCRIPTION_ASSETS . '/images/paddle.svg',
235 'is_installed' => class_exists( 'SmartPayWoo\Gateways\Paddle\SmartPay_Paddle' ),
236 'is_active' => $this->is_gateway_enabled( 'smartpay_paddle' ),
237 'supports_recurring' => true,
238 'actions' => [
239 [
240 'action' => 'install',
241 'label' => 'Install Now',
242 'type' => 'link',
243 'url' => admin_url( 'plugin-install.php?s=WooCommerce%2520Stripe%2520Payment%2520Gateway&tab=search&type=term' ),
244 ],
245 [
246 'action' => 'enable',
247 'label' => 'Enable Gateway',
248 'type' => 'toggle_option',
249 'option_key' => 'woocommerce_enable_paddle_gateway',
250 'value' => true,
251 ],
252 [
253 'action' => 'settings',
254 'label' => 'Settings',
255 'type' => 'link',
256 'url' => admin_url( 'admin.php?page=wc-settings&tab=checkout&section=smartpay_paddle&from=WCADMIN_PAYMENT_SETTINGS' ),
257 ],
258 [
259 'label' => 'More Details',
260 'type' => 'external_link',
261 'url' => 'https://wpsmartpay.com/paddle-for-woocommerce/',
262 ],
263 ],
264 ],
265 ];
266
267 // Add more integrations as needed.
268 add_filter( 'wp_subscription_integrations', $integrations );
269
270 return $integrations;
271 }
272
273 /**
274 * Filter actions.
275 *
276 * @param array $integrations Integrations array.
277 */
278 protected function filter_integration_actions( array $integrations ): array {
279 $cleaned_integrations = [];
280
281 foreach ( $integrations as $integration ) {
282 $is_installed = $integration['is_installed'] ?? false;
283 $is_active = $integration['is_active'] ?? false;
284
285 $cleaned_actions = [];
286
287 foreach ( $integration['actions'] as $integration_action ) {
288 $action_tag = $integration_action['action'] ?? null;
289
290 if ( 'install' === $action_tag ) {
291 if ( ! $is_installed ) {
292 $cleaned_actions[] = $integration_action;
293 }
294 continue;
295 }
296 if ( 'uninstall' === $action_tag ) {
297 if ( $is_installed ) {
298 $cleaned_actions[] = $integration_action;
299 }
300 continue;
301 }
302 if ( 'enable' === $action_tag ) {
303 if ( $is_installed && ! $is_active ) {
304 $cleaned_actions[] = $integration_action;
305 }
306 continue;
307 }
308 if ( 'settings' === $action_tag ) {
309 if ( $is_installed ) {
310 $cleaned_actions[] = $integration_action;
311 }
312 continue;
313 }
314
315 // Default.
316 $cleaned_actions[] = $integration_action;
317 }
318
319 // Overwrite.
320 $integration['actions'] = $cleaned_actions;
321 $cleaned_integrations[] = $integration;
322 }
323
324 return $cleaned_integrations;
325 }
326
327 /**
328 * Render the Integrations admin page.
329 */
330 public function render_integrations_page() {
331 $integrations = $this->integrations;
332 $integrations = $this->filter_integration_actions( $integrations );
333
334 // Integrations styles.
335 // wp_enqueue_style( 'wp-subs-integration-settings', WP_SUBSCRIPTION_ASSETS . '/css/integration_settings.css', [], WP_SUBSCRIPTION_VERSION, 'all' );
336
337 $menu = new \SpringDevs\Subscription\Admin\Menu();
338 $menu->render_admin_header();
339 include 'views/integrations.php';
340 $menu->render_admin_footer();
341 }
342 }
343