PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 2.4.15
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v2.4.15
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.15, at admin/GutenbergBlock/class-smart-post-show-gutenberg-block-init.php

193 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' ),
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 'shortCodeList' => $this->spsp_post_list(),
111 )
112 );
113 /**
114 * Register Gutenberg block on server-side.
115 */
116 register_block_type(
117 'smart-post-show-pro/shortcode',
118 array(
119 'attributes' => array(
120 'shortcodelist' => array(
121 'type' => 'object',
122 'default' => '',
123 ),
124 'shortcode' => array(
125 'type' => 'string',
126 'default' => '',
127 ),
128 'showInputShortcode' => array(
129 'type' => 'boolean',
130 'default' => true,
131 ),
132 'preview' => array(
133 'type' => 'boolean',
134 'default' => false,
135 ),
136 'is_admin' => array(
137 'type' => 'boolean',
138 'default' => is_admin(),
139 ),
140 ),
141 'example' => array(
142 'attributes' => array(
143 'preview' => true,
144 ),
145 ),
146 // Enqueue blocks.editor.build.js in the editor only.
147 'editor_script' => array(
148 'pcp_swiper',
149 'pcp_script',
150 ),
151 // Enqueue blocks.editor.build.css in the editor only.
152 'editor_style' => array(),
153 'render_callback' => array( $this, 'smart_post_show_render_shortcode' ),
154 )
155 );
156 }
157
158 /**
159 * Render callback.
160 *
161 * @param string $attributes Shortcode.
162 * @return string
163 */
164 public function smart_post_show_render_shortcode( $attributes ) {
165
166 $class_name = '';
167 if ( ! empty( $attributes['className'] ) ) {
168 $class_name = 'class="' . $attributes['className'] . '"';
169 }
170
171 if ( ! $attributes['is_admin'] ) {
172 return '<div ' . $class_name . '>' . do_shortcode( '[smart_post_show id="' . sanitize_text_field( $attributes['shortcode'] ) . '"]' ) . '</div>';
173 }
174
175 $pcp_settings = get_option( 'sp_post_carousel_settings' );
176 $pcp_id = $attributes['shortcode'];
177 $custom_css = '';
178 $pcp_custom_css = $pcp_settings['pcp_custom_css'];
179 $view_options = get_post_meta( $pcp_id, 'sp_pcp_view_options', true );
180 $layouts = get_post_meta( $pcp_id, 'sp_pcp_layouts', true );
181 // Include dynamic style file.
182 include SP_PC_PATH . 'public/dynamic-css/dynamic-css.php';
183 if ( ! empty( $pcp_custom_css ) ) {
184 $custom_css .= $pcp_custom_css;
185 }
186 // Add dynamic style.
187 $style = '<style>' . $custom_css . '</style>';
188
189 return $style . '<div id="' . uniqid() . '" ' . $class_name . ' >' . do_shortcode( '[smart_post_show id="' . sanitize_text_field( $attributes['shortcode'] ) . '"]' ) . '</div>';
190 }
191 }
192 }
193