| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
|
| 6 |
|
| 7 |
/* |
| 8 |
* To change this license header, choose License Headers in Project Properties. |
| 9 |
* To change this template file, choose Tools | Templates |
| 10 |
* and open the template in the editor. |
| 11 |
*/ |
| 12 |
|
| 13 |
|
| 14 |
/** |
| 15 |
* Description of class-mlsimport-item |
| 16 |
* |
| 17 |
* @author cretu |
| 18 |
*/ |
| 19 |
class Mlsimport_Item { |
| 20 |
|
| 21 |
|
| 22 |
// put your code here |
| 23 |
|
| 24 |
|
| 25 |
public function __construct() { |
| 26 |
} |
| 27 |
|
| 28 |
private function register_single_post_type( $fields ) { |
| 29 |
|
| 30 |
$labels = array( |
| 31 |
'name' => $fields['plural'], |
| 32 |
'singular_name' => $fields['singular'], |
| 33 |
'menu_name' => $fields['menu_name'], |
| 34 |
'new_item' => sprintf( __( 'New %s', 'mlsimport' ), $fields['singular'] ), |
| 35 |
'add_new_item' => sprintf( __( 'Add new %s', 'mlsimport' ), $fields['singular'] ), |
| 36 |
'edit_item' => sprintf( __( 'Edit %s', 'mlsimport' ), $fields['singular'] ), |
| 37 |
'view_item' => sprintf( __( 'View %s', 'mlsimport' ), $fields['singular'] ), |
| 38 |
'view_items' => sprintf( __( 'View %s', 'mlsimport' ), $fields['plural'] ), |
| 39 |
'search_items' => sprintf( __( 'Search %s', 'mlsimport' ), $fields['plural'] ), |
| 40 |
'not_found' => sprintf( __( 'No %s found', 'mlsimport' ), strtolower( $fields['plural'] ) ), |
| 41 |
'not_found_in_trash' => sprintf( __( 'No %s found in trash', 'mlsimport' ), strtolower( $fields['plural'] ) ), |
| 42 |
'all_items' => sprintf( __( 'All %s', 'mlsimport' ), $fields['plural'] ), |
| 43 |
'archives' => sprintf( __( '%s Archives', 'mlsimport' ), $fields['singular'] ), |
| 44 |
'attributes' => sprintf( __( '%s Attributes', 'mlsimport' ), $fields['singular'] ), |
| 45 |
'insert_into_item' => sprintf( __( 'Insert into %s', 'mlsimport' ), strtolower( $fields['singular'] ) ), |
| 46 |
'uploaded_to_this_item' => sprintf( __( 'Uploaded to this %s', 'mlsimport' ), strtolower( $fields['singular'] ) ), |
| 47 |
|
| 48 |
/* Labels for hierarchical post types only. */ |
| 49 |
'parent_item' => sprintf( __( 'Parent %s', 'mlsimport' ), $fields['singular'] ), |
| 50 |
'parent_item_colon' => sprintf( __( 'Parent %s:', 'mlsimport' ), $fields['singular'] ), |
| 51 |
|
| 52 |
/* Custom archive label. Must filter 'post_type_archive_title' to use. */ |
| 53 |
'archive_title' => $fields['plural'], |
| 54 |
); |
| 55 |
|
| 56 |
$args = array( |
| 57 |
'labels' => $labels, |
| 58 |
'description' => ( isset( $fields['description'] ) ) ? $fields['description'] : '', |
| 59 |
'public' => ( isset( $fields['public'] ) ) ? $fields['public'] : true, |
| 60 |
'publicly_queryable' => ( isset( $fields['publicly_queryable'] ) ) ? $fields['publicly_queryable'] : true, |
| 61 |
'exclude_from_search' => ( isset( $fields['exclude_from_search'] ) ) ? $fields['exclude_from_search'] : false, |
| 62 |
'show_ui' => ( isset( $fields['show_ui'] ) ) ? $fields['show_ui'] : true, |
| 63 |
'show_in_menu' => ( isset( $fields['show_in_menu'] ) ) ? $fields['show_in_menu'] : true, |
| 64 |
'query_var' => ( isset( $fields['query_var'] ) ) ? $fields['query_var'] : true, |
| 65 |
'show_in_admin_bar' => ( isset( $fields['show_in_admin_bar'] ) ) ? $fields['show_in_admin_bar'] : true, |
| 66 |
'capability_type' => ( isset( $fields['capability_type'] ) ) ? $fields['capability_type'] : 'post', |
| 67 |
'has_archive' => ( isset( $fields['has_archive'] ) ) ? $fields['has_archive'] : true, |
| 68 |
'hierarchical' => ( isset( $fields['hierarchical'] ) ) ? $fields['hierarchical'] : true, |
| 69 |
'supports' => ( isset( $fields['supports'] ) ) ? $fields['supports'] : array( |
| 70 |
'title', |
| 71 |
'editor', |
| 72 |
'excerpt', |
| 73 |
'author', |
| 74 |
'thumbnail', |
| 75 |
'comments', |
| 76 |
'trackbacks', |
| 77 |
'custom-fields', |
| 78 |
'revisions', |
| 79 |
'page-attributes', |
| 80 |
'post-formats', |
| 81 |
), |
| 82 |
'menu_position' => ( isset( $fields['menu_position'] ) ) ? $fields['menu_position'] : 21, |
| 83 |
'menu_icon' => ( isset( $fields['menu_icon'] ) ) ? $fields['menu_icon'] : 'dashicons-admin-generic', |
| 84 |
'show_in_nav_menus' => ( isset( $fields['show_in_nav_menus'] ) ) ? $fields['show_in_nav_menus'] : true, |
| 85 |
'taxonomies' => array( 'category', 'post_tag' ), |
| 86 |
); |
| 87 |
|
| 88 |
if ( isset( $fields['rewrite'] ) ) { |
| 89 |
|
| 90 |
/** |
| 91 |
* Add $this->plugin_name as translatable in the permalink structure, |
| 92 |
* to avoid conflicts with other plugins which may use customers as well. |
| 93 |
*/ |
| 94 |
$args['rewrite'] = $fields['rewrite']; |
| 95 |
} |
| 96 |
|
| 97 |
if ( $fields['custom_caps'] ) { |
| 98 |
|
| 99 |
/** |
| 100 |
* Provides more precise control over the capabilities than the defaults. By default, WordPress |
| 101 |
* will use the 'capability_type' argument to build these capabilities. More often than not, |
| 102 |
* this results in many extra capabilities that you probably don't need. The following is how |
| 103 |
* I set up capabilities for many post types, which only uses three basic capabilities you need |
| 104 |
* to assign to roles: 'manage_examples', 'edit_examples', 'create_examples'. Each post type |
| 105 |
* is unique though, so you'll want to adjust it to fit your needs. |
| 106 |
* |
| 107 |
* @link https://gist.github.com/creativembers/6577149 |
| 108 |
* @link http://justintadlock.com/archives/2010/07/10/meta-capabilities-for-custom-post-types |
| 109 |
*/ |
| 110 |
$args['capabilities'] = array( |
| 111 |
|
| 112 |
// Meta capabilities |
| 113 |
'edit_post' => 'edit_' . strtolower( $fields['singular'] ), |
| 114 |
'read_post' => 'read_' . strtolower( $fields['singular'] ), |
| 115 |
'delete_post' => 'delete_' . strtolower( $fields['singular'] ), |
| 116 |
|
| 117 |
// Primitive capabilities used outside of map_meta_cap(): |
| 118 |
'edit_posts' => 'edit_' . strtolower( $fields['plural'] ), |
| 119 |
'edit_others_posts' => 'edit_others_' . strtolower( $fields['plural'] ), |
| 120 |
'publish_posts' => 'publish_' . strtolower( $fields['plural'] ), |
| 121 |
'read_private_posts' => 'read_private_' . strtolower( $fields['plural'] ), |
| 122 |
|
| 123 |
// Primitive capabilities used within map_meta_cap(): |
| 124 |
'delete_posts' => 'delete_' . strtolower( $fields['plural'] ), |
| 125 |
'delete_private_posts' => 'delete_private_' . strtolower( $fields['plural'] ), |
| 126 |
'delete_published_posts' => 'delete_published_' . strtolower( $fields['plural'] ), |
| 127 |
'delete_others_posts' => 'delete_others_' . strtolower( $fields['plural'] ), |
| 128 |
'edit_private_posts' => 'edit_private_' . strtolower( $fields['plural'] ), |
| 129 |
'edit_published_posts' => 'edit_published_' . strtolower( $fields['plural'] ), |
| 130 |
'create_posts' => 'edit_' . strtolower( $fields['plural'] ), |
| 131 |
|
| 132 |
); |
| 133 |
|
| 134 |
/** |
| 135 |
* Adding map_meta_cap will map the meta correctly. |
| 136 |
* |
| 137 |
* @link https://wordpress.stackexchange.com/questions/108338/capabilities-and-custom-post-types/108375#108375 |
| 138 |
*/ |
| 139 |
$args['map_meta_cap'] = true; |
| 140 |
|
| 141 |
/** |
| 142 |
* Assign capabilities to users |
| 143 |
* Without this, users - also admins - can not see post type. |
| 144 |
*/ |
| 145 |
$this->assign_capabilities( $args['capabilities'], $fields['custom_caps_users'] ); |
| 146 |
} |
| 147 |
|
| 148 |
register_post_type( $fields['slug'], $args ); |
| 149 |
|
| 150 |
/** |
| 151 |
* Register Taxnonmies if any |
| 152 |
* |
| 153 |
* @link https://codex.wordpress.org/Function_Reference/register_taxonomy |
| 154 |
*/ |
| 155 |
unregister_taxonomy_for_object_type( 'post_tag', 'mlsimport_item' ); |
| 156 |
unregister_taxonomy_for_object_type( 'category', 'mlsimport_item' ); |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
/** |
| 163 |
* Assign capabilities to users |
| 164 |
* |
| 165 |
* @link https://codex.wordpress.org/Function_Reference/register_post_type |
| 166 |
* @link https://typerocket.com/ultimate-guide-to-custom-post-types-in-wordpress/ |
| 167 |
*/ |
| 168 |
public function assign_capabilities( $caps_map, $users ) { |
| 169 |
|
| 170 |
foreach ( $users as $user ) { |
| 171 |
$user_role = get_role( $user ); |
| 172 |
|
| 173 |
foreach ( $caps_map as $cap_map_key => $capability ) { |
| 174 |
$user_role->add_cap( $capability ); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
/** |
| 183 |
* Create post types |
| 184 |
*/ |
| 185 |
public function create_custom_post_type() { |
| 186 |
|
| 187 |
/** |
| 188 |
* This is not all the fields, only what I find important. Feel free to change this function ;) |
| 189 |
* |
| 190 |
* @link https://codex.wordpress.org/Function_Reference/register_post_type |
| 191 |
* |
| 192 |
* For more info on fields: |
| 193 |
* @link https://github.com/JoeSz/WordPress-Plugin-Boilerplate-Tutorial/blob/9fb56794bc1f8aebfe04e99b15881db0c4bc61bd/mlsimport/includes/class-mlsimport-post_types.php#L230 |
| 194 |
*/ |
| 195 |
|
| 196 |
$custom_slug = 'mlsimport'; |
| 197 |
|
| 198 |
$post_types_fields = array( |
| 199 |
array( |
| 200 |
'slug' => 'mlsimport_item', |
| 201 |
'singular' => __( 'Import Task', 'mlsimport' ), |
| 202 |
'plural' => __( 'Import Tasks', 'mlsimport' ), |
| 203 |
'menu_name' => __( 'Import Tasks', 'mlsimport' ), |
| 204 |
'description' => __( 'Import Task', 'mlsimport' ), |
| 205 |
'has_archive' => true, |
| 206 |
'hierarchical' => false, |
| 207 |
'menu_icon' => 'dashicons-tag', |
| 208 |
'rewrite' => array( |
| 209 |
'slug' => $custom_slug, |
| 210 |
'with_front' => true, |
| 211 |
'pages' => true, |
| 212 |
'feeds' => true, |
| 213 |
'ep_mask' => EP_PERMALINK, |
| 214 |
), |
| 215 |
'menu_position' => 21, |
| 216 |
'public' => true, |
| 217 |
'publicly_queryable' => false, |
| 218 |
'exclude_from_search' => true, |
| 219 |
'show_ui' => true, |
| 220 |
'show_in_menu' => true, |
| 221 |
'query_var' => true, |
| 222 |
'show_in_admin_bar' => true, |
| 223 |
'show_in_nav_menus' => true, |
| 224 |
'supports' => array( |
| 225 |
'title', |
| 226 |
|
| 227 |
), |
| 228 |
'custom_caps' => false, |
| 229 |
'custom_caps_users' => array( |
| 230 |
'administrator', |
| 231 |
), |
| 232 |
'taxonomies' => array(), |
| 233 |
'menu_icon' => MLSIMPORT_PLUGIN_URL . '/img/mlsimport_menu.png', |
| 234 |
), |
| 235 |
); |
| 236 |
|
| 237 |
// loop torugh custom post type array and register |
| 238 |
foreach ( $post_types_fields as $fields ) { |
| 239 |
$this->register_single_post_type( $fields ); |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|