auxin-elements
/
includes
/
elementor
/
modules
/
query-control
/
controls
/
group-control-posts.php
group-control-posts.php
4 years ago
group-control-query.php
6 years ago
group-control-related.php
6 years ago
query.php
6 years ago
group-control-posts.php
300 lines
| 1 | <?php |
| 2 | namespace Auxin\Plugin\CoreElements\Elementor\Modules\QueryControl\Controls; |
| 3 | |
| 4 | |
| 5 | use Elementor\Controls_Manager; |
| 6 | use Elementor\Group_Control_Base; |
| 7 | use Elementor\Utils as ElementorUtils; |
| 8 | |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; // Exit if accessed directly |
| 11 | } |
| 12 | |
| 13 | class Group_Control_Posts extends Group_Control_Base { |
| 14 | |
| 15 | const INLINE_MAX_RESULTS = 15; |
| 16 | |
| 17 | protected static $fields; |
| 18 | |
| 19 | public static function get_type() { |
| 20 | return 'aux-posts'; |
| 21 | } |
| 22 | |
| 23 | public static function on_export_remove_setting_from_element( $element, $control_id ) { |
| 24 | |
| 25 | unset( $element['settings'][ $control_id . '_posts_ids' ] ); |
| 26 | unset( $element['settings'][ $control_id . '_authors' ] ); |
| 27 | |
| 28 | foreach ( auxin_get_public_post_types() as $post_type => $label ) { |
| 29 | $taxonomy_filter_args = [ |
| 30 | 'show_in_nav_menus' => true, |
| 31 | 'object_type' => [ $post_type ], |
| 32 | ]; |
| 33 | |
| 34 | $taxonomies = get_taxonomies( $taxonomy_filter_args, 'objects' ); |
| 35 | |
| 36 | foreach ( $taxonomies as $taxonomy => $object ) { |
| 37 | unset( $element['settings'][ $control_id . '_' . $taxonomy . '_ids' ] ); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | return $element; |
| 42 | } |
| 43 | |
| 44 | protected function init_fields() { |
| 45 | $fields = []; |
| 46 | |
| 47 | $fields['post_type'] = [ |
| 48 | 'label' => __( 'Source', 'auxin-elements' ), |
| 49 | 'type' => Controls_Manager::SELECT, |
| 50 | ]; |
| 51 | |
| 52 | $fields['posts_ids'] = [ |
| 53 | 'label' => __( 'Search & Select', 'auxin-elements' ), |
| 54 | 'type' => 'aux-query', |
| 55 | 'post_type' => '', |
| 56 | 'options' => [], |
| 57 | 'label_block' => true, |
| 58 | 'multiple' => true, |
| 59 | 'filter_type' => 'by_id', |
| 60 | 'condition' => [ |
| 61 | 'post_type' => 'by_id', |
| 62 | ], |
| 63 | 'export' => false, |
| 64 | ]; |
| 65 | |
| 66 | $fields['authors'] = [ |
| 67 | 'label' => __( 'Author', 'auxin-elements' ), |
| 68 | 'label_block' => true, |
| 69 | 'type' => 'aux-query', |
| 70 | 'multiple' => true, |
| 71 | 'default' => [], |
| 72 | 'options' => [], |
| 73 | 'filter_type' => 'author', |
| 74 | 'condition' => [ |
| 75 | 'post_type!' => [ |
| 76 | 'by_id', |
| 77 | 'current_query', |
| 78 | ], |
| 79 | ], |
| 80 | 'export' => false, |
| 81 | ]; |
| 82 | |
| 83 | return $fields; |
| 84 | } |
| 85 | |
| 86 | protected function prepare_fields( $fields ) { |
| 87 | $args = $this->get_args(); |
| 88 | |
| 89 | $post_type_args = []; |
| 90 | if ( ! empty( $args['post_type'] ) ) { |
| 91 | $post_type_args['post_type'] = $args['post_type']; |
| 92 | } |
| 93 | |
| 94 | $post_types = auxin_get_public_post_types( $post_type_args ); |
| 95 | |
| 96 | $post_types_options = $post_types; |
| 97 | |
| 98 | $post_types_options['by_id'] = __( 'Manual Selection', 'auxin-elements' ); |
| 99 | $post_types_options['current_query'] = __( 'Current Query', 'auxin-elements' ); |
| 100 | |
| 101 | $fields['post_type']['options'] = $post_types_options; |
| 102 | |
| 103 | $fields['post_type']['default'] = key( $post_types ); |
| 104 | |
| 105 | $fields['posts_ids']['object_type'] = array_keys( $post_types ); |
| 106 | |
| 107 | $taxonomy_filter_args = [ |
| 108 | 'show_in_nav_menus' => true, |
| 109 | ]; |
| 110 | |
| 111 | $taxonomies = get_taxonomies( $taxonomy_filter_args, 'objects' ); |
| 112 | |
| 113 | // bypass bug in WP_List_Util::filter() causing wrong array comparison |
| 114 | // when a taxonomy belongs to several post-types (e.g. when using woocommerce-product-add-ons) |
| 115 | // ( using simple '==' rather than in_array() or array_intersect() ). |
| 116 | $filtered_taxonomies = []; |
| 117 | if ( ! empty( $args['post_type'] ) ) { |
| 118 | foreach ( $taxonomies as $taxonomy => $obj ) { |
| 119 | $tax_array = (array) $obj; |
| 120 | if ( in_array( $args['post_type'], $tax_array['object_type'] ) ) { |
| 121 | $filtered_taxonomies[ $taxonomy ] = $obj; |
| 122 | } |
| 123 | } |
| 124 | } else { |
| 125 | $filtered_taxonomies = $taxonomies; |
| 126 | } |
| 127 | |
| 128 | foreach ( $filtered_taxonomies as $taxonomy => $object ) { |
| 129 | $taxonomy_args = [ |
| 130 | 'label' => $object->label, |
| 131 | 'type' => 'aux-query', |
| 132 | 'label_block' => true, |
| 133 | 'multiple' => true, |
| 134 | 'object_type' => $taxonomy, |
| 135 | 'options' => [], |
| 136 | 'condition' => [ |
| 137 | 'post_type' => $object->object_type, |
| 138 | ], |
| 139 | 'export' => false, |
| 140 | ]; |
| 141 | |
| 142 | $count = wp_count_terms( $taxonomy ); |
| 143 | |
| 144 | $options = []; |
| 145 | |
| 146 | // For large websites, use Ajax to search |
| 147 | if ( $count > self::INLINE_MAX_RESULTS ) { |
| 148 | $taxonomy_args['type'] = 'aux-query'; |
| 149 | |
| 150 | $taxonomy_args['filter_type'] = 'taxonomy'; |
| 151 | } else { |
| 152 | $taxonomy_args['type'] = Controls_Manager::SELECT2; |
| 153 | |
| 154 | $terms = get_terms( [ |
| 155 | 'taxonomy' => $taxonomy, |
| 156 | 'hide_empty' => false, |
| 157 | ] ); |
| 158 | |
| 159 | foreach ( $terms as $term ) { |
| 160 | $options[ $term->term_id ] = $term->name; |
| 161 | } |
| 162 | |
| 163 | $taxonomy_args['options'] = $options; |
| 164 | } |
| 165 | |
| 166 | $fields[ $taxonomy . '_ids' ] = $taxonomy_args; |
| 167 | } |
| 168 | |
| 169 | return parent::prepare_fields( $fields ); |
| 170 | } |
| 171 | |
| 172 | protected function get_default_options() { |
| 173 | return [ |
| 174 | 'popover' => false, |
| 175 | ]; |
| 176 | } |
| 177 | |
| 178 | protected function fix_offset( $query_args, $settings, $prefix = '' ) { |
| 179 | if ( 0 < $settings[ $prefix . 'offset' ] ) { |
| 180 | /** |
| 181 | * Due to a WordPress bug, the offset will be set later, in $this->fix_query_offset() |
| 182 | * @see https://codex.wordpress.org/Making_Custom_Queries_using_Offset_and_Pagination |
| 183 | */ |
| 184 | $query_args['offset_to_fix'] = $settings[ $prefix . 'offset' ]; |
| 185 | } |
| 186 | |
| 187 | return $query_args; |
| 188 | } |
| 189 | |
| 190 | protected function build_query_args( $settings, $control_id_prefix ) { |
| 191 | |
| 192 | $prefix = $control_id_prefix . '_'; |
| 193 | |
| 194 | $post_type = $settings[ $prefix . 'post_type' ]; |
| 195 | |
| 196 | $query_args = [ |
| 197 | 'orderby' => $settings['orderby'], |
| 198 | 'order' => $settings['order'], |
| 199 | 'ignore_sticky_posts' => 1, |
| 200 | 'post_status' => 'publish', // Hide drafts/private posts for admins |
| 201 | ]; |
| 202 | |
| 203 | if ( 'by_id' === $post_type ) { |
| 204 | $post_types = auxin_get_public_post_types(); |
| 205 | |
| 206 | $query_args['post_type'] = array_keys( $post_types ); |
| 207 | $query_args['posts_per_page'] = -1; |
| 208 | |
| 209 | $query_args['post__in'] = $settings[ $prefix . 'posts_ids' ]; |
| 210 | |
| 211 | if ( empty( $query_args['post__in'] ) ) { |
| 212 | // If no selection - return an empty query |
| 213 | $query_args['post__in'] = [ 0 ]; |
| 214 | } |
| 215 | } else { |
| 216 | $query_args['post_type'] = $post_type; |
| 217 | $query_args['posts_per_page'] = $settings['posts_per_page']; |
| 218 | $query_args['tax_query'] = []; |
| 219 | |
| 220 | $query_args = $this->fix_offset( $query_args, $settings ); |
| 221 | |
| 222 | $taxonomies = get_object_taxonomies( $post_type, 'objects' ); |
| 223 | |
| 224 | foreach ( $taxonomies as $object ) { |
| 225 | $setting_key = $prefix . $object->name . '_ids'; |
| 226 | |
| 227 | if ( ! empty( $settings[ $setting_key ] ) ) { |
| 228 | $query_args['tax_query'][] = [ |
| 229 | 'taxonomy' => $object->name, |
| 230 | 'field' => 'term_id', |
| 231 | 'terms' => $settings[ $setting_key ], |
| 232 | ]; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | if ( ! empty( $settings[ $prefix . 'authors' ] ) ) { |
| 238 | $query_args['author__in'] = $settings[ $prefix . 'authors' ]; |
| 239 | } |
| 240 | |
| 241 | $post__not_in = []; |
| 242 | if ( ! empty( $settings['exclude'] ) ) { |
| 243 | if ( in_array( 'current_post', $settings['exclude'], true ) ) { |
| 244 | if ( ElementorUtils::is_ajax() && ! empty( $_REQUEST['post_id'] ) ) { |
| 245 | $post__not_in[] = $_REQUEST['post_id']; |
| 246 | } elseif ( is_singular() ) { |
| 247 | $post__not_in[] = get_queried_object_id(); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | if ( in_array( 'manual_selection', $settings['exclude'], true ) && ! empty( $settings['exclude_ids'] ) ) { |
| 252 | $post__not_in = array_merge( $post__not_in, $settings['exclude_ids'] ); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | if ( ! empty( $settings['avoid_duplicates'] ) && 'yes' === $settings['avoid_duplicates'] ) { |
| 257 | $post__not_in = array_merge( $post__not_in, Module::$displayed_ids ); |
| 258 | } |
| 259 | |
| 260 | $query_args['post__not_in'] = $post__not_in; |
| 261 | |
| 262 | return $query_args; |
| 263 | } |
| 264 | |
| 265 | public function get_query_args( $control_id_prefix, $settings ) { |
| 266 | global $wp_query; |
| 267 | $defaults = [ |
| 268 | $control_id_prefix . '_post_type' => 'post', |
| 269 | $control_id_prefix . '_posts_ids' => [], |
| 270 | 'orderby' => 'date', |
| 271 | 'order' => 'desc', |
| 272 | 'posts_per_page' => 3, |
| 273 | 'offset' => 0, |
| 274 | ]; |
| 275 | |
| 276 | $settings = wp_parse_args( $settings, $defaults ); |
| 277 | |
| 278 | $post_type = $settings[ $control_id_prefix . '_post_type' ]; |
| 279 | |
| 280 | if ( 'current_query' === $post_type ) { |
| 281 | $current_query_vars = $wp_query->query_vars; |
| 282 | |
| 283 | /** |
| 284 | * Current query variables. |
| 285 | * |
| 286 | * Filters the query variables for the current query. |
| 287 | * |
| 288 | * @since 1.0.0 |
| 289 | * |
| 290 | * @param array $current_query_vars Current query variables. |
| 291 | */ |
| 292 | $current_query_vars = apply_filters( 'auxin/core_elements/query_control/get_query_args/current_query', $current_query_vars ); |
| 293 | |
| 294 | return $current_query_vars; |
| 295 | } |
| 296 | |
| 297 | return $this->build_query_args( $settings, $control_id_prefix ); |
| 298 | } |
| 299 | } |
| 300 |