PluginProbe ʕ •ᴥ•ʔ
Shortcodes and extra features for Phlox theme / 2.5.8
Shortcodes and extra features for Phlox theme v2.5.8
2.17.21 2.17.20 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.6 1.0.9 1.1.0 1.3.0 1.3.1 1.3.10 1.3.14 1.3.2 1.3.3 1.3.6 1.4.0 1.4.1 1.4.2 1.5.0 1.5.2 1.6.0 1.6.2 1.6.4 1.7.0 1.7.2 2.10.0 2.10.1 2.10.3 2.10.5 2.10.7 2.10.8 2.10.9 2.11.0 2.11.1 2.11.2 2.12.0 2.14.0 2.15.0 2.15.2 2.15.4 2.15.5 2.15.6 2.15.7 2.15.8 2.15.9 2.16.0 2.16.1 2.16.2 2.16.3 2.16.4 2.17.0 2.17.1 2.17.12 2.17.13 2.17.14 2.17.15 2.17.16 2.17.2 2.17.3 2.17.4 2.17.5 2.17.6 2.17.8 2.17.9 2.4.12 2.4.13 2.4.14 2.4.16 2.4.18 2.4.19 2.4.9 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.14 2.5.15 2.5.16 2.5.17 2.5.19 2.5.2 2.5.20 2.5.3 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.10 2.6.12 2.6.13 2.6.14 2.6.15 2.6.16 2.6.17 2.6.19 2.6.2 2.6.20 2.6.4 2.6.5 2.6.7 2.7.0 2.7.1 2.7.10 2.7.11 2.7.12 2.7.13 2.7.14 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.9 2.9.0 2.9.12 2.9.14 2.9.15 2.9.16 2.9.17 2.9.18 2.9.19 2.9.2 2.9.20 2.9.21 2.9.22 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 2.9.8
auxin-elements / includes / elementor / modules / query-control / classes / elementor-related-query.php
auxin-elements / includes / elementor / modules / query-control / classes Last commit date
elementor-post-query.php 6 years ago elementor-related-query.php 6 years ago
elementor-related-query.php
152 lines
1 <?php
2 namespace Auxin\Plugin\CoreElements\Elementor\Modules\QueryControl\Classes;
3
4 use Elementor\Widget_Base;
5
6 class Elementor_Related_Query extends Elementor_Post_Query {
7 private $fallback_args;
8 private $related_post_id;
9
10 /**
11 * Elementor_Post_Query constructor.
12 *
13 * @param Widget_Base $widget
14 * @param string $group_query_name
15 * @param array $query_args
16 * @param array $fallback_args
17 */
18 public function __construct( $widget, $group_query_name, $query_args = [], $fallback_args = [] ) {
19 parent::__construct( $widget, $group_query_name, $query_args );
20 $this->related_post_id = -1;
21 $this->fallback_args = $fallback_args;
22 }
23
24 /**
25 * 1) build query args
26 * 2) invoke callback to fine-tune query-args
27 * 3) generate WP_Query object
28 * 4) if no results & fallback is set, generate a new WP_Query with fallback args
29 * 5) return WP_Query
30 *
31 * @return \WP_Query
32 */
33 public function get_query() {
34 $query = parent::get_query();
35
36 if ( ! $query->post_count && $this->is_valid_fallback() ) {
37 $query = $this->get_fallback_query( $query );
38 }
39
40 return $query;
41 }
42
43 protected function get_fallback_query( $original_query ) {
44 $this->set_fallback_query_args();
45 $this->set_fallback_arg_by_settings( 'posts_per_page', $original_query->query_vars['posts_per_page'] );
46 $this->fallback_args = apply_filters( 'elementor/query/fallback_query_args', $this->fallback_args, $this->widget );
47
48 return new \WP_Query( $this->fallback_args );
49 }
50
51 private function is_valid_fallback() {
52 $related_callback = $this->get_widget_settings( 'related_fallback' );
53 if ( empty( $related_callback ) ) {
54 return false;
55 }
56 $valid = false;
57 switch ( $this->get_widget_settings( 'related_fallback' ) ) {
58 case 'fallback_recent':
59 $valid = true;
60 break;
61 case 'fallback_by_id':
62 $fallback_id = $this->get_widget_settings( 'fallback_ids' );
63 if ( ! empty( $fallback_id ) ) {
64 $valid = true;
65 }
66 break;
67 }
68
69 return $valid;
70 }
71
72 protected function set_common_args() {
73 parent::set_common_args();
74 $post_id = get_queried_object_id();
75 $this->related_post_id = is_singular() && ( 0 !== $post_id ) ? $post_id : null;
76 $this->query_args['post_type'] = get_post_type( $this->related_post_id );
77 }
78
79 protected function set_post_exclude_args() {
80 parent::set_post_exclude_args();
81
82 if ( $this->related_post_id ) {
83 $post_not_in = isset( $this->query_args['post__not_in'] ) ? $this->query_args['post__not_in'] : [];
84 $post_not_in[] = $this->related_post_id;
85 $this->query_args['post__not_in'] = $post_not_in;
86 }
87 }
88
89 protected function build_terms_query_include( $control_id ) {
90 /**
91 * Build tax_query for the "related posts" query:
92 * 1) find the list of taxonomies associated with the current-post
93 * 2) extract the ids for each taxonomy
94 * 3) build tax_query array accordingly
95 *
96 */
97 if ( null === $this->get_widget_settings( 'include' ) || null === $this->get_widget_settings( 'related_taxonomies' ) || ! $this->maybe_in_array( 'terms', $this->get_widget_settings( 'include' ) ) ) {
98 return;
99 }
100
101 $taxonomies = $this->get_widget_settings( 'related_taxonomies' );
102 $terms = [];
103 if ( is_string( $taxonomies ) ) {
104 $terms[ $taxonomies ] = wp_get_post_terms( $this->related_post_id, $taxonomies, [ 'fields' => 'tt_ids' ] );
105 } else {
106 foreach ( $taxonomies as $taxonomy ) {
107 $terms[ $taxonomy ] = wp_get_post_terms( $this->related_post_id, $taxonomy, [ 'fields' => 'tt_ids' ] );
108 }
109 }
110
111 $this->insert_tax_query( $terms, false );
112 }
113
114 protected function set_author_args() {
115 if ( ! $this->maybe_in_array( 'authors', $this->get_widget_settings( 'include' ) ) ) {
116 return;
117 }
118
119 $this->query_args['author__in'] = get_post_field( 'post_author', $this->related_post_id );
120 }
121
122 /**
123 * @param string $key
124 * @param mixed $value
125 * @param string $control_name
126 */
127 private function set_fallback_arg_by_settings( $key, $value, $control_name = '' ) {
128 if ( empty( $this->fallback_args[ $key ] ) ) {
129 $settings = $this->widget->get_settings();
130 $this->fallback_args[ $key ] = ( '' === $control_name || empty( $settings[ $this->prefix . $control_name ] ) ) ? $value : $settings[ $this->prefix . $control_name ];
131 }
132 }
133
134 protected function set_fallback_query_args() {
135 $this->set_fallback_arg_by_settings( 'ignore_sticky_posts', true );
136 $this->set_fallback_arg_by_settings( 'post_status', 'publish' );
137
138 $post_types = Utils::get_public_post_types();
139 $post_types = array_keys( $post_types );
140
141 $this->set_fallback_arg_by_settings( 'post_type', $post_types );
142
143 if ( 'fallback_by_id' === $this->get_widget_settings( 'related_fallback' ) ) {
144 $this->set_fallback_arg_by_settings( 'post__in', [ 0 ], 'fallback_ids' );
145 $this->set_fallback_arg_by_settings( 'orderby', 'rand' );
146 } else { //recent posts
147 $this->set_fallback_arg_by_settings( 'orderby', 'date' );
148 $this->set_fallback_arg_by_settings( 'order', 'DESC' );
149 }
150 }
151 }
152