| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | if ( ! defined( 'ABSPATH' ) ) { |
| 14: | exit; |
| 15: | } |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 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: | |
| 40: | |
| 41: | |
| 42: | |
| 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: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | function epl_get_plural( $count, $singular ) { |
| 84: | $return = false; |
| 85: | |
| 86: | switch ( $singular ) { |
| 87: | case 'property': |
| 88: | |
| 89: | $return = sprintf( _n( '%s Property', '%s Property', $count, 'easy-property-listings' ), $count ); |
| 90: | break; |
| 91: | case 'land': |
| 92: | |
| 93: | $return = sprintf( _n( '%s Land', '%s Land', $count, 'easy-property-listings' ), $count ); |
| 94: | break; |
| 95: | case 'rental': |
| 96: | |
| 97: | $return = sprintf( _n( '%s Rental', '%s Rental', $count, 'easy-property-listings' ), $count ); |
| 98: | break; |
| 99: | case 'rural': |
| 100: | |
| 101: | $return = sprintf( _n( '%s Rural', '%s Rural', $count, 'easy-property-listings' ), $count ); |
| 102: | break; |
| 103: | case 'commercial': |
| 104: | |
| 105: | $return = sprintf( _n( '%s Commercial', '%s Commercial', $count, 'easy-property-listings' ), $count ); |
| 106: | break; |
| 107: | case 'commercial_land': |
| 108: | |
| 109: | $return = sprintf( _n( '%s Commercial Land', '%s Commercial Land', $count, 'easy-property-listings' ), $count ); |
| 110: | break; |
| 111: | case 'business': |
| 112: | |
| 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: | |
| 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: | |
| 127: | |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 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: | ), |
| 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: | ), |
| 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: | |
| 278: | |
| 279: | |
| 280: | |
| 281: | |
| 282: | |
| 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: | |
| 324: | |
| 325: | |
| 326: | |
| 327: | |
| 328: | |
| 329: | |
| 330: | function epl_dashboard_recent_comments( $total_items = 5 ) { |
| 331: | |
| 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: | |
| 390: | |
| 391: | |
| 392: | |
| 393: | |
| 394: | |
| 395: | |
| 396: | |
| 397: | |
| 398: | |
| 399: | |
| 400: | |
| 401: | |
| 402: | |
| 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: | |
| 440: | $relative = date_i18n( esc_html__( 'M jS Y', 'easy-property-listings' ), $time ); |
| 441: | } else { |
| 442: | |
| 443: | $relative = date_i18n( esc_html__( 'M jS', 'easy-property-listings' ), $time ); |
| 444: | } |
| 445: | |
| 446: | |
| 447: | $recent_post_link = current_user_can( 'edit_post', get_the_ID() ) ? get_edit_post_link() : get_permalink(); |
| 448: | |
| 449: | |
| 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() ); |
| 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: | |