Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
4.17% covered (danger)
4.17%
2 / 48
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
PayPal_Payment_Buttons
4.35% covered (danger)
4.35%
2 / 46
0.00% covered (danger)
0.00%
0 / 7
185.53
0.00% covered (danger)
0.00%
0 / 1
 instance
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 init
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 init_hooks
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 register_paypal_block
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 enqueue_block_availability_data
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
20
 plugin_deactivation
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Primary class file for the PayPal Payment Buttons plugin.
4 *
5 * @package automattic/paypal-payment-buttons
6 */
7
8if ( ! defined( 'ABSPATH' ) ) {
9    exit( 0 );
10}
11
12use Automattic\Jetpack\Blocks;
13use Automattic\Jetpack\PaypalPayments\PayPal_Payment_Buttons as Jetpack_PayPal_Payment_Buttons;
14
15/**
16 * Class PayPal_Payment_Buttons
17 */
18class PayPal_Payment_Buttons {
19
20    /**
21     * Plugin instance.
22     *
23     * @var PayPal_Payment_Buttons
24     */
25    private static $instance = null;
26
27    /**
28     * Get plugin instance.
29     *
30     * @return PayPal_Payment_Buttons
31     */
32    public static function instance() {
33        if ( null === self::$instance ) {
34            self::$instance = new self();
35        }
36
37        return self::$instance;
38    }
39
40    /**
41     * Initialize the plugin and register hooks.
42     *
43     * @return void
44     */
45    public static function init() {
46        $instance = self::instance();
47        $instance->init_hooks();
48    }
49
50    /**
51     * Constructor.
52     */
53    private function __construct() {
54        // Private constructor to prevent direct instantiation
55    }
56
57    /**
58     * Initialize WordPress hooks.
59     *
60     * @return void
61     */
62    public function init_hooks() {
63        // The API-managed buttons ship behind a flag; register it before anything reads it.
64        Jetpack_PayPal_Payment_Buttons::register_feature_flags();
65
66        // Initialize PayPal Payment Buttons block with correct dist path
67        add_action( 'init', array( $this, 'register_paypal_block' ), 9 );
68
69        // Initialize PayPal API integration (REST routes for OAuth + button management).
70        Jetpack_PayPal_Payment_Buttons::init_api();
71
72        // Initialize admin dashboard (Payment Links list page).
73        if ( is_admin() ) {
74            Jetpack_PayPal_Payment_Buttons::init_admin();
75        }
76
77        // Load scripts for the editing interface
78        add_action( 'enqueue_block_editor_assets', array( Jetpack_PayPal_Payment_Buttons::class, 'load_editor_scripts' ), 9 );
79
80        // Provide block availability data for editor
81        add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_availability_data' ), 10 );
82
83        // Load styles in the editor iframe context
84        if ( is_admin() ) {
85            add_action( 'enqueue_block_assets', array( Jetpack_PayPal_Payment_Buttons::class, 'load_editor_styles' ), 9 );
86        }
87    }
88
89    /**
90     * Register the PayPal Payment Buttons block with the correct dist path.
91     */
92    public function register_paypal_block() {
93        // Get the path to the dist directory in the paypal-payments package
94        $package_dir = dirname( __DIR__ ) . '/jetpack_vendor/automattic/jetpack-paypal-payments';
95        $dist_dir    = $package_dir . '/dist/paypal-payment-buttons';
96
97        if ( ! is_dir( $dist_dir ) ) {
98            return false;
99        }
100
101        Jetpack_PayPal_Payment_Buttons::register_block_style();
102
103        // Register the block using the Blocks package with the correct dist path
104        Blocks::jetpack_register_block(
105            $dist_dir,
106            array(
107                'render_callback' => array( Jetpack_PayPal_Payment_Buttons::class, 'render_block' ),
108                'style'           => Jetpack_PayPal_Payment_Buttons::STYLE_HANDLE,
109            )
110        );
111    }
112
113    /**
114     * Enqueue block availability data for the editor.
115     */
116    public function enqueue_block_availability_data() {
117        // Only enqueue in the block editor
118        if ( ! function_exists( 'get_current_screen' ) ) {
119            return;
120        }
121
122        $screen = get_current_screen();
123        if ( ! $screen || ! $screen->is_block_editor() ) {
124            return;
125        }
126
127        // Provide the availability data that the block registration JavaScript expects
128        $availability_data = array(
129            'available_blocks' => array(
130                'paypal-payment-buttons' => array(
131                    'available' => true,
132                ),
133            ),
134            'feature_flags'    => Jetpack_PayPal_Payment_Buttons::add_editor_feature_flags( array() ),
135        );
136
137        wp_localize_script(
138            'jp-paypal-payments-ncps-blocks',
139            'Jetpack_Editor_Initial_State',
140            $availability_data
141        );
142    }
143
144    /**
145     * Plugin deactivation handler.
146     *
147     * @access public
148     * @static
149     */
150    public static function plugin_deactivation() {
151        // Cleanup on deactivation
152    }
153}