File: /www/wwwroot/www.sceybwg.com/wp-content/themes/Modular/extends/wpjam-form/public/form-submit.php
<?php
class WPJAM_Form_Submit extends WPJAM_Post_Comment{
public function api_submit($post_id, $args=[]){
$args['status'] = wpjam_get_post_parameter('status', ['default'=>'approve']);
$args['data'] = wpjam_get_post_parameter('data', ['required'=>true]);
$form = wpjam_get_post_object($post_id);
$result = wpjam_try([$form, 'is_submittable']);
$submit = wpjam_try([$form, 'submit'], $args);
return [
'submission' => $submit->parse_for_json(),
'count' => count($form->get_submissions()),
'times' => count($form->get_submissions('user'))
];
}
public function reject($id, $data){
return wpjam_set_comment_status($id, 'rejected', $data);
}
public function render_by_field($field, $value=null){
$value ??= $field['value'] ?? null;
if($field['type'] == 'img'){
$value = $value ? '<a class="thickbox" href="'.wpjam_get_thumbnail($value, 600).'"><img src="'.wpjam_get_thumbnail($value, 120, 120).'" width="60" /></a>' : '';
}else{
$value = is_array($value) ? implode(" ", $value) : $value;
$value = wp_strip_all_tags($value);
}
return $value;
}
public function render_item($item, &$attr=[]){
$item = parent::render_item($item, $attr);
$object = wpjam_get_comment_object($item['id']);
$fields = $item['content'] ? $object->get_unserialized() : [];
foreach($fields as $field){
if($field['type'] == 'mu-fields'){
$subs = $field['fields'] ?? [];
$value = $field['value'] ?? [];
$value = wpjam_map($value, fn($v)=> implode("<br />", wpjam_map($v, fn($sv, $sn)=> ($sub = array_find($subs, fn($sub)=> $sub['name'] == $sn)) ? ($sn." : ".$this->render_by_field($sub, $sv)) : $sv)));
$value = $value ? implode("<br /><br />", $value) : '';
}else{
$value = $this->render_by_field($field);
}
$item[md5($field['name'])] = $value;
}
if(str_contains($item['author_email'], 'anonymous.anonymous')){
$item['author'] = '不知名网友';
}
if($object->get_status() == 'rejected'){
$item['author'] .= '<p style="color:#a00;">拒绝理由:'.$object->reject_reason.'</p>';
}
return $item;
}
public function get_actions($post_id){
return wpjam_except(parent::get_actions($post_id), ['add', 'edit', 'reply', 'spam'])+[
'reject' => ['title'=>'拒绝', 'order'=>20, 'show_if'=>['approved', '=', 0], 'callback'=>[$this, 'reject']],
'export' => ['title'=>'导出', 'export'=>true, 'direct'=>true, 'overall'=>true, 'class'=>'button-primary', 'callback' => fn()=> wpjam_get_post_object($post_id)->export()],
];
}
public function get_fields($post_id, $action_key='', $id=0){
if($action_key == 'reject'){
return ['reject_reason'=>['title'=>'拒绝理由', 'type'=>'textarea']];
}elseif($action_key == ''){
$fields = parent::get_fields($post_id, $action_key, $id);
$fields = wpjam_except($fields, 'comment');
$form = wpjam_get_post_object($post_id);
if($form){
foreach($form->parse_fields([], true) as $field){
$key = md5($field['name']);
$column = $field['type'] == 'textarea' ? ['style'=>'min-width:200px;'] : ['nowrap'=>true];
$fields[$key] = wpjam_except($field, 'options')+['title'=>$field['name'], 'column'=>$column];
}
}
}
return $fields;
}
}
class WPJAM_Form_Submit_Source{
public static function get_fields($type){
if($type == 'meta'){
return [
'source_type' => ['title'=>'来源', 'type'=>'select', 'options'=>[], 'column'=>['nowrap'=>true], 'order'=>0],
'source_data' => ['title'=>'', 'type'=>'text']
];
}
}
public static function render_item($item){
$source_type = get_comment_meta($item['id'], 'source_type', true);
if($source_type){ //[''=>'默认', 'home'=>'首页', 'post'=>'文章', 'module'=>'页面']
$source_data = get_comment_meta($item['id'], 'source_data', true);
if($source_type == 'home'){
$item['source_type'] = '来自首页';
}elseif($source_type == 'post'){
$title = $source_data ? esc_html(get_the_title($source_data)) : '';
$label = wpjam_get_post_type_setting(get_post_type($source_data), 'title');
$item['source_type'] = '来自'.$label.':'."<br />".$title;
}elseif($source_type == 'module'){
$page_title = $source_data ? get_term($source_data, 'module_page')->name : '';
$item['source_type'] = '来自页面:'."<br />".$page_title;
}else{
$item['source_type'] = '';
}
}
return $item;
}
}