| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 3 |
|
| 4 |
if ( ! class_exists( 'ewdotpBlocks' ) ) { |
| 5 |
/** |
| 6 |
* Class to handle plugin Gutenberg blocks |
| 7 |
* |
| 8 |
* @since 3.0.0 |
| 9 |
*/ |
| 10 |
class ewdotpBlocks { |
| 11 |
|
| 12 |
public function __construct() { |
| 13 |
|
| 14 |
add_action( 'init', array( $this, 'add_blocks' ) ); |
| 15 |
|
| 16 |
add_filter( 'block_categories_all', array( $this, 'add_block_category' ) ); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Add the Gutenberg block to the list of available blocks |
| 21 |
* @since 3.0.0 |
| 22 |
*/ |
| 23 |
public function add_blocks() { |
| 24 |
|
| 25 |
if ( ! function_exists( 'render_block_core_block' ) ) { return; } |
| 26 |
|
| 27 |
$this->enqueue_assets(); |
| 28 |
|
| 29 |
$args = array( |
| 30 |
'attributes' => array( |
| 31 |
'show_orders' => array( |
| 32 |
'type' => 'string', |
| 33 |
), |
| 34 |
), |
| 35 |
'render_callback' => 'ewd_otp_tracking_form_shortcode', |
| 36 |
); |
| 37 |
|
| 38 |
register_block_type( 'order-tracking/ewd-otp-display-tracking-form-block', $args ); |
| 39 |
|
| 40 |
$args = array( |
| 41 |
'render_callback' => 'ewd_otp_customer_form_shortcode', |
| 42 |
); |
| 43 |
|
| 44 |
register_block_type( 'order-tracking/ewd-otp-display-customer-form-block', $args ); |
| 45 |
|
| 46 |
$args = array( |
| 47 |
'render_callback' => 'ewd_otp_sales_rep_form_shortcode', |
| 48 |
); |
| 49 |
|
| 50 |
register_block_type( 'order-tracking/ewd-otp-display-sales-rep-form-block', $args ); |
| 51 |
|
| 52 |
$args = array( |
| 53 |
'attributes' => array( |
| 54 |
'location' => array( |
| 55 |
'type' => 'string', |
| 56 |
), |
| 57 |
), |
| 58 |
'render_callback' => 'ewd_otp_customer_order_form_shortcode', |
| 59 |
); |
| 60 |
|
| 61 |
register_block_type( 'order-tracking/ewd-otp-display-customer-order-form-block', $args ); |
| 62 |
|
| 63 |
add_action( 'current_screen', array( $this, 'localize_data' ) ); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Localize data for use in block parameters |
| 68 |
* @since 3.0.0 |
| 69 |
*/ |
| 70 |
public function localize_data() { |
| 71 |
|
| 72 |
global $ewd_otp_controller; |
| 73 |
|
| 74 |
$screen = get_current_screen(); |
| 75 |
|
| 76 |
if ( ! $screen->is_block_editor and $screen->id != 'widgets' ) { return; } |
| 77 |
|
| 78 |
wp_enqueue_style( 'ewd-otp-css' ); |
| 79 |
wp_enqueue_style( 'ewd-otp-blocks-css' ); |
| 80 |
wp_enqueue_script( 'ewd-otp-blocks-js' ); |
| 81 |
|
| 82 |
$locations = ewd_otp_decode_infinite_table_setting( $ewd_otp_controller->settings->get_setting( 'locations' ) ); |
| 83 |
|
| 84 |
$location_options = array( array( 'value' => '', 'label' => '' ) ); |
| 85 |
foreach ( $locations as $location ) { |
| 86 |
$location_options[] = array( |
| 87 |
'value' => esc_attr( $location->name ), |
| 88 |
'label' => esc_attr( $location->name ), |
| 89 |
); |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
wp_add_inline_script( |
| 94 |
'ewd-otp-blocks-js', |
| 95 |
sprintf( |
| 96 |
'var ewd_otp_blocks = %s;', |
| 97 |
json_encode( array( |
| 98 |
'locationOptions' => $location_options, |
| 99 |
) ) |
| 100 |
), |
| 101 |
'before' |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Create a new category of blocks to hold our block |
| 107 |
* @since 3.0.0 |
| 108 |
*/ |
| 109 |
public function add_block_category( $categories ) { |
| 110 |
|
| 111 |
$categories[] = array( |
| 112 |
'slug' => 'ewd-otp-blocks', |
| 113 |
'title' => __( 'Order Tracking', 'order-tracking' ), |
| 114 |
); |
| 115 |
|
| 116 |
return $categories; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Register the necessary JS and CSS to display the block in the editor |
| 121 |
* @since 3.1.2 |
| 122 |
*/ |
| 123 |
public function enqueue_assets() { |
| 124 |
|
| 125 |
wp_register_style( 'ewd-otp-css', EWD_OTP_PLUGIN_URL . '/assets/css/ewd-otp.css', EWD_OTP_VERSION ); |
| 126 |
wp_register_style( 'ewd-otp-blocks-css', EWD_OTP_PLUGIN_URL . '/assets/css/ewd-otp-blocks.css', array( 'wp-edit-blocks' ), EWD_OTP_VERSION ); |
| 127 |
wp_register_script( 'ewd-otp-blocks-js', EWD_OTP_PLUGIN_URL . '/assets/js/ewd-otp-blocks.js', array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-editor', 'wp-server-side-render' ), EWD_OTP_VERSION ); |
| 128 |
} |
| 129 |
|
| 130 |
} |
| 131 |
} |