| 1 |
<?php |
| 2 |
/* |
| 3 |
Custom Post Type Class |
| 4 |
used to help create custom post types for Wordpress |
| 5 |
http://github.com/jjgrainger/wp-custom-post-type-class/ |
| 6 |
|
| 7 |
@author jjgrainger |
| 8 |
@url http://jjgrainger.co.uk |
| 9 |
@version 1.2.3 |
| 10 |
@license http://www.opensource.org/licenses/mit-license.html MIT License |
| 11 |
*/ |
| 12 |
|
| 13 |
class CPT { |
| 14 |
|
| 15 |
/* |
| 16 |
@var string $post_type_name |
| 17 |
Public variable that holds the name of the post type |
| 18 |
assigned on __contstruct() |
| 19 |
*/ |
| 20 |
public $post_type_name; |
| 21 |
|
| 22 |
/* |
| 23 |
@var string $singular |
| 24 |
Public variable that holds the singular name of the post type |
| 25 |
This is a human friendly name, capitalized with spaces |
| 26 |
assigned on __contstruct() |
| 27 |
*/ |
| 28 |
public $singular; |
| 29 |
|
| 30 |
/* |
| 31 |
@var string $singular |
| 32 |
Public variable that holds the plural name of the post type |
| 33 |
This is a human friendly name, capitalized with spaces |
| 34 |
assigned on __contstruct() |
| 35 |
*/ |
| 36 |
public $plural; |
| 37 |
|
| 38 |
/* |
| 39 |
@var string $slug |
| 40 |
Public variable that holds the slug name of the post type |
| 41 |
This is a robot friendly name, all lowercase and uses hyphens |
| 42 |
assigned on __contstruct() |
| 43 |
*/ |
| 44 |
public $slug; |
| 45 |
|
| 46 |
/* |
| 47 |
@var array $options |
| 48 |
public variable that holds the user submited options of the post type |
| 49 |
assigined on __construct() |
| 50 |
*/ |
| 51 |
public $options; |
| 52 |
|
| 53 |
/* |
| 54 |
@var array $taxonomies |
| 55 |
public variable that holds an array of the taxonomies associated with the post type |
| 56 |
assigined on register_taxonomy() |
| 57 |
*/ |
| 58 |
public $taxonomies; |
| 59 |
|
| 60 |
/* |
| 61 |
@var array $taxonomy_settings |
| 62 |
public variable that holds an array of the taxonomies associated with the post type and their options |
| 63 |
used when registering the taxonomies |
| 64 |
assigined on register_taxonomy() |
| 65 |
*/ |
| 66 |
public $taxonomy_settings; |
| 67 |
|
| 68 |
/* |
| 69 |
@var array $filters |
| 70 |
use to define which filters are to appear on admin edit screen |
| 71 |
used in add_taxonmy_filters() |
| 72 |
*/ |
| 73 |
public $filters; |
| 74 |
|
| 75 |
/* |
| 76 |
@var array $columns |
| 77 |
use to define which columns are to appear on admin edit screen |
| 78 |
used in add_admin_columns() |
| 79 |
*/ |
| 80 |
public $columns; |
| 81 |
|
| 82 |
/* |
| 83 |
@var array $custom_populate_columns |
| 84 |
an array of user defined functions to populate admin columns |
| 85 |
*/ |
| 86 |
public $custom_populate_columns; |
| 87 |
|
| 88 |
/* |
| 89 |
@var array $sortable |
| 90 |
use to define which columns are to sortable on admin edit screen |
| 91 |
*/ |
| 92 |
public $sortable; |
| 93 |
|
| 94 |
/* |
| 95 |
@function __contructor(@post_type_name, @options) |
| 96 |
|
| 97 |
@param mixed $post_type_names The name(s) of the post type, accepts (post type name, slug, plural, singluar) |
| 98 |
@param array $options User submitted options |
| 99 |
*/ |
| 100 |
function __construct($post_type_names, $options = array()) { |
| 101 |
|
| 102 |
// check if post type names is string or array |
| 103 |
if(is_array($post_type_names)) { |
| 104 |
|
| 105 |
// add names to object |
| 106 |
$names = array( |
| 107 |
'singular', |
| 108 |
'plural', |
| 109 |
'slug' |
| 110 |
); |
| 111 |
|
| 112 |
// set the post type name |
| 113 |
$this->post_type_name = $post_type_names['post_type_name']; |
| 114 |
|
| 115 |
// cycle through possible names |
| 116 |
foreach($names as $name) { |
| 117 |
|
| 118 |
// if the name has been set by user |
| 119 |
if(isset($post_type_names[$name])) { |
| 120 |
|
| 121 |
// use the user setting |
| 122 |
$this->$name = $post_type_names[$name]; |
| 123 |
|
| 124 |
// else generate the name |
| 125 |
} else { |
| 126 |
|
| 127 |
// define the method to be used |
| 128 |
|
| 129 |
// generate the name |
| 130 |
$this->$name = $this->{'get_'.$name}(); |
| 131 |
|
| 132 |
} |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
// else the post type name is only supplied |
| 137 |
} else { |
| 138 |
|
| 139 |
// apply to post type name |
| 140 |
$this->post_type_name = $post_type_names; |
| 141 |
|
| 142 |
// set the slug name |
| 143 |
$this->slug = $this->get_slug(); |
| 144 |
|
| 145 |
// set the plural name label |
| 146 |
$this->plural = $this->get_plural(); |
| 147 |
|
| 148 |
// set the singular name label |
| 149 |
$this->singular = $this->get_singular(); |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
// set the user submitted options to the object |
| 154 |
$this->options = $options; |
| 155 |
|
| 156 |
// register the post type |
| 157 |
//$this->add_action('init', array(&$this, 'register_post_type')); |
| 158 |
$this->register_post_type(); |
| 159 |
|
| 160 |
// register taxonomies |
| 161 |
//$this->add_action('init', array(&$this, 'register_taxonomies')); |
| 162 |
$this->register_taxonomies(); |
| 163 |
|
| 164 |
// add taxonomy to admin edit columns |
| 165 |
$this->add_filter('manage_edit-' . $this->post_type_name . '_columns', array($this, 'add_admin_columns')); |
| 166 |
|
| 167 |
// populate the taxonomy columns with the posts terms |
| 168 |
$this->add_action('manage_' . $this->post_type_name . '_posts_custom_column', array($this, 'populate_admin_columns'), 10, 2); |
| 169 |
|
| 170 |
// add filter select option to admin edit |
| 171 |
$this->add_action('restrict_manage_posts', array($this, 'add_taxonomy_filters')); |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
/* |
| 178 |
helper function get |
| 179 |
used to get an object variable |
| 180 |
|
| 181 |
@param string $var the variable you would like to retrieve |
| 182 |
@return mixed returns the value on sucess, bool (false) when fails |
| 183 |
|
| 184 |
*/ |
| 185 |
|
| 186 |
function get($var) { |
| 187 |
|
| 188 |
// if the variable exisits |
| 189 |
if($this->$var) { |
| 190 |
|
| 191 |
// on success return the value |
| 192 |
return $this->$var; |
| 193 |
|
| 194 |
} else { |
| 195 |
|
| 196 |
// on fail return false |
| 197 |
return false; |
| 198 |
|
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
/* |
| 203 |
helper function set |
| 204 |
used to set an object variable |
| 205 |
can overwrite exisiting variables and create new ones |
| 206 |
cannot overwrite reserved variables |
| 207 |
|
| 208 |
@param mixed $var the variable you would like to create/overwrite |
| 209 |
@param mixed $value the value you would like to set to the variable |
| 210 |
|
| 211 |
*/ |
| 212 |
function set($var, $value) { |
| 213 |
|
| 214 |
// an array of reserved variables that cannot be overwritten |
| 215 |
$reserved = array( |
| 216 |
'config', |
| 217 |
'post_type_name', |
| 218 |
'singular', |
| 219 |
'plural', |
| 220 |
'slug', |
| 221 |
'options', |
| 222 |
'taxonomies' |
| 223 |
); |
| 224 |
|
| 225 |
// if the variable is not a reserved variable |
| 226 |
if(!in_array($var, $reserved,true)) { |
| 227 |
|
| 228 |
// write variable and value |
| 229 |
$this->$var = $value; |
| 230 |
|
| 231 |
} |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
/* |
| 236 |
helper function add_action |
| 237 |
used to create add_action wordpress filter |
| 238 |
|
| 239 |
see Wordpress Codex |
| 240 |
http://codex.wordpress.org/Function_Reference/add_action |
| 241 |
|
| 242 |
@param string $action name of the action to hook to, e.g 'init' |
| 243 |
@param string $function function to hook that will run on @action |
| 244 |
@param int $priority order in which to execute the function, relation to other function hooked to this action |
| 245 |
@param int $accepted_args the number of arguements the function accepts |
| 246 |
*/ |
| 247 |
function add_action($action, $function, $priority = 10, $accepted_args = 1) { |
| 248 |
|
| 249 |
// pass variables into Wordpress add_action function |
| 250 |
add_action($action, $function, $priority, $accepted_args); |
| 251 |
|
| 252 |
} |
| 253 |
|
| 254 |
/* |
| 255 |
helper function add_filter |
| 256 |
used to create add_filter wordpress filter |
| 257 |
|
| 258 |
see Wordpress Codex |
| 259 |
http://codex.wordpress.org/Function_Reference/add_filter |
| 260 |
|
| 261 |
@param string $action name of the action to hook to, e.g 'init' |
| 262 |
@param string $function function to hook that will run on @action |
| 263 |
@param int $priority order in which to execute the function, relation to other function hooked to this action |
| 264 |
@param int $accepted_args the number of arguements the function accepts |
| 265 |
*/ |
| 266 |
function add_filter($action, $function, $priority = 10, $accepted_args = 1) { |
| 267 |
|
| 268 |
// pass variables into Wordpress add_action function |
| 269 |
add_filter($action, $function, $priority, $accepted_args); |
| 270 |
|
| 271 |
} |
| 272 |
|
| 273 |
/* |
| 274 |
helper function get slug |
| 275 |
creates url friendly slug |
| 276 |
|
| 277 |
@param string $name name to slugify |
| 278 |
@return string $name returns the slug |
| 279 |
*/ |
| 280 |
function get_slug($name = null) { |
| 281 |
|
| 282 |
// if no name set use the post type name |
| 283 |
if(!isset($name)) { |
| 284 |
|
| 285 |
$name = $this->post_type_name; |
| 286 |
|
| 287 |
} |
| 288 |
|
| 289 |
// replace spaces with hyphen |
| 290 |
return str_replace(array(' ','_'), '-', strtolower($name)); |
| 291 |
} |
| 292 |
|
| 293 |
/* |
| 294 |
helper function get_plural |
| 295 |
returns the friendly plural name |
| 296 |
|
| 297 |
ucwords capitalize words |
| 298 |
strtolower makes string lowercase before capitalizing |
| 299 |
str_replace replace all instances of _ to space |
| 300 |
|
| 301 |
@param string $name the slug name you want to pluralize |
| 302 |
@return string the friendly pluralized name |
| 303 |
*/ |
| 304 |
function get_plural($name = null) { |
| 305 |
|
| 306 |
// if no name is passed the post_type_name is used |
| 307 |
if(!isset($name)) { |
| 308 |
|
| 309 |
$name = $this->post_type_name; |
| 310 |
|
| 311 |
} |
| 312 |
|
| 313 |
// return the plural name |
| 314 |
// add 's' to the end |
| 315 |
return $this->get_human_friendly($name) . 's'; |
| 316 |
} |
| 317 |
|
| 318 |
/* |
| 319 |
helper function get_singular |
| 320 |
returns the friendly singular 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 unpluralize |
| 327 |
@return string the friendly singular name |
| 328 |
*/ |
| 329 |
function get_singular($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 |
|
| 338 |
// return the string |
| 339 |
return $this->get_human_friendly($name); |
| 340 |
|
| 341 |
} |
| 342 |
|
| 343 |
/* |
| 344 |
helper function get_human_friendly |
| 345 |
returns the human friendly name |
| 346 |
|
| 347 |
ucwords capitalize words |
| 348 |
strtolower makes string lowercase before capitalizing |
| 349 |
str_replace replace all instances of hyphens and underscores to spaces |
| 350 |
|
| 351 |
@param string $name the name you want to make friendly |
| 352 |
@return string the human friendly name |
| 353 |
*/ |
| 354 |
function get_human_friendly($name = null) { |
| 355 |
|
| 356 |
// if no name is passed the post_type_name is used |
| 357 |
if(!isset($name)) { |
| 358 |
|
| 359 |
$name = $this->post_type_name; |
| 360 |
|
| 361 |
} |
| 362 |
|
| 363 |
// return human friendly name |
| 364 |
return ucwords(strtolower(str_replace('-', ' ', str_replace('_', ' ', $name)))); |
| 365 |
|
| 366 |
} |
| 367 |
|
| 368 |
/* |
| 369 |
register_post_type function |
| 370 |
object function to register the post type |
| 371 |
|
| 372 |
see Wordpress Codex |
| 373 |
http://codex.wordpress.org/Function_Reference/register_post_type |
| 374 |
*/ |
| 375 |
function register_post_type() { |
| 376 |
|
| 377 |
// friendly post type names |
| 378 |
$plural = $this->plural; |
| 379 |
$singular = $this->singular; |
| 380 |
$slug = $this->slug; |
| 381 |
|
| 382 |
// default labels |
| 383 |
$labels = array( |
| 384 |
'name' => $plural, |
| 385 |
'singular_name' => $singular, |
| 386 |
'menu_name' => $plural, |
| 387 |
'all_items' => $plural, |
| 388 |
'add_new' => __('Add New', 'themify'), |
| 389 |
'add_new_item' => sprintf( __('Add new %s', 'themify'), $singular ), |
| 390 |
'edit_item' => sprintf( __('Edit %s', 'themify'), $singular ), |
| 391 |
'new_item' => sprintf( __('New %s', 'themify'), $singular ), |
| 392 |
'view_item' => sprintf( __('View %s', 'themify'), $singular ), |
| 393 |
'search_items' => sprintf( __('Search %s', 'themify'), $plural ), |
| 394 |
'not_found' => sprintf( __('No %s found', 'themify'), $plural ), |
| 395 |
'not_found_in_trash' => sprintf( __('No %s found in Trash', 'themify'), $plural ), |
| 396 |
'parent_item_colon' => sprintf( __('Parent %s:', 'themify'), $singular ) |
| 397 |
); |
| 398 |
|
| 399 |
// default options |
| 400 |
$defaults = array( |
| 401 |
'labels' => $labels, |
| 402 |
'public' => true, |
| 403 |
'rewrite' => array( |
| 404 |
'slug' => $slug, |
| 405 |
) |
| 406 |
); |
| 407 |
|
| 408 |
// merge user submitted options with defaults |
| 409 |
$options = wp_parse_args( $this->options, $defaults ); |
| 410 |
|
| 411 |
// set the object options as full options passed |
| 412 |
$this->options = $options; |
| 413 |
|
| 414 |
// check that the post type doesn't already exist |
| 415 |
if(!post_type_exists($this->post_type_name)) { |
| 416 |
|
| 417 |
// register the post type |
| 418 |
register_post_type($this->post_type_name, $options); |
| 419 |
|
| 420 |
} |
| 421 |
|
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
/* |
| 426 |
function register_taxonomy |
| 427 |
register a taxonomy to a post type |
| 428 |
|
| 429 |
@param string $taxonomy_name the slug for the taxonomy |
| 430 |
@param array $options taxonomy options |
| 431 |
|
| 432 |
see Wordpress codex |
| 433 |
http://codex.wordpress.org/Function_Reference/register_taxonomy |
| 434 |
*/ |
| 435 |
|
| 436 |
function register_taxonomy($taxonomy_names, $options = array()) { |
| 437 |
|
| 438 |
|
| 439 |
// an array of the names required excluding taxonomy_name |
| 440 |
$names = array( |
| 441 |
'singular', |
| 442 |
'plural', |
| 443 |
'slug' |
| 444 |
); |
| 445 |
|
| 446 |
// if an array of names are passed |
| 447 |
if(is_array($taxonomy_names)) { |
| 448 |
|
| 449 |
// set the taxonomy name |
| 450 |
$taxonomy_name = $taxonomy_names['taxonomy_name']; |
| 451 |
|
| 452 |
// cycle through possible names |
| 453 |
foreach($names as $name) { |
| 454 |
|
| 455 |
// if the user has set the name |
| 456 |
if(isset($taxonomy_names[$name])) { |
| 457 |
|
| 458 |
// use user submitted name |
| 459 |
$$name = $taxonomy_names[$name]; |
| 460 |
|
| 461 |
// else generate the name |
| 462 |
} else { |
| 463 |
|
| 464 |
// define the fnction to be used |
| 465 |
|
| 466 |
// generate the name |
| 467 |
$$name = $this->{'get_'.$name}( $taxonomy_name ); |
| 468 |
|
| 469 |
} |
| 470 |
|
| 471 |
} |
| 472 |
|
| 473 |
// else if only the taxonomy_name has been supplied |
| 474 |
} else { |
| 475 |
|
| 476 |
// create user friendly names |
| 477 |
$taxonomy_name = $taxonomy_names; |
| 478 |
$singular = $this->get_singular($taxonomy_name); |
| 479 |
$plural = $this->get_plural($taxonomy_name); |
| 480 |
$slug = $this->get_slug($taxonomy_name); |
| 481 |
|
| 482 |
} |
| 483 |
|
| 484 |
// default labels |
| 485 |
$labels = array( |
| 486 |
'name' => $plural, |
| 487 |
'singular_name' => $singular, |
| 488 |
'menu_name' => $plural, |
| 489 |
'all_items' => sprintf( __('All %s', 'themify'), $plural ), |
| 490 |
'edit_item' => sprintf( __('Edit %s', 'themify'), $singular ), |
| 491 |
'view_item' => sprintf( __('View %s', 'themify'), $singular ), |
| 492 |
'update_item' => sprintf( __('Update %s', 'themify'), $singular ), |
| 493 |
'add_new_item' => sprintf( __('Add New %s', 'themify'), $singular ), |
| 494 |
'new_item_name' => sprintf( __('New %s', 'themify'), $singular ), |
| 495 |
'parent_item' => sprintf( __('Parent %s', 'themify'), $plural ), |
| 496 |
'parent_item_colon' => sprintf( __('Parent %s:', 'themify'), $plural ), |
| 497 |
'search_items' => sprintf( __('Search %s', 'themify'), $plural ), |
| 498 |
'popular_items' => sprintf( __('Popular %s', 'themify'), $plural ), |
| 499 |
'separate_items_with_commas' => sprintf( __('Separate %s with commas', 'themify'), $plural ), |
| 500 |
'add_or_remove_items' => sprintf( __('Add or remove %s', 'themify'), $plural ), |
| 501 |
'choose_from_most_used' => sprintf( __('Choose from most used %s', 'themify'), $plural ), |
| 502 |
'not_found' => sprintf( __('No %s found', 'themify'), $plural ) |
| 503 |
); |
| 504 |
|
| 505 |
// default options |
| 506 |
$defaults = array( |
| 507 |
'labels' => $labels, |
| 508 |
'hierarchical' => true, |
| 509 |
'rewrite' => array( |
| 510 |
'slug' => $slug |
| 511 |
) |
| 512 |
); |
| 513 |
|
| 514 |
// merge default options with user submitted options |
| 515 |
$options = array_replace_recursive($defaults, $options); |
| 516 |
|
| 517 |
// add the taxonomy to the object array |
| 518 |
// this is used to add columns and filters to admin pannel |
| 519 |
$this->taxonomies[] = $taxonomy_name; |
| 520 |
|
| 521 |
// create array used when registering taxonomies |
| 522 |
$this->taxonomy_settings[$taxonomy_name] = $options; |
| 523 |
|
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
/* |
| 529 |
function register_taxonomies |
| 530 |
cycles through taxonomies added with the class and registers them |
| 531 |
|
| 532 |
function is used with add_action |
| 533 |
*/ |
| 534 |
function register_taxonomies() { |
| 535 |
|
| 536 |
if(is_array($this->taxonomy_settings)) { |
| 537 |
// foreach taxonomy registered with the post type |
| 538 |
foreach($this->taxonomy_settings as $taxonomy_name => $options) { |
| 539 |
|
| 540 |
// register the taxonomy if it doesn't exist |
| 541 |
if(!taxonomy_exists($taxonomy_name)) { |
| 542 |
|
| 543 |
// register the taxonomy with Wordpress |
| 544 |
register_taxonomy($taxonomy_name, $this->post_type_name, $options); |
| 545 |
|
| 546 |
|
| 547 |
} else { |
| 548 |
|
| 549 |
// if taxonomy exists, attach exisiting taxonomy to post type |
| 550 |
register_taxonomy_for_object_type($taxonomy_name, $this->post_type_name); |
| 551 |
|
| 552 |
} |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
/* |
| 561 |
function add_admin_columns |
| 562 |
adds columns to the admin edit screen |
| 563 |
|
| 564 |
function is used with add_action |
| 565 |
*/ |
| 566 |
|
| 567 |
function add_admin_columns($columns) { |
| 568 |
|
| 569 |
// if no user columns have been specified use following defaults |
| 570 |
if(!isset($this->columns)) { |
| 571 |
|
| 572 |
// default columns |
| 573 |
$columns = array( |
| 574 |
'cb' => '<input type="checkbox" />', |
| 575 |
'title' => __('Title', 'themify') |
| 576 |
); |
| 577 |
|
| 578 |
// if there are taxonomies registered to the post type |
| 579 |
if(is_array($this->taxonomies)) { |
| 580 |
|
| 581 |
// create a column for each taxonomy |
| 582 |
foreach($this->taxonomies as $tax) { |
| 583 |
|
| 584 |
// get the taxonomy object for labels |
| 585 |
$taxonomy_object = get_taxonomy($tax); |
| 586 |
|
| 587 |
// column key is the slug, value is friendly name |
| 588 |
$columns[$tax] = $taxonomy_object->labels->name; |
| 589 |
|
| 590 |
} |
| 591 |
|
| 592 |
} |
| 593 |
|
| 594 |
// if post type supports comments |
| 595 |
if(post_type_supports($this->post_type_name, 'comments')) { |
| 596 |
$columns['comments'] = '<img alt="Comments" src="'. admin_url() .'images/comment-grey-bubble.png">'; |
| 597 |
} |
| 598 |
|
| 599 |
// add date of post to end of columns |
| 600 |
$columns['date'] = __('Date', 'themify'); |
| 601 |
|
| 602 |
} else { |
| 603 |
|
| 604 |
// use user submitted columns |
| 605 |
// these are defined using the object columns() method |
| 606 |
$columns = $this->columns; |
| 607 |
|
| 608 |
} |
| 609 |
|
| 610 |
return $columns; |
| 611 |
|
| 612 |
} |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
/* |
| 617 |
function populate_admin_columns |
| 618 |
populates custom columns on the admin edit screen |
| 619 |
|
| 620 |
function is used with add_action |
| 621 |
*/ |
| 622 |
|
| 623 |
function populate_admin_columns($column, $post_id) { |
| 624 |
|
| 625 |
// get wordpress $post object |
| 626 |
$post = get_post( $post_id ); |
| 627 |
|
| 628 |
// determine the column |
| 629 |
switch($column) { |
| 630 |
|
| 631 |
// if column is a taxonomy associated with the post type |
| 632 |
case (taxonomy_exists($column)) : |
| 633 |
|
| 634 |
// Get the taxonomy for the post |
| 635 |
$terms = get_the_terms($post_id, $column); |
| 636 |
|
| 637 |
// if we have terms |
| 638 |
if (!empty($terms)) { |
| 639 |
|
| 640 |
$output = array(); |
| 641 |
|
| 642 |
// Loop through each term, linking to the 'edit posts' page for the specific term. |
| 643 |
foreach($terms as $term) { |
| 644 |
|
| 645 |
// output is an array of terms associated with the post |
| 646 |
$output[] = sprintf( |
| 647 |
|
| 648 |
// define link |
| 649 |
'<a href="%s">%s</a>', |
| 650 |
|
| 651 |
// create filter url |
| 652 |
esc_url(add_query_arg(array('post_type' => $post->post_type, $column => $term->slug), 'edit.php')), |
| 653 |
|
| 654 |
// create friendly term name |
| 655 |
esc_html(sanitize_term_field('name', $term->name, $term->term_id, $column, 'display')) |
| 656 |
|
| 657 |
); |
| 658 |
|
| 659 |
} |
| 660 |
|
| 661 |
// Join the terms, separating them with a comma |
| 662 |
echo join(', ', $output); |
| 663 |
|
| 664 |
// if no terms found |
| 665 |
} else { |
| 666 |
|
| 667 |
// get the taxonomy object for labels |
| 668 |
$taxonomy_object = get_taxonomy($column); |
| 669 |
|
| 670 |
// echo no terms |
| 671 |
echo sprintf( __('No %s', 'themify'), $taxonomy_object->labels->name ); |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
break; |
| 676 |
|
| 677 |
// if column is for the post ID |
| 678 |
case 'post_id' : |
| 679 |
|
| 680 |
echo esc_attr( $post->ID ); |
| 681 |
|
| 682 |
break; |
| 683 |
|
| 684 |
// if the column is prepended with 'meta_' |
| 685 |
// this will automagically retrieve the meta values and display them |
| 686 |
case (preg_match('/^meta_/', $column) ? true : false) : |
| 687 |
|
| 688 |
// meta_book_author (meta key = book_author) |
| 689 |
$x = substr($column, 5); |
| 690 |
|
| 691 |
$meta = get_post_meta($post->ID, $x); |
| 692 |
|
| 693 |
echo join(', ', $meta); |
| 694 |
|
| 695 |
break; |
| 696 |
|
| 697 |
// if the column is post thumbnail |
| 698 |
case 'icon' : |
| 699 |
|
| 700 |
// create the edit link |
| 701 |
$link = add_query_arg(array('post' => $post->ID, 'action' => 'edit'), 'post.php'); |
| 702 |
|
| 703 |
// if it post has a featured image |
| 704 |
if(has_post_thumbnail()) { |
| 705 |
|
| 706 |
// display post featured image with edit link |
| 707 |
echo '<a href="'. esc_url( $link ) .'">'; |
| 708 |
the_post_thumbnail(array(60, 60)); |
| 709 |
echo '</a>'; |
| 710 |
|
| 711 |
} else { |
| 712 |
|
| 713 |
// display default media image with link |
| 714 |
echo '<a href="'.esc_url( $link ).'"><img src="'. site_url('/wp-includes/images/crystal/default.png') .'" alt="'. esc_attr( $post->post_title ) .'" /></a>'; |
| 715 |
|
| 716 |
} |
| 717 |
|
| 718 |
break; |
| 719 |
|
| 720 |
// default case checks if the column has a user function |
| 721 |
// this is most commonly used for custom fields |
| 722 |
default : |
| 723 |
|
| 724 |
// if there are user custom columns to populate |
| 725 |
if(isset($this->custom_populate_columns,$this->custom_populate_columns[$column]) && is_array($this->custom_populate_columns) && is_callable($this->custom_populate_columns[$column])) { |
| 726 |
|
| 727 |
// run the function |
| 728 |
//$this->custom_populate_columns[$column]($column, $post); |
| 729 |
call_user_func( $this->custom_populate_columns[$column], $column, $post ); |
| 730 |
|
| 731 |
} |
| 732 |
|
| 733 |
break; |
| 734 |
|
| 735 |
} // end switch($column) |
| 736 |
|
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
/* |
| 742 |
function filters |
| 743 |
user function to define which taxonomy filters to display on the admin page |
| 744 |
|
| 745 |
@param array $filters an array of taxonomy filters to display |
| 746 |
|
| 747 |
*/ |
| 748 |
|
| 749 |
function filters($filters = array()) { |
| 750 |
|
| 751 |
$this->filters = $filters; |
| 752 |
|
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
/* |
| 759 |
function add_taxtonomy_filters |
| 760 |
creates select fields for filtering posts by taxonomies on admin edit screen |
| 761 |
|
| 762 |
*/ |
| 763 |
|
| 764 |
function add_taxonomy_filters() { |
| 765 |
|
| 766 |
global $typenow; |
| 767 |
|
| 768 |
// must set this to the post type you want the filter(s) displayed on |
| 769 |
if($typenow === $this->post_type_name){ |
| 770 |
|
| 771 |
// if custom filters are defined use those |
| 772 |
if(is_array($this->filters)) { |
| 773 |
$filters = $this->filters; |
| 774 |
// else default to use all taxonomies associated with the post |
| 775 |
} else { |
| 776 |
$filters = (array) $this->taxonomies; |
| 777 |
} |
| 778 |
|
| 779 |
// foreach of the taxonomies we want to create filters for |
| 780 |
foreach($filters as $tax_slug) { |
| 781 |
|
| 782 |
// object for taxonomy, doesn't contain the terms |
| 783 |
$tax = get_taxonomy($tax_slug); |
| 784 |
|
| 785 |
// get taxonomy terms and order by name |
| 786 |
$args = array( |
| 787 |
'taxonomy'=>$tax_slug, |
| 788 |
'orderby' => 'name', |
| 789 |
'hide_empty' => false |
| 790 |
); |
| 791 |
|
| 792 |
// get taxonomy terms |
| 793 |
$terms = get_terms($args); |
| 794 |
|
| 795 |
// if we have terms |
| 796 |
if($terms) { |
| 797 |
|
| 798 |
// set up select box |
| 799 |
printf(' <select name="%s" class="postform">', $tax_slug); |
| 800 |
|
| 801 |
// default show all |
| 802 |
printf('<option value="0">%s</option>', 'Show all ' . $tax->label); |
| 803 |
|
| 804 |
// foreach term create an option field |
| 805 |
foreach ($terms as $term) { |
| 806 |
|
| 807 |
// if filtered by this term make it selected |
| 808 |
if(isset($_GET[$tax_slug]) && $_GET[$tax_slug] === $term->slug) { |
| 809 |
|
| 810 |
printf('<option value="%s" selected="selected">%s (%s)</option>', $term->slug, $term->name, $term->count); |
| 811 |
|
| 812 |
// create option for taxonomy |
| 813 |
} else { |
| 814 |
|
| 815 |
printf('<option value="%s">%s (%s)</option>', $term->slug, $term->name, $term->count); |
| 816 |
|
| 817 |
} |
| 818 |
|
| 819 |
} |
| 820 |
|
| 821 |
// end the select field |
| 822 |
print('</select> '); |
| 823 |
|
| 824 |
} |
| 825 |
|
| 826 |
} |
| 827 |
|
| 828 |
} |
| 829 |
|
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
/* |
| 835 |
function columns |
| 836 |
user function to choose columns to be displayed on the admin edit screen |
| 837 |
|
| 838 |
@param array $columns an array of columns to be displayed |
| 839 |
|
| 840 |
*/ |
| 841 |
|
| 842 |
function columns($columns) { |
| 843 |
|
| 844 |
// if columns is set |
| 845 |
if(isset($columns)) { |
| 846 |
|
| 847 |
// assign user submitted columns to object |
| 848 |
$this->columns = $columns; |
| 849 |
|
| 850 |
} |
| 851 |
|
| 852 |
} |
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
/* |
| 857 |
function populate_column |
| 858 |
user function to define what and how to populate a specific admin column |
| 859 |
|
| 860 |
@param string $column_name the name of the column to populate |
| 861 |
@param func $function an anonymous function to run when populating the column |
| 862 |
*/ |
| 863 |
|
| 864 |
function populate_column($column_name, $function) { |
| 865 |
|
| 866 |
$this->custom_populate_columns[$column_name] = $function; |
| 867 |
|
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
/* |
| 873 |
function sortable |
| 874 |
user function define what columns are sortable in admin edit screen |
| 875 |
|
| 876 |
@param array $columns an array of the columns that are sortable |
| 877 |
*/ |
| 878 |
|
| 879 |
function sortable($columns = array()) { |
| 880 |
|
| 881 |
// assign user defined sortable columns to object variable |
| 882 |
$this->sortable = $columns; |
| 883 |
|
| 884 |
// run filter to make columns sortable |
| 885 |
$this->add_filter('manage_edit-' . $this->post_type_name . '_sortable_columns', array($this, 'make_columns_sortable')); |
| 886 |
|
| 887 |
// run action that sorts columns on request |
| 888 |
$this->add_action('load-edit.php', array($this, 'load_edit')); |
| 889 |
|
| 890 |
} |
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
/* |
| 895 |
function make_columns_sortable |
| 896 |
internal function that adds any user defined sortable columns to wordpress default columns |
| 897 |
|
| 898 |
*/ |
| 899 |
|
| 900 |
function make_columns_sortable($columns) { |
| 901 |
|
| 902 |
// for each sortable column |
| 903 |
foreach($this->sortable as $column => $values) { |
| 904 |
|
| 905 |
// make an array to merege into wordpress sortable columns |
| 906 |
$sortable_columns[$column] = $values[0]; |
| 907 |
|
| 908 |
} |
| 909 |
|
| 910 |
|
| 911 |
// merge sortable columns array into wordpress sortable columns |
| 912 |
return array_merge($sortable_columns, $columns); |
| 913 |
|
| 914 |
} |
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
/* |
| 919 |
function load_edit |
| 920 |
only sort columns on the edit.php page when requested |
| 921 |
|
| 922 |
*/ |
| 923 |
|
| 924 |
function load_edit() { |
| 925 |
|
| 926 |
// run filter to sort columns when requested |
| 927 |
$this->add_filter( 'request', array($this, 'sort_columns') ); |
| 928 |
|
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
/* |
| 934 |
function sort columns |
| 935 |
internal function that sorts columns on request |
| 936 |
|
| 937 |
run by load_edit() filter |
| 938 |
|
| 939 |
@param array $vars the query vars submitted by user |
| 940 |
|
| 941 |
*/ |
| 942 |
|
| 943 |
function sort_columns($vars) { |
| 944 |
|
| 945 |
// cycle through all sortable columns submitted by the user |
| 946 |
foreach($this->sortable as $values) { |
| 947 |
|
| 948 |
// retrieve the meta key from the user submitted array of sortable columns |
| 949 |
$meta_key = $values[0]; |
| 950 |
|
| 951 |
|
| 952 |
// if the optional parameter is set and is set to true |
| 953 |
$orderby = isset($values[1]) && true === $values[1]?'meta_value_num':'meta_value'; |
| 954 |
|
| 955 |
// Check if we're viewing this post type |
| 956 |
if (isset($vars['post_type'],$vars['orderby']) && $meta_key === $vars['orderby'] && $this->post_type_name === $vars['post_type']) { |
| 957 |
|
| 958 |
|
| 959 |
// merge the query vars with our custom variables |
| 960 |
$vars = array_merge($vars, |
| 961 |
array( |
| 962 |
'meta_key' => $meta_key, |
| 963 |
'orderby' => $orderby |
| 964 |
) |
| 965 |
); |
| 966 |
|
| 967 |
} |
| 968 |
|
| 969 |
} |
| 970 |
|
| 971 |
return $vars; |
| 972 |
} |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
/* |
| 977 |
function menu icon |
| 978 |
used to change the menu icon in the admin dashboard |
| 979 |
pass name of dashicon, list found here http://melchoyce.github.io/dashicons/ |
| 980 |
|
| 981 |
@param mixed $icon a string of the name of the icon to use |
| 982 |
*/ |
| 983 |
|
| 984 |
function menu_icon($icon = 'dashicons-admin-page') { |
| 985 |
|
| 986 |
// WP 3.8 changed the icon system to use an icon font. |
| 987 |
// http://melchoyce.github.io/dashicons/ |
| 988 |
|
| 989 |
$this->options['menu_icon'] = is_string($icon) && stripos($icon, 'dashicons') !== FALSE?$icon:'dashicons-admin-page'; |
| 990 |
} |
| 991 |
} |