PluginProbe
Getwid – Gutenberg Blocks / 2.1.1
Getwid – Gutenberg Blocks v2.1.1
3.0.1 3.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.2.0 trunk 1.8.7 1.9.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9
getwid / includes / blocks / custom-post-type.php

custom-post-type.php in Getwid – Gutenberg Blocks 2.1.1, at includes/blocks/custom-post-type.php

311 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Getwid\Blocks;
4
5 class CustomPostType extends \Getwid\Blocks\AbstractBlock {
6
7 protected static $blockName = 'getwid/custom-post-type';
8
9 public function __construct() {
10
11 parent::__construct( self::$blockName );
12
13 register_block_type(
14 'getwid/custom-post-type',
15 array(
16 'attributes' => array(
17 'postTemplate' => array(
18 'type' => 'string'
19 ),
20
21 //Custom Post Type
22 'postsToShow' => array(
23 'type' => 'number',
24 'default' => 5
25 ),
26 'offset' => array(
27 'type' => 'number',
28 'default' => 0
29 ),
30 'pagination' => array(
31 'type' => 'boolean',
32 'default' => false
33 ),
34 'ignoreSticky' => array(
35 'type' => 'boolean',
36 'default' => true
37 ),
38 'filterById' => array(
39 'type' => 'string'
40 ),
41 'excludeById' => array(
42 'type' => 'string'
43 ),
44 'excludeCurrentPost' => array(
45 'type' => 'boolean',
46 'default' => false
47 ),
48 'childPagesCurrentPage' => array(
49 'type' => 'boolean',
50 'default' => false
51 ),
52 'parentPageId' => array(
53 'type' => 'string'
54 ),
55 'postType' => array(
56 'type' => 'string',
57 'default' => 'post'
58 ),
59 'taxonomy' => array(
60 'type' => 'array',
61 'items' => [
62 'type' => 'string'
63 ],
64 ),
65 'terms' => array(
66 'type' => 'array',
67 'items' => [
68 'type' => 'string'
69 ]
70 ),
71 'relation' => array(
72 'type' => 'string',
73 'default' => 'AND'
74 ),
75 'order' => array(
76 'type' => 'string',
77 'default' => 'desc'
78 ),
79 'orderBy' => array(
80 'type' => 'string',
81 'default' => 'date'
82 ),
83
84 //Custom Post Type
85 'postLayout' => array(
86 'type' => 'string',
87 'default' => 'list'
88 ),
89 'columns' => array(
90 'type' => 'number',
91 'default' => 3
92 ),
93 'spacing' => array(
94 'type' => 'string',
95 'default' => 'default'
96 ),
97 'align' => array(
98 'type' => 'string'
99 ),
100 'className' => array(
101 'type' => 'string'
102 ),
103
104 //Modal
105 'metaQuery' => array(
106 'type' => 'array',
107 'default' => []
108 ),
109 ),
110 'render_callback' => [ $this, 'render_callback' ]
111 )
112 );
113
114 if ( $this->isEnabled() ) {
115
116 add_filter( 'getwid/blocks_style_css/dependencies', [ $this, 'block_frontend_styles' ] );
117 }
118 }
119
120 public function getLabel() {
121 return __('Custom Post Type', 'getwid');
122 }
123
124 public function block_frontend_styles($styles) {
125
126 //fontawesome
127 // for /template-parts/*
128 $styles = getwid()->fontIconsManager()->enqueueFonts( $styles );
129
130 return $styles;
131 }
132
133 public function block_frontend_assets() {
134
135 if ( is_admin() ) {
136 return;
137 }
138
139 if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) {
140 return;
141 }
142
143 //fontawesome
144 // for /template-parts/*
145 $deps = getwid()->fontIconsManager()->enqueueFonts( [] );
146
147 add_filter( 'getwid/optimize/assets',
148 function ( $assets ) {
149 $assets[] = getwid()->settings()->getPrefix() . '-blocks-common';
150
151 return $assets;
152 }
153 );
154
155 add_filter( 'getwid/optimize/should_load_common_css', '__return_true' );
156
157 $rtl = is_rtl() ? '.rtl' : '';
158
159 wp_enqueue_style(
160 self::$blockName,
161 getwid_get_plugin_url( 'assets/blocks/custom-post-type/style' . $rtl . '.css' ),
162 $deps,
163 getwid()->settings()->getVersion()
164 );
165
166 }
167
168 public function render_callback( $attributes, $content ) {
169
170 //Custom Post Type
171 $query_args = getwid_build_custom_post_type_query( $attributes );
172
173 $q = new \WP_Query( $query_args );
174 //Custom Post Type
175
176 //Custom Template
177 $use_template = false;
178 $template_part_content = '';
179
180 if ( isset( $attributes['postTemplate'] ) && $attributes['postTemplate'] != '' ) {
181
182 $template_post = get_post($attributes['postTemplate'], ARRAY_A);
183
184 //If post exist and content not empty
185 if ( ! is_null( $template_post ) && $template_post[ 'post_content' ] != '' ) {
186 $use_template = true;
187 $template_part_content = $template_post[ 'post_content' ];
188 }
189 }
190
191 $block_name = 'wp-block-getwid-custom-post-type';
192 $post_type = isset( $attributes[ 'postType' ]) ? $attributes[ 'postType' ] : 'post';
193
194 $extra_attr = array(
195 'block_name' => $block_name
196 );
197
198 $class = $block_name;
199 $class .= ' custom-post-type-' . $post_type;
200
201 if ( isset( $attributes[ 'align' ] ) ) {
202 $class .= ' align' . $attributes[ 'align' ];
203 }
204 if ( isset( $attributes['postLayout'] ) ) {
205 $class .= ' has-layout-' . $attributes[ 'postLayout' ];
206 }
207 if ( isset( $attributes['spacing'] ) && $attributes[ 'spacing' ] != 'default' ) {
208 $class .= ' has-spacing-' . $attributes[ 'spacing' ];
209 }
210 if ( isset( $attributes[ 'className' ] ) ) {
211 $class .= ' ' . $attributes[ 'className' ];
212 }
213
214 $wrapper_class = $block_name . '__wrapper';
215
216 if ( isset( $attributes[ 'columns' ] ) && $attributes[ 'postLayout' ] === 'grid' ) {
217 $wrapper_class .= ' getwid-columns getwid-columns-' . $attributes[ 'columns' ];
218 }
219
220 ob_start();
221 ?>
222 <div class="<?php echo esc_attr( $class ); ?>">
223 <div class="<?php echo esc_attr( $wrapper_class );?>">
224 <?php
225
226 if ( ! $use_template ) {
227 $template = $post_type;
228 $located = getwid_locate_template( 'custom-post-type/' . $post_type );
229 if ( ! $located ) {
230 $template = 'post';
231 }
232 }
233
234 if ( $q->have_posts() ){
235 ob_start();
236
237 while( $q->have_posts() ):
238 $q->the_post();
239
240 ?>
241 <div class='wp-block-getwid-custom-post-type__post'>
242 <?php
243 if ($use_template){
244 echo do_blocks( $template_part_content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
245 } else {
246 getwid_get_template_part( 'custom-post-type/' . $template, $attributes, false, $extra_attr );
247 }
248 ?>
249 </div>
250 <?php
251
252 endwhile;
253
254 wp_reset_postdata();
255 ob_end_flush();
256 } else {
257 do_action( 'getwid/blocks/custom-post-type/no-items', $attributes, $content );
258 }
259 ?>
260 </div>
261
262 <?php if ( isset( $attributes[ 'pagination' ] ) && $attributes['pagination'] ){ ?>
263 <nav class="navigation pagination" role="navigation">
264 <h2 class="screen-reader-text"><?php __('Posts navigation', 'getwid') ?></h2>
265 <div class="nav-links">
266 <?php
267 $total_pages = $q->max_num_pages;
268
269 if ($attributes['offset'] != 0){
270 $total_rows = max( 0, $q->found_posts - $attributes['offset'] );
271 $total_pages = ceil( $total_rows / $attributes['postsToShow'] );
272 }
273
274 $paged = is_front_page() ? get_query_var( 'page', 1 ) : get_query_var( 'paged', 1 );
275
276 $pagination_args = array(
277 'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
278 'total' => $total_pages,
279 'current' => max( 1, $paged ),
280 'format' => '?paged=%#%',
281 'show_all' => false,
282 'type' => 'plain',
283 'end_size' => 2,
284 'mid_size' => 1,
285 'prev_next' => true,
286 'prev_text' => sprintf( '<i></i> %1$s', _x( '<', 'Previous post', 'getwid' ) ),
287 'next_text' => sprintf( '%1$s <i></i>', _x( '>', 'Next post', 'getwid' ) ),
288 'add_args' => false,
289 'add_fragment' => ''
290 );
291 $pagination_args = apply_filters( 'getwid/blocks/custom_post_type/pagination_args', $pagination_args );
292 echo paginate_links( $pagination_args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
293 ?>
294 </div>
295 </nav>
296 <?php } ?>
297 </div>
298 <?php
299
300 $result = ob_get_clean();
301
302 $this->block_frontend_assets();
303
304 return $result;
305 }
306 }
307
308 getwid()->blocksManager()->addBlock(
309 new \Getwid\Blocks\CustomPostType()
310 );
311