FreeShippingBlock.php
104 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPDesk\FS\Blocks\FreeShipping; |
| 4 | |
| 5 | use FSVendor\WPDesk\PluginBuilder\Plugin\Hookable; |
| 6 | use WPDesk\FS\TableRate\FreeShipping\FreeShippingNoticeData; |
| 7 | |
| 8 | class FreeShippingBlock implements Hookable { |
| 9 | |
| 10 | const BLOCK_NAME = 'flexible-shipping/free-shipping-notice'; |
| 11 | |
| 12 | /** |
| 13 | * @var string[] |
| 14 | */ |
| 15 | private $session_variable_names; |
| 16 | |
| 17 | /** |
| 18 | * FreeShippingBlock constructor. |
| 19 | * |
| 20 | * @param string[] $session_variable_names . |
| 21 | */ |
| 22 | public function __construct( $session_variable_names ) { |
| 23 | if ( ! is_array( $session_variable_names ) ) { |
| 24 | $session_variable_names = []; |
| 25 | } |
| 26 | $this->session_variable_names = $session_variable_names; |
| 27 | } |
| 28 | |
| 29 | public function hooks() { |
| 30 | add_action( 'init', [ $this, 'create_free_shipping_notice_block' ] ); |
| 31 | add_action( 'rest_api_init', [ $this, 'add_free_shipping_notice_endpoint' ] ); |
| 32 | } |
| 33 | |
| 34 | public function add_free_shipping_notice_endpoint() { |
| 35 | register_rest_route( |
| 36 | 'flexible-shipping/v1', |
| 37 | '/free-shipping-notice', |
| 38 | [ |
| 39 | 'methods' => 'GET', |
| 40 | 'callback' => [ $this, 'get_free_shipping_notices' ], |
| 41 | 'permission_callback' => '__return_true', |
| 42 | ] |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | public function get_free_shipping_notices() { |
| 47 | $free_shipping_notices = []; |
| 48 | foreach ($this->get_free_shipping_notices_data() as $free_shipping_notice_data) { |
| 49 | $free_shipping_notices[] = [ |
| 50 | 'content' => apply_filters( 'flexible-shipping/free-shipping/render-notice', $free_shipping_notice_data ), |
| 51 | 'blocks' => apply_filters( 'flexible-shipping/free-shipping-block/allowed-blocks', [ |
| 52 | 'checkout', |
| 53 | 'cart' |
| 54 | ], $free_shipping_notice_data ), |
| 55 | ]; |
| 56 | } |
| 57 | |
| 58 | return $free_shipping_notices; |
| 59 | } |
| 60 | |
| 61 | public function create_free_shipping_notice_block() { |
| 62 | register_block_type( |
| 63 | __DIR__ . '/../../../../../assets/blocks/free-shipping-notice', |
| 64 | [ |
| 65 | 'render_callback' => [ $this, 'render' ], |
| 66 | ] |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | public function render() { |
| 71 | return '<div class="flexible-shipping-free-shipping-root"></div>'; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @return FreeShippingNoticeData[] |
| 76 | */ |
| 77 | private function get_free_shipping_notices_data() { |
| 78 | $free_shipping_notice_data = []; |
| 79 | $session_variable_names = apply_filters( 'flexible-shipping/free-shipping-block/session-variables', $this->session_variable_names ); |
| 80 | if ( ! is_array( $session_variable_names) ) { |
| 81 | return []; |
| 82 | } |
| 83 | foreach ( $session_variable_names as $session_variable_name ) { |
| 84 | $session_data = $this->get_session()->get( $session_variable_name, '' ); |
| 85 | if ( $session_data instanceof FreeShippingNoticeData ) { |
| 86 | $free_shipping_notice_data[] = $session_data; |
| 87 | } |
| 88 | if ( is_array( $session_data ) ) { |
| 89 | $free_shipping_notice_data[] = FreeShippingNoticeData::create_from_array( $session_data ); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return $free_shipping_notice_data; |
| 94 | } |
| 95 | |
| 96 | protected function get_session() { |
| 97 | if ( WC()->session === null ) { |
| 98 | WC()->initialize_session(); |
| 99 | } |
| 100 | return WC()->session; |
| 101 | } |
| 102 | |
| 103 | } |
| 104 |