1: <?php
2: /**
3: * Custom Post Object
4: *
5: * @package EPL
6: * @subpackage Classes/CustomPostType
7: * @copyright Copyright (c) 2019, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 2.3
10: */
11:
12: // Exit if accessed directly.
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: /**
18: * EPL_CPT Class
19: *
20: * @since 2.3
21: */
22: class EPL_CPT {
23:
24: /**
25: * Post type name.
26: *
27: * @var string $post_type_name Holds the name of the post type.
28: */
29: public $post_type_name;
30:
31: /**
32: * Holds the singular name of the post type. This is a human friendly
33: * name, capitalized with spaces assigned on __construct().
34: *
35: * @var string $singular Post type singular name.
36: */
37: public $singular;
38:
39: /**
40: * Holds the plural name of the post type. This is a human friendly
41: * name, capitalized with spaces assigned on __construct().
42: *
43: * @var string $plural Singular post type name.
44: */
45: public $plural;
46:
47: /**
48: * Post type slug. This is a robot friendly name, all lowercase and uses
49: * hyphens assigned on __construct().
50: *
51: * @var string $slug Holds the post type slug name.
52: */
53: public $slug;
54:
55: /**
56: * User submitted options assigned on __construct().
57: *
58: * @var array $options Holds the user submitted post type options.
59: */
60: public $options;
61:
62: /**
63: * Taxonomies
64: *
65: * @var array $taxonomies Holds an array of taxonomies associated with the post type.
66: */
67: public $taxonomies;
68:
69: /**
70: * Taxonomy settings, an array of the taxonomies associated with the post
71: * type and their options used when registering the taxonomies.
72: *
73: * @var array $taxonomy_settings Holds the taxonomy settings.
74: */
75: public $taxonomy_settings;
76:
77: /**
78: * Exisiting taxonomies to be registered after the post has been registered
79: *
80: * @var array $exisiting_taxonomies holds exisiting taxonomies
81: */
82: public $exisiting_taxonomies;
83:
84: /**
85: * Taxonomy filters. Defines which filters are to appear on admin edit
86: * screen used in add_taxonmy_filters().
87: *
88: * @var array $filters Taxonomy filters.
89: */
90: public $filters;
91:
92: /**
93: * Defines which columns are to appear on the admin edit screen used
94: * in add_admin_columns().
95: *
96: * @var array $columns Columns visible in admin edit screen.
97: */
98: public $columns;
99:
100: /**
101: * User defined functions to populate admin columns.
102: *
103: * @var array $custom_populate_columns User functions to populate columns.
104: */
105: public $custom_populate_columns;
106:
107: /**
108: * Sortable columns.
109: *
110: * @var array $sortable Define which columns are sortable on the admin edit screen.
111: */
112: public $sortable;
113:
114: /**
115: * Textdomain used for translation. Use the set_textdomain() method to set a custom textdomain.
116: *
117: * @var string $textdomain Used for internationalising. Defaults to "cpt" without quotes.
118: */
119: public $textdomain = 'easy-property-listings';
120:
121: /**
122: * Constructor
123: *
124: * Register a custom post type.
125: *
126: * @param mixed $post_type_names The name(s) of the post type, accepts (post type name, slug, plural, singular).
127: * @param array $options User submitted options.
128: */
129: public function __construct( $post_type_names, $options = array() ) {
130:
131: // Check if post type names is a string or an array.
132: if ( is_array( $post_type_names ) ) {
133:
134: // Add names to object.
135: $names = array(
136: 'singular',
137: 'plural',
138: 'slug',
139: );
140:
141: // Set the post type name.
142: $this->post_type_name = $post_type_names['post_type_name'];
143:
144: // Cycle through possible names.
145: foreach ( $names as $name ) {
146:
147: // If the name has been set by user.
148: if ( isset( $post_type_names[ $name ] ) ) {
149:
150: // Use the user setting.
151: $this->$name = $post_type_names[ $name ];
152:
153: // Else generate the name.
154: } else {
155:
156: // Define the method to be used.
157: $method = 'get_' . $name;
158:
159: // Generate the name.
160: $this->$name = $this->$method();
161: }
162: }
163:
164: // Else the post type name is only supplied.
165: } else {
166:
167: // Apply to post type name.
168: $this->post_type_name = $post_type_names;
169:
170: // Set the slug name.
171: $this->slug = $this->get_slug();
172:
173: // Set the plural name label.
174: $this->plural = $this->get_plural();
175:
176: // Set the singular name label.
177: $this->singular = $this->get_singular();
178: }
179:
180: // Set the user submitted options to the object.
181: $this->options = $options;
182:
183: // Register taxonomies.
184: $this->add_action( 'init', array( &$this, 'register_taxonomies' ) );
185:
186: // Register the post type.
187: $this->add_action( 'init', array( &$this, 'register_post_type' ) );
188:
189: // Register exisiting taxonomies.
190: $this->add_action( 'init', array( &$this, 'register_exisiting_taxonomies' ) );
191:
192: // Add taxonomy to admin edit columns.
193: $this->add_filter( 'manage_edit-' . $this->post_type_name . '_columns', array( &$this, 'add_admin_columns' ) );
194:
195: // Populate the taxonomy columns with the posts terms.
196: $this->add_action( 'manage_' . $this->post_type_name . '_posts_custom_column', array( &$this, 'populate_admin_columns' ), 10, 2 );
197:
198: // Add filter select option to admin edit.
199: $this->add_action( 'restrict_manage_posts', array( &$this, 'add_taxonomy_filters' ) );
200: }
201:
202: /**
203: * Get
204: *
205: * Helper function to get an object variable.
206: *
207: * @param string $variable The variable you would like to retrieve.
208: * @return mixed Returns the value on success, boolean false whe it fails.
209: */
210: public function get( $variable ) {
211:
212: // If the variable exists.
213: if ( $this->$variable ) {
214:
215: // On success return the value.
216: return $this->$variable;
217:
218: } else {
219:
220: // On fail return false.
221: return false;
222: }
223: }
224:
225: /**
226: * Set
227: *
228: * Helper function used to set an object variable. Can overwrite existsing
229: * variables or create new ones. Cannot overwrite reserved variables.
230: *
231: * @param mixed $variable The variable you would like to create/overwrite.
232: * @param mixed $value The value you would like to set to the variable.
233: */
234: public function set( $variable, $value ) {
235:
236: // An array of reserved variables that cannot be overwritten.
237: $reserved = array(
238: 'config',
239: 'post_type_name',
240: 'singular',
241: 'plural',
242: 'slug',
243: 'options',
244: 'taxonomies',
245: );
246:
247: // If the variable is not a reserved variable.
248: if ( ! in_array( $variable, $reserved, true ) ) {
249:
250: // Write variable and value.
251: $this->$variable = $value;
252: }
253: }
254:
255: /**
256: * Add Action
257: *
258: * Helper function to add add_action WordPress filters.
259: *
260: * @param string $action Name of the action.
261: * @param string $callback Function to hook that will run on action.
262: * @param int $priority Order in which to execute the function, relation to other functions hooked to this action.
263: * @param integer $accepted_args The number of arguments the function accepts.
264: */
265: public function add_action( $action, $callback, $priority = 10, $accepted_args = 1 ) {
266:
267: // Pass variables into WordPress add_action function.
268: add_action( $action, $callback, $priority, $accepted_args );
269: }
270:
271: /**
272: * Add Filter
273: *
274: * Create add_filter WordPress filter.
275: *
276: * @see http://codex.wordpress.org/Function_Reference/add_filter
277: *
278: * @param string $action Name of the action to hook to, e.g 'init'.
279: * @param string $callback Function to hook that will run on @action.
280: * @param int $priority Order in which to execute the function, relation to other function hooked to this action.
281: * @param int $accepted_args The number of arguements the function accepts.
282: */
283: public function add_filter( $action, $callback, $priority = 10, $accepted_args = 1 ) {
284:
285: // Pass variables into WordPress add_action function.
286: add_filter( $action, $callback, $priority, $accepted_args );
287: }
288:
289: /**
290: * Get slug
291: *
292: * Creates an url friendly slug.
293: *
294: * @param string $name Name to slugify.
295: * @return string $name Returns the slug.
296: */
297: public function get_slug( $name = null ) {
298:
299: // If no name set use the post type name.
300: if ( ! isset( $name ) ) {
301:
302: $name = $this->post_type_name;
303: }
304:
305: // Name to lower case.
306: $name = strtolower( $name );
307:
308: // Replace spaces with hyphen.
309: $name = str_replace( ' ', '-', $name );
310:
311: // Replace underscore with hyphen.
312: $name = str_replace( '_', '-', $name );
313:
314: return $name;
315: }
316:
317: /**
318: * Get plural
319: *
320: * Returns the friendly plural name.
321: *
322: * ucwords capitalize words
323: * strtolower makes string lowercase before capitalizing
324: * str_replace replace all instances of _ to space
325: *
326: * @param string $name The slug name you want to pluralize.
327: * @return string the friendly pluralized name.
328: */
329: public function get_plural( $name = null ) {
330:
331: // If no name is passed the post_type_name is used.
332: if ( ! isset( $name ) ) {
333:
334: $name = $this->post_type_name;
335: }
336:
337: // Return the plural name. Add 's' to the end.
338: return $this->get_human_friendly( $name ) . 's';
339: }
340:
341: /**
342: * Get singular
343: *
344: * Returns the friendly singular name.
345: *
346: * ucwords capitalize words
347: * strtolower makes string lowercase before capitalizing
348: * str_replace replace all instances of _ to space
349: *
350: * @param string $name The slug name you want to unpluralize.
351: * @return string The friendly singular name.
352: */
353: public function get_singular( $name = null ) {
354:
355: // If no name is passed the post_type_name is used.
356: if ( ! isset( $name ) ) {
357:
358: $name = $this->post_type_name;
359:
360: }
361:
362: // Return the string.
363: return $this->get_human_friendly( $name );
364: }
365:
366: /**
367: * Get human friendly
368: *
369: * Returns the human friendly name.
370: *
371: * ucwords capitalize words
372: * strtolower makes string lowercase before capitalizing
373: * str_replace replace all instances of hyphens and underscores to spaces
374: *
375: * @param string $name The name you want to make friendly.
376: * @return string The human friendly name.
377: */
378: public function get_human_friendly( $name = null ) {
379:
380: // If no name is passed the post_type_name is used.
381: if ( ! isset( $name ) ) {
382:
383: $name = $this->post_type_name;
384: }
385:
386: // Return human friendly name.
387: return ucwords( strtolower( str_replace( '-', ' ', str_replace( '_', ' ', $name ) ) ) );
388: }
389:
390: /**
391: * Register Post Type
392: *
393: * @see http://codex.wordpress.org/Function_Reference/register_post_type
394: *
395: * @since 3.5.1 Plural for viewing posts.
396: */
397: public function register_post_type() {
398:
399: // Friendly post type names.
400: $plural = $this->plural;
401: $singular = $this->singular;
402: $slug = $this->slug;
403:
404: // Default labels.
405: $labels = array(
406:
407: /*
408: TODO: %s Strings should have translatable content.
409: */
410:
411: /* Translators: %s is the post type name. */
412: 'name' => $plural,
413: /* Translators: %s is the post type name. */
414: 'singular_name' => $singular,
415: /* Translators: %s is the post type name. */
416: 'menu_name' => $plural,
417: /* Translators: %s is the post type name. */
418: 'all_items' => $plural,
419: /* Translators: %s is the post type name. */
420: 'add_new' => __( 'Add New', 'easy-property-listings' ),
421: /* Translators: %s is the post type name. */
422: 'add_new_item' => sprintf( __( 'Add New %s', 'easy-property-listings' ), $singular ),
423: /* Translators: %s is the post type name. */
424: 'edit_item' => sprintf( __( 'Edit %s', 'easy-property-listings' ), $singular ),
425: /* Translators: %s is the post type name. */
426: 'new_item' => sprintf( __( 'New %s', 'easy-property-listings' ), $singular ),
427: /* Translators: %s is the post type name. */
428: 'view_item' => sprintf( __( 'View %s', 'easy-property-listings' ), $singular ),
429: /* Translators: %s is the post type name. */
430: 'view_items' => sprintf( __( 'View %s', 'easy-property-listings' ), $plural ),
431: /* Translators: %s is the post type name. */
432: 'search_items' => sprintf( __( 'Search %s', 'easy-property-listings' ), $plural ),
433: /* Translators: %s is the post type name. */
434: 'not_found' => sprintf( __( 'No %s found', 'easy-property-listings' ), $plural ),
435: /* Translators: %s is the post type name. */
436: 'not_found_in_trash' => sprintf( __( 'No %s found in Trash', 'easy-property-listings' ), $plural ),
437: /* Translators: %s is the post type name. */
438: 'parent_item_colon' => sprintf( __( 'Parent %s:', 'easy-property-listings' ), $singular ),
439: );
440:
441: // Default options.
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: // Merge user submitted options with defaults.
458: $options = epl_array_replace_recursive( $defaults, $this->options );
459:
460: // Set the object options as full options passed.
461: $this->options = $options;
462:
463: // Check that the post type doesn't already exist.
464: if ( ! post_type_exists( $this->post_type_name ) ) {
465:
466: // Register the post type.
467: register_post_type( $this->post_type_name, $options );
468: }
469: }
470:
471: /**
472: * Register taxonomy
473: *
474: * @see http://codex.wordpress.org/Function_Reference/register_taxonomy
475: *
476: * @param string $taxonomy_names The slug for the taxonomy.
477: * @param array $options Taxonomy options.
478: */
479: public function register_taxonomy( $taxonomy_names, $options = array() ) {
480:
481: // Post type defaults to $this post type if unspecified.
482: $post_type = $this->post_type_name;
483:
484: // An array of the names required excluding taxonomy_name.
485: $names = array(
486: 'singular',
487: 'plural',
488: 'slug',
489: );
490:
491: // If an array of names are passed.
492: if ( is_array( $taxonomy_names ) ) {
493:
494: // Set the taxonomy name.
495: $taxonomy_name = $taxonomy_names['taxonomy_name'];
496:
497: // Cycle through possible names.
498: foreach ( $names as $name ) {
499:
500: // If the user has set the name.
501: if ( isset( $taxonomy_names[ $name ] ) ) {
502:
503: // Use user submitted name.
504: $$name = $taxonomy_names[ $name ];
505:
506: // Else generate the name.
507: } else {
508:
509: // Define the function to be used.
510: $method = 'get_' . $name;
511:
512: // Generate the name.
513: $$name = $this->$method( $taxonomy_name );
514:
515: }
516: }
517:
518: // Else if only the taxonomy_name has been supplied.
519: } else {
520:
521: // Create user friendly names.
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: // Default labels.
530: $labels = array(
531: /* Translators: %s is taxonomy label. */
532: 'name' => $plural,
533: /* Translators: %s is taxonomy label. */
534: 'singular_name' => $singular,
535: /* Translators: %s is taxonomy label. */
536: 'menu_name' => $plural,
537: /* Translators: %s is taxonomy label. */
538: 'all_items' => sprintf( __( 'All %s', 'easy-property-listings' ), $plural ),
539: /* Translators: %s is taxonomy label. */
540: 'edit_item' => sprintf( __( 'Edit %s', 'easy-property-listings' ), $singular ),
541: /* Translators: %s is taxonomy label. */
542: 'view_item' => sprintf( __( 'View %s', 'easy-property-listings' ), $singular ),
543: /* Translators: %s is taxonomy label. */
544: 'update_item' => sprintf( __( 'Update %s', 'easy-property-listings' ), $singular ),
545: /* Translators: %s is taxonomy label. */
546: 'add_new_item' => sprintf( __( 'Add New %s', 'easy-property-listings' ), $singular ),
547: /* Translators: %s is taxonomy label. */
548: 'new_item_name' => sprintf( __( 'New %s Name', 'easy-property-listings' ), $singular ),
549: /* Translators: %s is taxonomy label. */
550: 'parent_item' => sprintf( __( 'Parent %s', 'easy-property-listings' ), $plural ),
551: /* Translators: %s is taxonomy label. */
552: 'parent_item_colon' => sprintf( __( 'Parent %s:', 'easy-property-listings' ), $plural ),
553: /* Translators: %s is taxonomy label. */
554: 'search_items' => sprintf( __( 'Search %s', 'easy-property-listings' ), $plural ),
555: /* Translators: %s is taxonomy label. */
556: 'popular_items' => sprintf( __( 'Popular %s', 'easy-property-listings' ), $plural ),
557: /* Translators: %s is taxonomy label. */
558: 'separate_items_with_commas' => sprintf( __( 'Separate %s with commas', 'easy-property-listings' ), $plural ),
559: /* Translators: %s is taxonomy label. */
560: 'add_or_remove_items' => sprintf( __( 'Add or remove %s', 'easy-property-listings' ), $plural ),
561: /* Translators: %s is taxonomy label. */
562: 'choose_from_most_used' => sprintf( __( 'Choose from most used %s', 'easy-property-listings' ), $plural ),
563: /* Translators: %s is taxonomy label. */
564: 'not_found' => sprintf( __( 'No %s found', 'easy-property-listings' ), $plural ),
565: );
566:
567: // Default options.
568: $defaults = array(
569: 'labels' => $labels,
570: 'hierarchical' => true,
571: 'rewrite' => array(
572: 'slug' => $slug,
573: ),
574: );
575:
576: // Merge default options with user submitted options.
577: $options = epl_array_replace_recursive( $defaults, $options );
578:
579: // Add the taxonomy to the object array, this is used to add columns and filters to admin panel.
580: $this->taxonomies[] = $taxonomy_name;
581:
582: // Create array used when registering taxonomies.
583: $this->taxonomy_settings[ $taxonomy_name ] = $options;
584: }
585:
586: /**
587: * Register taxonomies
588: *
589: * Cycles through taxonomies added with the class and registers them.
590: */
591: public function register_taxonomies() {
592:
593: if ( is_array( $this->taxonomy_settings ) ) {
594:
595: // Foreach taxonomy registered with the post type.
596: foreach ( $this->taxonomy_settings as $taxonomy_name => $options ) {
597:
598: // Register the taxonomy if it doesn't exist.
599: if ( ! taxonomy_exists( $taxonomy_name ) ) {
600:
601: // Register the taxonomy with WordPress.
602: register_taxonomy( $taxonomy_name, $this->post_type_name, $options );
603:
604: } else {
605:
606: // If taxonomy exists, register it later with register_exisiting_taxonomies.
607: $this->exisiting_taxonomies[] = $taxonomy_name;
608: }
609: }
610: }
611: }
612:
613: /**
614: * Register Exisiting Taxonomies
615: *
616: * Cycles through exisiting taxonomies and registers them after the post type has been registered
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: * Add admin columns
629: *
630: * Adds columns to the admin edit screen. Function is used with add_action
631: *
632: * @param array $columns Columns to be added to the admin edit screen.
633: * @return array
634: */
635: public function add_admin_columns( $columns ) {
636:
637: // If no user columns have been specified, add taxonomies.
638: if ( ! isset( $this->columns ) ) {
639:
640: $new_columns = array();
641:
642: // determine which column to add custom taxonomies after.
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: // Foreach exisiting columns.
654: foreach ( $columns as $key => $title ) {
655:
656: // Add exisiting column to the new column array.
657: $new_columns[ $key ] = $title;
658:
659: // we want to add taxonomy columns after a specific column.
660: if ( $key === $after ) {
661:
662: // If there are taxonomies registered to the post type.
663: if ( is_array( $this->taxonomies ) ) {
664:
665: // Create a column for each taxonomy.
666: foreach ( $this->taxonomies as $tax ) {
667:
668: // WordPress adds Categories and Tags automatically, ignore these.
669: if ( 'category' !== $tax && 'post_tag' !== $tax ) {
670: // Get the taxonomy object for labels.
671: $taxonomy_object = get_taxonomy( $tax );
672:
673: // Column key is the slug, value is friendly name.
674: /* Translators: %s is taxonomy label. */
675: $new_columns[ $tax ] = $taxonomy_object->labels->name;
676: }
677: }
678: }
679: }
680: }
681:
682: // Overide with new columns.
683: $columns = $new_columns;
684:
685: } else {
686:
687: // Use user submitted columns, these are defined using the object columns() method.
688: $columns = $this->columns;
689: }
690:
691: return $columns;
692: }
693:
694: /**
695: * Populate admin columns
696: *
697: * Populate custom columns on the admin edit screen.
698: *
699: * @param string $column The name of the column.
700: * @param integer $post_id The post ID.
701: * @since 3.4.33 Fixed the output escaping.
702: */
703: public function populate_admin_columns( $column, $post_id ) {
704:
705: // Get WordPress $post object.
706: global $post;
707:
708: // Determine the column.
709: switch ( $column ) {
710:
711: // If column is a taxonomy associated with the post type.
712: case ( taxonomy_exists( $column ) ):
713: // Get the taxonomy for the post.
714: $terms = get_the_terms( $post_id, $column );
715:
716: // If we have terms.
717: if ( ! empty( $terms ) ) {
718:
719: $output = array();
720:
721: // Loop through each term, linking to the 'edit posts' page for the specific term.
722: foreach ( $terms as $term ) {
723:
724: // Output is an array of terms associated with the post.
725: $output[] = sprintf(
726: // Define link.
727: '<a href="%s">%s</a>',
728: // Create filter url.
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: // Create friendly term name.
739: esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, $column, 'display' ) )
740: );
741:
742: }
743:
744: // Join the terms, separating them with a comma.
745: echo join( ', ', array_map( 'wp_kses_post', $output ) );
746:
747: // If no terms found.
748: } else {
749:
750: // Get the taxonomy object for labels.
751: $taxonomy_object = get_taxonomy( $column );
752:
753: // Echo no terms.
754: // translators: tax label.
755: printf( esc_html__( 'No %s', 'easy-property-listings' ), esc_attr( $taxonomy_object->labels->name ) );
756: }
757:
758: break;
759:
760: // If column is for the post ID.
761: case 'post_id':
762: echo esc_attr( $post->ID );
763:
764: break;
765:
766: // If the column is prepended with 'meta_', this will automagically retrieve the meta values and display them.
767: case ( preg_match( '/^meta_/', $column ) ? true : false ):
768: // Meta_book_author (meta key = book_author).
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: // If the column is post thumbnail.
778: case 'icon':
779: // Create the edit link.
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: // If it post has a featured image.
791: if ( has_post_thumbnail() ) {
792:
793: // Display post featured image with edit link.
794: echo '<a href="' . esc_url( $link ) . '">';
795: the_post_thumbnail( array( 60, 60 ) );
796: echo '</a>';
797:
798: } else {
799:
800: // Display default media image with link.
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: // Default case checks if the column has a user function, this is most commonly used for custom fields.
808: default:
809: // If there are user custom columns to populate.
810: if ( isset( $this->custom_populate_columns ) && is_array( $this->custom_populate_columns ) ) {
811:
812: // If this column has a user submitted function to run.
813: if ( isset( $this->custom_populate_columns[ $column ] ) && is_callable( $this->custom_populate_columns[ $column ] ) ) {
814:
815: // Run the function.
816: $this->custom_populate_columns[ $column ]( $column, $post );
817:
818: }
819: }
820:
821: break;
822: } // end switch
823: }
824:
825: /**
826: * Filters
827: *
828: * User function to define which taxonomy filters to display on the admin page.
829: *
830: * @param array $filters An array of taxonomy filters to display.
831: */
832: public function filters( $filters = array() ) {
833:
834: $this->filters = $filters;
835: }
836:
837: /**
838: * Add taxtonomy filters
839: *
840: * Creates select fields for filtering posts by taxonomies on admin edit screen.
841: */
842: public function add_taxonomy_filters() {
843:
844: global $typenow;
845: global $wp_query;
846:
847: // Must set this to the post type you want the filter(s) displayed on.
848: if ( $typenow === $this->post_type_name ) {
849:
850: // If custom filters are defined use those.
851: if ( is_array( $this->filters ) ) {
852:
853: $filters = $this->filters;
854:
855: // Else default to use all taxonomies associated with the post.
856: } else {
857:
858: $filters = $this->taxonomies;
859: }
860:
861: if ( ! empty( $filters ) ) {
862:
863: // Foreach of the taxonomies we want to create filters for...
864: foreach ( $filters as $tax_slug ) {
865:
866: // ...object for taxonomy, doesn't contain the terms.
867: $tax = get_taxonomy( $tax_slug );
868:
869: // Get taxonomy terms and order by name.
870: $args = array(
871: 'taxonomy' => $tax_slug,
872: 'orderby' => 'name',
873: 'hide_empty' => false,
874: );
875:
876: // Get taxonomy terms.
877: $terms = get_terms( $args );
878:
879: // If we have terms.
880: if ( $terms ) {
881:
882: // Set up select box.
883: printf( ' &nbsp;<select name="%s" class="postform">', esc_attr( $tax_slug ) );
884:
885: // Default show all.
886: // translators: tax label.
887: printf( '<option value="0">%s</option>', sprintf( esc_html__( 'Show all %s', 'easy-property-listings' ), esc_attr( $tax->label ) ) );
888:
889: // Foreach term create an option field...
890: foreach ( $terms as $term ) {
891:
892: // ...if filtered by this term make it selected.
893: if ( isset( $_GET[ $tax_slug ] ) && sanitize_title( wp_unslash( $_GET[ $tax_slug ] ) ) === $term->slug ) { // phpcs:ignore
894: // translators: tax slug, tax name, tax count.
895: printf( '<option value="%s" selected="selected">%s (%s)</option>', esc_attr( $term->slug ), esc_attr( $term->name ), esc_attr( $term->count ) );
896:
897: // ...create option for taxonomy.
898: } else {
899: // translators: tax slug, tax name, tax count.
900: printf( '<option value="%s">%s (%s)</option>', esc_attr( $term->slug ), esc_attr( $term->name ), esc_attr( $term->count ) );
901: }
902: }
903: // End the select field.
904: print( '</select>&nbsp;' );
905: }
906: }
907: }
908: }
909: }
910:
911: /**
912: * Columns
913: *
914: * Choose columns to be displayed on the admin edit screen.
915: *
916: * @param array $columns An array of columns to be displayed.
917: */
918: public function columns( $columns ) {
919:
920: // If columns is set.
921: if ( isset( $columns ) ) {
922:
923: // Assign user submitted columns to object.
924: $this->columns = $columns;
925:
926: }
927: }
928:
929: /**
930: * Populate columns
931: *
932: * Define what and how to populate a speicific admin column.
933: *
934: * @param string $column_name The name of the column to populate.
935: * @param string $callback An anonyous function to run when populating the column.
936: */
937: public function populate_column( $column_name, $callback ) {
938:
939: $this->custom_populate_columns[ $column_name ] = $callback;
940: }
941:
942: /**
943: * Sortable
944: *
945: * Define what columns are sortable in the admin edit screen.
946: *
947: * @param array $columns An array of columns that are sortable.
948: */
949: public function sortable( $columns = array() ) {
950:
951: // Assign user defined sortable columns to object variable.
952: $this->sortable = $columns;
953:
954: // Run filter to make columns sortable.
955: $this->add_filter( 'manage_edit-' . $this->post_type_name . '_sortable_columns', array( &$this, 'make_columns_sortable' ) );
956:
957: // Run action that sorts columns on request.
958: $this->add_action( 'load-edit.php', array( &$this, 'load_edit' ) );
959: }
960:
961: /**
962: * Make columns sortable
963: *
964: * Internal function that adds user defined sortable columns to WordPress default columns.
965: *
966: * @param array $columns Columns to be sortable.
967: *
968: * @return array
969: */
970: public function make_columns_sortable( $columns ) {
971:
972: // For each sortable column.
973: foreach ( $this->sortable as $column => $values ) {
974:
975: // Make an array to merge into WordPress sortable columns.
976: $sortable_columns[ $column ] = $values[0];
977: }
978:
979: // Merge sortable columns array into WordPress sortable columns.
980: $columns = array_merge( $sortable_columns, $columns );
981:
982: return $columns;
983: }
984:
985: /**
986: * Load edit
987: *
988: * Sort columns only on the edit.php page when requested.
989: *
990: * @see http://codex.wordpress.org/Plugin_API/Filter_Reference/request
991: */
992: public function load_edit() {
993:
994: // Run filter to sort columns when requested.
995: $this->add_filter( 'request', array( &$this, 'sort_columns' ) );
996: }
997:
998: /**
999: * Sort columns
1000: *
1001: * Internal function that sorts columns on request.
1002: *
1003: * @see load_edit()
1004: *
1005: * @param array $vars The query vars submitted by user.
1006: * @return array A sorted array.
1007: */
1008: public function sort_columns( $vars ) {
1009:
1010: // Cycle through all sortable columns submitted by the user.
1011: foreach ( $this->sortable as $column => $values ) {
1012:
1013: // Retrieve the meta key from the user submitted array of sortable columns.
1014: $meta_key = $values[0];
1015:
1016: // If the meta_key is a taxonomy.
1017: if ( taxonomy_exists( $meta_key ) ) {
1018:
1019: // Sort by taxonomy.
1020: $key = 'taxonomy';
1021:
1022: } else {
1023:
1024: // else by meta key.
1025: $key = 'meta_key';
1026: }
1027:
1028: // If the optional parameter is set and is set to true.
1029: if ( isset( $values[1] ) && true === $values[1] ) {
1030:
1031: // Vaules needed to be ordered by integer value.
1032: $orderby = 'meta_value_num';
1033:
1034: } else {
1035:
1036: // Values are to be order by string value.
1037: $orderby = 'meta_value';
1038: }
1039:
1040: // Check if we're viewing this post type.
1041: if ( isset( $vars['post_type'] ) && $this->post_type_name === $vars['post_type'] ) {
1042:
1043: // Find the meta key we want to order posts by.
1044: if ( isset( $vars['orderby'] ) && $meta_key === $vars['orderby'] ) {
1045:
1046: // Merge the query vars with our custom variables.
1047: $vars = array_merge(
1048: $vars,
1049: array(
1050: 'meta_key' => $meta_key, // phpcs:ignore
1051: 'orderby' => $orderby,
1052: )
1053: );
1054: }
1055: }
1056: }
1057: return $vars;
1058: }
1059:
1060: /**
1061: * Set menu icon
1062: *
1063: * Use this function to set the menu icon in the admin dashboard. Since WordPress v3.8
1064: * dashicons are used. For more information see @link http://melchoyce.github.io/dashicons/
1065: *
1066: * @param string $icon dashicon name.
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: // Set a default menu icon.
1077: $this->options['menu_icon'] = 'dashicons-admin-page';
1078: }
1079: }
1080:
1081: /**
1082: * Set textdomain
1083: *
1084: * @param string $textdomain Textdomain used for translation.
1085: */
1086: public function set_textdomain( $textdomain ) {
1087: $this->textdomain = 'easy-property-listings';
1088: }
1089: }
1090: