| @@ -1,174 +1,121 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -/** | |
| 4 | - * Snippet block for Gutenberg editor | |
| 5 | - * | |
| 6 | - * @author Webcraftic <[email protected]> | |
| 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 | -} | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Snippet block for Gutenberg editor | |
| 4 | + * | |
| 5 | + * @package Woody_Code_Snippets | |
| 6 | + */ | |
| 7 | + | |
| 8 | +// Exit if accessed directly. | |
| 9 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 10 | + exit; | |
| 11 | +} | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * WINP_Gutenberg_Snippet Class | |
| 15 | + */ | |
| 16 | +class WINP_Gutenberg_Snippet { | |
| 17 | + | |
| 18 | + /** | |
| 19 | + * Shortcode data. | |
| 20 | + * | |
| 21 | + * @var array<int, array<string, mixed>> | |
| 22 | + */ | |
| 23 | + protected $shortcode_data; | |
| 24 | + | |
| 25 | + /** | |
| 26 | + * WINP_Gutenberg_Snippet constructor. | |
| 27 | + */ | |
| 28 | + public function __construct() { | |
| 29 | + $this->register_hooks(); | |
| 30 | + } | |
| 31 | + | |
| 32 | + /** | |
| 33 | + * Register WordPress hooks. | |
| 34 | + * | |
| 35 | + * @return void | |
| 36 | + */ | |
| 37 | + private function register_hooks() { | |
| 38 | + add_action( 'init', [ $this, 'init' ] ); | |
| 39 | + } | |
| 40 | + | |
| 41 | + /** | |
| 42 | + * Initialize the Gutenberg block. | |
| 43 | + * | |
| 44 | + * @return void | |
| 45 | + */ | |
| 46 | + public function init() { | |
| 47 | + $this->shortcode_data = WINP_Helper::get_shortcode_data(); | |
| 48 | + | |
| 49 | + // Register the block type and get the WP_Block_Type instance. | |
| 50 | + $block_type = $this->register_block(); | |
| 51 | + | |
| 52 | + /** | |
| 53 | + * Register snippets object, so it can be accessible within Gutenberg. | |
| 54 | + * The script handle is automatically generated by WordPress from the block name. | |
| 55 | + */ | |
| 56 | + if ( $block_type && ! empty( $block_type->editor_script_handles ) ) { | |
| 57 | + wp_localize_script( | |
| 58 | + $block_type->editor_script_handles[0], | |
| 59 | + 'winp_snippets', | |
| 60 | + [ | |
| 61 | + 'data' => $this->prepared_snippets_data(), | |
| 62 | + 'createUrl' => admin_url( 'post-new.php?post_type=' . WINP_SNIPPETS_POST_TYPE ), | |
| 63 | + ] | |
| 64 | + ); | |
| 65 | + } | |
| 66 | + } | |
| 67 | + | |
| 68 | + /** | |
| 69 | + * Register the block type. | |
| 70 | + * | |
| 71 | + * @return WP_Block_Type|false | |
| 72 | + */ | |
| 73 | + protected function register_block() { | |
| 74 | + return register_block_type( WINP_PLUGIN_DIR . '/admin/assets/gutenberg/build/block.json' ); | |
| 75 | + } | |
| 76 | + | |
| 77 | + /** | |
| 78 | + * Prepare snippets data for the block editor. | |
| 79 | + * | |
| 80 | + * @return array<int, array<string, mixed>> | |
| 81 | + */ | |
| 82 | + public function prepared_snippets_data() { | |
| 83 | + return self::get_prepared_snippets_data(); | |
| 84 | + } | |
| 85 | + | |
| 86 | + /** | |
| 87 | + * Get prepared snippets data statically for use in render.php. | |
| 88 | + * | |
| 89 | + * @return array<int, array<string, mixed>> | |
| 90 | + */ | |
| 91 | + public static function get_prepared_snippets_data() { | |
| 92 | + $shortcode_data = WINP_Helper::get_shortcode_data(); | |
| 93 | + $prepared_object = []; | |
| 94 | + | |
| 95 | + foreach ( (array) $shortcode_data as $item ) { | |
| 96 | + | |
| 97 | + if ( ! isset( $item['id'] ) ) { | |
| 98 | + continue; | |
| 99 | + } | |
| 100 | + | |
| 101 | + $tags = isset( $item['snippet_tags'] ) ? $item['snippet_tags'] : []; | |
| 102 | + $tags = array_values( | |
| 103 | + array_filter( | |
| 104 | + $tags, | |
| 105 | + function ( $tag ) { | |
| 106 | + return 'id' !== $tag; | |
| 107 | + } | |
| 108 | + ) | |
| 109 | + ); | |
| 110 | + | |
| 111 | + $prepared_object[ $item['id'] ] = [ | |
| 112 | + 'id' => $item['id'], | |
| 113 | + 'title' => $item['title'], | |
| 114 | + 'type' => isset( $item['type'] ) ? $item['type'] : '', | |
| 115 | + 'tags' => $tags, | |
| 116 | + ]; | |
| 117 | + } | |
| 118 | + | |
| 119 | + return $prepared_object; | |
| 120 | + } | |
| 121 | +} | |