PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 2.4.12
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v2.4.12
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 2.3.5 2.3.6 2.4.0 2.4.1 2.4.10 2.4.11 2.4.12 2.4.13 2.4.14 2.4.15 2.4.16 2.4.17 2.4.18 2.4.19 2.4.2 2.4.20 2.4.21 All 88 releases
post-carousel / admin / GutenbergBlock / class-smart-post-show-gutenberg-block-init.php

class-smart-post-show-gutenberg-block-init.php in Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News 2.4.12, at admin/GutenbergBlock/class-smart-post-show-gutenberg-block-init.php

192 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The plugin gutenberg block Initializer.
4 *
5 * @link https://shapedplugin.com/
6 * @since 2.4.2
7 *
8 * @package Smart_Post_Show
9 * @subpackage Smart_Post_Show/Admin
10 * @author ShapedPlugin <[email protected]>
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 if ( ! class_exists( 'Smart_Post_Show_Gutenberg_Block_Init' ) ) {
19 /**
20 * Smart_Post_Show_Gutenberg_Block_Init class.
21 */
22 class Smart_Post_Show_Gutenberg_Block_Init {
23 /**
24 * Script and style suffix
25 *
26 * @since 2.4.2
27 * @access protected
28 * @var string
29 */
30 protected $suffix;
31 /**
32 * Custom Gutenberg Block Initializer.
33 */
34 public function __construct() {
35 $this->suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || defined( 'WP_DEBUG' ) && WP_DEBUG ? '' : '.min';
36 add_action( 'init', array( $this, 'spsp_gutenberg_shortcode_block' ) );
37 add_action( 'enqueue_block_editor_assets', array( $this, 'spsp_block_editor_assets' ) );
38 }
39
40 /**
41 * Register block editor script for backend.
42 */
43 public function spsp_block_editor_assets() {
44 wp_enqueue_script(
45 'smart-post-show-shortcode-block',
46 plugins_url( '/GutenbergBlock/build/index.js', dirname( __FILE__ ) ),
47 array( 'jquery', 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components' ),
48 SP_PC_VERSION,
49 true
50 );
51
52 /**
53 * Register block editor css file enqueue for backend.
54 */
55 $pcp_settings = get_option( 'sp_post_carousel_settings' );
56 if ( $pcp_settings['pcp_fontawesome_css'] ) {
57 wp_enqueue_style( 'font-awesome', SP_PC_URL . 'public/assets/css/font-awesome.min.css', array(), SP_PC_VERSION, 'all' );
58 }
59 if ( $pcp_settings['pcp_swiper_css'] ) {
60 wp_enqueue_style( 'pcp_swiper', SP_PC_URL . 'public/assets/css/swiper-bundle' . $this->suffix . '.css', array(), SP_PC_VERSION, 'all' );
61 }
62 wp_enqueue_style( 'pcp-style', SP_PC_URL . 'public/assets/css/style' . $this->suffix . '.css', array(), SP_PC_VERSION, 'all' );
63 }
64 /**
65 * Shortcode list.
66 *
67 * @return array
68 */
69 public function spsp_post_list() {
70 $shortcodes = get_posts(
71 array(
72 'post_type' => 'sp_post_carousel',
73 'post_status' => 'publish',
74 'posts_per_page' => 9999,
75 )
76 );
77
78 if ( count( $shortcodes ) < 1 ) {
79 return array();
80 }
81
82 return array_map(
83 function ( $shortcode ) {
84 return (object) array(
85 'id' => absint( $shortcode->ID ),
86 'title' => esc_html( $shortcode->post_title ),
87 );
88 },
89 $shortcodes
90 );
91 }
92
93 /**
94 * Register Gutenberg shortcode block.
95 */
96 public function spsp_gutenberg_shortcode_block() {
97 /**
98 * Register block editor js file enqueue for backend.
99 */
100 wp_register_script( 'pcp_swiper', SP_PC_URL . 'public/assets/js/swiper-bundle' . $this->suffix . '.js', array( 'jquery' ), SP_PC_VERSION, true );
101 wp_register_script( 'pcp_script', SP_PC_URL . 'public/assets/js/scripts' . $this->suffix . '.js', array( 'jquery' ), SP_PC_VERSION, true );
102
103 wp_localize_script(
104 'pcp_script',
105 'smartPostShowGbScript',
106 array(
107 'url' => SP_PC_URL,
108 'loadScript' => SP_PC_URL . 'public/assets/js/scripts.js',
109 'link' => admin_url( 'post-new.php?post_type=sp_post_carousel' ),
110 )
111 );
112 /**
113 * Register Gutenberg block on server-side.
114 */
115 register_block_type(
116 'smart-post-show/shortcode',
117 array(
118 'attributes' => array(
119 'shortcodelist' => array(
120 'type' => 'object',
121 'default' => $this->spsp_post_list(),
122 ),
123 'shortcode' => array(
124 'type' => 'string',
125 'default' => '',
126 ),
127 'showInputShortcode' => array(
128 'type' => 'boolean',
129 'default' => true,
130 ),
131 'preview' => array(
132 'type' => 'boolean',
133 'default' => false,
134 ),
135 'is_admin' => array(
136 'type' => 'boolean',
137 'default' => is_admin(),
138 ),
139 ),
140 'example' => array(
141 'attributes' => array(
142 'preview' => true,
143 ),
144 ),
145 // Enqueue blocks.editor.build.js in the editor only.
146 'editor_script' => array(
147 'pcp_swiper',
148 'pcp_script',
149 ),
150 // Enqueue blocks.editor.build.css in the editor only.
151 'editor_style' => array(),
152 'render_callback' => array( $this, 'smart_post_show_render_shortcode' ),
153 )
154 );
155 }
156
157 /**
158 * Render callback.
159 *
160 * @param string $attributes Shortcode.
161 * @return string
162 */
163 public function smart_post_show_render_shortcode( $attributes ) {
164
165 $class_name = '';
166 if ( ! empty( $attributes['className'] ) ) {
167 $class_name = 'class="' . $attributes['className'] . '"';
168 }
169
170 if ( ! $attributes['is_admin'] ) {
171 return '<div ' . $class_name . '>' . do_shortcode( '[smart_post_show id="' . sanitize_text_field( $attributes['shortcode'] ) . '"]' ) . '</div>';
172 }
173
174 $pcp_settings = get_option( 'sp_post_carousel_settings' );
175 $pcp_id = $attributes['shortcode'];
176 $custom_css = '';
177 $pcp_custom_css = $pcp_settings['pcp_custom_css'];
178 $view_options = get_post_meta( $pcp_id, 'sp_pcp_view_options', true );
179 $layouts = get_post_meta( $pcp_id, 'sp_pcp_layouts', true );
180 // Include dynamic style file.
181 include SP_PC_PATH . 'public/dynamic-css/dynamic-css.php';
182 if ( ! empty( $pcp_custom_css ) ) {
183 $custom_css .= $pcp_custom_css;
184 }
185 // Add dynamic style.
186 $style = '<style>' . $custom_css . '</style>';
187
188 return $style . '<div id="' . uniqid() . '" ' . $class_name . ' >' . do_shortcode( '[smart_post_show id="' . sanitize_text_field( $attributes['shortcode'] ) . '"]' ) . '</div>';
189 }
190 }
191 }
192