This repository was archived by the owner on Jan 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathclass-agentpress-featured-listings-widget.php
174 lines (137 loc) · 5.17 KB
/
class-agentpress-featured-listings-widget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
* AgentPress Featured Listing Widget.
*
* @package agentpress-listings
*/
/**
* This widget presents loop content, based on your input, specifically for the homepage.
*
* @package AgentPress
* @since 2.0
* @author Nathan Rice
*/
class AgentPress_Featured_Listings_Widget extends WP_Widget {
/**
* Constructor.
*/
public function __construct() {
$widget_ops = array(
'classname' => 'featured-listings',
'description' => __( 'Display grid-style featured listings', 'agentpress-listings' ),
);
$control_ops = array(
'width' => 300,
'height' => 350,
);
parent::__construct( 'featured-listings', __( 'AgentPress - Featured Listings', 'agentpress-listings' ), $widget_ops, $control_ops );
}
/**
* Widget function.
*
* @param array $args Arguments.
* @param array $instance Instance.
*/
public function widget( $args, $instance ) {
// Defaults.
$instance = wp_parse_args(
$instance,
array(
'title' => '',
'posts_per_page' => 10,
)
);
$before_widget = $args['before_widget'];
$after_widget = $args['after_widget'];
$before_title = $args['before_title'];
$after_title = $args['after_title'];
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $before_widget;
if ( ! empty( $instance['title'] ) ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $before_title . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $after_title;
}
$toggle = ''; /** For left/right class. */
$query_args = array(
'post_type' => 'listing',
'posts_per_page' => $instance['posts_per_page'],
'paged' => get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1,
);
$query = new WP_Query( $query_args );
if ( $query->have_posts() ) :
while ( $query->have_posts() ) :
$query->the_post();
// Initialze the $loop variable.
$loop = '';
// Pull all the listing information.
$custom_text = genesis_get_custom_field( '_listing_text' );
$price = genesis_get_custom_field( '_listing_price' );
$address = genesis_get_custom_field( '_listing_address' );
$city = genesis_get_custom_field( '_listing_city' );
$state = genesis_get_custom_field( '_listing_state' );
$zip = genesis_get_custom_field( '_listing_zip' );
$loop .= sprintf( '<a href="%s">%s</a>', get_permalink(), genesis_get_image( array( 'size' => 'properties' ) ) );
if ( $price ) {
$loop .= sprintf( '<span class="listing-price">%s</span>', $price );
}
if ( strlen( $custom_text ) ) {
$loop .= sprintf( '<span class="listing-text">%s</span>', esc_html( $custom_text ) );
}
if ( $address ) {
$loop .= sprintf( '<span class="listing-address">%s</span>', $address );
}
if ( $city || $state || $zip ) {
// Count number of completed fields.
$pass = count( array_filter( array( $city, $state, $zip ) ) );
/**
* If only 1 field filled out, no comma.
* If city filled out, comma after city.
* Otherwise, comma after state.
*/
if ( 1 === $pass ) {
$city_state_zip = $city . $state . $zip;
} elseif ( $city ) {
$city_state_zip = $city . ', ' . $state . ' ' . $zip;
} else {
$city_state_zip = $city . ' ' . $state . ', ' . $zip;
}
$loop .= sprintf( '<span class="listing-city-state-zip">%s</span>', trim( $city_state_zip ) );
}
$loop .= sprintf( '<a href="%s" class="more-link">%s</a>', get_permalink(), __( 'View Listing', 'agentpress-listings' ) );
$toggle = ( 'left' === $toggle ) ? 'right' : 'left';
// Wrap in post class div, and output.
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
printf( '<div class="%s"><div class="widget-wrap"><div class="listing-wrap">%s</div></div></div>', esc_attr( join( ' ', get_post_class( $toggle ) ) ), apply_filters( 'agentpress_featured_listings_widget_loop', $loop ) );
endwhile;
endif;
wp_reset_postdata();
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $after_widget;
}
/**
* Update function.
*
* @param array $new_instance New instance.
* @param array $old_instance Old Instance.
* @return array New instance.
*/
public function update( $new_instance, $old_instance ) {
return $new_instance;
}
/**
* Form function.
*
* @param array $instance Instance.
*/
public function form( $instance ) {
$instance = wp_parse_args(
$instance,
array(
'title' => '',
'posts_per_page' => 10,
)
);
printf( '<p><label for="%s">%s</label><input type="text" id="%s" name="%s" value="%s" style="%s" /></p>', esc_attr( $this->get_field_id( 'title' ) ), esc_html__( 'Title:', 'agentpress-listings' ), esc_attr( $this->get_field_id( 'title' ) ), esc_attr( $this->get_field_name( 'title' ) ), esc_attr( $instance['title'] ), 'width: 95%;' );
printf( '<p>%s <input type="text" name="%s" value="%s" size="3" /></p>', esc_html__( 'How many results should be returned?', 'agentpress-listings' ), esc_attr( $this->get_field_name( 'posts_per_page' ) ), esc_attr( $instance['posts_per_page'] ) );
}
}