PluginProbe
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts / 2.3.1
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts v2.3.1
2.7.7 2.7.6 2.7.5 2.7.4 trunk 1.3 2.0.4 2.0.6 2.1.91 2.2.4 2.2.7 2.2.9 2.3.1 2.3.10 2.4.10 2.4.2 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.6.0 2.6.1 2.7.0 All 28 releases
insert-php / admin / includes / class.gutenberg.snippet.php

class.gutenberg.snippet.php in Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts 2.3.1, at admin/includes/class.gutenberg.snippet.php

175 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Snippet block for Gutenberg editor
5 *
6 * @author Webcraftic <wordpress.webraftic@gmail.com>
7 * @copyright (c) 11.12.2018, Webcraftic
8 * @version 1.0
9 */
10
11 // Exit if accessed directly
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 class WINP_Gutenberg_Snippet {
17
18 /**
19 * @var array
20 */
21 protected $shortcode_data;
22
23 /**
24 * WINP_Gutenberg_Snippet constructor.
25 */
26 public function __construct() {
27 $this->registerHooks();
28 }
29
30 private function registerHooks() {
31 add_action( 'init', array( $this, 'init' ) );
32 }
33
34 public function init() {
35 $this->shortcode_data = WINP_Helper::get_shortcode_data();
36
37 if ( empty( $this->shortcode_data ) ) {
38 return;
39 }
40
41 if ( function_exists( 'register_block_type' ) ) {
42 wp_register_script( 'wp-plugin-insert-php', WINP_PLUGIN_URL . '/admin/assets/gutenberg/build/index.build.js', array(
43 'wp-editor',
44 'wp-blocks',
45 'wp-element',
46 'wp-i18n'
47 ), WINP_PLugin::app()->getPluginVersion() );
48
49 wp_register_style( 'wp-plugin-insert-php', WINP_PLUGIN_URL . '/admin/assets/css/snippet-block.css', array() );
50
51 /**
52 * Register snippets object, so it can be accessible within Gutenberg.
53 */
54 wp_localize_script( 'wp-plugin-insert-php', 'winp_snippets', array(
55 'data' => $this->prepared_snippets_data()
56 ) );
57
58 register_block_type( 'wp-plugin-insert-php/winp-snippet', array(
59 'editor_script' => 'wp-plugin-insert-php',
60 'editor_style' => 'wp-plugin-insert-php',
61 'render_callback' => array( $this, 'render_snippet_content' )
62 ) );
63 }
64 }
65
66 /**
67 * Prepare snippets data.
68 *
69 * @return array
70 */
71 public function prepared_snippets_data() {
72 $prepared_object = array();
73
74 foreach ( (array) $this->shortcode_data as $item ) {
75
76 if ( ! isset( $item['id'] ) ) {
77 continue;
78 }
79
80 $tags = isset( $item['snippet_tags'] ) ? $item['snippet_tags'] : array();
81
82 $tags = array_values( array_filter( $tags, array( $this, 'snippet_tags_filter' ) ) );
83
84 $prepared_object[ $item['id'] ] = array(
85 'id' => $item['id'],
86 'title' => $item['title'],
87 'type' => isset( $item['type'] ) ? $item['type'] : '',
88 'tags' => $tags
89 );
90 }
91
92 return $prepared_object;
93 }
94
95 public function snippet_tags_filter( $tag ) {
96 return $tag !== 'id';
97 }
98
99 /**
100 * Renders snippets content which comes from Gutenber's render_callback.
101 *
102 * @param array $attributes Array list of attributes passed.
103 * @param string|null $content Block content.
104 *
105 * @return string
106 */
107 public function render_snippet_content( $attributes, $content ) {
108
109 $snipped_id = isset( $attributes['id'] ) ? $attributes['id'] : null;
110
111 if ( ! is_numeric( $snipped_id ) ) {
112 return '';
113 }
114
115 unset( $attributes['id'] );
116
117 $snippets = $this->prepared_snippets_data();
118 $current_snippet = isset( $snippets[ $snipped_id ] ) ? $snippets[ $snipped_id ] : null;
119
120 if ( empty( $current_snippet ) ) {
121 return '';
122 }
123
124 $snippet_attrs = $current_snippet['tags'];
125 $type = $current_snippet['type'];
126
127 $shortcode_attributes = apply_filters( 'wbcr/inp/gutenberg/shortcode_attributes', ' id="' . $snipped_id . '" ', $snipped_id );
128
129 $attr_values = isset( $attributes['attrValues'] ) ? $attributes['attrValues'] : null;
130 if ( ! empty( $attr_values ) ) {
131 if ( empty( $snippet_attrs ) ) {
132 return '';
133 }
134
135 if ( count( $snippet_attrs ) !== count( $attr_values ) ) {
136 return '';
137 }
138
139 foreach ( $attr_values as $key => $value ) {
140 $snippet_attr = $snippet_attrs[ $key ];
141 $value = esc_attr( $value );
142
143 if ( empty( $value ) ) {
144 continue;
145 }
146 $shortcode_attributes .= " {$snippet_attr}=\"{$value}\"";
147 }
148
149 $shortcode_attributes = trim( $shortcode_attributes );
150 }
151
152 $shortcode_name = apply_filters( 'wbcr/inp/gutenberg/shortcode_name', sprintf( "wbcr%s_snippet", ( $type === WINP_SNIPPET_TYPE_UNIVERSAL ? '' : '_' . $type ) ), $snipped_id );
153
154 $shortcode = "[{$shortcode_name} {$shortcode_attributes}]";
155
156 if ( ! empty( $content ) ) {
157 $shortcode .= "{$content}[/{$shortcode_name}]";
158 }
159
160 return do_shortcode( $shortcode );
161 }
162
163 function renderShippet( $attributes ) {
164 if ( empty( $attributes ) ) {
165 return;
166 }
167 ob_start();
168 ?>
169 <div style="background:red; color:white; padding:1em;"><?php esc_html_e( $attributes['content'] ); ?></div>
170 <?php
171 return ob_get_clean();
172 }
173
174 }
175