Я работаю над расширением API WordPress Rest для контактной формы 7, но я не уверен, как структурировать их почтовые объекты в моей схеме JSON. Для любого, кто использовал плагин 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- и mail-2 к mail-2, чтобы значения post не переопределялись при попытке отредактировать любой объект. Это лучший способ справиться с этой ситуацией?
Код, который в настоящее время использует схему:
prepare_item_for_response () код:
(Возвращенные значения ключа $ data [‘properties’] в настоящее время не отображаются в определенной схеме, я предполагаю, что это нужно исправить? $ 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;
}
Задача ещё не решена.
Других решений пока нет …