PluginProbe
Getwid – Gutenberg Blocks / 3.0.1
Getwid – Gutenberg Blocks v3.0.1
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
getwid / includes / blocks / custom-post-type.php

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

152 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 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 );
152