class-cf7-2-custom-post-mapper.php
6.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
require_once plugin_dir_path( __FILE__ ) . 'class-cf7-2-post-mapper.php';
class Form_2_Custom_Post extends Form_2_Post_Mapper{
public function __construct($cf7_id, $factory){
$this->cf7_post_ID = $cf7_id;
self::$factory = $factory;
$this->post_properties['type_source'] = 'factory';
}
/**
* Initialises a default custom post for a new form mapper.
*
*@since 5.0.0
*@param String $post_type post type id.
*@param String $singular_name post singular name.
*@param String $plural_name post plural name.
*@return string text_description
*/
public function init_default($cf7_key, $singular_name, $plural_name){
//set some default values
$this->post_properties=array(
'hierarchical' => false, //like post.
'public' => true, //visible on front-end.
'show_ui' => true, //visible in admin dashboard.
'show_in_menu' => true, //visible in admin menu.
'menu_position' => 5, //position in admin menu.
'show_in_admin_bar' => false, //visible in admin bar menu.
'show_in_nav_menus' => false, //available in navigation menu.
'can_export' => true, //can be exported.
'has_archive' => true, //can be archived.
'exclude_from_search' => true, //cannot be searched from front-end.
'publicly_queryable' => false //can be queried from front-end.
);
$this->post_properties['default']=0;
$post_type = $this->cf7_key = $cf7_key;
$this->post_properties['map']='draft';
$this->post_properties['type']=$post_type;
if(strlen($post_type) > 20){ //fix pho notice for post registration.
$this->post_properties['type']=substr($post_type,0,20);
}
$this->post_properties['version'] = CF7_2_POST_VERSION;
$this->post_properties['singular_name']=$singular_name;
$this->post_properties['plural_name']=$plural_name;
$this->post_properties['type_source'] = 'factory';
$this->post_properties['cf7_title']=$singular_name;
$this->post_properties['taxonomy']=array();
//supports
$this->post_properties['supports'] = array(
'title', 'editor', 'excerpt', 'author',
'thumbnail', 'revisions', 'custom-fields' );
//filter the support capabilities for more user customisation
$this->post_properties['supports'] = apply_filters('cf7_2_post_supports_'.$post_type, $this->post_properties['supports']);
//make sure we have custom-fields support
// if( !in_array('custom-fields',$this->post_properties['supports']) ){
// $this->post_properties['supports'][]='custom-fields';
// }
//capabilities
$reference = array(
'edit_post' => '',
'edit_posts' => '',
'edit_others_posts' => '',
'publish_posts' => '',
'read_post' => '',
'read_private_posts' => '',
'delete_post' => ''
);
$capabilities = array_filter(apply_filters('cf7_2_post_capabilities_'.$post_type, $reference));
$diff = array_diff_key($reference, $capabilities);
if( empty( $diff ) ) {
$this->post_properties['capabilities'] = $capabilities;
$this->post_properties['map_meta_cap'] = true;
}else{ //some keys are not set, so capabilities will not work
//set to defaul post capabilities
$this->post_properties['capability_type'] = 'post';
}
}
/**
* set custom properties from $_POST admin submission page.
* @since 5.0.0
*/
protected function set_post_properties(){
//initial supports, set the rest from the admin $_POST
$this->post_properties['supports'] = array('custom-fields' );
//make sure the arrays are initialised
$len_mapped_post = strlen('mapped_post_');
$properties = $this->get_mapped_fields('mapped_post_');
// debug_msg($properties, 'props ');
//properties of factory post
foreach($properties as $prop =>$value){
switch ($prop){
case 'menu_position': //properties with values.
case 'type':
case 'type_source':
case 'singular_name':
case 'plural_name':
case 'default':
case 'map':
$this->post_properties[$prop]=$value;
break;
default: //properties with boolean, unchked are blank and skipped.
$this->post_properties[$prop]=true;
break;
}
}
//let's save the properties if this is a factory mapping
if(isset($this->post_properties['default']) && $this->post_properties['default']){ //default mapping.
$form = get_post($this->cf7_post_ID);
$names = $name = $form->post_title;
if( 's'!= substr($names,-1) ) $names.='s';
$this->post_properties['singular_name']=$name;
$this->post_properties['plural_name']=$names;
$this->post_properties['type']=$this->cf7_key;
if(strlen($this->cf7_key) > 20){ //fix pho notice for post registration.
$this->post_properties['type']=substr($this->cf7_key,0,20);
}
$this->post_properties['default']=0;
}
//let's get the capabilities
$reference = array(
'edit_post' => '',
'edit_posts' => '',
'edit_others_posts' => '',
'publish_posts' => '',
'read_post' => '',
'read_private_posts' => '',
'delete_post' => ''
);
$capabilities = array_filter(apply_filters('cf7_2_post_capabilities_'.$this->post_properties['type'], $reference));
$diff=array_diff_key($reference, $capabilities);
if( empty( $diff ) ) {
$this->post_properties['capabilities'] = $capabilities;
$this->post_properties['map_meta_cap'] = true;
}else{ //some keys are not set, so capabilities will not work
//set to defaul post capabilities
$this->post_properties['capability_type'] = 'post';
}
//enable support for selected post fields.
$fields = $this->get_mapped_fields('cf7_2_post_map-');
foreach($fields as $cf7_field=>$post_field){
//keep track of custom post type support
switch($post_field){
case 'title':
case 'excerpt':
case 'author':
case 'thumbnail':
case 'editor':
$this->post_properties['supports'][]=$post_field;
break;
default:
break;
}
}
//set flush rules flag. @since 3.8.2.
if($this->get('public') || $this->get('publicly_queryable')){
$this->flush_permalink_rules = true;
}
return true;
}
/**
* Setup default mapping.
*
*@since 5.0.0
*@param Array $args array of key=>value arguments.
*/
public function init_default_mapping($args){
$this->post_properties['default']=1; //flag this as a default mapping.
if(isset($args['post'])){
foreach($args['post'] as $pf=>$ff) $this->post_map_fields[$ff]=$pf;
}
if(isset($args['meta'])){
foreach($args['meta'] as $pf=>$ff) $this->post_map_meta_fields[$ff]=$pf;
}
}
}