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

173 lines 4.6 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 wp_enqueue_style( 'font-awesome' );
56 wp_enqueue_style( 'pcp_swiper' );
57 wp_enqueue_style( 'pcp-style' );
58 }
59 /**
60 * Shortcode list.
61 *
62 * @return array
63 */
64 public function spsp_post_list() {
65 $shortcodes = get_posts(
66 array(
67 'post_type' => 'sp_post_carousel',
68 'post_status' => 'publish',
69 'posts_per_page' => 9999,
70 )
71 );
72
73 if ( count( $shortcodes ) < 1 ) {
74 return array();
75 }
76
77 return array_map(
78 function ( $shortcode ) {
79 return (object) array(
80 'id' => absint( $shortcode->ID ),
81 'title' => esc_html( $shortcode->post_title ),
82 );
83 },
84 $shortcodes
85 );
86 }
87
88 /**
89 * Register Gutenberg shortcode block.
90 */
91 public function spsp_gutenberg_shortcode_block() {
92 /**
93 * Register block editor js file enqueue for backend.
94 */
95 wp_register_script( 'pcp_gb_script', SP_PC_URL . 'public/assets/js/scripts' . $this->suffix . '.js', array( 'jquery' ), SP_PC_VERSION, true );
96 wp_localize_script(
97 'pcp_gb_script',
98 'smartPostShowGbScript',
99 array(
100 'url' => SP_PC_URL,
101 'loadScript' => SP_PC_URL . 'public/assets/js/scripts.js',
102 'link' => admin_url( 'post-new.php?post_type=sp_post_carousel' ),
103 'shortCodeList' => $this->spsp_post_list(),
104 )
105 );
106 /**
107 * Register Gutenberg block on server-side.
108 */
109 register_block_type(
110 'smart-post-show-pro/shortcode',
111 array(
112 'attributes' => array(
113 'shortcodelist' => array(
114 'type' => 'object',
115 'default' => '',
116 ),
117 'shortcode' => array(
118 'type' => 'string',
119 'default' => '',
120 ),
121 'showInputShortcode' => array(
122 'type' => 'boolean',
123 'default' => true,
124 ),
125 'preview' => array(
126 'type' => 'boolean',
127 'default' => false,
128 ),
129 'is_admin' => array(
130 'type' => 'boolean',
131 'default' => is_admin(),
132 ),
133 ),
134 'example' => array(
135 'attributes' => array(
136 'preview' => true,
137 ),
138 ),
139 // Enqueue blocks.editor.build.js in the editor only.
140 'editor_script' => array(
141 'pcp_swiper',
142 'pcp_gb_script',
143 ),
144 // Enqueue blocks.editor.build.css in the editor only.
145 'editor_style' => array(),
146 'render_callback' => array( $this, 'smart_post_show_render_shortcode' ),
147 )
148 );
149 }
150
151 /**
152 * Render callback.
153 *
154 * @param string $attributes Shortcode.
155 * @return string
156 */
157 public function smart_post_show_render_shortcode( $attributes ) {
158 $class_name = '';
159 if ( ! empty( $attributes['className'] ) ) {
160 $class_name = 'class="' . esc_attr( $attributes['className'] ) . '"';
161 }
162
163 if ( ! $attributes['is_admin'] ) {
164 return '<div ' . $class_name . '>' . do_shortcode( '[smart_post_show id="' . sanitize_text_field( $attributes['shortcode'] ) . '"]' ) . '</div>';
165 }
166
167 $edit_page_link = get_edit_post_link( sanitize_text_field( $attributes['shortcode'] ) );
168
169 return '<div id="' . uniqid() . '" ' . $class_name . ' ><a href="' . $edit_page_link . '" target="_blank" class="sp_sps_block_edit_button">Edit View </a>' . do_shortcode( '[smart_post_show id="' . sanitize_text_field( $attributes['shortcode'] ) . '"]' ) . '</div>';
170 }
171 }
172 }
173