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

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