PluginProbe
Getwid – Gutenberg Blocks / 3.0.2
Getwid – Gutenberg Blocks v3.0.2
3.0.2 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
← All changes | includes/blocks/custom-post-type.php +151 -308 1.8.73.0.2 View file →
@@ -1,308 +1,151 @@
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 - $pagination_args = array(
275 - 'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
276 - 'total' => $total_pages,
277 - 'current' => max( 1, get_query_var( 'paged' ) ),
278 - 'format' => '?paged=%#%',
279 - 'show_all' => false,
280 - 'type' => 'plain',
281 - 'end_size' => 2,
282 - 'mid_size' => 1,
283 - 'prev_next' => true,
284 - 'prev_text' => sprintf( '<i></i> %1$s', _x( '<', 'Previous post', 'getwid' ) ),
285 - 'next_text' => sprintf( '%1$s <i></i>', _x( '>', 'Next post', 'getwid' ) ),
286 - 'add_args' => false,
287 - 'add_fragment' => ''
288 - );
289 - $pagination_args = apply_filters( 'getwid/blocks/custom_post_type/pagination_args', $pagination_args );
290 - echo paginate_links( $pagination_args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
291 - ?>
292 - </div>
293 - </nav>
294 - <?php } ?>
295 - </div>
296 - <?php
297 -
298 - $result = ob_get_clean();
299 -
300 - $this->block_frontend_assets();
301 -
302 - return $result;
303 - }
304 -}
305 -
306 -getwid()->blocksManager()->addBlock(
307 - new \Getwid\Blocks\CustomPostType()
308 -);
1 +<?php
2 +
3 +namespace Getwid\Blocks;
4 +
5 +class CustomPostType extends AbstractBlock {
6 +
7 + public function __construct() {
8 +
9 + parent::__construct( 'getwid/custom-post-type' );
10 +
11 + register_block_type(
12 + getwid_get_plugin_path( 'assets/blocks/custom-post-type' ),
13 + array(
14 + 'render_callback' => array( $this, 'render_callback' ),
15 + )
16 + );
17 + }
18 +
19 + public function get_label() {
20 + return __( 'Custom Post Type', 'getwid' );
21 + }
22 +
23 + public function render_callback( $attributes, $content ) {
24 +
25 + $query_args = getwid_build_custom_post_type_query( $attributes );
26 + $q = new \WP_Query( $query_args );
27 +
28 + $use_template = false;
29 + $template_part_content = '';
30 +
31 + if ( isset( $attributes['postTemplate'] ) && '' !== $attributes['postTemplate'] ) {
32 + $template_post = get_post( $attributes['postTemplate'], ARRAY_A );
33 +
34 + if ( ! is_null( $template_post ) && '' !== $template_post['post_content'] ) {
35 + $use_template = true;
36 + $template_part_content = $template_post['post_content'];
37 + }
38 + }
39 +
40 + $block_name = 'wp-block-getwid-custom-post-type';
41 + $post_type = isset( $attributes['postType'] ) ? $attributes['postType'] : 'post';
42 +
43 + $extra_attr = array(
44 + 'block_name' => $block_name,
45 + );
46 +
47 + $class = $block_name;
48 + $class .= ' custom-post-type-' . $post_type;
49 +
50 + if ( isset( $attributes['align'] ) ) {
51 + $class .= ' align' . $attributes['align'];
52 + }
53 + if ( isset( $attributes['postLayout'] ) ) {
54 + $class .= ' has-layout-' . $attributes['postLayout'];
55 + }
56 + if ( isset( $attributes['spacing'] ) && 'default' !== $attributes['spacing'] ) {
57 + $class .= ' has-spacing-' . $attributes['spacing'];
58 + }
59 + if ( isset( $attributes['className'] ) ) {
60 + $class .= ' ' . $attributes['className'];
61 + }
62 +
63 + $wrapper_class = $block_name . '__wrapper';
64 +
65 + if ( isset( $attributes['columns'] ) && isset( $attributes['postLayout'] ) && 'grid' === $attributes['postLayout'] ) {
66 + $wrapper_class .= ' getwid-columns getwid-columns-' . $attributes['columns'];
67 + }
68 +
69 + ob_start();
70 + ?>
71 + <div class="<?php echo esc_attr( $class ); ?>">
72 + <div class="<?php echo esc_attr( $wrapper_class ); ?>">
73 + <?php
74 + if ( ! $use_template ) {
75 + $template = $post_type;
76 + $located = getwid_locate_template( 'custom-post-type/' . $post_type );
77 + if ( ! $located ) {
78 + $template = 'post';
79 + }
80 + }
81 +
82 + if ( $q->have_posts() ) {
83 + while ( $q->have_posts() ) :
84 + $q->the_post();
85 + ?>
86 + <div class="wp-block-getwid-custom-post-type__post">
87 + <?php
88 + if ( $use_template ) {
89 + echo do_blocks( $template_part_content ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
90 + } else {
91 + getwid_get_template_part( 'custom-post-type/' . $template, $attributes, false, $extra_attr );
92 + }
93 + ?>
94 + </div>
95 + <?php
96 + endwhile;
97 +
98 + wp_reset_postdata();
99 + } else {
100 + do_action( 'getwid/blocks/custom-post-type/no-items', $attributes, $content );
101 + }
102 + ?>
103 + </div>
104 +
105 + <?php if ( isset( $attributes['pagination'] ) && $attributes['pagination'] ) : ?>
106 + <nav class="navigation pagination" role="navigation">
107 + <h2 class="screen-reader-text"><?php esc_html_e( 'Posts navigation', 'getwid' ); ?></h2>
108 + <div class="nav-links">
109 + <?php
110 + $total_pages = $q->max_num_pages;
111 +
112 + if ( isset( $attributes['offset'], $attributes['postsToShow'] ) && 0 !== $attributes['offset'] ) {
113 + $total_rows = max( 0, $q->found_posts - $attributes['offset'] );
114 + $total_pages = ceil( $total_rows / $attributes['postsToShow'] );
115 + }
116 +
117 + $paged = is_front_page() ? get_query_var( 'page', 1 ) : get_query_var( 'paged', 1 );
118 +
119 + $pagination_args = array(
120 + 'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
121 + 'total' => $total_pages,
122 + 'current' => max( 1, $paged ),
123 + 'format' => '?paged=%#%',
124 + 'show_all' => false,
125 + 'type' => 'plain',
126 + 'end_size' => 2,
127 + 'mid_size' => 1,
128 + 'prev_next' => true,
129 + 'prev_text' => sprintf( '<i></i> %1$s', _x( '<', 'Previous post', 'getwid' ) ),
130 + 'next_text' => sprintf( '%1$s <i></i>', _x( '>', 'Next post', 'getwid' ) ),
131 + 'add_args' => false,
132 + 'add_fragment' => '',
133 + );
134 + $pagination_args = apply_filters( 'getwid/blocks/custom_post_type/pagination_args', $pagination_args );
135 + echo paginate_links( $pagination_args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
136 + ?>
137 + </div>
138 + </nav>
139 + <?php endif; ?>
140 + </div>
141 + <?php
142 +
143 + $result = ob_get_clean();
144 +
145 + return $result;
146 + }
147 +}
148 +
149 +getwid()->blocksManager()->addBlock(
150 + new CustomPostType()
151 +);