class-do-css.php
4 years ago
class-dynamic-content.php
4 years ago
class-enqueue-css.php
4 years ago
class-legacy-attributes.php
4 years ago
class-plugin-update.php
5 years ago
class-query-loop.php
4 years ago
class-render-blocks.php
4 years ago
class-rest.php
4 years ago
class-settings.php
4 years ago
dashboard.php
4 years ago
defaults.php
4 years ago
functions.php
4 years ago
general.php
4 years ago
generate-css.php
4 years ago
class-query-loop.php
263 lines
| 1 | <?php |
| 2 | /** |
| 3 | * This file handles the Query Loop functions. |
| 4 | * |
| 5 | * @package GenerateBlocks |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; // Exit if accessed directly. |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Query Loop functions. |
| 14 | * |
| 15 | * @since 1.5.0 |
| 16 | */ |
| 17 | class GenerateBlocks_Query_Loop { |
| 18 | /** |
| 19 | * Instance. |
| 20 | * |
| 21 | * @access private |
| 22 | * @var object Instance |
| 23 | * @since 1.2.0 |
| 24 | */ |
| 25 | private static $instance; |
| 26 | |
| 27 | /** |
| 28 | * Initiator. |
| 29 | * |
| 30 | * @since 1.2.0 |
| 31 | * @return object initialized object of class. |
| 32 | */ |
| 33 | public static function get_instance() { |
| 34 | if ( ! isset( self::$instance ) ) { |
| 35 | self::$instance = new self(); |
| 36 | } |
| 37 | |
| 38 | return self::$instance; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Constructor. |
| 43 | */ |
| 44 | public function __construct() { |
| 45 | add_filter( 'generateblocks_attr_grid-wrapper', array( $this, 'add_grid_wrapper_attributes' ), 10, 2 ); |
| 46 | add_filter( 'generateblocks_attr_grid-item', array( $this, 'add_grid_item_attributes' ), 10, 2 ); |
| 47 | add_filter( 'generateblocks_attr_button-container', array( $this, 'add_button_wrapper_attributes' ), 10, 2 ); |
| 48 | add_filter( 'generateblocks_defaults', array( $this, 'add_block_defaults' ) ); |
| 49 | add_filter( 'generateblocks_query_loop_args', array( $this, 'set_query_loop_defaults' ) ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Helper function that constructs a WP_Query args array from |
| 54 | * a `Query` block properties. |
| 55 | * |
| 56 | * @param WP_Block $block Block instance. |
| 57 | * @param int $page Current query's page. |
| 58 | * |
| 59 | * @todo: https://github.com/WordPress/wordpress-develop/blob/44e308c12e68b5c6b63845fd84369ba36985e193/src/wp-includes/blocks.php#L1126 |
| 60 | */ |
| 61 | public static function get_query_args( $block, $page ) { |
| 62 | $query_attributes = ( is_array( $block->context ) && isset( $block->context['generateblocks/query'] ) ) |
| 63 | ? $block->context['generateblocks/query'] |
| 64 | : array(); |
| 65 | |
| 66 | // Set up our pagination. |
| 67 | $query_attributes['paged'] = $page; |
| 68 | |
| 69 | $query_args = self::map_post_type_attributes( $query_attributes ); |
| 70 | |
| 71 | if ( isset( $query_args['tax_query'] ) ) { |
| 72 | $query_args['tax_query'] = self::normalize_tax_query_attributes( $query_args['tax_query'] ); |
| 73 | } |
| 74 | |
| 75 | if ( isset( $query_args['date_query_after'] ) || isset( $query_args['date_query_before'] ) ) { |
| 76 | $query_args['date_query'] = self::normalize_date_query_attributes( |
| 77 | isset( $query_args['date_query_after'] ) ? $query_args['date_query_after'] : null, |
| 78 | isset( $query_args['date_query_before'] ) ? $query_args['date_query_before'] : null |
| 79 | ); |
| 80 | |
| 81 | unset( $query_args['date_query_after'] ); |
| 82 | unset( $query_args['date_query_before'] ); |
| 83 | } |
| 84 | |
| 85 | if ( isset( $query_args['sticky'] ) ) { |
| 86 | $sticky_posts = get_option( 'sticky_posts' ); |
| 87 | $query_args['post__in'] = $sticky_posts; |
| 88 | unset( $query_args['sticky'] ); |
| 89 | } |
| 90 | |
| 91 | if ( isset( $query_args['tax_query_exclude'] ) ) { |
| 92 | $not_in_tax_query = self::normalize_tax_query_attributes( $query_args['tax_query_exclude'], 'NOT IN' ); |
| 93 | $query_args['tax_query'] = isset( $query_args['tax_query'] ) |
| 94 | ? array_merge( $query_args['tax_query'], $not_in_tax_query ) |
| 95 | : $not_in_tax_query; |
| 96 | |
| 97 | unset( $query_args['tax_query_exclude'] ); |
| 98 | } |
| 99 | |
| 100 | if ( |
| 101 | isset( $query_args['posts_per_page'] ) && |
| 102 | is_numeric( $query_args['posts_per_page'] ) |
| 103 | ) { |
| 104 | $per_page = intval( $query_args['posts_per_page'] ); |
| 105 | $offset = 0; |
| 106 | |
| 107 | if ( |
| 108 | isset( $query_args['offset'] ) && |
| 109 | is_numeric( $query_args['offset'] ) |
| 110 | ) { |
| 111 | $offset = absint( $query_args['offset'] ); |
| 112 | } |
| 113 | |
| 114 | $query_args['offset'] = ( $per_page * ( $page - 1 ) ) + $offset; |
| 115 | $query_args['posts_per_page'] = $per_page; |
| 116 | } |
| 117 | |
| 118 | return $query_args; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Map query parameters to their correct query names. |
| 123 | * |
| 124 | * @param array $attributes Block attributes. |
| 125 | */ |
| 126 | public static function map_post_type_attributes( $attributes ) { |
| 127 | $attributes_map = array( |
| 128 | 'page' => 'paged', |
| 129 | 'per_page' => 'posts_per_page', |
| 130 | 'search' => 's', |
| 131 | 'after' => 'date_query_after', |
| 132 | 'before' => 'date_query_before', |
| 133 | 'author' => 'author__in', |
| 134 | 'exclude' => 'post__not_in', |
| 135 | 'include' => 'post__in', |
| 136 | 'order' => 'order', |
| 137 | 'orderby' => 'orderby', |
| 138 | 'status' => 'post_status', |
| 139 | 'parent' => 'post_parent__in', |
| 140 | 'parent_exclude' => 'post_parent__not_in', |
| 141 | 'author_exclude' => 'author__not_in', |
| 142 | ); |
| 143 | |
| 144 | return generateblocks_map_array_keys( $attributes, $attributes_map ); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Normalize the tax query attributes to be used in the WP_Query |
| 149 | * |
| 150 | * @param array $raw_tax_query Tax query. |
| 151 | * @param string $operator Tax operator. |
| 152 | * |
| 153 | * @return array|array[] |
| 154 | */ |
| 155 | public static function normalize_tax_query_attributes( $raw_tax_query, $operator = 'IN' ) { |
| 156 | return array_map( |
| 157 | function( $tax ) use ( $operator ) { |
| 158 | return array( |
| 159 | 'taxonomy' => $tax['taxonomy'], |
| 160 | 'field' => 'term_id', |
| 161 | 'terms' => $tax['terms'], |
| 162 | 'operator' => $operator, |
| 163 | 'include_children' => false, |
| 164 | ); |
| 165 | }, |
| 166 | $raw_tax_query |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Normalize the date query attributes to be used in the WP_Query |
| 172 | * |
| 173 | * @param string|null $after The after date. |
| 174 | * @param string|null $before The before date. |
| 175 | * |
| 176 | * @return array |
| 177 | */ |
| 178 | public static function normalize_date_query_attributes( $after = null, $before = null ) { |
| 179 | $result = array( 'inclusive' => true ); |
| 180 | |
| 181 | if ( generateblocks_is_valid_date( $after ) ) { |
| 182 | $result['after'] = $after; |
| 183 | } |
| 184 | |
| 185 | if ( generateblocks_is_valid_date( $before ) ) { |
| 186 | $result['before'] = $before; |
| 187 | } |
| 188 | |
| 189 | return $result; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Add defaults for our Query settings. |
| 194 | * |
| 195 | * @param array $defaults Block defaults. |
| 196 | */ |
| 197 | public function add_block_defaults( $defaults ) { |
| 198 | $defaults['container']['isQueryLoopItem'] = false; |
| 199 | $defaults['gridContainer']['isQueryLoop'] = false; |
| 200 | $defaults['buttonContainer']['isPagination'] = false; |
| 201 | |
| 202 | return $defaults; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Add HTML attributes to the Query Loop wrapper. |
| 207 | * |
| 208 | * @param array $attributes Existing HTML attributes. |
| 209 | * @param array $settings Block settings. |
| 210 | */ |
| 211 | public function add_grid_wrapper_attributes( $attributes, $settings ) { |
| 212 | if ( $settings['isQueryLoop'] ) { |
| 213 | $attributes['class'] .= ' gb-query-loop-wrapper'; |
| 214 | } |
| 215 | |
| 216 | return $attributes; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Add HTML attributes to the Query Loop Item wrapper. |
| 221 | * |
| 222 | * @param array $attributes Existing HTML attributes. |
| 223 | * @param array $settings Block settings. |
| 224 | */ |
| 225 | public function add_grid_item_attributes( $attributes, $settings ) { |
| 226 | if ( $settings['isQueryLoopItem'] ) { |
| 227 | $attributes['class'] .= ' ' . implode( ' ', get_post_class( 'gb-query-loop-item' ) ); |
| 228 | } |
| 229 | |
| 230 | return $attributes; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Add HTML attributes to the Button wrapper. |
| 235 | * |
| 236 | * @param array $attributes Existing HTML attributes. |
| 237 | * @param array $settings Block settings. |
| 238 | */ |
| 239 | public function add_button_wrapper_attributes( $attributes, $settings ) { |
| 240 | if ( $settings['isPagination'] ) { |
| 241 | $attributes['class'] .= ' gb-query-loop-pagination'; |
| 242 | } |
| 243 | |
| 244 | return $attributes; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Set the default query loop arguments. |
| 249 | * |
| 250 | * @param array $query_args The query loop arguments. |
| 251 | * @return array The query loop arguments with defaults. |
| 252 | */ |
| 253 | public function set_query_loop_defaults( $query_args ) { |
| 254 | if ( ! isset( $query_args['posts_per_page'] ) || '' === $query_args['posts_per_page'] ) { |
| 255 | $query_args['posts_per_page'] = 10; |
| 256 | } |
| 257 | |
| 258 | return $query_args; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | GenerateBlocks_Query_Loop::get_instance(); |
| 263 |