PluginProbe
WP Coder – Insert & Manage Code Snippets / 4.5
WP Coder – Insert & Manage Code Snippets v4.5
4.5.1 1.1 2.3.1 2.3.2 2.4.1 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1 3.1.1 3.2 3.2.1 3.3 3.4 3.5 3.5.1 All 39 releases
wp-coder / classes / Block / WPCoder_Block.php

WPCoder_Block.php in WP Coder – Insert & Manage Code Snippets 4.5, at classes/Block/WPCoder_Block.php

146 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 register_rest_route( 'wpcoder/v1', '/preview', array(
65 'methods' => 'POST',
66 'callback' => [ $this, 'preview_callback' ],
67 'permission_callback' => '__return_true'
68 ));
69
70 register_rest_route( 'wpcoder/v1', '/attributes', array(
71 'methods' => 'POST',
72 'callback' => [ $this, 'attributes_callback' ],
73 'permission_callback' => '__return_true'
74 ));
75 }
76
77 public function preview_callback( WP_REST_Request $request ): array {
78 global $wpdb;
79
80 $id = (int) $request['id'];
81 $extra = $request['extraAttrs'] ?? [];
82
83 $shortcode = '['.WPCoder::SHORTCODE.' id="' . $id . '"';
84
85 if ( ! empty($extra) && is_array($extra) ) {
86 foreach ( $extra as $attr ) {
87 if ( ! empty($attr['key']) && $attr['value'] !== '' ) {
88 $shortcode .= ' ' . esc_attr($attr['key']) . '="' . esc_attr($attr['value']) . '"';
89 }
90 }
91 }
92
93 $shortcode .= ']';
94
95 $table = $wpdb->prefix . WPCoder::PREFIX;
96
97 $snippet = $wpdb->get_row( $wpdb->prepare(
98 "SELECT html_code, css_code FROM $table WHERE id = %d",
99 $id
100 ) );
101
102 if ( ! $snippet ) {
103 return [
104 'html' => '<p style="color:red;">'.__('Snippet not found', 'wpcoderpro').'</p>',
105 'css' => '',
106 ];
107 }
108
109 $html = do_shortcode( $shortcode );
110
111 return [
112 'html' => $html,
113 'css' => $snippet->css_code,
114 ];
115 }
116
117 public function attributes_callback( WP_REST_Request $request ): array {
118 global $wpdb;
119 $id = (int) $request['id'];
120 $table = $wpdb->prefix . WPCoder::PREFIX;
121
122 $row = $wpdb->get_row( $wpdb->prepare(
123 "SELECT param FROM $table WHERE id = %d", $id
124 ) );
125
126 if ( ! $row ) {
127 return [];
128 }
129
130 $attrs = [];
131 if ( ! empty( $row->param ) ) {
132 $data = maybe_unserialize( $row->param );
133
134 if ( isset( $data['shortcode_attribute'] ) && is_array( $data['shortcode_attribute'] ) ) {
135 foreach ( $data['shortcode_attribute'] as $attr ) {
136 $attrs[] = array(
137 'key' => $attr,
138 'value' => ''
139 );
140 }
141 }
142 }
143
144 return $attrs;
145 }
146 }