| 1 |
<?php |
| 2 |
/** |
| 3 |
* Inits Custom Post Types, Taxonimies, Meta |
| 4 |
* |
| 5 |
* @author Usability Dynamics, Inc. <info@usabilitydynamics.com> |
| 6 |
* @package Theme |
| 7 |
* @author peshkov@UD |
| 8 |
*/ |
| 9 |
namespace UsabilityDynamics { |
| 10 |
|
| 11 |
if( !class_exists( 'UsabilityDynamics\Structure' ) ) { |
| 12 |
|
| 13 |
class Structure { |
| 14 |
|
| 15 |
/** |
| 16 |
* |
| 17 |
* |
| 18 |
*/ |
| 19 |
static private $args = array(); |
| 20 |
|
| 21 |
/** |
| 22 |
* |
| 23 |
* |
| 24 |
*/ |
| 25 |
static private $structure = array(); |
| 26 |
|
| 27 |
/** |
| 28 |
* Define Data Structure |
| 29 |
* |
| 30 |
* @param $args |
| 31 |
* @param $args.types (array) Post type definitions |
| 32 |
* @param $args.meta (array) Meta definitions. |
| 33 |
* @param $args.taxonomies (array) Taxonomy fields. |
| 34 |
* |
| 35 |
* @return array|bool |
| 36 |
*/ |
| 37 |
static public function define( $args = array() ) { |
| 38 |
|
| 39 |
self::$args = wp_parse_args( $args, array( |
| 40 |
'types' => array(), // Custom post types |
| 41 |
'meta' => array(), // Meta fields |
| 42 |
'taxonomies' => array(), // Taxonomies |
| 43 |
) ); |
| 44 |
|
| 45 |
$structure = array(); |
| 46 |
|
| 47 |
foreach( (array) self::$args[ 'types' ] as $object_type => $type ) { |
| 48 |
|
| 49 |
$object_type = sanitize_key( $object_type ); |
| 50 |
|
| 51 |
self::$structure[ $object_type ] = array( |
| 52 |
'meta' => array(), |
| 53 |
'terms' => array(), |
| 54 |
); |
| 55 |
|
| 56 |
// STEP 1. Register post_type |
| 57 |
|
| 58 |
// Register Post Type |
| 59 |
$data = ( isset( $type[ 'data' ] ) && is_array( $type[ 'data' ] ) ) ? $type[ 'data' ] : array(); |
| 60 |
|
| 61 |
if( !post_type_exists( $object_type ) ) { |
| 62 |
register_post_type( $object_type, self::_prepare_post_type( $object_type, $data )); |
| 63 |
} |
| 64 |
|
| 65 |
// STEP 2. Register taxonomy ( and Taxonomy's Post Type if theme supports 'extended-taxonomies' feature ) |
| 66 |
|
| 67 |
// Define post type's taxonomies |
| 68 |
$taxonomies = ( isset( $type[ 'taxonomies' ] ) && is_array( $type[ 'taxonomies' ] ) ) ? $type[ 'taxonomies' ] : array( |
| 69 |
'post_tag', |
| 70 |
'category', |
| 71 |
); |
| 72 |
|
| 73 |
// Initialize taxonomies if they don't exist and assign them to the current post type |
| 74 |
foreach( (array) $taxonomies as $taxonomy ) { |
| 75 |
|
| 76 |
if( empty( $taxonomy ) || !is_string( $taxonomy ) ) { |
| 77 |
continue; |
| 78 |
} |
| 79 |
|
| 80 |
if( !taxonomy_exists( $taxonomy ) ) { |
| 81 |
$data = self::_prepare_taxonomy( $taxonomy ); |
| 82 |
register_taxonomy( $taxonomy, null, $data ); |
| 83 |
} |
| 84 |
|
| 85 |
register_taxonomy_for_object_type( $taxonomy, $object_type ); |
| 86 |
|
| 87 |
//** Add custom post type for our taxonomy if theme supports extended-taxonomies */ |
| 88 |
$taxonomy_post_type = '_tp_' . $taxonomy; |
| 89 |
if( current_theme_supports( 'extended-taxonomies' ) && !post_type_exists( $taxonomy_post_type ) ) { |
| 90 |
register_post_type( $taxonomy_post_type, array( |
| 91 |
'label' => $data[ 'label' ], |
| 92 |
'public' => false, |
| 93 |
'rewrite' => false, |
| 94 |
'labels' => array( |
| 95 |
'name' => $data[ 'label' ], |
| 96 |
'edit_item' => 'Edit Term: ' . $data[ 'label' ] |
| 97 |
), |
| 98 |
'supports' => array( 'title', 'editor' ), |
| 99 |
)); |
| 100 |
} |
| 101 |
|
| 102 |
if( isset( $structure[ $object_type ] ) && isset( $structure[ $object_type ]['terms' ] ) && is_array( $structure[ $object_type ]['terms' ] ) ) { |
| 103 |
array_push( $structure[ $object_type ][ 'terms' ], $taxonomy ); |
| 104 |
} |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
// STEP 3. Set meta fields and meta boxes |
| 109 |
|
| 110 |
// Stop here if Meta Box class doesn't exist |
| 111 |
if( !class_exists( '\RW_Meta_Box' ) ) { |
| 112 |
continue; |
| 113 |
} |
| 114 |
|
| 115 |
// Init \RW_Meta_Box defines if needed |
| 116 |
if ( !defined( 'RWMB_VER' ) ) { |
| 117 |
|
| 118 |
$reflector = new \ReflectionClass( '\RW_Meta_Box' ); |
| 119 |
|
| 120 |
$file = dirname( dirname( $reflector->getFileName() ) ) . '/meta-box.php'; |
| 121 |
if( !file_exists( $file ) ) { |
| 122 |
continue; |
| 123 |
} |
| 124 |
include_once( $file ); |
| 125 |
} |
| 126 |
|
| 127 |
$metaboxes = ( isset( $type[ 'meta' ] ) && is_array( $type[ 'meta' ] ) ) ? $type[ 'meta' ] : array(); |
| 128 |
|
| 129 |
foreach( $metaboxes as $key => $data ) { |
| 130 |
$data = self::_prepare_metabox( $key, $object_type, $data ); |
| 131 |
|
| 132 |
if( $data ) { |
| 133 |
new \RW_Meta_Box( $data ); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
// STEP 4. reset static vars and return structure data. |
| 140 |
$structure = array( |
| 141 |
'post_types' => self::$structure, |
| 142 |
'schema' => self::$args, |
| 143 |
); |
| 144 |
|
| 145 |
self::$args = array(); |
| 146 |
self::$structure = array(); |
| 147 |
|
| 148 |
return $structure; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* |
| 153 |
* |
| 154 |
*/ |
| 155 |
static private function _prepare_metabox( $key, $object_type, $data ) { |
| 156 |
$label = \UsabilityDynamics\Utility::de_slug( $key ); |
| 157 |
|
| 158 |
$data = wp_parse_args( $data, array( |
| 159 |
'id' => $key, |
| 160 |
'title' => $label, |
| 161 |
'pages' => array( $object_type ), |
| 162 |
'context' => 'normal', |
| 163 |
'priority' => 'high', |
| 164 |
'autosave' => false, |
| 165 |
'fields' => array(), |
| 166 |
) ); |
| 167 |
|
| 168 |
// There is no sense to init empty metabox |
| 169 |
if( !is_array( $data[ 'fields' ] ) || empty( $data[ 'fields' ] ) ) { |
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
$fields = array(); |
| 174 |
foreach( $data[ 'fields' ] as $field ) { |
| 175 |
array_push( self::$structure[ $object_type ][ 'meta' ], $field ); |
| 176 |
$fields[] = self::_prepare_metafield( $field ); |
| 177 |
} |
| 178 |
|
| 179 |
$data[ 'fields' ] = $fields; |
| 180 |
|
| 181 |
return $data; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* |
| 186 |
* |
| 187 |
*/ |
| 188 |
static private function _prepare_metafield( $key ) { |
| 189 |
$data = isset( self::$args[ 'meta' ][ $key ] ) ? (array) self::$args[ 'meta' ][ $key ] : array(); |
| 190 |
$data = wp_parse_args( $data, array( |
| 191 |
'id' => $key, |
| 192 |
'name' => \UsabilityDynamics\Utility::de_slug( $key ), |
| 193 |
'type' => 'text', |
| 194 |
) ); |
| 195 |
return $data; |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* |
| 200 |
* |
| 201 |
*/ |
| 202 |
static private function _prepare_taxonomy( $key ) { |
| 203 |
$data = isset( self::$args[ 'taxonomies' ][ $key ] ) && is_array( self::$args[ 'taxonomies' ][ $key ] ) ? self::$args[ 'taxonomies' ][ $key ] : array(); |
| 204 |
$data = wp_parse_args( $data, array( |
| 205 |
'label' => \UsabilityDynamics\Utility::de_slug( $key ), |
| 206 |
) ); |
| 207 |
return $data; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* |
| 212 |
* |
| 213 |
*/ |
| 214 |
static private function _prepare_post_type( $key, $args = array() ) { |
| 215 |
$args = wp_parse_args( $args, array( |
| 216 |
'label' => \UsabilityDynamics\Utility::de_slug( $key ), |
| 217 |
'exclude_from_search' => false, |
| 218 |
) ); |
| 219 |
return $args; |
| 220 |
} |
| 221 |
|
| 222 |
} |
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|