我正在为Contact Form 7的WordPress Rest API扩展工作,但我不确定如何在我的JSON Schema中构建他们的邮件对象。对于使用过Contact Form 7插件的任何人,您可能已经注意到他们有常规邮件对象(您可以在其中添加表单值,收件人等),并且他们有一个mail_2对象,供想要发送副本的人使用。给最终用户的电子邮件。
mail和mail_2这两个对象提供的属性与您在下面的架构中看到的相同:
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'form',
'type' => 'object',
'properties' => array(
'id' => array(
'description' => __( 'Unique identifier for the form.' ),
'type' => 'integer',
'context' => array( 'embed', 'view' ),
'readonly' => true,
),
'title' => array(
'description' => __( 'Display name for the form.' ),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => 'sanitize_text_field',
),
),
'name' => array(
'description' => __( 'An alphanumeric identifier for the object unique to its type.' ),
'type' => 'string',
'context' => array( 'embed', 'view' ),
'arg_options' => array(
'sanitize_callback' => 'sanitize_text_field',
),
),
'locale' => array(
'description' => __( 'The locale setting of the form' ),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => 'sanitize_text_field',
),
),
'properties' => array(
'description' => __('The properties of the form'),
'type' => 'object',
'context' => array( 'embed', 'view', 'edit' ),
'properties' => array(
'form' => array(
'description' => __('The content for the form'),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail' => array(
'type' => 'object',
'context' => array( 'embed', 'view', 'edit' ),
'properties' => array(
'mail-subject' => array(
'description' => __('Subject of the form'),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-sender' => array(
'description' => __('Sending email address of the form'),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-recipient' => array(
'description' => __('Recipient email address of the form'),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-additional-headers' => array(
'description' => __('Additional form headers'),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-attachment' => array(
'description' => __('Attachments to include with form'),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-use-html' => array(
'description' => __('Send emails using HTML formatting'),
'type' => 'boolean',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-exclude-blank' => array(
'description' => __('Exclude lines with blank mail-tags from output'),
'type' => 'boolean',
'context' => array( 'embed', 'view', 'edit' ),
),
),
),
'mail-2' => array(
'type' => 'object',
'context' => array( 'embed', 'view', 'edit' ),
'properties' => array(
'mail-2-subject' => array(
'description' => __('Subject of the form'),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-2-sender' => array(
'description' => __('Sending email address of the form'),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-2-recipient' => array(
'description' => __('Recipient email address of the form'),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-2-additional-headers' => array(
'description' => __('Additional form headers'),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-2-attachment' => array(
'description' => __('Files to attach to the form'),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-2-use-html' => array(
'description' => __('Send emails using HTML formatting'),
'type' => 'boolean',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-2-exclude-blank' => array(
'description' => __('Exclude lines with blank mail-tags from output'),
'type' => 'boolean',
'context' => array( 'embed', 'view', 'edit' ),
),
),
),
),
),
)
);
目前,我使用mail-2附加mail-mail-2属性的邮件属性,以便在尝试编辑任一对象时不会覆盖帖子值。这是处理这种情况的最佳方法吗?
当前使用架构的代码:
prepare_item_for_response()代码: (返回的$ data ['属性']键值当前未映射到已定义的架构,我假设需要修复此问题?$ data [' properties']容纳邮件对象)
/**
* The returned $data['properties'] values do not currently conform to the defined schema
*/
public function prepare_item_for_response($form, $request)
{
$data = array();
$data['id'] = $form->id();
$data['name'] = $form->name();
$data['title'] = $form->title();
$data['locale'] = $form->locale;
$data['properties'] = $form->get_properties();
$data = $this->add_additional_fields_to_object($data, $request);
$response = rest_ensure_response($data);
$response = apply_filters('rest_api_prepare_wpcf7', $response, $form, $request);
return $response;
}
prepare_item_for_database()代码:
protected function prepare_item_for_database($request) {
$id = $request->get_param('id');
$prepared_form = WPCF7_ContactForm::get_instance( $id );
/**
* update_item ensures the form ID exists before calling this method to prevent non-existant forms from being created accidentally.
*/
if(empty($prepared_form)) {
$prepared_form = WPCF7_ContactForm::get_template();
}
if ( isset( $request['post-title'] ) ) {
$prepared_form->set_title( $request['post-title'] );
}
if ( isset( $request['locale'] ) ) {
$locale = trim( $request['locale'] );
if ( wpcf7_is_valid_locale( $locale ) ) {
$prepared_form->set_locale($locale);
}
}
$properties = $prepared_form->get_properties();
if ( isset( $request['form'] ) ) {
$properties['form'] = trim( $request['form'] );
}
$mail = $properties['mail'];
if ( isset( $request['mail-subject'] ) ) {
$mail['subject'] = trim( $request['mail-subject'] );
}
if ( isset( $request['mail-sender'] ) ) {
$mail['sender'] = trim( $request['mail-sender'] );
}
if ( isset( $request['mail-body'] ) ) {
$mail['body'] = trim( $request['mail-body'] );
}
if ( isset( $request['mail-recipient'] ) ) {
$mail['recipient'] = trim( $request['mail-recipient'] );
}
if ( isset( $request['mail-additional-headers'] ) ) {
$headers = '';
$tempheaders = str_replace(
"\r\n", "\n", $request['mail-additional-headers'] );
$tempheaders = explode( "\n", $tempheaders );
foreach ( $tempheaders as $header ) {
$header = trim( $header );
if ( '' !== $header ) {
$headers .= $header . "\n";
}
}
$mail['additional_headers'] = trim( $headers );
}
if ( isset( $request['mail-attachments'] ) ) {
$mail['attachments'] = trim( $request['mail-attachments'] );
}
$mail['use_html'] = ! empty( $request['mail-use-html'] );
$mail['exclude_blank'] = ! empty( $request['mail-exclude-blank'] );
$properties['mail'] = $mail;
$mail_2 = $properties['mail_2'];
$mail_2['active'] = ! empty( $request['mail-2-active'] );
if ( isset( $request['mail-2-subject'] ) ) {
$mail_2['subject'] = trim( $request['mail-2-subject'] );
}
if ( isset( $request['mail-2-sender'] ) ) {
$mail_2['sender'] = trim( $request['mail-2-sender'] );
}
if ( isset( $request['mail-2-body'] ) ) {
$mail_2['body'] = trim( $request['mail-2-body'] );
}
if ( isset( $request['mail-2-recipient'] ) ) {
$mail_2['recipient'] = trim( $request['mail-2-recipient'] );
}
if ( isset( $request['mail-2-additional-headers'] ) ) {
$headers = '';
$tempheaders = str_replace(
"\r\n", "\n", $request['mail-2-additional-headers'] );
$tempheaders = explode( "\n", $tempheaders );
foreach ( $tempheaders as $header ) {
$header = trim( $header );
if ( '' !== $header ) {
$headers .= $header . "\n";
}
}
$mail_2['additional_headers'] = trim( $headers );
}
if ( isset( $request['mail-2-attachments'] ) ) {
$mail_2['attachments'] = trim( $request['mail-2-attachments'] );
}
$mail_2['use_html'] = ! empty( $request['mail-2-use-html'] );
$mail_2['exclude_blank'] = ! empty( $request['mail-2-exclude-blank'] );
$properties['mail_2'] = $mail_2;
// For setting/updating confirmation/error messages
foreach ( wpcf7_messages() as $key => $arr ) {
$field_name = 'message-' . strtr( $key, '_', '-' );
if ( isset( $request[$field_name] ) ) {
$properties['messages'][$key] = trim( $request[$field_name] );
}
}
if ( isset( $request['additional-settings'] ) ) {
$properties['additional_settings'] = trim( $request['additional-settings'] );
}
$prepared_form->set_properties( $properties );
//Preceded original with rest_api_
do_action( 'rest_api_wpcf7_save_contact_form', $prepared_form );
if ( wpcf7_validate_configuration() ) {
$config_validator = new WPCF7_ConfigValidator( $prepared_form );
$config_validator->validate();
}
return $prepared_form;
}