| 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: | class EPL_CPT { |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | public $post_type_name; |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | public $singular; |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | public $plural; |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | public $slug; |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | public $options; |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | public $taxonomies; |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | public $taxonomy_settings; |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | public $exisiting_taxonomies; |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | public $filters; |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | public $columns; |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | public $custom_populate_columns; |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: | public $sortable; |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | public $textdomain = 'easy-property-listings'; |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | |
| 129: | public function __construct( $post_type_names, $options = array() ) { |
| 130: | |
| 131: | |
| 132: | if ( is_array( $post_type_names ) ) { |
| 133: | |
| 134: | |
| 135: | $names = array( |
| 136: | 'singular', |
| 137: | 'plural', |
| 138: | 'slug', |
| 139: | ); |
| 140: | |
| 141: | |
| 142: | $this->post_type_name = $post_type_names['post_type_name']; |
| 143: | |
| 144: | |
| 145: | foreach ( $names as $name ) { |
| 146: | |
| 147: | |
| 148: | if ( isset( $post_type_names[ $name ] ) ) { |
| 149: | |
| 150: | |
| 151: | $this->$name = $post_type_names[ $name ]; |
| 152: | |
| 153: | |
| 154: | } else { |
| 155: | |
| 156: | |
| 157: | $method = 'get_' . $name; |
| 158: | |
| 159: | |
| 160: | $this->$name = $this->$method(); |
| 161: | } |
| 162: | } |
| 163: | |
| 164: | |
| 165: | } else { |
| 166: | |
| 167: | |
| 168: | $this->post_type_name = $post_type_names; |
| 169: | |
| 170: | |
| 171: | $this->slug = $this->get_slug(); |
| 172: | |
| 173: | |
| 174: | $this->plural = $this->get_plural(); |
| 175: | |
| 176: | |
| 177: | $this->singular = $this->get_singular(); |
| 178: | } |
| 179: | |
| 180: | |
| 181: | $this->options = $options; |
| 182: | |
| 183: | |
| 184: | $this->add_action( 'init', array( &$this, 'register_taxonomies' ) ); |
| 185: | |
| 186: | |
| 187: | $this->add_action( 'init', array( &$this, 'register_post_type' ) ); |
| 188: | |
| 189: | |
| 190: | $this->add_action( 'init', array( &$this, 'register_exisiting_taxonomies' ) ); |
| 191: | |
| 192: | |
| 193: | $this->add_filter( 'manage_edit-' . $this->post_type_name . '_columns', array( &$this, 'add_admin_columns' ) ); |
| 194: | |
| 195: | |
| 196: | $this->add_action( 'manage_' . $this->post_type_name . '_posts_custom_column', array( &$this, 'populate_admin_columns' ), 10, 2 ); |
| 197: | |
| 198: | |
| 199: | $this->add_action( 'restrict_manage_posts', array( &$this, 'add_taxonomy_filters' ) ); |
| 200: | } |
| 201: | |
| 202: | |
| 203: | |
| 204: | |
| 205: | |
| 206: | |
| 207: | |
| 208: | |
| 209: | |
| 210: | public function get( $variable ) { |
| 211: | |
| 212: | |
| 213: | if ( $this->$variable ) { |
| 214: | |
| 215: | |
| 216: | return $this->$variable; |
| 217: | |
| 218: | } else { |
| 219: | |
| 220: | |
| 221: | return false; |
| 222: | } |
| 223: | } |
| 224: | |
| 225: | |
| 226: | |
| 227: | |
| 228: | |
| 229: | |
| 230: | |
| 231: | |
| 232: | |
| 233: | |
| 234: | public function set( $variable, $value ) { |
| 235: | |
| 236: | |
| 237: | $reserved = array( |
| 238: | 'config', |
| 239: | 'post_type_name', |
| 240: | 'singular', |
| 241: | 'plural', |
| 242: | 'slug', |
| 243: | 'options', |
| 244: | 'taxonomies', |
| 245: | ); |
| 246: | |
| 247: | |
| 248: | if ( ! in_array( $variable, $reserved, true ) ) { |
| 249: | |
| 250: | |
| 251: | $this->$variable = $value; |
| 252: | } |
| 253: | } |
| 254: | |
| 255: | |
| 256: | |
| 257: | |
| 258: | |
| 259: | |
| 260: | |
| 261: | |
| 262: | |
| 263: | |
| 264: | |
| 265: | public function add_action( $action, $callback, $priority = 10, $accepted_args = 1 ) { |
| 266: | |
| 267: | |
| 268: | add_action( $action, $callback, $priority, $accepted_args ); |
| 269: | } |
| 270: | |
| 271: | |
| 272: | |
| 273: | |
| 274: | |
| 275: | |
| 276: | |
| 277: | |
| 278: | |
| 279: | |
| 280: | |
| 281: | |
| 282: | |
| 283: | public function add_filter( $action, $callback, $priority = 10, $accepted_args = 1 ) { |
| 284: | |
| 285: | |
| 286: | add_filter( $action, $callback, $priority, $accepted_args ); |
| 287: | } |
| 288: | |
| 289: | |
| 290: | |
| 291: | |
| 292: | |
| 293: | |
| 294: | |
| 295: | |
| 296: | |
| 297: | public function get_slug( $name = null ) { |
| 298: | |
| 299: | |
| 300: | if ( ! isset( $name ) ) { |
| 301: | |
| 302: | $name = $this->post_type_name; |
| 303: | } |
| 304: | |
| 305: | |
| 306: | $name = strtolower( $name ); |
| 307: | |
| 308: | |
| 309: | $name = str_replace( ' ', '-', $name ); |
| 310: | |
| 311: | |
| 312: | $name = str_replace( '_', '-', $name ); |
| 313: | |
| 314: | return $name; |
| 315: | } |
| 316: | |
| 317: | |
| 318: | |
| 319: | |
| 320: | |
| 321: | |
| 322: | |
| 323: | |
| 324: | |
| 325: | |
| 326: | |
| 327: | |
| 328: | |
| 329: | public function get_plural( $name = null ) { |
| 330: | |
| 331: | |
| 332: | if ( ! isset( $name ) ) { |
| 333: | |
| 334: | $name = $this->post_type_name; |
| 335: | } |
| 336: | |
| 337: | |
| 338: | return $this->get_human_friendly( $name ) . 's'; |
| 339: | } |
| 340: | |
| 341: | |
| 342: | |
| 343: | |
| 344: | |
| 345: | |
| 346: | |
| 347: | |
| 348: | |
| 349: | |
| 350: | |
| 351: | |
| 352: | |
| 353: | public function get_singular( $name = null ) { |
| 354: | |
| 355: | |
| 356: | if ( ! isset( $name ) ) { |
| 357: | |
| 358: | $name = $this->post_type_name; |
| 359: | |
| 360: | } |
| 361: | |
| 362: | |
| 363: | return $this->get_human_friendly( $name ); |
| 364: | } |
| 365: | |
| 366: | |
| 367: | |
| 368: | |
| 369: | |
| 370: | |
| 371: | |
| 372: | |
| 373: | |
| 374: | |
| 375: | |
| 376: | |
| 377: | |
| 378: | public function get_human_friendly( $name = null ) { |
| 379: | |
| 380: | |
| 381: | if ( ! isset( $name ) ) { |
| 382: | |
| 383: | $name = $this->post_type_name; |
| 384: | } |
| 385: | |
| 386: | |
| 387: | return ucwords( strtolower( str_replace( '-', ' ', str_replace( '_', ' ', $name ) ) ) ); |
| 388: | } |
| 389: | |
| 390: | |
| 391: | |
| 392: | |
| 393: | |
| 394: | |
| 395: | |
| 396: | |
| 397: | public function register_post_type() { |
| 398: | |
| 399: | |
| 400: | $plural = $this->plural; |
| 401: | $singular = $this->singular; |
| 402: | $slug = $this->slug; |
| 403: | |
| 404: | |
| 405: | $labels = array( |
| 406: | |
| 407: | |
| 408: | |
| 409: | |
| 410: | |
| 411: | |
| 412: | 'name' => $plural, |
| 413: | |
| 414: | 'singular_name' => $singular, |
| 415: | |
| 416: | 'menu_name' => $plural, |
| 417: | |
| 418: | 'all_items' => $plural, |
| 419: | |
| 420: | 'add_new' => __( 'Add New', 'easy-property-listings' ), |
| 421: | |
| 422: | 'add_new_item' => sprintf( __( 'Add New %s', 'easy-property-listings' ), $singular ), |
| 423: | |
| 424: | 'edit_item' => sprintf( __( 'Edit %s', 'easy-property-listings' ), $singular ), |
| 425: | |
| 426: | 'new_item' => sprintf( __( 'New %s', 'easy-property-listings' ), $singular ), |
| 427: | |
| 428: | 'view_item' => sprintf( __( 'View %s', 'easy-property-listings' ), $singular ), |
| 429: | |
| 430: | 'view_items' => sprintf( __( 'View %s', 'easy-property-listings' ), $plural ), |
| 431: | |
| 432: | 'search_items' => sprintf( __( 'Search %s', 'easy-property-listings' ), $plural ), |
| 433: | |
| 434: | 'not_found' => sprintf( __( 'No %s found', 'easy-property-listings' ), $plural ), |
| 435: | |
| 436: | 'not_found_in_trash' => sprintf( __( 'No %s found in Trash', 'easy-property-listings' ), $plural ), |
| 437: | |
| 438: | 'parent_item_colon' => sprintf( __( 'Parent %s:', 'easy-property-listings' ), $singular ), |
| 439: | ); |
| 440: | |
| 441: | |
| 442: | $defaults = array( |
| 443: | 'labels' => $labels, |
| 444: | 'public' => true, |
| 445: | 'publicly_queryable' => true, |
| 446: | 'show_ui' => true, |
| 447: | 'show_in_menu' => true, |
| 448: | 'query_var' => true, |
| 449: | 'rewrite' => array( 'slug' => $slug ), |
| 450: | 'capability_type' => 'post', |
| 451: | 'has_archive' => true, |
| 452: | 'hierarchical' => false, |
| 453: | 'menu_position' => '26.87', |
| 454: | 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'revisions' ), |
| 455: | ); |
| 456: | |
| 457: | |
| 458: | $options = epl_array_replace_recursive( $defaults, $this->options ); |
| 459: | |
| 460: | |
| 461: | $this->options = $options; |
| 462: | |
| 463: | |
| 464: | if ( ! post_type_exists( $this->post_type_name ) ) { |
| 465: | |
| 466: | |
| 467: | register_post_type( $this->post_type_name, $options ); |
| 468: | } |
| 469: | } |
| 470: | |
| 471: | |
| 472: | |
| 473: | |
| 474: | |
| 475: | |
| 476: | |
| 477: | |
| 478: | |
| 479: | public function register_taxonomy( $taxonomy_names, $options = array() ) { |
| 480: | |
| 481: | |
| 482: | $post_type = $this->post_type_name; |
| 483: | |
| 484: | |
| 485: | $names = array( |
| 486: | 'singular', |
| 487: | 'plural', |
| 488: | 'slug', |
| 489: | ); |
| 490: | |
| 491: | |
| 492: | if ( is_array( $taxonomy_names ) ) { |
| 493: | |
| 494: | |
| 495: | $taxonomy_name = $taxonomy_names['taxonomy_name']; |
| 496: | |
| 497: | |
| 498: | foreach ( $names as $name ) { |
| 499: | |
| 500: | |
| 501: | if ( isset( $taxonomy_names[ $name ] ) ) { |
| 502: | |
| 503: | |
| 504: | $$name = $taxonomy_names[ $name ]; |
| 505: | |
| 506: | |
| 507: | } else { |
| 508: | |
| 509: | |
| 510: | $method = 'get_' . $name; |
| 511: | |
| 512: | |
| 513: | $$name = $this->$method( $taxonomy_name ); |
| 514: | |
| 515: | } |
| 516: | } |
| 517: | |
| 518: | |
| 519: | } else { |
| 520: | |
| 521: | |
| 522: | $taxonomy_name = $taxonomy_names; |
| 523: | $singular = $this->get_singular( $taxonomy_name ); |
| 524: | $plural = $this->get_plural( $taxonomy_name ); |
| 525: | $slug = $this->get_slug( $taxonomy_name ); |
| 526: | |
| 527: | } |
| 528: | |
| 529: | |
| 530: | $labels = array( |
| 531: | |
| 532: | 'name' => $plural, |
| 533: | |
| 534: | 'singular_name' => $singular, |
| 535: | |
| 536: | 'menu_name' => $plural, |
| 537: | |
| 538: | 'all_items' => sprintf( __( 'All %s', 'easy-property-listings' ), $plural ), |
| 539: | |
| 540: | 'edit_item' => sprintf( __( 'Edit %s', 'easy-property-listings' ), $singular ), |
| 541: | |
| 542: | 'view_item' => sprintf( __( 'View %s', 'easy-property-listings' ), $singular ), |
| 543: | |
| 544: | 'update_item' => sprintf( __( 'Update %s', 'easy-property-listings' ), $singular ), |
| 545: | |
| 546: | 'add_new_item' => sprintf( __( 'Add New %s', 'easy-property-listings' ), $singular ), |
| 547: | |
| 548: | 'new_item_name' => sprintf( __( 'New %s Name', 'easy-property-listings' ), $singular ), |
| 549: | |
| 550: | 'parent_item' => sprintf( __( 'Parent %s', 'easy-property-listings' ), $plural ), |
| 551: | |
| 552: | 'parent_item_colon' => sprintf( __( 'Parent %s:', 'easy-property-listings' ), $plural ), |
| 553: | |
| 554: | 'search_items' => sprintf( __( 'Search %s', 'easy-property-listings' ), $plural ), |
| 555: | |
| 556: | 'popular_items' => sprintf( __( 'Popular %s', 'easy-property-listings' ), $plural ), |
| 557: | |
| 558: | 'separate_items_with_commas' => sprintf( __( 'Separate %s with commas', 'easy-property-listings' ), $plural ), |
| 559: | |
| 560: | 'add_or_remove_items' => sprintf( __( 'Add or remove %s', 'easy-property-listings' ), $plural ), |
| 561: | |
| 562: | 'choose_from_most_used' => sprintf( __( 'Choose from most used %s', 'easy-property-listings' ), $plural ), |
| 563: | |
| 564: | 'not_found' => sprintf( __( 'No %s found', 'easy-property-listings' ), $plural ), |
| 565: | ); |
| 566: | |
| 567: | |
| 568: | $defaults = array( |
| 569: | 'labels' => $labels, |
| 570: | 'hierarchical' => true, |
| 571: | 'rewrite' => array( |
| 572: | 'slug' => $slug, |
| 573: | ), |
| 574: | ); |
| 575: | |
| 576: | |
| 577: | $options = epl_array_replace_recursive( $defaults, $options ); |
| 578: | |
| 579: | |
| 580: | $this->taxonomies[] = $taxonomy_name; |
| 581: | |
| 582: | |
| 583: | $this->taxonomy_settings[ $taxonomy_name ] = $options; |
| 584: | } |
| 585: | |
| 586: | |
| 587: | |
| 588: | |
| 589: | |
| 590: | |
| 591: | public function register_taxonomies() { |
| 592: | |
| 593: | if ( is_array( $this->taxonomy_settings ) ) { |
| 594: | |
| 595: | |
| 596: | foreach ( $this->taxonomy_settings as $taxonomy_name => $options ) { |
| 597: | |
| 598: | |
| 599: | if ( ! taxonomy_exists( $taxonomy_name ) ) { |
| 600: | |
| 601: | |
| 602: | register_taxonomy( $taxonomy_name, $this->post_type_name, $options ); |
| 603: | |
| 604: | } else { |
| 605: | |
| 606: | |
| 607: | $this->exisiting_taxonomies[] = $taxonomy_name; |
| 608: | } |
| 609: | } |
| 610: | } |
| 611: | } |
| 612: | |
| 613: | |
| 614: | |
| 615: | |
| 616: | |
| 617: | |
| 618: | public function register_exisiting_taxonomies() { |
| 619: | |
| 620: | if ( is_array( $this->exisiting_taxonomies ) ) { |
| 621: | foreach ( $this->exisiting_taxonomies as $taxonomy_name ) { |
| 622: | register_taxonomy_for_object_type( $taxonomy_name, $this->post_type_name ); |
| 623: | } |
| 624: | } |
| 625: | } |
| 626: | |
| 627: | |
| 628: | |
| 629: | |
| 630: | |
| 631: | |
| 632: | |
| 633: | |
| 634: | |
| 635: | public function add_admin_columns( $columns ) { |
| 636: | |
| 637: | |
| 638: | if ( ! isset( $this->columns ) ) { |
| 639: | |
| 640: | $new_columns = array(); |
| 641: | |
| 642: | |
| 643: | if ( in_array( 'post_tag', $this->taxonomies, true ) || 'post' === $this->post_type_name ) { |
| 644: | $after = 'tags'; |
| 645: | } elseif ( in_array( 'category', $this->taxonomies, true ) || 'post' === $this->post_type_name ) { |
| 646: | $after = 'categories'; |
| 647: | } elseif ( post_type_supports( $this->post_type_name, 'author' ) ) { |
| 648: | $after = 'author'; |
| 649: | } else { |
| 650: | $after = 'title'; |
| 651: | } |
| 652: | |
| 653: | |
| 654: | foreach ( $columns as $key => $title ) { |
| 655: | |
| 656: | |
| 657: | $new_columns[ $key ] = $title; |
| 658: | |
| 659: | |
| 660: | if ( $key === $after ) { |
| 661: | |
| 662: | |
| 663: | if ( is_array( $this->taxonomies ) ) { |
| 664: | |
| 665: | |
| 666: | foreach ( $this->taxonomies as $tax ) { |
| 667: | |
| 668: | |
| 669: | if ( 'category' !== $tax && 'post_tag' !== $tax ) { |
| 670: | |
| 671: | $taxonomy_object = get_taxonomy( $tax ); |
| 672: | |
| 673: | |
| 674: | |
| 675: | $new_columns[ $tax ] = $taxonomy_object->labels->name; |
| 676: | } |
| 677: | } |
| 678: | } |
| 679: | } |
| 680: | } |
| 681: | |
| 682: | |
| 683: | $columns = $new_columns; |
| 684: | |
| 685: | } else { |
| 686: | |
| 687: | |
| 688: | $columns = $this->columns; |
| 689: | } |
| 690: | |
| 691: | return $columns; |
| 692: | } |
| 693: | |
| 694: | |
| 695: | |
| 696: | |
| 697: | |
| 698: | |
| 699: | |
| 700: | |
| 701: | |
| 702: | |
| 703: | public function populate_admin_columns( $column, $post_id ) { |
| 704: | |
| 705: | |
| 706: | global $post; |
| 707: | |
| 708: | |
| 709: | switch ( $column ) { |
| 710: | |
| 711: | |
| 712: | case ( taxonomy_exists( $column ) ): |
| 713: | |
| 714: | $terms = get_the_terms( $post_id, $column ); |
| 715: | |
| 716: | |
| 717: | if ( ! empty( $terms ) ) { |
| 718: | |
| 719: | $output = array(); |
| 720: | |
| 721: | |
| 722: | foreach ( $terms as $term ) { |
| 723: | |
| 724: | |
| 725: | $output[] = sprintf( |
| 726: | |
| 727: | '<a href="%s">%s</a>', |
| 728: | |
| 729: | esc_url( |
| 730: | add_query_arg( |
| 731: | array( |
| 732: | 'post_type' => $post->post_type, |
| 733: | $column => $term->slug, |
| 734: | ), |
| 735: | 'edit.php' |
| 736: | ) |
| 737: | ), |
| 738: | |
| 739: | esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, $column, 'display' ) ) |
| 740: | ); |
| 741: | |
| 742: | } |
| 743: | |
| 744: | |
| 745: | echo join( ', ', array_map( 'wp_kses_post', $output ) ); |
| 746: | |
| 747: | |
| 748: | } else { |
| 749: | |
| 750: | |
| 751: | $taxonomy_object = get_taxonomy( $column ); |
| 752: | |
| 753: | |
| 754: | |
| 755: | printf( esc_html__( 'No %s', 'easy-property-listings' ), esc_attr( $taxonomy_object->labels->name ) ); |
| 756: | } |
| 757: | |
| 758: | break; |
| 759: | |
| 760: | |
| 761: | case 'post_id': |
| 762: | echo esc_attr( $post->ID ); |
| 763: | |
| 764: | break; |
| 765: | |
| 766: | |
| 767: | case ( preg_match( '/^meta_/', $column ) ? true : false ): |
| 768: | |
| 769: | $x = substr( $column, 5 ); |
| 770: | |
| 771: | $meta = get_post_meta( $post->ID, $x ); |
| 772: | |
| 773: | echo join( ', ', wp_kses_post( $meta ) ); |
| 774: | |
| 775: | break; |
| 776: | |
| 777: | |
| 778: | case 'icon': |
| 779: | |
| 780: | $link = esc_url( |
| 781: | add_query_arg( |
| 782: | array( |
| 783: | 'post' => $post->ID, |
| 784: | 'action' => 'edit', |
| 785: | ), |
| 786: | 'post.php' |
| 787: | ) |
| 788: | ); |
| 789: | |
| 790: | |
| 791: | if ( has_post_thumbnail() ) { |
| 792: | |
| 793: | |
| 794: | echo '<a href="' . esc_url( $link ) . '">'; |
| 795: | the_post_thumbnail( array( 60, 60 ) ); |
| 796: | echo '</a>'; |
| 797: | |
| 798: | } else { |
| 799: | |
| 800: | |
| 801: | echo '<a href="' . esc_url( $link ) . '"><img src="' . esc_url( site_url( '/wp-includes/images/crystal/default.png' ) ) . '" alt="' . esc_attr( $post->post_title ) . '" /></a>'; |
| 802: | |
| 803: | } |
| 804: | |
| 805: | break; |
| 806: | |
| 807: | |
| 808: | default: |
| 809: | |
| 810: | if ( isset( $this->custom_populate_columns ) && is_array( $this->custom_populate_columns ) ) { |
| 811: | |
| 812: | |
| 813: | if ( isset( $this->custom_populate_columns[ $column ] ) && is_callable( $this->custom_populate_columns[ $column ] ) ) { |
| 814: | |
| 815: | |
| 816: | $this->custom_populate_columns[ $column ]( $column, $post ); |
| 817: | |
| 818: | } |
| 819: | } |
| 820: | |
| 821: | break; |
| 822: | } |
| 823: | } |
| 824: | |
| 825: | |
| 826: | |
| 827: | |
| 828: | |
| 829: | |
| 830: | |
| 831: | |
| 832: | public function filters( $filters = array() ) { |
| 833: | |
| 834: | $this->filters = $filters; |
| 835: | } |
| 836: | |
| 837: | |
| 838: | |
| 839: | |
| 840: | |
| 841: | |
| 842: | public function add_taxonomy_filters() { |
| 843: | |
| 844: | global $typenow; |
| 845: | global $wp_query; |
| 846: | |
| 847: | |
| 848: | if ( $typenow === $this->post_type_name ) { |
| 849: | |
| 850: | |
| 851: | if ( is_array( $this->filters ) ) { |
| 852: | |
| 853: | $filters = $this->filters; |
| 854: | |
| 855: | |
| 856: | } else { |
| 857: | |
| 858: | $filters = $this->taxonomies; |
| 859: | } |
| 860: | |
| 861: | if ( ! empty( $filters ) ) { |
| 862: | |
| 863: | |
| 864: | foreach ( $filters as $tax_slug ) { |
| 865: | |
| 866: | |
| 867: | $tax = get_taxonomy( $tax_slug ); |
| 868: | |
| 869: | |
| 870: | $args = array( |
| 871: | 'taxonomy' => $tax_slug, |
| 872: | 'orderby' => 'name', |
| 873: | 'hide_empty' => false, |
| 874: | ); |
| 875: | |
| 876: | |
| 877: | $terms = get_terms( $args ); |
| 878: | |
| 879: | |
| 880: | if ( $terms ) { |
| 881: | |
| 882: | |
| 883: | printf( ' <select name="%s" class="postform">', esc_attr( $tax_slug ) ); |
| 884: | |
| 885: | |
| 886: | |
| 887: | printf( '<option value="0">%s</option>', sprintf( esc_html__( 'Show all %s', 'easy-property-listings' ), esc_attr( $tax->label ) ) ); |
| 888: | |
| 889: | |
| 890: | foreach ( $terms as $term ) { |
| 891: | |
| 892: | |
| 893: | if ( isset( $_GET[ $tax_slug ] ) && sanitize_title( wp_unslash( $_GET[ $tax_slug ] ) ) === $term->slug ) { |
| 894: | |
| 895: | printf( '<option value="%s" selected="selected">%s (%s)</option>', esc_attr( $term->slug ), esc_attr( $term->name ), esc_attr( $term->count ) ); |
| 896: | |
| 897: | |
| 898: | } else { |
| 899: | |
| 900: | printf( '<option value="%s">%s (%s)</option>', esc_attr( $term->slug ), esc_attr( $term->name ), esc_attr( $term->count ) ); |
| 901: | } |
| 902: | } |
| 903: | |
| 904: | print( '</select> ' ); |
| 905: | } |
| 906: | } |
| 907: | } |
| 908: | } |
| 909: | } |
| 910: | |
| 911: | |
| 912: | |
| 913: | |
| 914: | |
| 915: | |
| 916: | |
| 917: | |
| 918: | public function columns( $columns ) { |
| 919: | |
| 920: | |
| 921: | if ( isset( $columns ) ) { |
| 922: | |
| 923: | |
| 924: | $this->columns = $columns; |
| 925: | |
| 926: | } |
| 927: | } |
| 928: | |
| 929: | |
| 930: | |
| 931: | |
| 932: | |
| 933: | |
| 934: | |
| 935: | |
| 936: | |
| 937: | public function populate_column( $column_name, $callback ) { |
| 938: | |
| 939: | $this->custom_populate_columns[ $column_name ] = $callback; |
| 940: | } |
| 941: | |
| 942: | |
| 943: | |
| 944: | |
| 945: | |
| 946: | |
| 947: | |
| 948: | |
| 949: | public function sortable( $columns = array() ) { |
| 950: | |
| 951: | |
| 952: | $this->sortable = $columns; |
| 953: | |
| 954: | |
| 955: | $this->add_filter( 'manage_edit-' . $this->post_type_name . '_sortable_columns', array( &$this, 'make_columns_sortable' ) ); |
| 956: | |
| 957: | |
| 958: | $this->add_action( 'load-edit.php', array( &$this, 'load_edit' ) ); |
| 959: | } |
| 960: | |
| 961: | |
| 962: | |
| 963: | |
| 964: | |
| 965: | |
| 966: | |
| 967: | |
| 968: | |
| 969: | |
| 970: | public function make_columns_sortable( $columns ) { |
| 971: | |
| 972: | |
| 973: | foreach ( $this->sortable as $column => $values ) { |
| 974: | |
| 975: | |
| 976: | $sortable_columns[ $column ] = $values[0]; |
| 977: | } |
| 978: | |
| 979: | |
| 980: | $columns = array_merge( $sortable_columns, $columns ); |
| 981: | |
| 982: | return $columns; |
| 983: | } |
| 984: | |
| 985: | |
| 986: | |
| 987: | |
| 988: | |
| 989: | |
| 990: | |
| 991: | |
| 992: | public function load_edit() { |
| 993: | |
| 994: | |
| 995: | $this->add_filter( 'request', array( &$this, 'sort_columns' ) ); |
| 996: | } |
| 997: | |
| 998: | |
| 999: | |
| 1000: | |
| 1001: | |
| 1002: | |
| 1003: | |
| 1004: | |
| 1005: | |
| 1006: | |
| 1007: | |
| 1008: | public function sort_columns( $vars ) { |
| 1009: | |
| 1010: | |
| 1011: | foreach ( $this->sortable as $column => $values ) { |
| 1012: | |
| 1013: | |
| 1014: | $meta_key = $values[0]; |
| 1015: | |
| 1016: | |
| 1017: | if ( taxonomy_exists( $meta_key ) ) { |
| 1018: | |
| 1019: | |
| 1020: | $key = 'taxonomy'; |
| 1021: | |
| 1022: | } else { |
| 1023: | |
| 1024: | |
| 1025: | $key = 'meta_key'; |
| 1026: | } |
| 1027: | |
| 1028: | |
| 1029: | if ( isset( $values[1] ) && true === $values[1] ) { |
| 1030: | |
| 1031: | |
| 1032: | $orderby = 'meta_value_num'; |
| 1033: | |
| 1034: | } else { |
| 1035: | |
| 1036: | |
| 1037: | $orderby = 'meta_value'; |
| 1038: | } |
| 1039: | |
| 1040: | |
| 1041: | if ( isset( $vars['post_type'] ) && $this->post_type_name === $vars['post_type'] ) { |
| 1042: | |
| 1043: | |
| 1044: | if ( isset( $vars['orderby'] ) && $meta_key === $vars['orderby'] ) { |
| 1045: | |
| 1046: | |
| 1047: | $vars = array_merge( |
| 1048: | $vars, |
| 1049: | array( |
| 1050: | 'meta_key' => $meta_key, |
| 1051: | 'orderby' => $orderby, |
| 1052: | ) |
| 1053: | ); |
| 1054: | } |
| 1055: | } |
| 1056: | } |
| 1057: | return $vars; |
| 1058: | } |
| 1059: | |
| 1060: | |
| 1061: | |
| 1062: | |
| 1063: | |
| 1064: | |
| 1065: | |
| 1066: | |
| 1067: | |
| 1068: | public function menu_icon( $icon = 'dashicons-admin-page' ) { |
| 1069: | |
| 1070: | if ( is_string( $icon ) && stripos( $icon, 'dashicons' ) !== false ) { |
| 1071: | |
| 1072: | $this->options['menu_icon'] = $icon; |
| 1073: | |
| 1074: | } else { |
| 1075: | |
| 1076: | |
| 1077: | $this->options['menu_icon'] = 'dashicons-admin-page'; |
| 1078: | } |
| 1079: | } |
| 1080: | |
| 1081: | |
| 1082: | |
| 1083: | |
| 1084: | |
| 1085: | |
| 1086: | public function set_textdomain( $textdomain ) { |
| 1087: | $this->textdomain = 'easy-property-listings'; |
| 1088: | } |
| 1089: | } |
| 1090: | |