PluginProbe
LIQUID BLOCKS – Slider, Carousel, Accordion / 1.3.5
LIQUID BLOCKS – Slider, Carousel, Accordion v1.3.5
1.3.7 1.3.6 trunk 1.0 1.0.2 1.0.3 1.0.4 1.0.5 1.0.8 1.0.9 1.1.1 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5
liquid-blocks / liquid-blocks.php

liquid-blocks.php in LIQUID BLOCKS – Slider, Carousel, Accordion 1.3.5, at liquid-blocks.php

978 lines 41.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: LIQUID BLOCKS
4 Plugin URI: https://lqd.jp/wp/plugin.html
5 Description: A magic tool to master WordPress blocks.
6 Author: LIQUID DESIGN Ltd.
7 Author URI: https://lqd.jp/wp/
8 License: GPLv2
9 License URI: https://www.gnu.org/licenses/gpl-2.0.html
10 Text Domain: liquid-blocks
11 Version: 1.3.5
12 Requires at least: 6.0
13 */
14 /* Copyright 2019 LIQUID DESIGN Ltd. (email : info@lqd.jp)
15
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License, version 2, as
18 published by the Free Software Foundation.
19
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24
25 You should have received a copy of the GNU General Public License
26 along with this program; if not, write to the Free Software
27 */
28
29 // ------------------------------------
30 // Plugin
31 // ------------------------------------
32 $liquid_blocks_data = get_file_data( __FILE__, array( 'version' => 'Version'));
33
34 // api
35 if ( is_admin() ) {
36 $liquid_blocks_api = require_once 'inc/api.php';
37 $liquid_blocks_json = $liquid_blocks_api('https://lqd.jp/wp/data/p/liquid-blocks.json', 'liquid_blocks_json');
38 }
39
40 // notices
41 function liquid_blocks_admin_notices() {
42 global $liquid_blocks_json;
43 if( !empty($liquid_blocks_json) ) {
44 if ( isset( $_GET['liquid_admin_notices_dismissed'] ) && current_user_can( 'manage_options' ) ) {
45 set_transient( 'liquid_admin_notices', 'dismissed', 60*60*24*30 );
46 }
47 if ( isset( $_GET['liquid_admin_offer_dismissed'] ) && current_user_can( 'manage_options' ) ) {
48 set_transient( 'liquid_admin_offer', 'dismissed', 60*60*24*30 );
49 }
50 if( !empty($liquid_blocks_json['news']) && get_transient( 'liquid_admin_notices' ) != 'dismissed' ){
51 echo '<div class="notice notice-info" style="position: relative;"><p>'.wp_kses_post($liquid_blocks_json['news']).'</p><a href="?liquid_admin_notices_dismissed" style="position: absolute; right: 10px; top: 10px;">&times;</a></div>';
52 }
53 if( !empty($liquid_blocks_json['offer']) && get_transient( 'liquid_admin_offer' ) != 'dismissed' ){
54 echo '<div class="notice notice-info" style="position: relative;"><p>'.wp_kses_post($liquid_blocks_json['offer']).'</p><a href="?liquid_admin_offer_dismissed" style="position: absolute; right: 10px; top: 10px;">&times;</a></div>';
55 }
56 }
57 }
58 add_action( 'admin_notices', 'liquid_blocks_admin_notices' );
59
60 // get_option
61 $liquid_blocks_toggle = get_option( 'liquid_blocks_toggle' );
62 $liquid_blocks_no = get_option( 'liquid_blocks_no' ) ? get_option( 'liquid_blocks_no' ) : [];
63 $liquid_blocks_type = get_option( 'liquid_blocks_type' ) ? get_option( 'liquid_blocks_type' ) : [];
64 $liquid_blocks_name = get_option( 'liquid_blocks_name' ) ? get_option( 'liquid_blocks_name' ) : [];
65 $liquid_blocks_clean = get_option( 'liquid_blocks_clean' ) ? get_option( 'liquid_blocks_clean' ) : [];
66 if( empty( $liquid_blocks_toggle ) ){
67 add_action( 'init', 'liquid_blocks_init' );
68 add_action( 'enqueue_block_assets', 'liquid_blocks_assets' );
69 add_action( 'enqueue_block_editor_assets', 'liquid_blocks_enqueue_block_editor_assets' );
70 add_action( 'admin_footer', 'liquid_blocks_admin_footer' );
71 add_action( 'plugins_loaded', function () {
72 load_plugin_textdomain(
73 'liquid-blocks',
74 false,
75 dirname(plugin_basename(__FILE__)) . '/languages/'
76 );
77 } );
78 }
79
80 // plugin_action_links
81 function liquid_blocks_plugin_action_links( $links ) {
82 $settings = '<a href="' . esc_url( admin_url('options-general.php?page=liquid-blocks') ) . '">'
83 . esc_html__( 'Settings', 'liquid-blocks' ) . '</a>';
84 $pro = '<a href="' . esc_url( 'https://lqd.jp/wp/plugin/blocks_pro.html' ) . '" target="_blank" rel="noopener" style="font-weight:bold;">'
85 . esc_html__( 'Get Pro', 'liquid-blocks' ) . '</a>';
86 array_unshift( $links, $pro, $settings );
87 return $links;
88 }
89 add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), 'liquid_blocks_plugin_action_links' );
90
91 function liquid_blocks_assets() {
92 global $liquid_blocks_data;
93 wp_enqueue_style( 'liquid-blocks', plugins_url( 'css/block.css' , __FILE__ ), array( 'swiper' ), $liquid_blocks_data['version'] );
94 // slider
95 wp_enqueue_style( 'swiper', plugins_url( 'css/swiper-bundle.min.css' , __FILE__ ), array() );
96 wp_enqueue_script( 'swiper', plugins_url( 'lib/swiper-bundle.min.js' , __FILE__ ), array() );
97 wp_enqueue_script( 'liquid-blocks', plugins_url( 'lib/liquid-blocks.js' , __FILE__ ), array( 'swiper' ) );
98 }
99
100 function liquid_blocks_enqueue_block_editor_assets(){
101 global $liquid_blocks_data, $liquid_blocks_no, $liquid_blocks_type, $liquid_blocks_name;
102 // wp_enqueue_script
103 wp_enqueue_script( 'clipboard' );
104 wp_enqueue_script( 'liquid-blocks-template', plugins_url( 'lib/template.js', __FILE__ ), array(), $liquid_blocks_data['version'] );
105 wp_enqueue_script( 'liquid-blocks-block', plugins_url( 'lib/block.js', __FILE__ ), array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-block-editor', 'wp-plugins', 'wp-edit-post', 'wp-components', 'liquid-blocks-template', 'clipboard' ), $liquid_blocks_data['version'] );
106 wp_enqueue_script( 'liquid-blocks-cards', plugins_url( 'lib/block-cards.js', __FILE__ ), array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-block-editor', 'wp-components' ) );
107 wp_enqueue_script( 'liquid-blocks-tabs', plugins_url( 'lib/block-tabs.js', __FILE__ ), array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-block-editor', 'wp-components' ) );
108 wp_enqueue_script( 'liquid-blocks-table', plugins_url( 'lib/block-table.js', __FILE__ ), array('wp-blocks', 'wp-i18n', 'wp-hooks', 'wp-element', 'wp-components', 'wp-compose', 'wp-block-editor') );
109 wp_enqueue_script( 'liquid-blocks-fixed', plugins_url( 'lib/block-fixed.js', __FILE__ ), array( 'wp-blocks', 'wp-i18n', 'wp-hooks', 'wp-element', 'wp-components', 'wp-compose', 'wp-block-editor', 'wp-data' ) );
110
111 // wp_set_script_translations
112 wp_set_script_translations( 'liquid-blocks', 'liquid-blocks', plugin_dir_path( __FILE__ ) . 'languages' );
113 wp_set_script_translations( 'liquid-blocks-block', 'liquid-blocks', plugin_dir_path( __FILE__ ) . 'languages' );
114 wp_set_script_translations( 'liquid-blocks-cards', 'liquid-blocks', plugin_dir_path( __FILE__ ) . 'languages' );
115 wp_set_script_translations( 'liquid-blocks-tabs', 'liquid-blocks', plugin_dir_path( __FILE__ ) . 'languages' );
116 wp_set_script_translations( 'liquid-blocks-slider', 'liquid-blocks', plugin_dir_path( __FILE__ ) . 'languages' );
117 wp_set_script_translations( 'liquid-blocks-custom-field', 'liquid-blocks', plugin_dir_path( __FILE__ ) . 'languages' );
118 wp_set_script_translations( 'liquid-blocks-if-conditions', 'liquid-blocks', plugin_dir_path( __FILE__ ) . 'languages' );
119 wp_set_script_translations( 'liquid-blocks-table', 'liquid-blocks', plugin_dir_path( __FILE__ ) . 'languages' );
120 wp_set_script_translations( 'liquid-blocks-fixed', 'liquid-blocks', plugin_dir_path( __FILE__ ) . 'languages' );
121
122 // wp_localize_script
123 wp_localize_script( 'liquid-blocks-template', 'liquid_blocks_imgurl', array( plugins_url( 'images/', __FILE__ ) ) );
124 wp_localize_script( 'liquid-blocks-block', 'liquid_blocks_imgurl', array( plugins_url( 'images/', __FILE__ ) ) );
125 wp_localize_script( 'liquid-blocks-block', 'liquid_blocks_no', $liquid_blocks_no );
126 wp_localize_script( 'liquid-blocks-block', 'liquid_blocks_type', $liquid_blocks_type );
127 wp_localize_script( 'liquid-blocks-block', 'liquid_blocks_name', $liquid_blocks_name );
128
129 // wp_enqueue_style
130 wp_enqueue_style( 'liquid-blocks', plugins_url( 'css/block.css' , __FILE__ ), array(), $liquid_blocks_data['version'] );
131 wp_enqueue_style( 'liquid-blocks-editor', plugins_url( 'css/block-editor.css' , __FILE__ ), array(), $liquid_blocks_data['version'] );
132
133 // theme.json の layout.contentSize をブレークポイントとして使用
134 $content_size = '';
135 if (function_exists('wp_get_global_settings')) {
136 // まず theme.json 由来を取りに行く(環境差に強い)
137 $content_size = (string) wp_get_global_settings(['layout', 'contentSize'], ['origin' => 'base']);
138 }
139 $content_size = trim((string) $content_size);
140 // フォールバック:theme.json に無い / 空なら $content_width を採用
141 if ($content_size === '' && isset($content_width) && is_numeric($content_width) && (int) $content_width > 0) {
142 $content_size = (int) $content_width . 'px';
143 }
144 // media query に使えない値(var(...))は無効扱い
145 $usable_bp = ($content_size !== '' && stripos($content_size, 'var(') === false);
146 $settings = array(
147 'hasContentSize' => $usable_bp,
148 'breakpoint' => $usable_bp ? $content_size : '',
149 );
150 wp_add_inline_script(
151 'liquid-blocks-table',
152 'window.LiquidBlocksBlockTableSettings=' . wp_json_encode($settings) . ';',
153 'before'
154 );
155 }
156
157 function liquid_blocks_admin_footer() {
158 global $pagenow, $post;
159 if( $pagenow == 'post.php' || $pagenow == 'post-new.php' ){
160 if ( apply_filters( 'replace_editor', false, $post ) !== true ) {
161 if ( use_block_editor_for_post( $post ) ) {
162 ?>
163 <!-- Gallery -->
164 <div id="liquid_blocks_modal" class="liquid_blocks_modal" style="display:none;">
165 <div class="liquid_blocks_modal_gallery">
166
167 <?php
168 /* ブロックCSS+グローバルスタイル+テーマCSSを読み込んだプレビュー === */
169 if ( ! function_exists( 'liquid_blocks_preview_src' ) ) {
170 function liquid_blocks_preview_src( $raw_html ) {
171 $rendered = function_exists('do_blocks') ? do_blocks($raw_html) : apply_filters('the_content', $raw_html);
172
173 $links = '';
174 $styles = wp_styles();
175 if ( wp_style_is('wp-block-library','registered') && ! empty($styles->registered['wp-block-library']->src) ) {
176 $href = $styles->registered['wp-block-library']->src;
177 if ( 0 !== strpos($href,'http') ) $href = site_url($href);
178 $links .= '<link rel="stylesheet" href="'.esc_url($href).'">';
179 } else {
180 $links .= '<link rel="stylesheet" href="'.esc_url( includes_url('css/dist/block-library/style.min.css') ).'">';
181 }
182 if ( function_exists('wp_get_global_stylesheet') ) {
183 $links .= '<style>'.wp_get_global_stylesheet().'</style>';
184 }
185 $links .= '<link rel="stylesheet" href="'.esc_url( get_stylesheet_uri() ).'">';
186
187 $doc = '<!doctype html><html><head><meta charset="'.esc_attr( get_bloginfo('charset') ).'">'.$links.'<style>img{max-width:100%;height:auto;display:block}</style></head><body>'.$rendered.'</body></html>';
188
189 return 'data:text/html;charset=UTF-8;base64,'.base64_encode($doc);
190 }
191 }
192
193 /* === Site Editor のユーザーパターン(wp_block)を一覧表示 === */
194 $user_args = array(
195 'post_type' => array( 'wp_block' ), // Site Editor で作るユーザーパターン
196 'post_status' => array( 'publish' ),
197 'posts_per_page' => -1,
198 'orderby' => 'date', // 安定のため日付→ID
199 'order' => 'ASC',
200 );
201 $uquery = new WP_Query( $user_args );
202
203 if ( $uquery->have_posts() ) {
204 echo '<h3>' . esc_html__( 'Block Patterns', 'liquid-blocks' ) . ' <small>User</small></h3>';
205 echo '<script>if(!window.LiquidGallery){window.LiquidGallery={}}; if(!LiquidGallery["user"]){LiquidGallery["user"]=[]}</script>';
206
207 $i = 0;
208 while ( $uquery->have_posts() ) {
209 $uquery->the_post();
210 $post_id = get_the_ID();
211 $pattern_title = get_the_title();
212 if ( ! $pattern_title ) {
213 $pattern_title = __( 'Untitled pattern', 'liquid-blocks' );
214 }
215
216 // 生のブロックコンテンツ(クリック時にそのまま parse → insert)
217 $raw = get_post_field( 'post_content', $post_id );
218 if ( ! $raw ) {
219 continue;
220 }
221
222 // プレビューURL(コアCSS適用の実HTMLを返す)
223 $preview_src = liquid_blocks_preview_src( $raw );
224
225 // JSに詰める(JSONエンコードで安�
226 �に)
227 $raw_no_newline = preg_replace( "/\r|\n/", '', $raw );
228 $raw_json = wp_json_encode( $raw_no_newline );
229
230 // 画像 + タイトル(既存と同フォーマット)
231 echo '<script>LiquidGallery["user"][' . (int) $i . '] = ' . $raw_json . ';</script>';
232 echo '<a onclick="LiquidGalleryButton(LiquidGallery[\'user\'][' . (int) $i . '], this)" class="liquid_blocks_iframe">';
233
234 // iframe
235 $frame_w = 420; // サムネイル枠の見た目�
236
237 $frame_h = 280; // サムネイル枠の見た目高さ
238 $desktop_vw = 1280; // PCレイアウトとして見せたい�
239 (ブレークポイント用)
240 $scale = $frame_w / $desktop_vw;
241 $desktop_h = (int) round($frame_h / $scale);
242 $preview_src = liquid_blocks_preview_src( $raw );
243 echo '<iframe src="'.esc_attr($preview_src).'"
244 style="width:'.$desktop_vw.'px;height:'.$desktop_h.'px;
245 transform:scale('.$scale.');transform-origin:0 0;
246 border:0;pointer-events:none;display:block;background:#fff"
247 loading="lazy" referrerpolicy="no-referrer"></iframe>';
248
249 echo '<span>' . ( (int) ($i + 1) ) . ': ' . esc_html( $pattern_title ) . '</span>';
250 echo '</a>';
251
252 $i++;
253 }
254 wp_reset_postdata();
255 }
256 ?>
257
258 <?php
259 /* === liquid_block === */
260 $args = array(
261 'post_type' => array( 'liquid_block' ),
262 'post_status' => array( 'publish' ),
263 'posts_per_page' => -1,
264 'order' => 'ASC',
265 );
266 $the_query = new WP_Query( $args );
267 if ( $the_query->have_posts() ) {
268 $i = 0;
269 echo '<h3>'.__( 'Block Patterns', 'liquid-blocks' ).' <small>Patterns</small></h3>';
270 echo '<script>LiquidGallery[\'Patterns\'] = []</script>';
271 while ( $the_query->have_posts() ) {
272 $j = $i + 1;
273 $the_query->the_post();
274 $pattern_title = get_the_title();
275 if( !$pattern_title ){
276 $pattern_title = __( 'Untitled block', 'liquid-blocks' );
277 }
278 $pattern_content = get_the_content();
279 $pattern_categories = array();
280 $terms = get_the_terms( 0, 'liquid_patterns' );
281 if( !empty( $terms ) && !is_wp_error( $terms ) ){
282 foreach( $terms as $term ){
283 $pattern_categories[] = $term->slug;
284 }
285 }
286 if( has_post_thumbnail() ) {
287 $thumbnail_id = get_post_thumbnail_id();
288 $src_info = wp_get_attachment_image_src($thumbnail_id, $size = array(600,400));
289 $src = $src_info ? $src_info[0] : '';
290 $class = '';
291 } else {
292 $src = plugins_url( 'images/noimage.png', __FILE__ );
293 $class = 'liquid_noimage';
294 }
295 if( $pattern_content ){
296 $pattern_content = str_replace( array("\r", "\n"), '', $pattern_content );
297 echo '<script>LiquidGallery[\'Patterns\']['.$i.'] = '.wp_json_encode($pattern_content).';</script>';
298 echo '<a onclick="LiquidGalleryButton(LiquidGallery[\'Patterns\']['.$i.'], this)" class="liquid_blocks_img '.$class.'"><img src="'.$src.'" alt=""><span>'.$j.': '.$pattern_title.'</span></a>';
299 $i++;
300 }
301 }
302 wp_reset_postdata();
303 }
304 ?>
305
306 <h3><?php _e( 'Headlines', 'liquid-blocks' ); ?> <small>Headlines</small></h3>
307 <script>LiquidGalleryList('Headlines');</script>
308 <h3><?php _e( 'Layouts', 'liquid-blocks' ); ?> <small>Layouts</small></h3>
309 <script>LiquidGalleryList('Layouts');</script>
310 <h3><?php _e( 'Price list', 'liquid-blocks' ); ?> <small>Price</small></h3>
311 <script>LiquidGalleryList('Price');</script>
312 <h3><?php _e( 'CTA: Call To Action', 'liquid-blocks' ); ?> <small>CTA</small></h3>
313 <script>LiquidGalleryList('CTA');</script>
314 <h3><?php _e( 'Landing pages', 'liquid-blocks' ); ?> <small>Landing</small></h3>
315 <script>LiquidGalleryList('Landing');</script>
316 <h3><?php _e( 'Recommended Plugins', 'liquid-blocks' ); ?></h3>
317 <a href="https://wordpress.org/plugins/liquid-speech-balloon/" target="_blank"><img src="<?php echo plugins_url( 'images/Recommend/liquid-speech-balloon.png', __FILE__ ); ?>" alt=""></a>
318 </div>
319 </div>
320 <!-- Multiple -->
321 <div id="liquid_blocks_buttons" class="liquid_blocks_buttons">
322 <a id="liquid_blocks_copy" class="liquid_blocks_copy liquid_blocks_btn none"><?php _e( 'Copy', 'liquid-blocks' ); ?></a><script>LiquidGalleryCopy();</script>
323 <a id="liquid_blocks_insert" onclick="LiquidGalleryInsert()" class="liquid_blocks_btn none"><?php _e( 'Insert', 'liquid-blocks' ); ?><span id="liquid_blocks_count"></span></a>
324 <a id="liquid_blocks_multiple" onclick="LiquidGalleryMultiple()" class="liquid_blocks_btn"><?php _e( 'Select', 'liquid-blocks' ); ?></a>
325 <a id="liquid_blocks_cancel" onclick="LiquidGalleryCancel()" class="liquid_blocks_btn none"><?php _e( 'Cancel', 'liquid-blocks' ); ?></a>
326 <a id="liquid_blocks_close" onclick="LiquidGalleryClose()" class="liquid_blocks_btn"><?php _e( 'Close', 'liquid-blocks' ); ?></a>
327 </div>
328 <?php
329 }
330 }
331 }
332 }
333
334 // ------------------------------------
335 // init
336 // ------------------------------------
337
338 // block_categories
339 function liquid_block_categories($categories, $post) {
340 return array_merge(
341 $categories,
342 array(
343 array(
344 'slug' => 'liquid',
345 'title' => 'LIQUID',
346 ),
347 )
348 );
349 }
350 add_filter('block_categories_all', 'liquid_block_categories', 10, 2);
351
352 function liquid_blocks_init(){
353 global $liquid_blocks_clean;
354
355 // pattern_category
356 register_block_pattern_category(
357 'liquid_headlines',
358 array( 'label' => __( 'Headlines', 'liquid-blocks' ) )
359 );
360 // img_path
361 $path = plugins_url( 'images/', __FILE__ );
362
363 // slider
364 register_block_type( 'liquid/slider', array(
365 'editor_script' => 'liquid-blocks-slider',
366 'editor_style' => 'liquid-blocks-editor',
367 'attributes' => array(
368 'fields' => array(
369 'type' => 'array',
370 ),
371 'delay' => array(
372 'type' => 'number',
373 ),
374 'speed' => array(
375 'type' => 'number',
376 ),
377 'slidesPerView' => array(
378 'type' => 'number',
379 ),
380 'animationType' => array(
381 'type' => 'string',
382 ),
383 ),
384 ) );
385 wp_register_script(
386 'liquid-blocks-slider',
387 plugins_url( 'lib/block-slider.js', __FILE__ ),
388 array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-block-editor', 'wp-components' )
389 );
390
391 // カスタムフィールド
392 register_block_type('liquid/custom-field', array(
393 'editor_script' => 'liquid-blocks-custom-field',
394 'render_callback' => 'liquid_blocks_render_custom_field',
395 ));
396 wp_register_script(
397 'liquid-blocks-custom-field',
398 plugin_dir_url(__FILE__) . 'lib/block-custom-field.js',
399 array('wp-blocks', 'wp-i18n', 'wp-element', 'wp-block-editor', 'wp-components')
400 );
401
402 // isカスタム
403 register_block_type( 'liquid/if-conditions', array(
404 'editor_script' => 'liquid-blocks-if-conditions',
405 'render_callback' => 'liquid_blocks_is_custom',
406 ) );
407 wp_register_script(
408 'liquid-blocks-if-conditions',
409 plugins_url( 'lib/block-if-conditions.js', __FILE__ ),
410 array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-block-editor', 'wp-components' )
411 );
412 function liquid_blocks_is_custom( $attributes, $content ) {
413 $visibility = isset( $attributes['visibility'] ) ? $attributes['visibility'] : '';
414 $role = isset( $attributes['role'] ) ? $attributes['role'] : '';
415 $parameters = isset( $attributes['parameters'] ) ? sanitize_text_field($attributes['parameters']) : '';
416 $conditionType = isset( $attributes['conditionType'] ) ? $attributes['conditionType'] : '';
417
418 // helper: Not Match 判定の�
419 �通化
420 $invert = ( $conditionType === 'not_match' );
421
422 // is_user_logged_in
423 if ( 'is_user_logged_in' === $visibility ) {
424 if( ! $conditionType ){
425 if ( ! is_user_logged_in() ) { return ''; }
426 // role
427 if ( $role ) {
428 $current_user = wp_get_current_user();
429 if ( ! in_array( $role, (array) $current_user->roles, true ) ) { return ''; }
430 }
431 } else {
432 if ( is_user_logged_in() ) { return ''; }
433 }
434 }
435 // is_front_page
436 if ( 'is_front_page' === $visibility ) {
437 if( ! $conditionType ){
438 if ( ! is_front_page() ) { return ''; }
439 } else {
440 if ( is_front_page() ) { return ''; }
441 }
442 }
443 // is_front_page
444 if ( 'is_front_page' === $visibility ) {
445 if( ! $conditionType ){
446 if ( ! is_front_page() ) { return ''; }
447 } else {
448 if ( is_front_page() ) { return ''; }
449 }
450 }
451 // is_home
452 if ( 'is_home' === $visibility ) {
453 if( ! $conditionType ){
454 if ( ! is_home() ) { return ''; }
455 } else {
456 if ( is_home() ) { return ''; }
457 }
458 }
459 // is_page
460 if ( 'is_page' === $visibility ) {
461 if( ! $conditionType ){
462 if ( ! is_page($parameters) ) { return ''; }
463 } else {
464 if ( is_page($parameters) ) { return ''; }
465 }
466 }
467 // is_single
468 if ( 'is_single' === $visibility ) {
469 if( ! $conditionType ){
470 if ( ! is_single($parameters) ) { return ''; }
471 } else {
472 if ( is_single($parameters) ) { return ''; }
473 }
474 }
475 // is_singular
476 if ( 'is_singular' === $visibility ) {
477 if( ! $conditionType ){
478 if ( ! is_singular($parameters) ) { return ''; }
479 } else {
480 if ( is_singular($parameters) ) { return ''; }
481 }
482 }
483 // is_archive
484 if ( 'is_archive' === $visibility ) {
485 if( ! $conditionType ){
486 if ( ! is_archive() ) { return ''; }
487 } else {
488 if ( is_archive() ) { return ''; }
489 }
490 }
491 // is_category
492 if ( 'is_category' === $visibility ) {
493 if( ! $conditionType ){
494 if ( ! is_category($parameters) ) { return ''; }
495 } else {
496 if ( is_category($parameters) ) { return ''; }
497 }
498 }
499 // is_tag
500 if ( 'is_tag' === $visibility ) {
501 if( ! $conditionType ){
502 if ( ! is_tag($parameters) ) { return ''; }
503 } else {
504 if ( is_tag($parameters) ) { return ''; }
505 }
506 }
507 // is_author
508 if ( 'is_author' === $visibility ) {
509 if( ! $conditionType ){
510 if ( ! is_author($parameters) ) { return ''; }
511 } else {
512 if ( is_author($parameters) ) { return ''; }
513 }
514 }
515 // is_tax
516 if ( 'is_tax' === $visibility ) {
517 if( ! $conditionType ){
518 if ( ! is_tax($parameters) ) { return ''; }
519 } else {
520 if ( is_tax($parameters) ) { return ''; }
521 }
522 }
523 // has_post_format
524 if ( 'has_post_format' === $visibility ) {
525 if( ! $conditionType ){
526 if ( ! has_post_format($parameters) ) { return ''; }
527 } else {
528 if ( has_post_format($parameters) ) { return ''; }
529 }
530 }
531 // wp_is_mobile
532 if ( 'wp_is_mobile' === $visibility ) {
533 if( ! $conditionType ){
534 if ( ! wp_is_mobile() ) { return ''; }
535 } else {
536 if ( wp_is_mobile() ) { return ''; }
537 }
538 }
539 // get_locale
540 if ( 'get_locale' === $visibility ) {
541 $locale = function_exists('get_locale') ? (string) get_locale() : '';
542 $list = array_filter( array_map( 'trim', preg_split( '/[,\s]+/', $parameters ) ) );
543 $matched = false;
544 foreach ( $list as $p ) {
545 if ( $p !== '' && strpos( $locale, $p ) === 0 ) { // prefix match
546 $matched = true;
547 break;
548 }
549 }
550 if ( ! $invert ) {
551 if ( ! $matched ) { return ''; }
552 } else {
553 if ( $matched ) { return ''; }
554 }
555 }
556
557 // content
558 return '<div class="liquid-if-conditions">' . $content . '</div>';
559 }
560
561 // fixed
562 register_block_type('liquid/fixed', array(
563 'editor_script' => 'liquid-blocks-fixed',
564 'style' => 'liquid-blocks-fixed',
565 'render_callback' => 'liquid_blocks_render_fixed',
566 'attributes' => array(
567 'positionX' => array('type' => 'string', 'default' => 'right'), // left/right
568 'positionY' => array('type' => 'string', 'default' => 'bottom'), // top/bottom
569 'offsetTop' => array('type' => 'string', 'default' => '0px'),
570 'offsetBottom' => array('type' => 'string', 'default' => '0px'),
571 'offsetLeft' => array('type' => 'string', 'default' => '0px'),
572 'offsetRight' => array('type' => 'string', 'default' => '0px'),
573
574 'showOnScroll' => array('type' => 'boolean', 'default' => false),
575 'scrollThreshold' => array('type' => 'number', 'default' => 200),
576 'scrollMode' => array('type' => 'string', 'default' => 'after'), // after/up/down
577
578 'closeable' => array('type' => 'boolean', 'default' => false),
579 'closePersist' => array('type' => 'boolean', 'default' => true),
580 'persistKey' => array('type' => 'string', 'default' => ''), // 自動生成
581 ),
582 ));
583
584 // ------------------------------------
585 // unregister_block_pattern
586 // ------------------------------------
587 if( !empty($liquid_blocks_clean) ){
588 add_action( 'after_setup_theme', function() {
589 remove_theme_support( 'core-block-patterns' );
590 } );
591 add_filter( 'should_load_remote_block_patterns', '__return_false' );
592 }
593
594 // register_post_type
595 register_post_type(
596 'liquid_block',
597 array(
598 'public' => true,
599 'description' => __( 'Block Pattern Manager', 'liquid-blocks' ),
600 'hierarchical' => false,
601 'has_archive' => false,
602 'labels' => array(
603 'name' => __( 'Block Pattern', 'liquid-blocks' ),
604 'menu_name' => __( 'Block Pattern', 'liquid-blocks' ),
605 'singular_name' => __( 'Block Pattern', 'liquid-blocks' ),
606 'all_items' => __( 'Block Pattern List', 'liquid-blocks' ),
607 'add_new' => __( 'Add New', 'liquid-blocks' ),
608 'add_new_item' => __( 'Add New', 'liquid-blocks' ),
609 'edit_item' => __( 'Edit', 'liquid-blocks' ),
610 ),
611 'publicly_queryable' => false,
612 'show_ui' => true,
613 'show_in_menu' => false,
614 'show_in_nav_menus' => false,
615 'show_in_admin_bar' => true,
616 'capability_type' => 'post',
617 'menu_position' => 9,
618 'query_var' => false,
619 'exclude_from_search' => true,
620 'publicly_queryable' => false,
621 'supports' => array(
622 'title',
623 'editor',
624 'author',
625 'thumbnail',
626 ),
627 'taxonomies' => array('liquid_block'),
628 'show_in_rest' => true,
629 )
630 );
631 register_taxonomy(
632 'liquid_patterns',
633 'liquid_block',
634 array(
635 'label' => __( 'Block Pattern Category', 'liquid-blocks' ),
636 'hierarchical' => false,
637 'public' => false,
638 'show_ui' => true,
639 'show_in_rest' => true,
640 'show_tagcloud' => false,
641 'labels' => array(
642 'singular_name' => __( 'Block Pattern Category', 'liquid-blocks' ),
643 'add_new_item' => __( 'Add New', 'liquid-blocks' ),
644 'separate_items_with_commas' => __( 'Press the Enter key.', 'liquid-blocks' ),
645 ),
646 )
647 );
648
649 $terms = get_terms( array( 'taxonomy' => 'liquid_patterns', 'get'=>'all' ) );
650 if( !empty( $terms ) && !is_wp_error( $terms ) ){
651 foreach( $terms as $term ){
652 register_block_pattern_category(
653 $term->slug,
654 array( 'label' => $term->name )
655 );
656 }
657 }
658
659 // register_block_pattern self-made
660 $args = array(
661 'post_type' => array( 'liquid_block' ),
662 'post_status' => array( 'publish' ),
663 'posts_per_page' => -1,
664 'order' => 'ASC',
665 );
666 $the_query = new WP_Query( $args );
667 if ( $the_query->have_posts() ) {
668 $i = 0;
669 while ( $the_query->have_posts() ) {
670 $the_query->the_post();
671 $pattern_title = get_the_title();
672 if( !$pattern_title ){
673 $pattern_title = __( 'Untitled block', 'liquid-blocks' );
674 }
675 $pattern_content = get_the_content();
676 $pattern_categories = array();
677 $terms = get_the_terms( 0, 'liquid_patterns' );
678 if( !empty( $terms ) && !is_wp_error( $terms ) ){
679 foreach( $terms as $term ){
680 $pattern_categories[] = $term->slug;
681 }
682 }
683 if( $pattern_content ){
684 register_block_pattern(
685 'liquid/liquid_pattern_' . $i,
686 array(
687 'title' => $pattern_title,
688 'content' => $pattern_content,
689 'categories' => $pattern_categories,
690 )
691 );
692 $i++;
693 }
694 }
695 wp_reset_postdata();
696 }
697 }
698
699 // カスタムフィールドブロックのレンダリング
700 function liquid_blocks_render_custom_field($attributes) {
701 // カスタムフィールドキーと投稿IDの取得
702 $fieldKey = isset($attributes['fieldKey']) ? $attributes['fieldKey'] : '';
703 $postId = isset($attributes['postId']) && !empty($attributes['postId']) ? intval($attributes['postId']) : get_the_ID();
704
705 // フィールドタイプを取得(テキストか画像か)
706 $fieldType = isset($attributes['fieldType']) ? $attributes['fieldType'] : 'text';
707
708 // デフォルトの alignment を設定
709 $alignment = isset($attributes['alignment']) ? $attributes['alignment'] : 'left';
710
711 // 追加CSSクラスを取得
712 $customClass = isset($attributes['className']) ? $attributes['className'] : '';
713
714 // スタイル属性を構築
715 $style = 'text-align:' . esc_attr($alignment) . ';';
716
717 // カスタムフィールドデータの取得
718 if (!empty($fieldKey)) {
719 $fieldValue = get_post_meta($postId, $fieldKey, true);
720
721 // フィールドタイプに基づいて処理を分ける
722 if ($fieldType === 'image') {
723 // 画像の場合
724 if (is_array($fieldValue)) {
725 // 繰り返し画像フィールドの場合の処理
726 $output = '<div class="liquid_custom_field_repeatable">';
727 foreach ($fieldValue as $mediaId) {
728 $image_url = wp_get_attachment_url($mediaId);
729 if ($image_url) {
730 $output .= '<div class="liquid_custom_field_item"><img src="' . esc_url($image_url) . '" alt="" /></div>';
731 }
732 }
733 $output .= '</div>';
734 } else {
735 // 単一画像フィールドの場合
736 $image_url = wp_get_attachment_url($fieldValue);
737 if ($image_url) {
738 $output = '<div class="liquid_custom_field_item"><img src="' . esc_url($image_url) . '" alt="" /></div>';
739 }
740 }
741 } else {
742 // テキストの場合
743 if (is_array($fieldValue)) {
744 // 繰り返しテキストフィールドの場合の処理
745 $output = '<div class="liquid_custom_field_repeatable">';
746 foreach ($fieldValue as $value) {
747 $output .= '<div class="liquid_custom_field_item">' . esc_html($value) . '</div>';
748 }
749 $output .= '</div>';
750 } else {
751 // 単一テキストフィールドの場合の処理
752 $output = '<div class="liquid_custom_field_item">' . esc_html($fieldValue) . '</div>';
753 }
754 }
755
756 // 追加CSSクラスを適用してカスタムフィールドの�
757 容をスタイル付きのdivでラップ
758 return '<div class="liquid_custom_field ' . esc_attr($customClass) . '" style="' . esc_attr($style) . '">' . $output . '</div>';
759 } else {
760 return '<div class="liquid_custom_field ' . esc_attr($customClass) . '">' . __('No custom field selected.', 'liquid-blocks') . '</div>';
761 }
762 }
763
764 // Fixedブロックのレンダリング
765 function liquid_blocks_render_fixed($attributes, $content, $block) {
766 $x = $attributes['positionX'] ?? 'right';
767 $y = $attributes['positionY'] ?? 'top';
768
769 $style = array(
770 'position:fixed',
771 'z-index:9999',
772 );
773
774 if ($y === 'top') {
775 $style[] = 'top:' . ($attributes['offsetTop'] ?? '0');
776 } else {
777 $style[] = 'bottom:' . ($attributes['offsetBottom'] ?? '0');
778 }
779
780 if ($x === 'left') {
781 $style[] = 'left:' . ($attributes['offsetLeft'] ?? '0');
782 } else {
783 $style[] = 'right:' . ($attributes['offsetRight'] ?? '0');
784 }
785
786 $persist_key = trim((string)($attributes['persistKey'] ?? ''));
787 $close_persist = !empty($attributes['closePersist']) && $persist_key !== '';
788
789 $wrapper_attrs = get_block_wrapper_attributes(array(
790 'class' => 'liquid-fixed',
791 'style' => implode(';', $style) . ';',
792 'data-liquid-fixed' => '1',
793 'data-liquid-scroll' => !empty($attributes['showOnScroll']) ? '1' : '0',
794 'data-liquid-scroll-threshold'=> (string)($attributes['scrollThreshold'] ?? 0),
795 'data-liquid-scroll-mode' => (string)($attributes['scrollMode'] ?? 'after'),
796 'data-liquid-close' => !empty($attributes['closeable']) ? '1' : '0',
797 'data-liquid-close-key' => $persist_key,
798 'data-liquid-close-persist' => $close_persist ? '1' : '0',
799 ));
800
801 return '<div ' . $wrapper_attrs . '>' . $content . '</div>';
802 }
803
804 // ------------------------------------
805 // Admin
806 // ------------------------------------
807 function liquid_blocks_admin() {
808 add_options_page(
809 'LIQUID BLOCKS',
810 'LIQUID BLOCKS',
811 'administrator',
812 'liquid-blocks',
813 'liquid_blocks_admin_page'
814 );
815 add_theme_page(
816 __( 'Patterns', 'liquid-blocks' ),
817 __( 'Patterns', 'liquid-blocks' ),
818 'administrator',
819 'edit.php?post_type=liquid_block'
820 );
821 register_setting(
822 'liquid_blocks_group',
823 'liquid_blocks_toggle',
824 'liquid_blocks_toggle_validation'
825 );
826 register_setting(
827 'liquid_blocks_group',
828 'liquid_blocks_no', array(
829 'sanitize_callback' => function( $input ) {
830 if ( ! is_array( $input ) ) {
831 return array();
832 }
833 return array_map( 'absint', $input );
834 },
835 )
836 );
837 register_setting(
838 'liquid_blocks_group',
839 'liquid_blocks_type', array(
840 'sanitize_callback' => function( $input ) {
841 if ( ! is_array( $input ) ) {
842 return array();
843 }
844 return array_map( 'sanitize_text_field', $input );
845 },
846 )
847 );
848 register_setting(
849 'liquid_blocks_group',
850 'liquid_blocks_name', array(
851 'sanitize_callback' => function( $input ) {
852 if ( ! is_array( $input ) ) {
853 return array();
854 }
855 return array_map( 'sanitize_text_field', $input );
856 },
857 )
858 );
859 register_setting(
860 'liquid_blocks_group',
861 'liquid_blocks_clean', array(
862 'sanitize_callback' => 'absint',
863 )
864 );
865 }
866 add_action( 'admin_menu', 'liquid_blocks_admin' );
867
868 add_action( 'admin_enqueue_scripts', function( $hook ) {
869 if ( $hook !== 'settings_page_liquid-blocks' ) {
870 return;
871 }
872 wp_enqueue_script(
873 'liquid-blocks-settings',
874 plugins_url( 'lib/liquid-blocks-settings.js', __FILE__ ),
875 array( 'jquery' ),
876 '1.0.0',
877 true
878 );
879 } );
880
881 function liquid_blocks_toggle_validation( $input ) {
882 $input = (int) $input;
883 if ( $input === 0 || $input === 1 ) {
884 return $input;
885 } else {
886 add_settings_error(
887 'liquid_blocks_toggle',
888 'liquid_blocks_toggle_validation_error',
889 __( 'illegal data', 'liquid-blocks' ),
890 'error'
891 );
892 }
893 }
894
895 function liquid_blocks_admin_page() {
896 global $liquid_blocks_json, $liquid_blocks_toggle, $liquid_blocks_no, $liquid_blocks_type, $liquid_blocks_name, $liquid_blocks_clean;
897 if( empty( $liquid_blocks_toggle ) ){
898 $toggle_on = 'checked="checked"';
899 $toggle_off = '';
900 } else {
901 $toggle_on = '';
902 $toggle_off = 'checked="checked"';
903 }
904 if( empty( $liquid_blocks_clean ) ){
905 $clean_on = 'checked="checked"';
906 $clean_off = '';
907 } else {
908 $clean_on = '';
909 $clean_off = 'checked="checked"';
910 }
911 ?>
912 <div class="wrap">
913 <h1>LIQUID BLOCKS</h1>
914
915 <div id="poststuff">
916
917 <!-- Recommend -->
918 <?php if( !empty($liquid_blocks_json) && !empty($liquid_blocks_json['recommend']) ){ ?>
919 <div class="postbox" id="liquid-blocks-recommend">
920 <h2 style="border-bottom: 1px solid #eee; cursor:pointer;"><?php _e( 'Recommend', 'liquid-blocks' ); ?></h2>
921 <div class="inside"><?php echo wp_kses_post($liquid_blocks_json['recommend']); ?></div>
922 </div>
923 <?php } ?>
924
925 <!-- Settings -->
926 <div class="postbox">
927 <h2 style="border-bottom: 1px solid #eee;"><?php _e( 'Settings', 'liquid-blocks' ); ?></h2>
928 <div class="inside">
929 <form method="post" action="options.php">
930 <?php
931 settings_fields( 'liquid_blocks_group' );
932 do_settings_sections( 'default' );
933 ?>
934 <?php if ( function_exists('register_block_pattern') ) { ?>
935 <h3 class="liquid-blocks-pro-ttl"><?php _e( 'LIQUID BLOCKS Pro', 'liquid-blocks' ); ?></h3>
936 <div class="liquid-blocks-pro-box">
937 <p><?php _e('Unlock all features with the Pro version.', 'liquid-blocks'); ?></p>
938 <ul style="list-style:disc;padding-left:1.5em;margin-bottom:1.5em;">
939 <li><?php _e('Unlimited Slider and Tabs (free version: up to 10 each)', 'liquid-blocks'); ?></li>
940 <li><?php _e('Fixed blocks and If conditional blocks: Advanced options', 'liquid-blocks'); ?></li>
941 </ul>
942 <a href="https://lqd.jp/wp/plugin/blocks_pro.html" target="_blank" rel="noopener" class="button button-primary" style="font-size:14px;height:auto;padding:8px 20px;"><?php _e( 'Get Pro', 'liquid-blocks' ); ?></a>
943 </div>
944 <h3><?php _e( 'Block Pattern Manager', 'liquid-blocks' ); ?></h3>
945 <a href="site-editor.php?p=%2Fpattern" class="button button-primary"><?php _e( 'Block Pattern Manager', 'liquid-blocks' ); ?></a>
946 <h3><?php _e( 'Enable default block pattern', 'liquid-blocks' ); ?></h3>
947 <p>
948 <label for="liquid_blocks_clean_on"><input type="radio" id="liquid_blocks_clean_on" name="liquid_blocks_clean" value="0" <?php echo $clean_on; ?>>On</label>
949 <label for="liquid_blocks_clean_off"><input type="radio" id="liquid_blocks_clean_off" name="liquid_blocks_clean" value="1" <?php echo $clean_off; ?>>Off</label>
950 </p>
951 <?php } ?>
952 <h3><?php _e( 'Shortcuts', 'liquid-blocks' ); ?></h3>
953 <p><?php _e( 'Sidebar shortcut button settings.', 'liquid-blocks' ); ?><br><?php _e( 'Enter any name, gallery category name, and number.', 'liquid-blocks' ); ?></p>
954 <?php
955 for ($i=0; $i<10; $i++) {
956 $j = $i + 1;
957 $no = !empty($liquid_blocks_no[$i]) ? $liquid_blocks_no[$i] : "";
958 $type = !empty($liquid_blocks_type[$i]) ? $liquid_blocks_type[$i] : "";
959 $name = !empty($liquid_blocks_name[$i]) ? $liquid_blocks_name[$i] : "";
960 echo '<p>'.$j.' Name:<input type="text" name="liquid_blocks_name['.$i.']" value="'.esc_html($name).'" placeholder="intro"> ';
961 echo 'Type:<input type="text" name="liquid_blocks_type['.$i.']" value="'.esc_html($type).'" placeholder="Headlines"> ';
962 echo 'No:<input type="number" name="liquid_blocks_no['.$i.']" value="'.esc_html($no).'"></p>';
963 }
964 ?>
965 <h3><?php _e( 'Enable', 'liquid-blocks' ); ?> LIQUID BLOCKS</h3>
966 <p>
967 <label for="liquid_blocks_toggle_on"><input type="radio" id="liquid_blocks_toggle_on" name="liquid_blocks_toggle" value="0" <?php echo $toggle_on; ?>>On</label>
968 <label for="liquid_blocks_toggle_off"><input type="radio" id="liquid_blocks_toggle_off" name="liquid_blocks_toggle" value="1" <?php echo $toggle_off; ?>>Off</label>
969 </p>
970 <?php submit_button(); ?>
971 </form>
972 </div>
973 </div>
974
975 </div><!-- /poststuff -->
976 <hr><a href="https://lqd.jp/wp/" target="_blank">LIQUID PRESS</a>
977 </div><!-- /wrap -->
978 <?php } ?>