'menu-294', 'callback'=>'' ]*/]; function __construct() { // } private $newletters_form_id; private $newletters_field_name; private $newletters_table_name; private $newletters_table_label; private $newletters_post_type = ['post']; function prefill_form_by_url_params() { // add_action('wp_enqueue_scripts', function () { wp_enqueue_script( 'prefill_form_by_url_params', ADMINZ_DIR_URL . 'assets/js/wpcf7/prefill_form_by_url_params.js', [], ADMINZ_VERSION, true ); }); } function make_form_newletters($form_id, $field_name) { if (!$form_id) { return; } // prepare data $this->newletters_form_id = $form_id; $this->newletters_field_name = $field_name; $this->newletters_table_name = 'adminz_wpcf7_newletters'; $this->newletters_table_label = 'Adminz Wpcf7 newletters'; $this->newletters_post_type = apply_filters('adminz_newletters_post_type', $this->newletters_post_type); // prepare table \WpDatabaseHelperV2\Database\DbTable::make() ->name($this->newletters_table_name) ->title($this->newletters_table_label) ->fields([ // \WpDatabaseHelperV2\Database\DbColumn::make() ->name('id') ->type('INT(11)') ->notNull() ->autoIncrement() ->primary(), // \WpDatabaseHelperV2\Database\DbColumn::make() ->name('form_id') ->type('INT(11)') ->notNull(), // \WpDatabaseHelperV2\Database\DbColumn::make() ->name('email') ->type('varchar(255)') ->notNull(), // \WpDatabaseHelperV2\Database\DbColumn::make() ->name('created_at') ->type('DATETIME') ->notNull() ->default('CURRENT_TIMESTAMP'), ]) ->registerAdminPage() ->create(); // save submitted data add_action('wpcf7_mail_sent', function ($contact_form) { if ($contact_form->id() == $this->newletters_form_id) { $submission = \WPCF7_Submission::get_instance(); if ($submission) { $posted_data = $submission->get_posted_data(); if ($email = ($posted_data[$this->newletters_field_name] ?? '')) { global $wpdb; $table_name = $wpdb->prefix . $this->newletters_table_name; $wpdb->insert( $table_name, [ 'form_id' => $contact_form->id(), 'email' => $email, ] ); } } } }); // send email when submit new post add_action('transition_post_status', function ($new_status, $old_status, $post) { if ($new_status === 'publish' && $old_status !== 'publish' && $old_status !== 'trash') { // Kiểm tra nếu post type là một trong các loại newsletter if (in_array($post->post_type, $this->newletters_post_type)) { // Lấy thông tin bài viết $title = get_the_title($post->ID); $excerpt = wp_trim_words(get_the_excerpt($post->ID), 40); $categories = get_the_term_list($post->ID, 'category', '', ', ', ''); $publish_date = get_the_date(get_option('date_format'), $post->ID); $link = get_permalink($post->ID); // Tạo nội dung email dưới dạng HTML $text_title = __("Title"); $text_excerpt = __("Excerpt"); $text_category = __('Categories'); $text_date = __('Published'); $text_readmore = __('Read more'); $message = <<$text_title: $title
$text_excerpt: $excerpt
$text_category: $categories
$text_date: $publish_date
HTML; // Tiêu đề email $domain = parse_url(get_home_url(), PHP_URL_HOST); $subject = sprintf(__("There is a new article on the site %s", 'administrator-z'), $domain); // Headers của email $headers = array('Content-Type: text/html; charset=UTF-8'); // Các file đính kèm nếu có $attachments = []; // Lấy danh sách email từ bảng trong cơ sở dữ liệu global $wpdb; $table_name = $wpdb->prefix . $this->newletters_table_name; $sql = "SELECT DISTINCT email FROM $table_name"; $results = $wpdb->get_results($sql, ARRAY_A); if (!empty($results)) { // Lấy mảng các email $emails = wp_list_pluck($results, 'email'); $emails = array_unique($emails); foreach ((array) $emails as $key => $email) { $text_unregister = __('Unsubscribe', 'administrator-z'); $link_unregister = add_query_arg( [ 'adminz_newletters_unregister' => $email, 'token' => wp_create_nonce('adminz_unsubscribe_' . $email) ], get_site_url() ); $message .= <<$text_unregister HTML; wp_mail( $email, $subject, $message, $headers, $attachments ); } } } } }, 10, 3); // unregister add_action('init', function () { if ($email = ($_GET['adminz_newletters_unregister'] ?? '')) { if (!wp_verify_nonce($_GET['token'] ?? '', 'adminz_unsubscribe_' . $email)) { wp_die('Action failed. Invalid nonce.'); } global $wpdb; $table_name = $wpdb->prefix . $this->newletters_table_name; $sql = "DELETE FROM wp_adminz_wpcf7_newletters WHERE `wp_adminz_wpcf7_newletters`.`id` = 2"; $sql = $wpdb->prepare( "DELETE FROM $table_name WHERE email = %s", $email, ); $result = $wpdb->query($sql); if ($result) { wp_die(_x('Completed', 'request status')); } } }); } function make_form_tags() { if (!empty($this->form_tags) and is_array($this->form_tags)) { foreach ($this->form_tags as $key => $item) { add_filter('wpcf7_form_tag', function ($tag, $replace) use ($item) { if (in_array($tag['type'], ['select', 'select*', 'radio', 'checkbox', 'checkbox*'])) { if (isset($item['tag_name']) and $tag['name'] == $item['tag_name']) { $callback_return = call_user_func($item['callback']); if (!empty($callback_return) and is_array($callback_return)) { // reset array $tag['values'] = []; $tag['labels'] = []; if (in_array($tag['type'], ['select', 'select*'])) { // Option đầu tiên được lấy làm mặc định, nó ko có giá trị if (isset($tag['raw_values'][0])) { $default = $tag['raw_values'][0]; $tag['values'] = ['']; $tag['labels'] = [$default]; } } foreach ($callback_return as $key => $value) { $tag['values'][] = $key; $tag['labels'][] = $value; } if ($search = array_search('include_blank', $tag['options']) >= 0) { if ($search == true) { $search = 0; } unset($tag['options'][$search]); } } } } return $tag; }, 10, 2); } } } function save_submissions() { // create post type add_action('init', function () { $labels = array( 'name' => 'Wpcf7 Submit', 'singular_name' => 'Wpcf7 Submit', ); $args = array( 'labels' => $labels, 'public' => false, 'show_ui' => true, 'supports' => array('title', 'editor'), 'menu_icon' => 'dashicons-email', ); register_post_type('wpcf7_submission', $args); }); // create metabox add_action('add_meta_boxes', function () { add_meta_box( 'wpcf7_submission_data', 'Form Submission Data', function ($post) { $meta = get_post_meta($post->ID); echo '| Meta Key | Meta Value |
|---|---|
| ' . esc_html($key) . ' | '; echo '' . esc_html(implode(', ', $value)) . ' | '; echo '
HTML; }, ); return $panels; }, 10, 1); // Lưu dữ liệu từ tab "Thankyou Settings" add_action('wpcf7_save_contact_form', function ($contact_form) { $form_id = $contact_form->id(); if (isset($_POST['_enable_thankyou'])) { update_post_meta($form_id, '_enable_thankyou', 1); } else { update_post_meta($form_id, '_enable_thankyou', 0); } if (isset($_POST['_thankyou_style'])) { update_post_meta($form_id, '_thankyou_style', sanitize_text_field($_POST['_thankyou_style'])); } if (isset($_POST['_thankyou_content'])) { update_post_meta($form_id, '_thankyou_content', wp_unslash($_POST['_thankyou_content'])); } if (isset($_POST['_thankyou_redirect'])) { update_post_meta($form_id, '_thankyou_redirect', esc_url_raw(wp_unslash($_POST['_thankyou_redirect']))); } }); // footer add_action('wp_footer', function () { // Kiểm tra xem có phải là trang đơn (singular) không // if (!is_singular()) { // return; // } // Lấy tất cả các form CF7 trên trang $forms = \WPCF7_ContactForm::find(); if (empty($forms)) { return; } // Mảng lưu trữ nội dung "thankyou" của các form $thankyou_data = []; // Duyệt qua từng form foreach ($forms as $form) { $form_id = $form->id(); $enable_thankyou = get_post_meta($form_id, '_enable_thankyou', true); $thankyou_style = get_post_meta($form_id, '_thankyou_style', true); $thankyou_content = get_post_meta($form_id, '_thankyou_content', true); $thankyou_redirect = get_post_meta($form_id, '_thankyou_redirect', true); // Nếu không bật tính năng thankyou, bỏ qua if (!$enable_thankyou) { continue; } // Xử lý shortcode và giữ nguyên HTML $processed_content = do_shortcode(wp_kses_post($thankyou_content)); // Lưu nội dung "thankyou" vào mảng $thankyou_data[$form_id] = [ 'style' => $thankyou_style, 'content' => $processed_content, 'redirect' => $thankyou_redirect ]; } // Nếu không có form nào được kích hoạt tính năng thankyou, thoát if (empty($thankyou_data)) { return; } // Chuyển mảng PHP thành chuỗi JSON $thankyou_data_json = wp_json_encode($thankyou_data); // Tạo mã HTML một lần duy nhất echo <<