1: <?php
2: /**
3: * DASHBOARD WIDGET :: Easy Property Listings Status
4: *
5: * @package EPL
6: * @subpackage Widget/Admin/Dashboard
7: * @copyright Copyright (c) 2022, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 1.3.0
10: */
11:
12: // Exit if accessed directly.
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: /**
18: * Add Dashboard Widgets
19: *
20: * @since 1.3.0
21: */
22: function epl_add_dashboard_widgets() {
23:
24: wp_add_dashboard_widget(
25: 'epl_status_dashboard_widget',
26: __( 'Listings', 'easy-property-listings' ),
27: 'epl_status_dashboard_widget_callback'
28: );
29:
30: wp_add_dashboard_widget(
31: 'epl_dashboard_activity_widget',
32: __( 'Easy Property Listings Activities', 'easy-property-listings' ),
33: 'epl_dashboard_activity_widget'
34: );
35: }
36: add_action( 'wp_dashboard_setup', 'epl_add_dashboard_widgets' );
37:
38: /**
39: * Status Dashboard Widget
40: *
41: * @since 1.3.0
42: * @since 3.4.10 Corrected issue with listings dashboard widget when no post types are active.
43: */
44: function epl_status_dashboard_widget_callback() {
45:
46: $activate_post_types = epl_get_option( 'activate_post_types', array() );
47: $activate_post_types = apply_filters( 'epl_filter_dashboard_widget_posts', $activate_post_types ); ?>
48: <div class="main">
49: <ul class="epl_status_list">
50: <?php
51: if ( ! empty( $activate_post_types ) ) {
52: $counter = 0;
53: foreach ( $activate_post_types as $activate_post_type ) {
54: $clear = ( 0 === $counter % 2 && 0 !== $counter ) ? 'epl-clearfix' : '';
55: $count = wp_count_posts( $activate_post_type );
56: ?>
57: <li class="epl_type_<?php echo esc_attr( $activate_post_type . ' ' . $clear ); ?>">
58: <a href="edit.php?post_type=<?php echo esc_attr( $activate_post_type ); ?>">
59: <strong><?php echo esc_attr( epl_get_plural( $count->publish, $activate_post_type ) ); ?></strong>
60: <?php epl_posts_highlights( $activate_post_type ); ?>
61: </a>
62:
63: </li>
64: <?php
65: ++$counter;
66: }
67: }
68: ?>
69: </ul>
70: </div>
71: <?php
72: }
73:
74: /**
75: * Status Dashboard Widget Counter
76: *
77: * @param int $count Number.
78: * @param string $singular Option for the case.
79: *
80: * @return bool|string
81: * @since 1.3.0
82: */
83: function epl_get_plural( $count, $singular ) {
84: $return = false;
85:
86: switch ( $singular ) {
87: case 'property':
88: // translators: count.
89: $return = sprintf( _n( '%s Property', '%s Property', $count, 'easy-property-listings' ), $count );
90: break;
91: case 'land':
92: // translators: count.
93: $return = sprintf( _n( '%s Land', '%s Land', $count, 'easy-property-listings' ), $count );
94: break;
95: case 'rental':
96: // translators: count.
97: $return = sprintf( _n( '%s Rental', '%s Rental', $count, 'easy-property-listings' ), $count );
98: break;
99: case 'rural':
100: // translators: count.
101: $return = sprintf( _n( '%s Rural', '%s Rural', $count, 'easy-property-listings' ), $count );
102: break;
103: case 'commercial':
104: // translators: count.
105: $return = sprintf( _n( '%s Commercial', '%s Commercial', $count, 'easy-property-listings' ), $count );
106: break;
107: case 'commercial_land':
108: // translators: count.
109: $return = sprintf( _n( '%s Commercial Land', '%s Commercial Land', $count, 'easy-property-listings' ), $count );
110: break;
111: case 'business':
112: // translators: count.
113: $return = sprintf( _n( '%s Business', '%s Business', $count, 'easy-property-listings' ), $count );
114: break;
115: default:
116: $singular = ucwords( str_replace( 'epl', '', str_replace( '-', ' ', str_replace( '_', ' ', $singular ) ) ) );
117: // translators: count.
118: $return = sprintf( _n( '%1$s %2$s', '%1$s %2$s', $count, 'easy-property-listings' ), $count, $singular );
119: break;
120: }
121:
122: return $return;
123: }
124:
125: /**
126: * Status Dashboard Widget Highlights
127: *
128: * @param string $type Option for the case.
129: *
130: * @since 1.3.0
131: * @since 3.4.45 Added deleted status.
132: * @since 3.5.0 Using global function to get status labels.
133: */
134: function epl_posts_highlights( $type ) {
135: switch ( $type ) {
136:
137: case 'rental':
138: $filters = array(
139: array(
140: 'key' => 'property_status',
141: 'value' => 'current',
142: 'string' => epl_get_the_status_label( 'current' ),
143: ),
144: array(
145: 'key' => 'property_status',
146: 'value' => 'leased',
147: 'string' => apply_filters( 'epl_leased_label_status_filter', epl_get_the_status_label( 'leased' ) ),
148: ),
149: array(
150: 'key' => 'property_status',
151: 'value' => 'withdrawn',
152: 'string' => epl_get_the_status_label( 'withdrawn' ),
153: ),
154: array(
155: 'key' => 'property_status',
156: 'value' => 'offmarket',
157: 'string' => epl_get_the_status_label( 'offmarket' ),
158: ),
159: array(
160: 'key' => 'property_status',
161: 'value' => 'deleted',
162: 'string' => epl_get_the_status_label( 'deleted' ),
163: ),
164: );
165:
166: foreach ( $filters as $filter_key => $filter_value ) {
167: $count = epl_get_post_count( $type, $filter_value['key'], $filter_value['value'] );
168: if ( 0 !== (int) $count ) {
169: echo '<span>' . esc_attr( $count ) . ' ' . esc_attr( $filter_value['string'] ) . ' </span>';
170: }
171: }
172: break;
173:
174: case 'commercial':
175: $filters = array(
176: array(
177: 'key' => 'property_status',
178: 'value' => 'current',
179: 'string' => epl_get_the_status_label( 'current' ),
180: ),
181: array(
182: 'key' => 'property_authority',
183: 'value' => 'auction',
184: 'string' => __( 'Auction', 'easy-property-listings' ),
185: ), // ONLY if == current.
186: array(
187: 'key' => 'property_under_offer',
188: 'value' => 'yes',
189: 'string' => apply_filters( 'epl_under_offer_label_status_filter', __( 'Under Offer', 'easy-property-listings' ) ),
190: ),
191: array(
192: 'key' => 'property_status',
193: 'value' => 'sold',
194: 'string' => apply_filters( 'epl_sold_label_status_filter', epl_get_the_status_label( 'sold' ) ),
195: ),
196: array(
197: 'key' => 'property_status',
198: 'value' => 'leased',
199: 'string' => apply_filters( 'epl_leased_label_status_filter', epl_get_the_status_label( 'leased' ) ),
200: ),
201: array(
202: 'key' => 'property_status',
203: 'value' => 'withdrawn',
204: 'string' => epl_get_the_status_label( 'withdrawn' ),
205: ),
206: array(
207: 'key' => 'property_status',
208: 'value' => 'offmarket',
209: 'string' => epl_get_the_status_label( 'offmarket' ),
210: ),
211: array(
212: 'key' => 'property_status',
213: 'value' => 'deleted',
214: 'string' => epl_get_the_status_label( 'deleted' ),
215: ),
216: );
217: foreach ( $filters as $filter_key => $filter_value ) {
218: $count = epl_get_post_count( $type, $filter_value['key'], $filter_value['value'] );
219: if ( 0 !== (int) $count ) {
220: echo '<span>' . esc_attr( $count ) . ' ' . esc_attr( $filter_value['string'] ) . ' </span>';
221: }
222: }
223: break;
224:
225: case 'property':
226: case 'land':
227: case 'rural':
228: default:
229: $filters = array(
230: array(
231: 'key' => 'property_status',
232: 'value' => 'current',
233: 'string' => epl_get_the_status_label( 'current' ),
234: ),
235: array(
236: 'key' => 'property_authority',
237: 'value' => 'auction',
238: 'string' => __( 'Auction', 'easy-property-listings' ),
239: ), // ONLY if == current.
240: array(
241: 'key' => 'property_under_offer',
242: 'value' => 'yes',
243: 'string' => apply_filters( 'epl_under_offer_label_status_filter', __( 'Under Offer', 'easy-property-listings' ) ),
244: ),
245: array(
246: 'key' => 'property_status',
247: 'value' => 'sold',
248: 'string' => apply_filters( 'epl_sold_label_status_filter', epl_get_the_status_label( 'sold' ) ),
249: ),
250: array(
251: 'key' => 'property_status',
252: 'value' => 'withdrawn',
253: 'string' => epl_get_the_status_label( 'withdrawn' ),
254: ),
255: array(
256: 'key' => 'property_status',
257: 'value' => 'offmarket',
258: 'string' => epl_get_the_status_label( 'offmarket' ),
259: ),
260: array(
261: 'key' => 'property_status',
262: 'value' => 'deleted',
263: 'string' => epl_get_the_status_label( 'deleted' ),
264: ),
265: );
266: foreach ( $filters as $filter_key => $filter_value ) {
267: $count = epl_get_post_count( $type, $filter_value['key'], $filter_value['value'] );
268: if ( 0 !== (int) $count ) {
269: echo '<span>' . esc_attr( $count ) . ' ' . esc_attr( $filter_value['string'] ) . ' </span>';
270: }
271: }
272: break;
273: }
274: }
275:
276: /**
277: * Activity Dashboard Widget
278: *
279: * Renders the activity widget in the dashboard and controls the content of thje widgets items.
280: *
281: * @since 3.0.0
282: * @since 3.4.40 Fix: Order corrected to newest first in dashboard activity widget.
283: */
284: function epl_dashboard_activity_widget() {
285:
286: echo '<div id="activity-widget" class="epl-activity-widget">';
287:
288: $future_posts = epl_dashboard_recent_posts(
289: array(
290: 'post_type' => epl_get_core_post_types(),
291: 'max' => 5,
292: 'status' => 'future',
293: 'order' => 'ASC',
294: 'title' => __( 'Listings Publishing Soon', 'easy-property-listings' ),
295: 'id' => 'epl-future-posts',
296: )
297: );
298:
299: $future_posts = epl_dashboard_recent_posts(
300: array(
301: 'post_type' => epl_get_core_post_types(),
302: 'max' => 5,
303: 'status' => 'publish',
304: 'order' => 'DESC',
305: 'title' => __( 'Recently Published Listings', 'easy-property-listings' ),
306: 'id' => 'epl-recent-posts',
307: )
308: );
309:
310: $recent_comments = epl_dashboard_recent_comments();
311:
312: if ( ! $recent_comments ) {
313: echo '<div class="no-activity">';
314: echo '<p class="smiley"></p>';
315: echo '<p>' . esc_html__( 'No activity yet!', 'easy-property-listings' ) . '</p>';
316: echo '</div>';
317: }
318:
319: echo '</div>';
320: }
321:
322: /**
323: * Show Comments section.
324: *
325: * @since 3.0.0
326: *
327: * @param int $total_items Optional. Number of comments to query. Default 5.
328: * @return bool False if no comments were found. True otherwise.
329: */
330: function epl_dashboard_recent_comments( $total_items = 5 ) {
331: // Select all comment types and filter out spam later for better query performance.
332: $comments = array();
333:
334: $comments_query = array(
335: 'type__in' => array( 'note', 'call', 'email', 'listing_alert' ),
336: 'number' => $total_items * 5,
337: 'offset' => 0,
338: 'order' => 'comment_date',
339: );
340: if ( ! current_user_can( 'edit_posts' ) ) {
341: $comments_query['status'] = 'approve';
342: }
343:
344: $possible = get_comments( $comments_query );
345: $comments_count = count( $comments );
346: while ( $comments_count < $total_items && $possible ) {
347: if ( ! is_array( $possible ) ) {
348: break;
349: }
350: foreach ( $possible as $comment ) {
351: if ( ! current_user_can( 'read_post', $comment->comment_post_ID ) ) {
352: continue;
353: }
354: $comments[] = $comment;
355: $com_count = count( $comments );
356: if ( $com_count === $total_items ) {
357: break 2;
358: }
359: }
360: $comments_query['offset'] += $comments_query['number'];
361: $comments_query['number'] = $total_items * 10;
362: $comments_count = count( $comments );
363: }
364:
365: if ( $comments ) {
366: $activity_types = array();
367: echo '<div id="latest-comments" class="epl-dashboard-activity-feed activity-block epl-activity-block epl-feed-block">';
368: echo '<h3>' . esc_html__( 'Activity', 'easy-property-listings' ) . '</h3>';
369:
370: echo '<ul id="the-comment-list" data-wp-lists="list:comment">';
371: foreach ( $comments as $comment ) {
372: $comment->comment_type = array_key_exists( $comment->comment_type, $activity_types ) ?
373: $activity_types[ $comment->comment_type ] : $comment->comment_type;
374: _wp_dashboard_recent_comments_row( $comment );
375: }
376: echo '</ul>';
377:
378: wp_comment_reply( -1, false, 'dashboard', false );
379: wp_comment_trashnotice();
380:
381: echo '</div>';
382: } else {
383: return false;
384: }
385: return true;
386: }
387:
388: /**
389: * Generates Publishing Soon and Recently Published listings.
390: *
391: * @since 3.0.0
392: *
393: * @param array $args {
394: * An array of query and display arguments.
395: *
396: * @type int $max Number of posts to display.
397: * @type string $status Post status.
398: * @type string $order Designates ascending ('ASC') or descending ('DESC') order.
399: * @type string $title Section title.
400: * @type string $id The container id.
401: * }
402: * @return bool False if no posts were found. True otherwise.
403: */
404: function epl_dashboard_recent_posts( $args ) {
405: $query_args = array(
406: 'post_type' => $args['post_type'],
407: 'post_status' => $args['status'],
408: 'orderby' => 'date',
409: 'order' => $args['order'],
410: 'posts_per_page' => intval( $args['max'] ),
411: 'no_found_rows' => true,
412: 'cache_results' => false,
413: 'perm' => ( 'future' === $args['status'] ) ? 'editable' : 'readable',
414: );
415:
416: $query_args = apply_filters( 'epl_dashboard_recent_posts_query_args', $query_args );
417: $posts = new WP_Query( $query_args );
418:
419: if ( $posts->have_posts() ) {
420:
421: echo '<div id="' . esc_attr( $args['id'] ) . '" class="epl-activity-block activity-block">';
422:
423: echo '<h3>' . esc_attr( $args['title'] ) . '</h3>';
424:
425: echo '<ul>';
426:
427: $today = current_datetime()->format( 'Y-m-d' );
428: $tomorrow = current_datetime()->modify( '+1 day' )->format( 'Y-m-d' );
429:
430: while ( $posts->have_posts() ) {
431: $posts->the_post();
432:
433: $time = get_the_time( 'U' );
434: if ( gmdate( 'Y-m-d', $time ) === $today ) {
435: $relative = esc_html__( 'Today', 'easy-property-listings' );
436: } elseif ( gmdate( 'Y-m-d', $time ) === $tomorrow ) {
437: $relative = esc_html__( 'Tomorrow', 'easy-property-listings' );
438: } elseif ( gmdate( 'Y', $time ) !== current_datetime()->format( 'Y' ) ) {
439: /* translators: date and time format for recent posts on the dashboard, from a different calendar year, see http://php.net/date */
440: $relative = date_i18n( esc_html__( 'M jS Y', 'easy-property-listings' ), $time );
441: } else {
442: /* translators: date and time format for recent posts on the dashboard, see http://php.net/date */
443: $relative = date_i18n( esc_html__( 'M jS', 'easy-property-listings' ), $time );
444: }
445:
446: // Use the post edit link for those who can edit, the permalink otherwise.
447: $recent_post_link = current_user_can( 'edit_post', get_the_ID() ) ? get_edit_post_link() : get_permalink();
448:
449: /* translators: 1: relative date, 2: time, 3: post edit link or permalink, 4: post title */
450: $format = wp_kses_post( __( '<span>%1$s, %2$s</span> <a href="%3$s">%4$s</a>', 'easy-property-listings' ) );
451: printf( "<li>$format</li>", $relative, get_the_time(), $recent_post_link, _draft_or_post_title() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
452: }
453:
454: echo '</ul>';
455: echo '</div>';
456:
457: } else {
458: return false;
459: }
460:
461: wp_reset_postdata();
462:
463: return true;
464: }
465: