Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
4.88% covered (danger)
4.88%
2 / 41
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
PayPal_Payment_Buttons
5.13% covered (danger)
5.13%
2 / 39
0.00% covered (danger)
0.00%
0 / 7
157.31
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 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 register_paypal_block
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 enqueue_block_availability_data
0.00% covered (danger)
0.00%
0 / 17
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        // Initialize PayPal Payment Buttons block with correct dist path
64        add_action( 'init', array( $this, 'register_paypal_block' ), 9 );
65
66        // Load scripts for the editing interface
67        add_action( 'enqueue_block_editor_assets', array( Jetpack_PayPal_Payment_Buttons::class, 'load_editor_scripts' ), 9 );
68
69        // Provide block availability data for editor
70        add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_availability_data' ), 10 );
71
72        // Load styles in the editor iframe context
73        if ( is_admin() ) {
74            add_action( 'enqueue_block_assets', array( Jetpack_PayPal_Payment_Buttons::class, 'load_editor_styles' ), 9 );
75        }
76    }
77
78    /**
79     * Register the PayPal Payment Buttons block with the correct dist path.
80     */
81    public function register_paypal_block() {
82        // Get the path to the dist directory in the paypal-payments package
83        $package_dir = dirname( __DIR__ ) . '/jetpack_vendor/automattic/jetpack-paypal-payments';
84        $dist_dir    = $package_dir . '/dist/paypal-payment-buttons';
85
86        if ( ! is_dir( $dist_dir ) ) {
87            return false;
88        }
89
90        // Register the block using the Blocks package with the correct dist path
91        Blocks::jetpack_register_block(
92            $dist_dir,
93            array(
94                'render_callback' => array( Jetpack_PayPal_Payment_Buttons::class, 'render_block' ),
95            )
96        );
97    }
98
99    /**
100     * Enqueue block availability data for the editor.
101     */
102    public function enqueue_block_availability_data() {
103        // Only enqueue in the block editor
104        if ( ! function_exists( 'get_current_screen' ) ) {
105            return;
106        }
107
108        $screen = get_current_screen();
109        if ( ! $screen || ! $screen->is_block_editor() ) {
110            return;
111        }
112
113        // Provide the availability data that the block registration JavaScript expects
114        $availability_data = array(
115            'available_blocks' => array(
116                'paypal-payment-buttons' => array(
117                    'available' => true,
118                ),
119            ),
120        );
121
122        wp_localize_script(
123            'jp-paypal-payments-ncps-blocks',
124            'Jetpack_Editor_Initial_State',
125            $availability_data
126        );
127    }
128
129    /**
130     * Plugin deactivation handler.
131     *
132     * @access public
133     * @static
134     */
135    public static function plugin_deactivation() {
136        // Cleanup on deactivation
137    }
138}