Current path: home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d5/server/Admin/Pages/
?? Go up: /home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d5/server/Admin
<?php
namespace KS_PAC_DCFH\Admin\Pages;
if (!defined('ABSPATH')) {
exit;
}
class CreatePost
{
private int $post_id;
public function load(): void
{
$page = isset($_GET['page']) ? sanitize_text_field($_GET['page']) : ''; // phpcs:ignore WordPress.Security.NonceVerification
$this->post_id = isset($_GET['post_id']) ? sanitize_key($_GET['post_id']) : 0; // phpcs:ignore WordPress.Security.NonceVerification
add_action('admin_menu', [$this, 'maybe_add_menu_item']);
if (empty($this->post_id) && $page === 'create_post') {
wp_safe_redirect(add_query_arg(['post_type' => 'pwh_dcfh'], admin_url('edit.php')));
exit();
}
}
public function maybe_add_menu_item(): void
{
add_submenu_page(
'edit.php?post_type=pwh_dcfh',
__('Create Post', 'divi-contact-form-helper'),
null,
'perform_entry_actions_pwh_dcfh',
'create_post',
[$this, 'process_form']
);
}
public function process_form(): void
{
$post_request = [
'clone_post_type' => '',
'post_status' => '',
'post_term' => '',
'field_mapping' => '',
'custom_field' => '',
'custom_featured_img' => '',
'custom_featured_img_url' => '',
'send_email_to_submitter' => '',
];
if (isset($_POST['_wpnonce']) && !wp_verify_nonce(sanitize_text_field($_POST['_wpnonce']), 'nonce_create_post')) {
wp_die(esc_html__('The security check failed due to an invalid nonce. Please try again.', 'divi-contact-form-helper'));
}
if (isset($_POST['btn_create_post'], $_POST['clone_post_type'])) {
$post_request = $_POST; // phpcs:ignore
$post_request['custom_featured_img_url'] = null;
$validate_request = true;
$clone_post_type = sanitize_text_field($_POST['clone_post_type']);
$post_status = isset($_POST['post_status']) ? sanitize_text_field($_POST['post_status']) : 'pending';
$post_term = isset($_POST['post_term']) ? sanitize_text_field($_POST['post_term']) : null;
$copy_as = $_POST['field_mapping'] ?? null; // phpcs:ignore
$custom_featured_img = isset($_POST['custom_featured_img']) ? sanitize_text_field($_POST['custom_featured_img']) : null;
$send_email_to_submitter = isset($_POST['send_email_to_submitter']) && sanitize_text_field($_POST['send_email_to_submitter']);
$email_to = '';
if (!empty($custom_featured_img)) {
$post_request['custom_featured_img_url'] = wp_get_attachment_url($custom_featured_img);
}
if (empty($clone_post_type)) {
$validate_request = false;
ks_pac_dcfh_app()->admin_alert()::error(__('Please select post type.', 'divi-contact-form-helper'));
}
$post_data = [];
$mapped_data = [];
// Mapping Title and Content AND Field As Meta
foreach ($copy_as as $copy_key => $copy_value) {
if (!empty($_POST['post_data'][$copy_key])) {
$post_data = $_POST['post_data'][$copy_key]; // phpcs:ignore
if ('title' === $copy_value || 'content' === $copy_value) {
$mapped_data[$copy_value] = $post_data;
}
}
if ((!empty($_POST['custom_field'][$copy_key])) && !is_array($copy_value)) {
$meta_key = ks_pac_dcfh_app()->misc_utils()::sanitize_key(sanitize_text_field($_POST['custom_field'][$copy_key]));
if ($copy_value === 'custom_field') {
$mapped_data['meta_input'][$meta_key] = $post_data;
}
}
}
if (empty($mapped_data)) {
$validate_request = false;
ks_pac_dcfh_app()->admin_alert()::error(__('Data is not mapped. Please contact administrator,', 'divi-contact-form-helper'));
}
if (!isset($mapped_data['title'], $mapped_data['content'])) {
$validate_request = false;
ks_pac_dcfh_app()->admin_alert()::error(__('Please map title and content of post.', 'divi-contact-form-helper'));
}
// Mapping File As Meta
foreach ($copy_as as $copy_key => $copy_value) {
if (is_array($copy_value)) {
foreach ($copy_value as $k => $v) {
$meta_key = ks_pac_dcfh_app()->misc_utils()::sanitize_key(sanitize_text_field($_POST['custom_field'][$copy_key][$k])); // phpcs:ignore
$attachment_dir = isset($_POST['post_data'][$copy_key.'_dir'][$k]) ? sanitize_text_field($_POST['post_data'][$copy_key.'_dir'][$k]) : null;
//$attachment_url = isset($_POST['post_data'][$copy_key.'_url'][$k])?sanitize_text_field($_POST['post_data'][$copy_key.'_url'][$k]):null;
$attachment_id = isset($_POST['post_data'][$copy_key.'_id'][$k]) ? sanitize_text_field($_POST['post_data'][$copy_key.'_id'][$k]) : null;
if (empty($attachment_id)) {
if ('custom_field' === $v && isset($_POST['custom_field'][$copy_key][$k])) {
$media_response = ks_pac_dcfh_app()->file_utils()::upload_files_to_media_library($attachment_dir, $mapped_data['title']);
$mapped_data['meta_input'][$meta_key] = $media_response['attachment_url'];
$mapped_data['attachment_ids'][] = $media_response['attachment_id'];
} elseif ('featured_image' === $v) {
$media_response = ks_pac_dcfh_app()->file_utils()::upload_files_to_media_library($attachment_dir, $mapped_data['title']);
$mapped_data['featured_image'] = $media_response['attachment_id'];
}
} else if ('custom_field' === $v && isset($_POST['custom_field'][$copy_key][$k])) {
$mapped_data['meta_input'][$meta_key] = wp_get_attachment_url($attachment_id);
$mapped_data['attachment_ids'][] = $attachment_id;
} elseif ('featured_image' === $v) {
$mapped_data['featured_image'] = $attachment_id;
}
}
}
}
if ($validate_request) {
$post_title = $mapped_data['title'];
$post_content = $mapped_data['content'];
$post_meta = $mapped_data['meta_input'] ?? null;
$post_attachment_ids = $mapped_data['attachment_ids'] ?? null;
$cloned_post_id = wp_insert_post([
'post_author' => get_current_user_id(),
'post_title' => $post_title,
'post_name' => $post_title,
'post_content' => $post_content,
'post_excerpt' => wp_trim_words(wp_trim_excerpt($post_content), 25),
'post_status' => $post_status,
'post_type' => $clone_post_type,
'comment_status' => 'closed',
'ping_status' => 'closed',
'meta_input' => $post_meta,
], true);
if (!is_wp_error($cloned_post_id)) {
// Term
if (!empty($post_term)) {
$term_id = (int)$post_term;
$term = get_term($term_id);
wp_set_post_terms($cloned_post_id, $term_id, $term->taxonomy);
}
// Clone History
ks_pac_dcfh_app()->entry_meta_manager()::update_cloned_history($this->post_id, $cloned_post_id);
if ($send_email_to_submitter && 'publish' === $post_status) {
ks_pac_dcfh_app()->notification_dispatcher()->entry_clone_notification($this->post_id, $cloned_post_id);
}
// Featured Image
if ($custom_featured_img > 0) {
set_post_thumbnail($cloned_post_id, $custom_featured_img);
}
if (empty($custom_featured_img) && (!empty($mapped_data['featured_image']))) {
$featured_img_id = $mapped_data['featured_image'];
set_post_thumbnail($cloned_post_id, $featured_img_id);
wp_update_post([
'ID' => $featured_img_id,
'post_parent' => $cloned_post_id
]);
}
// Update Attachment ID To Post
if (!empty($post_attachment_ids)) {
foreach ($post_attachment_ids as $post_attachment_id) {
wp_update_post([
'ID' => $post_attachment_id,
'post_parent' => $cloned_post_id
]);
}
}
// Redirect
wp_safe_redirect(add_query_arg([
'post_type' => 'pwh_dcfh',
'post' => $this->post_id,
'success' => true,
'action' => 'edit',
'_wpnonce' => wp_create_nonce('nonce_admin_notice'),
'cloned_post_id' => $cloned_post_id,
'email_sent_to' => $email_to,
], admin_url('post.php')));
exit();
}
ks_pac_dcfh_app()->admin_alert()::error(__('Something went wrong. Please contact ', 'divi-contact-form-helper'));
}
}
$this->display_form($post_request);
}
public function display_form($post_request): void
{
$post_obj = get_post($this->post_id);
if (is_serialized($post_obj->post_excerpt)) {
$form_entries = ks_pac_dcfh_app()->misc_utils()::maybe_unserialize($post_obj->post_excerpt);
if (!empty($form_entries) && is_array($form_entries)) { ?>
<div class="wrap dcfh__page" id="page__create_post">
<h1 class="wp-heading-inline"><?php esc_html_e('Create Post', 'divi-contact-form-helper'); ?></h1>
<a href="<?php echo esc_url(add_query_arg(['post_type' => 'pwh_dcfh'], admin_url('edit.php'))); ?>" class="page-title-action"><?php esc_html_e('Entries',
'divi-contact-form-helper'); ?></a>
<hr class="wp-header-end">
<?php ks_pac_dcfh_app()->admin_alert()::show_message(); ?>
<span class="spinner"></span>
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-2">
<div class="postbox-container">
<div class="postbox">
<div class="inside">
<form method="POST" autocomplete="off" enctype="application/x-www-form-urlencoded">
<table class="form-table post-selection">
<tbody>
<tr>
<th><?php esc_html_e('Type:', 'divi-contact-form-helper'); ?>
<span class="required">*</span></th>
<td>
<select title="" class="form-control" name="clone_post_type" id="clone_post_type" required>;
<option value=""><?php esc_html_e('Please Select', 'divi-contact-form-helper'); ?></option>
<?php foreach (ks_pac_dcfh_app()->misc_utils()::get_post_types() as $post_key => $posts_type) {
$posts_type = ucwords($posts_type); ?>
<option value="<?php echo esc_html($post_key); ?>" <?php selected($post_key, esc_html($post_request['clone_post_type'])); ?>><?php echo esc_html($posts_type); ?></option>";
<?php } ?>
</select>
</td>
<th><?php esc_html_e('Status:', 'divi-contact-form-helper'); ?></th>
<td>
<select title="" class="form-control" name="post_status" id="post_status">;
<?php foreach (array_reverse(get_post_statuses()) as $status_key => $status) {
$status = ucwords($status); ?>
<option value="<?php echo esc_html($status_key); ?>" <?php selected($status_key, esc_html($post_request['post_status'])); ?>><?php echo esc_html($status); ?></option>";
<?php } ?>
</select>
</td>
<th><?php esc_html_e('Term:', 'divi-contact-form-helper'); ?></th>
<td>
<select title="" class="form-control" name="post_term" id="post_term" disabled>;
<option value=""><?php esc_html_e('Please Select', 'divi-contact-form-helper'); ?></option>
</select>
</td>
</tr>
</tbody>
</table>
<table class="form-table form-data">
<thead class="d-none">
<tr>
<th><?php esc_html_e('Field', 'divi-contact-form-helper'); ?></th>
<th><?php esc_html_e('Value', 'divi-contact-form-helper'); ?></th>
<th><?php esc_html_e('Type', 'divi-contact-form-helper'); ?></th>
<th><?php esc_html_e('Custom Field', 'divi-contact-form-helper'); ?></th>
</tr>
</thead>
<tbody>
<?php
$row_id = 0;
foreach ($form_entries as $form_key => $form_entry) {
$row_id++;
$field_label = esc_html($form_entry['label']);
$field_type = esc_html($form_entry['type']);
$post_data = $post_request['post_data'][$form_key] ?? esc_html($form_entry['value']);
$copy_as = $post_request['field_mapping'][$form_key] ?? null;
$custom_field = $post_request['custom_field'][$form_key] ?? null;
$readonly = empty($custom_field) ? 'readonly' : null;
$row_id = md5($row_id.$form_key.uniqid('', true));
if ('text' !== $field_type && 'file' !== $field_type && 'signature-pad' !== $field_type && 'signature_pad' !== $field_type) {
?>
<tr>
<th><?php echo esc_html("$field_label:"); ?></th>
<td>
<input title="" type="text" name="post_data[<?php echo esc_attr($form_key); ?>]" id="<?php echo esc_attr('post_data_'.$row_id); ?>" class="regular-text" value="<?php echo esc_attr($post_data); ?>">
</td>
<td>
<select title="" class="form-control field-mapping" name="field_mapping[<?php echo esc_attr($form_key); ?>]" id="<?php echo esc_attr("copy_as_$row_id"); ?>" data-row="<?php echo esc_attr($row_id); ?>">
<?php foreach ($this->clone_options() as $key => $type) { ?>
<option value="<?php echo esc_attr($key); ?>" <?php selected($key, $copy_as); ?>><?php echo esc_html($type); ?></option>
<?php } ?>
</select>
</td>
<td>
<input title="" type="text" class="form-control" name="custom_field[<?php echo esc_attr($form_key); ?>]" id="<?php echo esc_attr($row_id); ?>" placeholder="<?php esc_html_e('Choose Custom Field', 'divi-contact-form-helper'); ?>" value="<?php echo esc_html($custom_field); ?>" <?php echo esc_attr($readonly); ?>>
</td>
</tr>
<?php } elseif ('file' === $field_type || 'signature-pad' === $field_type || 'signature_pad' === $field_type) {
$wp_upload_dir = wp_upload_dir();
$wp_subdir = isset($form_entry['subdir']) ? esc_html($form_entry['subdir']) : '';
$wp_basedir = rtrim($wp_upload_dir['basedir'], '/').'/'.$wp_subdir;
$wp_baseurl = rtrim($wp_upload_dir['baseurl'], '/').'/'.$wp_subdir;
$attachments = is_array($post_data) ? $post_data : explode(',', $post_data);
if (!empty($attachments) && is_array($attachments) && !empty($wp_subdir)) {
$row_id = 0;
$contact_form_id = ks_pac_dcfh_app()->entry_meta_manager()::get_contact_form_id_meta_value($this->post_id);
foreach ($attachments as $attachment) {
$row_id++;
$row_id = md5($row_id.$form_key.uniqid('', true));
if (isset($form_entry['is_media_library']) && $form_entry['is_media_library']) {
$upload_dir = path_join($wp_basedir, $attachment);
$upload_url = path_join($wp_baseurl, $attachment);
$upload_id = ks_pac_dcfh_app()->general_db_manager()::get_attachment_id_by_name($attachment);
} else {
$upload_dir = ks_pac_dcfh_app()->file_utils()::get_form_upload_dir($contact_form_id, $wp_subdir, $attachment);
$upload_url = ks_pac_dcfh_app()->file_utils()::get_form_upload_url($contact_form_id, $wp_subdir, $attachment);
$upload_id = '';
}
if (et_()->WPFS()->is_file($upload_dir) && et_()->WPFS()->exists($upload_dir)) { ?>
<tr>
<th><?php echo esc_html("$field_label:"); ?></th>
<td>
<?php echo sprintf('<a href="%1$s" target="_blank">%2$s</a>', esc_url($upload_url), esc_html($attachment)); ?>
<input type="hidden" name="post_data[<?php echo esc_attr($form_key); ?>_dir][<?php echo esc_attr($row_id); ?>]" value="<?php echo esc_attr($upload_dir); ?>">
<input type="hidden" name="post_data[<?php echo esc_attr($form_key); ?>_url][<?php echo esc_attr($row_id); ?>]" value="<?php echo esc_attr($upload_url); ?>">
<?php if (!empty($upload_id)) { ?>
<input type="hidden" name="post_data[<?php echo esc_attr($form_key); ?>_id][<?php echo esc_attr($row_id); ?>]" value="<?php echo esc_attr($upload_id); ?>">
<?php } ?>
</td>
<td>
<select title="" class="form-control field-mapping" name="field_mapping[<?php echo esc_attr($form_key); ?>][<?php echo esc_attr($row_id); ?>]" data-row="<?php echo esc_attr($row_id); ?>">
<?php foreach ($this->clone_options() as $key => $type) { ?>
<option value="<?php echo esc_html($key); ?>" <?php selected($key, $copy_as[$row_id] ?? ''); ?>><?php echo esc_html($type); ?></option>
<?php } ?>
</select>
</td>
<td>
<input title="" type="text" class="form-control" name="custom_field[<?php echo esc_attr($form_key); ?>][<?php echo esc_attr($row_id); ?>]" id="<?php echo esc_attr($row_id); ?>" placeholder="<?php esc_html_e('Choose Custom Field', 'divi-contact-form-helper'); ?>" value="<?php echo esc_html($custom_field[$row_id] ?? ''); ?>" <?php echo esc_attr($readonly); ?>>
</td>
</tr>
<?php
}
}
}
} else { ?>
<tr>
<th><?php echo esc_html($field_label); ?></th>
<td colspan="2">
<select title="" class="form-control field-mapping" name="field_mapping[<?php echo esc_attr($form_key); ?>]" id="<?php echo esc_attr("copy_as_$row_id"); ?>" data-row="<?php echo esc_attr($row_id); ?>">
<?php foreach ($this->clone_options() as $key => $type) {
$selected = $post_request['field_mapping'][$form_key] ?? 'content'; ?>
<option value="<?php echo esc_html($key); ?>" <?php selected($key, $selected); ?>><?php echo esc_html($type); ?></option>
<?php } ?>
</select>
</td>
<td>
<input title="" type="text" class="form-control" name="custom_field[<?php echo esc_attr($form_key); ?>]" id="<?php echo esc_attr($row_id); ?>" placeholder="<?php esc_html_e('Choose Custom Field', 'divi-contact-form-helper'); ?>" readonly>
</td>
</tr>
<tr>
<td colspan="4">
<?php wp_editor($post_data, 'post_data', [
'wpautop' => true,
'textarea_name' => "post_data[$form_key]",
'textarea_rows' => 10
]);
?>
</td>
</tr>
<?php }
} ?>
</tbody>
</table>
<div class="media">
<div class="featured__container" <?php echo empty($post_request['custom_featured_img_url']) ? ' style="display: none"' : ' style="display: block"'; // phpcs:ignore ?>>
<img src="<?php echo esc_url($post_request['custom_featured_img_url']); ?>" id="featured_image_url" alt="">
<a href="javascript:void(0)" id="btn_remove_featured_image"><?php esc_html_e('Remove', 'divi-contact-form-helper'); ?></a>
</div>
<a href="javascript:void(0)" class="button-secondary" id="btn_upload_featured_image"><?php esc_html_e('Featured Image',
'divi-contact-form-helper'); ?></a>
<input type="hidden" name="custom_featured_img" id="featured_image_id" value="<?php echo esc_html($post_request['custom_featured_img']); ?>"/>
</div>
<?php if (!empty(ks_pac_dcfh_app()->user_manager()::get_post_user_email($this->post_id))) { ?>
<div class="send-email-to-submitter">
<label class="bulk-select-button" for="send_email_to_submitter">
<input type="checkbox" id="send_email_to_submitter" name="send_email_to_submitter" class="bulk-select-switcher" value="1">
<span class="bulk-select-button-label"><?php esc_html_e('Send Email To Submitter', 'divi-contact-form-helper'); ?></span>
</label>
</div>
<?php }
wp_nonce_field('nonce_create_post');
submit_button(__('Create Post', 'divi-contact-form-helper'), 'primary large', 'btn_create_post');
?>
</form>
</div>
</div>
</div>
<div id="postbox-container-1" class="postbox-container">
<?php
require_once dirname(__DIR__)."/template/widget/documentation-widget.php";
require_once dirname(__DIR__)."/template/widget/support-widget.php";
?>
</div>
</div>
<br class="clear">
</div>
</div>
<?php
}
}
}
private function clone_options(): array
{
return [
'' => __('Please Select', 'divi-contact-form-helper'),
'title' => __('Title', 'divi-contact-form-helper'),
'content' => __('Content', 'divi-contact-form-helper'),
'featured_image' => __('Featured Image', 'divi-contact-form-helper'),
'custom_field' => __('Custom Field', 'divi-contact-form-helper'),
];
}
}