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

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