PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 1.10.0
Subscriptions for WooCommerce with Stripe Recurring Payments v1.10.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.10.0, at includes/Admin/Integrations.php

376 lines 10.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 // WPSubscription navbar.
34 add_filter( 'subscrpt_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', 'subscription' ),
62 __( 'Integrations', '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 WPSubscription 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', '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 $screen = get_current_screen();
90 if ( ! $screen || false === strpos( $screen->id, 'wp-subscription-integrations' ) ) {
91 return;
92 }
93
94 wp_enqueue_script( 'subscrpt-integrations', SUBSCRPT_ASSETS . '/js/integration_settings.js', [], SUBSCRPT_VERSION, true );
95
96 wp_localize_script(
97 'subscrpt-integrations',
98 'subscrptIntegrations',
99 array(
100 'nonce' => wp_create_nonce( 'subscrpt_integration_install_nonce' ),
101 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
102 )
103 );
104 }
105
106 /**
107 * Handle AJAX request for integrations.
108 */
109 public function integrations_handler_callback() {
110 check_ajax_referer( 'wp_subs_integrations_nonce', 'nonce' );
111
112 $action_callback = ! empty( $_POST['action_callback'] ) ? sanitize_text_field( wp_unslash( $_POST['action_callback'] ) ) : '';
113
114 dd( '🔽 action_callback', $action_callback );
115 }
116
117 /**
118 * Check if a payment gateway is installed and active.
119 *
120 * @param string $gateway_id Gateway ID.
121 * @param bool $partial_match Whether to allow partial match of gateway ID.
122 * @return bool
123 */
124 public static function is_gateway_installed( $gateway_id, $partial_match = false ) {
125 $installed_gateways = WC()->payment_gateways()->payment_gateways();
126
127 // Partial match check. For gateways with dynamic IDs.
128 if ( $partial_match ) {
129 foreach ( $installed_gateways as $key => $gateway ) {
130 if ( strpos( $key, $gateway_id ) !== false ) {
131 return true;
132 }
133 }
134 return false;
135 }
136
137 return isset( $installed_gateways[ $gateway_id ] );
138 }
139
140 /**
141 * Check if a payment gateway is enabled.
142 *
143 * @param string $gateway_id Gateway ID.
144 * @param bool $partial_match Whether to allow partial match of gateway ID.
145 * @return bool
146 */
147 public static function is_gateway_enabled( $gateway_id, $partial_match = false ) {
148 $installed_gateways = WC()->payment_gateways()->payment_gateways();
149
150 // Partial match check. For gateways with dynamic IDs.
151 if ( $partial_match ) {
152 foreach ( $installed_gateways as $key => $gateway ) {
153 if ( strpos( $key, $gateway_id ) !== false ) {
154 return $gateway->is_available();
155 }
156 }
157 return false;
158 }
159
160 if ( ! isset( $installed_gateways[ $gateway_id ] ) ) {
161 return false;
162 }
163 return $installed_gateways[ $gateway_id ]->is_available();
164 }
165
166
167
168 /**
169 * Check if a gateway is installed by plugin file.
170 *
171 * @param string $plugin_file Plugin file path.
172 * @return bool
173 */
174 protected function is_plugin_installed( $plugin_file ) {
175 if ( ! function_exists( 'get_plugins' ) ) {
176 require_once ABSPATH . 'wp-admin/includes/plugin.php';
177 }
178 $plugins = get_plugins();
179 return isset( $plugins[ $plugin_file ] );
180 }
181
182 /**
183 * Check if a gateway plugin is active.
184 *
185 * @param string $plugin_file Plugin file path.
186 * @return bool
187 */
188 protected function is_plugin_active( $plugin_file ) {
189 return is_plugin_active( $plugin_file );
190 }
191
192 /**
193 * Check if a payment gateway is enabled.
194 *
195 * @param string $gateway_id Gateway ID.
196 */
197 public function is_payment_gateway_enabled( $gateway_id ) {
198 $gateways = WC()->payment_gateways->get_available_payment_gateways();
199 return isset( $gateways[ $gateway_id ] );
200 }
201
202 /**
203 * Get the list of integrations.
204 *
205 * @return array
206 */
207 protected function get_integrations(): array {
208 $integrations = [
209 [
210 'title' => 'PayPal',
211 'description' => 'Accept subscription payments via PayPal.',
212 'icon_url' => SUBSCRPT_ASSETS . '/images/paypal.svg',
213 'is_installed' => 'on' === get_option( 'wp_subs_paypal_integration_enabled', 'off' ),
214 'is_active' => self::is_gateway_enabled( 'wp_subscription_paypal' ),
215 'supports_recurring' => true,
216 'actions' => [
217 // [
218 // 'action' => 'install',
219 // 'label' => 'Install Now',
220 // 'type' => 'function',
221 // 'function' => 'wpSubsInstallPaypalIntegration()',
222 // ],
223 [
224 'action' => 'settings',
225 'label' => 'Settings',
226 'type' => 'link',
227 'url' => admin_url( 'admin.php?page=wc-settings&tab=checkout&section=wp_subscription_paypal' ),
228 ],
229 // [
230 // 'action' => 'uninstall',
231 // 'label' => 'Uninstall',
232 // 'type' => 'function',
233 // 'function' => 'wpSubsUninstallPaypalIntegration()',
234 // 'class' => 'button button-primary wp-subs-button-danger',
235 // ],
236 ],
237 ],
238 [
239 'title' => 'Stripe',
240 'description' => 'Process subscription payments securely with Stripe.',
241 'icon_url' => 'https://ps.w.org/woocommerce-gateway-stripe/assets/icon-256x256.png',
242 'is_installed' => class_exists( 'WC_Stripe' ),
243 'is_active' => self::is_gateway_enabled( 'stripe' ),
244 'supports_recurring' => true,
245 'actions' => [
246 [
247 'action' => 'install',
248 'label' => 'Install Now',
249 'type' => 'function',
250 'function' => "subscrptInstallPlugin(this, 'woocommerce-gateway-stripe')",
251 ],
252 [
253 'action' => 'settings',
254 'label' => 'Settings',
255 'type' => 'link',
256 'url' => admin_url( 'admin.php?page=wc-settings&tab=checkout&section=stripe&panel=settings' ),
257 ],
258 ],
259 ],
260 [
261 'title' => 'Paddle',
262 'description' => 'Process subscription payments securely with Paddle.',
263 'icon_url' => SUBSCRPT_ASSETS . '/images/paddle.svg',
264 'is_installed' => class_exists( 'SmartPayWoo\Gateways\Paddle\SmartPay_Paddle' ),
265 'is_active' => self::is_gateway_enabled( 'smartpay_paddle' ),
266 'supports_recurring' => true,
267 'actions' => [
268 [
269 'action' => 'install',
270 'label' => 'Get Paddle',
271 'type' => 'external_link',
272 'url' => 'https://wpsmartpay.com/paddle-for-woocommerce/',
273 ],
274 [
275 'action' => 'enable',
276 'label' => 'Enable Gateway',
277 'type' => 'toggle_option',
278 'option_key' => 'woocommerce_enable_paddle_gateway',
279 'value' => true,
280 ],
281 [
282 'action' => 'settings',
283 'label' => 'Settings',
284 'type' => 'link',
285 'url' => admin_url( 'admin.php?page=wc-settings&tab=checkout&section=smartpay_paddle&from=WCADMIN_PAYMENT_SETTINGS' ),
286 ],
287 [
288 'label' => 'More Details',
289 'type' => 'external_link',
290 'url' => 'https://wpsmartpay.com/paddle-for-woocommerce/',
291 ],
292 ],
293 ],
294 ];
295
296 // Add more integrations as needed.
297 $integrations = apply_filters( 'wpsubs_integrations', $integrations );
298
299 return $integrations;
300 }
301
302 /**
303 * Filter actions.
304 *
305 * @param array $integrations Integrations array.
306 */
307 protected function filter_integration_actions( array $integrations ): array {
308 $cleaned_integrations = [];
309
310 foreach ( $integrations as $integration ) {
311 $is_installed = $integration['is_installed'] ?? false;
312 $is_active = $integration['is_active'] ?? false;
313
314 $cleaned_actions = [];
315
316 foreach ( $integration['actions'] as $integration_action ) {
317 $action_tag = $integration_action['action'] ?? null;
318
319 if ( 'install' === $action_tag ) {
320 if ( ! $is_installed ) {
321 $cleaned_actions[] = $integration_action;
322 }
323 continue;
324 }
325 if ( 'uninstall' === $action_tag ) {
326 if ( $is_installed ) {
327 $cleaned_actions[] = $integration_action;
328 }
329 continue;
330 }
331 if ( 'enable' === $action_tag ) {
332 if ( $is_installed && ! $is_active ) {
333 $cleaned_actions[] = $integration_action;
334 }
335 continue;
336 }
337 if ( 'settings' === $action_tag ) {
338 if ( $is_installed ) {
339 $cleaned_actions[] = $integration_action;
340 }
341 continue;
342 }
343
344 // Default.
345 $cleaned_actions[] = $integration_action;
346 }
347
348 // Overwrite.
349 $integration['actions'] = $cleaned_actions;
350 $cleaned_integrations[] = $integration;
351 }
352
353 return $cleaned_integrations;
354 }
355
356 /**
357 * Render the Integrations admin page.
358 */
359 public function render_integrations_page() {
360 if ( ! empty( $_GET['subscrpt_installed'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
361 echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Plugin installed and activated successfully.', 'subscription' ) . '</p></div>';
362 }
363
364 $integrations = $this->integrations;
365 $integrations = $this->filter_integration_actions( $integrations );
366
367 // Integrations styles.
368 // wp_enqueue_style( 'wp-subs-integration-settings', SUBSCRPT_ASSETS . '/css/integration_settings.css', [], SUBSCRPT_VERSION, 'all' );
369
370 $menu = new \SpringDevs\Subscription\Admin\Menu();
371 $menu->render_admin_header( __( 'Integrations', 'subscription' ) );
372 include 'views/integrations.php';
373 $menu->render_admin_footer();
374 }
375 }
376