QueryLoopBase.php
156 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Core\Blocks\Query; |
| 4 | |
| 5 | use Kubio\Core\Blocks\BlockContainerBase; |
| 6 | use Kubio\Core\Layout\LayoutHelper; |
| 7 | use Kubio\Core\LodashBasic; |
| 8 | use IlluminateAgnostic\Arr\Support\Arr; |
| 9 | |
| 10 | class QueryLoopBase extends BlockContainerBase { |
| 11 | const CONTAINER = 'container'; |
| 12 | const INNER = 'inner'; |
| 13 | const CENTER = 'center'; |
| 14 | const INNER_GAPS = 'innerGaps'; |
| 15 | |
| 16 | private $query_context = null; |
| 17 | |
| 18 | |
| 19 | |
| 20 | public function mapPropsToElements() { |
| 21 | $layout_media = $this->getPropByMedia( 'layout' ); |
| 22 | $layout_helper = new LayoutHelper( $layout_media ); |
| 23 | $masonry = $this->getAttribute( 'masonry', false ); |
| 24 | $masonry_classes = array(); |
| 25 | if ( $masonry ) { |
| 26 | $masonry_classes[] = 'kubio-query-loop--use-masonry'; |
| 27 | } |
| 28 | $map = array(); |
| 29 | $map[ self::CONTAINER ] = array( 'className' => $layout_helper->getRowGapClasses() ); |
| 30 | $map[ self::INNER ] = array( |
| 31 | 'className' => LodashBasic::concat( $layout_helper->getRowGapInnerClasses(), $layout_helper->getRowAlignClasses(), $masonry_classes ), |
| 32 | 'innerHTML' => $this->renderList(), |
| 33 | ); |
| 34 | |
| 35 | return $map; |
| 36 | } |
| 37 | |
| 38 | public function renderList() { |
| 39 | |
| 40 | $posts = $this->getPostList(); |
| 41 | $content = "<!--kubio post list : start-->\n"; |
| 42 | |
| 43 | if ( empty( $posts ) && $this->isUsingMainQuery() ) { |
| 44 | $post_type = get_post_type_object( $this->getPostType() ); |
| 45 | $label = $post_type->labels->name; |
| 46 | |
| 47 | $not_found_text = $this->getAttribute( 'notFound' ); |
| 48 | |
| 49 | if ( $not_found_text === null ) { |
| 50 | $not_found_text = __( 'No {post_title} found!', 'kubio' ); |
| 51 | } |
| 52 | |
| 53 | $content .= '<h2 class="kubio-empty-query-result">' . str_replace( '{post_title}', $label, kubio_wpml_get_translated_string( $not_found_text ) ) . '</h2>'; |
| 54 | } else { |
| 55 | foreach ( (array) $posts as $post ) { |
| 56 | foreach ( $this->block_data['innerBlocks'] as $inner_block ) { |
| 57 | $content .= ( |
| 58 | new \WP_Block( |
| 59 | $inner_block, |
| 60 | array( |
| 61 | 'postType' => $post->post_type, |
| 62 | 'postId' => $post->ID, |
| 63 | ) |
| 64 | ) |
| 65 | )->render(); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | $content .= "\n<!--kubio post list : end-->"; |
| 71 | |
| 72 | return $content; |
| 73 | } |
| 74 | |
| 75 | public function getPostList() { |
| 76 | if ( $this->isUsingMainQuery() ) { |
| 77 | /** |
| 78 | * @var \WP_Query $wp_query ; |
| 79 | */ |
| 80 | global $wp_query; |
| 81 | |
| 82 | return $wp_query->posts; |
| 83 | } |
| 84 | |
| 85 | return $this->getPostsQueryList(); |
| 86 | } |
| 87 | |
| 88 | public function getQueryContext() { |
| 89 | if ( ! $this->query_context ) { |
| 90 | $this->query_context = array_merge( |
| 91 | $this->queryDefault(), |
| 92 | $this->block_context |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | return $this->query_context; |
| 97 | } |
| 98 | |
| 99 | public function queryDefault() { |
| 100 | return array( |
| 101 | 'queryId' => 1, |
| 102 | 'useMainQuery' => false, |
| 103 | 'query' => array( |
| 104 | 'postType' => 'post', |
| 105 | 'sticky' => false, |
| 106 | 'post__in' => array(), |
| 107 | // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in |
| 108 | 'post__not_in' => array(), |
| 109 | // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude |
| 110 | 'exclude' => array(), |
| 111 | 'categoryIds' => array(), |
| 112 | 'perPage' => get_option( 'posts_per_page' ), |
| 113 | 'offset' => 0, |
| 114 | 'tagIds' => array(), |
| 115 | 'author' => array(), |
| 116 | 'search' => array(), |
| 117 | 'orderBy' => 'date', |
| 118 | 'order' => 'DESC', |
| 119 | ), |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | public function getPostsQueryList() { |
| 124 | $context = $this->getQueryContext(); |
| 125 | $page_key = isset( $context['queryId'] ) ? 'query-' . $context['queryId'] . '-page' : 'query-page'; |
| 126 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 127 | $page = empty( $_GET[ $page_key ] ) ? 1 : filter_var( $_GET[ $page_key ], FILTER_VALIDATE_INT ); |
| 128 | |
| 129 | /** @noinspection PhpParamsInspection */ |
| 130 | $query_args = build_query_vars_from_query_block( (object) array( 'context' => $context ), $page ); |
| 131 | $query_args['ignore_sticky_posts'] = Arr::get( $context, 'query.sticky' ) ? false : true; |
| 132 | $query = new \WP_Query( $query_args ); |
| 133 | |
| 134 | return $query->posts; |
| 135 | } |
| 136 | |
| 137 | public function getPostType() { |
| 138 | |
| 139 | if ( $this->isUsingMainQuery() ) { |
| 140 | $post_type = get_query_var( 'post_type', '' ); |
| 141 | if ( ! $post_type ) { |
| 142 | $post_type = 'post'; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | $context = $this->getQueryContext(); |
| 147 | |
| 148 | return $context['query']['postType']; |
| 149 | } |
| 150 | |
| 151 | public function isUsingMainQuery() { |
| 152 | $context = $this->getQueryContext(); |
| 153 | return isset( $context['useMainQuery'] ) && $context['useMainQuery']; |
| 154 | } |
| 155 | } |
| 156 |