| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPCoder\Block; |
| 4 |
|
| 5 |
use WP_REST_Request; |
| 6 |
use WPCoder\WPCoder; |
| 7 |
|
| 8 |
class WPCoder_Block { |
| 9 |
public function __construct() { |
| 10 |
add_action( 'admin_enqueue_scripts', [ $this, 'admin_scripts' ] ); |
| 11 |
add_action( 'init', [ $this, 'register_block' ] ); |
| 12 |
add_action( 'rest_api_init', [ $this, 'rest_api' ] ); |
| 13 |
} |
| 14 |
|
| 15 |
public function admin_scripts( $hook ): void { |
| 16 |
wp_localize_script( 'wpcoder-block-editor', 'wpcoderBlockData', array( |
| 17 |
'editBaseUrl' => admin_url( 'admin.php?page=' . WPCoder::SLUG . '-settings' ) |
| 18 |
) ); |
| 19 |
} |
| 20 |
|
| 21 |
public function register_block(): void { |
| 22 |
wp_register_script( |
| 23 |
'wpcoder-block-editor', |
| 24 |
plugins_url( 'block.js', __FILE__ ), |
| 25 |
array( 'wp-blocks', 'wp-element', 'wp-editor', 'wp-block-editor', 'wp-components' ), |
| 26 |
filemtime( plugin_dir_path( __FILE__ ) . 'block.js' ) |
| 27 |
); |
| 28 |
|
| 29 |
register_block_type( 'wpcoder/shortcode', array( |
| 30 |
'editor_script' => 'wpcoder-block-editor', |
| 31 |
'render_callback' => [ $this, 'render_block' ], |
| 32 |
'attributes' => array( |
| 33 |
'id' => array( |
| 34 |
'type' => 'string', |
| 35 |
'default' => '' |
| 36 |
), |
| 37 |
), |
| 38 |
'icon' => 'editor-code', |
| 39 |
) ); |
| 40 |
} |
| 41 |
|
| 42 |
public function render_block( $attributes ): string { |
| 43 |
if ( empty( $attributes['id'] ) ) { |
| 44 |
return '<p style="color:red;">' . esc_html__( 'Please select a WP Coder snippet.', 'wpcoderpro' ) . '</p>'; |
| 45 |
} |
| 46 |
|
| 47 |
$shortcode = '['.WPCoder::SHORTCODE.' id="' . (int) $attributes['id'] . '"'; |
| 48 |
|
| 49 |
if ( ! empty( $attributes['extraAttrs'] ) && is_array( $attributes['extraAttrs'] ) ) { |
| 50 |
foreach ( $attributes['extraAttrs'] as $attr ) { |
| 51 |
if ( ! empty( $attr['key'] ) ) { |
| 52 |
$val = esc_attr( $attr['value'] ?? '' ); |
| 53 |
$shortcode .= ' ' . sanitize_key( $attr['key'] ) . '="' . $val . '"'; |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
$shortcode .= ']'; |
| 59 |
|
| 60 |
return do_shortcode( $shortcode ); |
| 61 |
} |
| 62 |
|
| 63 |
public function rest_api(): void { |
| 64 |
$permission = static function () { |
| 65 |
return current_user_can( 'edit_posts' ); |
| 66 |
}; |
| 67 |
|
| 68 |
register_rest_route( 'wpcoder/v1', '/preview', array( |
| 69 |
'methods' => 'POST', |
| 70 |
'callback' => [ $this, 'preview_callback' ], |
| 71 |
'permission_callback' => $permission |
| 72 |
)); |
| 73 |
|
| 74 |
register_rest_route( 'wpcoder/v1', '/attributes', array( |
| 75 |
'methods' => 'POST', |
| 76 |
'callback' => [ $this, 'attributes_callback' ], |
| 77 |
'permission_callback' => $permission |
| 78 |
)); |
| 79 |
} |
| 80 |
|
| 81 |
public function preview_callback( WP_REST_Request $request ): array { |
| 82 |
global $wpdb; |
| 83 |
|
| 84 |
$id = (int) $request['id']; |
| 85 |
$extra = $request['extraAttrs'] ?? []; |
| 86 |
|
| 87 |
$shortcode = '['.WPCoder::SHORTCODE.' id="' . $id . '"'; |
| 88 |
|
| 89 |
if ( ! empty($extra) && is_array($extra) ) { |
| 90 |
foreach ( $extra as $attr ) { |
| 91 |
if ( ! empty($attr['key']) && $attr['value'] !== '' ) { |
| 92 |
$shortcode .= ' ' . esc_attr($attr['key']) . '="' . esc_attr($attr['value']) . '"'; |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
$shortcode .= ']'; |
| 98 |
|
| 99 |
$table = $wpdb->prefix . WPCoder::PREFIX; |
| 100 |
|
| 101 |
$snippet = $wpdb->get_row( $wpdb->prepare( |
| 102 |
"SELECT html_code, css_code FROM $table WHERE id = %d", |
| 103 |
$id |
| 104 |
) ); |
| 105 |
|
| 106 |
if ( ! $snippet ) { |
| 107 |
return [ |
| 108 |
'html' => '<p style="color:red;">'.__('Snippet not found', 'wpcoderpro').'</p>', |
| 109 |
'css' => '', |
| 110 |
]; |
| 111 |
} |
| 112 |
|
| 113 |
$html = do_shortcode( $shortcode ); |
| 114 |
|
| 115 |
return [ |
| 116 |
'html' => $html, |
| 117 |
'css' => $snippet->css_code, |
| 118 |
]; |
| 119 |
} |
| 120 |
|
| 121 |
public function attributes_callback( WP_REST_Request $request ): array { |
| 122 |
global $wpdb; |
| 123 |
$id = (int) $request['id']; |
| 124 |
$table = $wpdb->prefix . WPCoder::PREFIX; |
| 125 |
|
| 126 |
$row = $wpdb->get_row( $wpdb->prepare( |
| 127 |
"SELECT param FROM $table WHERE id = %d", $id |
| 128 |
) ); |
| 129 |
|
| 130 |
if ( ! $row ) { |
| 131 |
return []; |
| 132 |
} |
| 133 |
|
| 134 |
$attrs = []; |
| 135 |
if ( ! empty( $row->param ) ) { |
| 136 |
$data = maybe_unserialize( $row->param ); |
| 137 |
|
| 138 |
if ( isset( $data['shortcode_attribute'] ) && is_array( $data['shortcode_attribute'] ) ) { |
| 139 |
foreach ( $data['shortcode_attribute'] as $attr ) { |
| 140 |
$attrs[] = array( |
| 141 |
'key' => $attr, |
| 142 |
'value' => '' |
| 143 |
); |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
return $attrs; |
| 149 |
} |
| 150 |
} |