Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
38.78% covered (danger)
38.78%
19 / 49
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
PayPal_Payment_Buttons
40.43% covered (danger)
40.43%
19 / 47
0.00% covered (danger)
0.00%
0 / 7
55.44
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
89.47% covered (warning)
89.47%
17 / 19
0.00% covered (danger)
0.00%
0 / 1
4.02
 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        // Jetpack fills the same global, so add to it: wp_localize_script() would replace it.
138        wp_add_inline_script(
139            'jp-paypal-payments-ncps-blocks',
140            '( function ( data ) {
141    var state = ( window.Jetpack_Editor_Initial_State = window.Jetpack_Editor_Initial_State || {} );
142    state.available_blocks = Object.assign( {}, state.available_blocks, data.available_blocks );
143    state.feature_flags = Object.assign( {}, state.feature_flags, data.feature_flags );
144} )( ' . wp_json_encode( $availability_data, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ' );',
145            'before'
146        );
147    }
148
149    /**
150     * Plugin deactivation handler.
151     *
152     * @access public
153     * @static
154     */
155    public static function plugin_deactivation() {
156        // Cleanup on deactivation
157    }
158}