Task_12281 (#1)
Co-authored-by: User A0264400 <a0264400@jarl.from.sh> Reviewed-on: #1 Co-authored-by: Anton.AE <ab.an.ev@yandex.ru> Co-committed-by: Anton.AE <ab.an.ev@yandex.ru>
This commit is contained in:
@@ -400,7 +400,7 @@ function twentytwentyfour_shop_template( $template ) {
|
||||
add_filter( 'template_include', 'twentytwentyfour_shop_template', 20 );
|
||||
|
||||
/**
|
||||
* Use a standalone PHP template for the posts page (/blog).
|
||||
* Force the custom blog index template for the posts page.
|
||||
*
|
||||
* @since Twenty Twenty-Four 1.0
|
||||
*
|
||||
@@ -420,17 +420,17 @@ function twentytwentyfour_blog_home_template( $template ) {
|
||||
|
||||
return $template;
|
||||
}
|
||||
add_filter( 'template_include', 'twentytwentyfour_blog_home_template', 30 );
|
||||
add_filter( 'template_include', 'twentytwentyfour_blog_home_template', 25 );
|
||||
|
||||
/**
|
||||
* Use a standalone PHP template for single blog posts.
|
||||
* Force the custom single post template.
|
||||
*
|
||||
* @since Twenty Twenty-Four 1.0
|
||||
*
|
||||
* @param string $template Resolved template path.
|
||||
* @return string
|
||||
*/
|
||||
function twentytwentyfour_blog_single_template( $template ) {
|
||||
function twentytwentyfour_single_post_template( $template ) {
|
||||
if ( ! is_singular( 'post' ) ) {
|
||||
return $template;
|
||||
}
|
||||
@@ -443,7 +443,7 @@ function twentytwentyfour_blog_single_template( $template ) {
|
||||
|
||||
return $template;
|
||||
}
|
||||
add_filter( 'template_include', 'twentytwentyfour_blog_single_template', 30 );
|
||||
add_filter( 'template_include', 'twentytwentyfour_single_post_template', 25 );
|
||||
|
||||
/**
|
||||
* Enqueue standalone landing assets for the page slug "test1".
|
||||
@@ -458,8 +458,9 @@ function twentytwentyfour_test1_assets() {
|
||||
return;
|
||||
}
|
||||
|
||||
$css_file = ABSPATH . 'index3.css';
|
||||
$js_file = ABSPATH . 'index3.js';
|
||||
$landing_css_file = get_theme_file_path( 'assets/css/test1-landing.css' );
|
||||
$landing_js_file = get_theme_file_path( 'assets/js/test1-landing.js' );
|
||||
$landing_fonts_file = get_theme_file_path( 'assets/css/fonts-manrope.css' );
|
||||
|
||||
if ( $use_test1_assets ) {
|
||||
wp_dequeue_style( 'global-styles' );
|
||||
@@ -468,42 +469,43 @@ function twentytwentyfour_test1_assets() {
|
||||
wp_dequeue_style( 'classic-theme-styles' );
|
||||
}
|
||||
|
||||
wp_enqueue_style(
|
||||
'twentytwentyfour-test1-fonts',
|
||||
'https://fonts.googleapis.com/css2?family=Manrope:wght@400;500;700;800&display=swap',
|
||||
array(),
|
||||
null
|
||||
);
|
||||
|
||||
if ( file_exists( $css_file ) ) {
|
||||
if ( file_exists( $landing_fonts_file ) ) {
|
||||
wp_enqueue_style(
|
||||
'twentytwentyfour-test1-style',
|
||||
home_url( '/index3.css' ),
|
||||
array( 'twentytwentyfour-test1-fonts' ),
|
||||
(string) filemtime( $css_file )
|
||||
'twentytwentyfour-test1-fonts',
|
||||
get_theme_file_uri( 'assets/css/fonts-manrope.css' ),
|
||||
array(),
|
||||
(string) filemtime( $landing_fonts_file )
|
||||
);
|
||||
}
|
||||
|
||||
if ( ( is_front_page() || is_page( 'test1' ) ) && file_exists( $js_file ) ) {
|
||||
if ( file_exists( $landing_css_file ) ) {
|
||||
wp_enqueue_style(
|
||||
'twentytwentyfour-test1-style',
|
||||
get_theme_file_uri( 'assets/css/test1-landing.css' ),
|
||||
array( 'twentytwentyfour-test1-fonts' ),
|
||||
(string) filemtime( $landing_css_file )
|
||||
);
|
||||
}
|
||||
|
||||
if ( ( is_front_page() || is_page( 'test1' ) ) && file_exists( $landing_js_file ) ) {
|
||||
wp_enqueue_script(
|
||||
'twentytwentyfour-test1-script',
|
||||
home_url( '/index3.js' ),
|
||||
get_theme_file_uri( 'assets/js/test1-landing.js' ),
|
||||
array(),
|
||||
(string) filemtime( $js_file ),
|
||||
(string) filemtime( $landing_js_file ),
|
||||
true
|
||||
);
|
||||
|
||||
if ( class_exists( 'WooCommerce' ) ) {
|
||||
wp_localize_script(
|
||||
'twentytwentyfour-test1-script',
|
||||
'test1WooCommerce',
|
||||
array(
|
||||
'addToCartUrl' => WC_AJAX::get_endpoint( 'add_to_cart' ),
|
||||
'cartUrl' => wc_get_cart_url(),
|
||||
'checkoutUrl' => wc_get_checkout_url(),
|
||||
)
|
||||
);
|
||||
}
|
||||
wp_localize_script(
|
||||
'twentytwentyfour-test1-script',
|
||||
'test1LeadForm',
|
||||
array(
|
||||
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
|
||||
'action' => 'twentytwentyfour_submit_lead',
|
||||
'nonce' => wp_create_nonce( 'twentytwentyfour_submit_lead' ),
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
if ( is_page( 'faq' ) ) {
|
||||
@@ -531,12 +533,251 @@ function twentytwentyfour_test1_assets() {
|
||||
(string) filemtime( $contacts_js_file ),
|
||||
true
|
||||
);
|
||||
|
||||
wp_localize_script(
|
||||
'twentytwentyfour-test1-contacts-script',
|
||||
'test1ContactsForm',
|
||||
array(
|
||||
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
|
||||
'action' => 'twentytwentyfour_submit_lead',
|
||||
'nonce' => wp_create_nonce( 'twentytwentyfour_submit_lead' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
add_action( 'wp_enqueue_scripts', 'twentytwentyfour_test1_assets', 100 );
|
||||
|
||||
/**
|
||||
* Trim non-critical WooCommerce frontend assets on the custom homepage.
|
||||
*
|
||||
* @since Twenty Twenty-Four 1.0
|
||||
* @return void
|
||||
*/
|
||||
function twentytwentyfour_optimize_front_page_assets() {
|
||||
if ( is_admin() || ! is_front_page() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$style_handles = array(
|
||||
'woocommerce-layout',
|
||||
'woocommerce-smallscreen',
|
||||
'woocommerce-general',
|
||||
'woocommerce-blocktheme',
|
||||
'wc-blocks-style',
|
||||
);
|
||||
|
||||
foreach ( $style_handles as $handle ) {
|
||||
wp_dequeue_style( $handle );
|
||||
}
|
||||
|
||||
wp_deregister_script( 'jquery-migrate' );
|
||||
|
||||
global $wp_scripts;
|
||||
|
||||
if ( $wp_scripts instanceof WP_Scripts && isset( $wp_scripts->registered['jquery'] ) ) {
|
||||
$wp_scripts->registered['jquery']->deps = array_diff( $wp_scripts->registered['jquery']->deps, array( 'jquery-migrate' ) );
|
||||
}
|
||||
}
|
||||
add_action( 'wp_enqueue_scripts', 'twentytwentyfour_optimize_front_page_assets', 120 );
|
||||
|
||||
/**
|
||||
* Send a formatted message to Telegram.
|
||||
*
|
||||
* Configure credentials in wp-config.php:
|
||||
* define( 'TWENTYTWENTYFOUR_TELEGRAM_BOT_TOKEN', '...' );
|
||||
* define( 'TWENTYTWENTYFOUR_TELEGRAM_CHAT_ID', '...' );
|
||||
*
|
||||
* @param string $title Message title.
|
||||
* @param array $fields Key-value pairs for message rows.
|
||||
* @return bool
|
||||
*/
|
||||
function twentytwentyfour_send_telegram_message( $title, $fields ) {
|
||||
$token = defined( 'TWENTYTWENTYFOUR_TELEGRAM_BOT_TOKEN' ) ? (string) constant( 'TWENTYTWENTYFOUR_TELEGRAM_BOT_TOKEN' ) : '';
|
||||
$chat_id = defined( 'TWENTYTWENTYFOUR_TELEGRAM_CHAT_ID' ) ? (string) constant( 'TWENTYTWENTYFOUR_TELEGRAM_CHAT_ID' ) : '';
|
||||
|
||||
if ( '' === trim( $token ) || '' === trim( $chat_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$rows = array();
|
||||
$rows[] = '<b>' . esc_html( wp_strip_all_tags( (string) $title ) ) . '</b>';
|
||||
|
||||
foreach ( $fields as $label => $value ) {
|
||||
$clean_value = trim( wp_strip_all_tags( (string) $value ) );
|
||||
|
||||
if ( '' === $clean_value ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$rows[] = '<b>' . esc_html( wp_strip_all_tags( (string) $label ) ) . ':</b> ' . esc_html( $clean_value );
|
||||
}
|
||||
|
||||
$response = wp_remote_post(
|
||||
'https://api.telegram.org/bot' . rawurlencode( $token ) . '/sendMessage',
|
||||
array(
|
||||
'timeout' => 12,
|
||||
'body' => array(
|
||||
'chat_id' => $chat_id,
|
||||
'parse_mode' => 'HTML',
|
||||
'text' => implode( "\n", $rows ),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$status_code = (int) wp_remote_retrieve_response_code( $response );
|
||||
|
||||
return $status_code >= 200 && $status_code < 300;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle AJAX lead submissions from landing and contacts forms.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function twentytwentyfour_submit_lead_ajax() {
|
||||
if ( ! check_ajax_referer( 'twentytwentyfour_submit_lead', 'nonce', false ) ) {
|
||||
wp_send_json_error( array( 'message' => __( 'Ошибка проверки формы. Обновите страницу и попробуйте снова.', 'twentytwentyfour' ) ), 403 );
|
||||
}
|
||||
|
||||
$form_type = isset( $_POST['form_type'] ) ? sanitize_key( wp_unslash( $_POST['form_type'] ) ) : 'contacts_page';
|
||||
$page_url = isset( $_POST['page_url'] ) ? esc_url_raw( wp_unslash( $_POST['page_url'] ) ) : '';
|
||||
|
||||
if ( 'contacts' === $form_type ) {
|
||||
$form_type = false !== strpos( $page_url, '/contacts' ) ? 'contacts_page' : 'landing_contacts';
|
||||
}
|
||||
|
||||
if ( 'landing_order' === $form_type ) {
|
||||
$name = isset( $_POST['customerName'] ) ? sanitize_text_field( wp_unslash( $_POST['customerName'] ) ) : '';
|
||||
$phone = isset( $_POST['customerPhone'] ) ? sanitize_text_field( wp_unslash( $_POST['customerPhone'] ) ) : '';
|
||||
$address = isset( $_POST['customerAddress'] ) ? sanitize_text_field( wp_unslash( $_POST['customerAddress'] ) ) : '';
|
||||
$type = isset( $_POST['customerType'] ) ? sanitize_text_field( wp_unslash( $_POST['customerType'] ) ) : '';
|
||||
$comment = isset( $_POST['customerOrder'] ) ? sanitize_textarea_field( wp_unslash( $_POST['customerOrder'] ) ) : '';
|
||||
|
||||
if ( '' === $name || '' === $phone ) {
|
||||
wp_send_json_error( array( 'message' => __( 'Укажите имя и телефон.', 'twentytwentyfour' ) ), 422 );
|
||||
}
|
||||
|
||||
$sent = twentytwentyfour_send_telegram_message(
|
||||
'Новая заявка с главной страницы (модалка заказа)',
|
||||
array(
|
||||
'Имя' => $name,
|
||||
'Телефон' => $phone,
|
||||
'Адрес' => $address,
|
||||
'Тип' => $type,
|
||||
'Комментарий' => $comment,
|
||||
'Источник' => 'Главная страница (модалка заказа)',
|
||||
'URL' => $page_url,
|
||||
)
|
||||
);
|
||||
} elseif ( 'landing_contacts' === $form_type ) {
|
||||
$name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '';
|
||||
$phone = isset( $_POST['phone'] ) ? sanitize_text_field( wp_unslash( $_POST['phone'] ) ) : '';
|
||||
$address = isset( $_POST['address'] ) ? sanitize_text_field( wp_unslash( $_POST['address'] ) ) : '';
|
||||
$comment = isset( $_POST['comment'] ) ? sanitize_textarea_field( wp_unslash( $_POST['comment'] ) ) : '';
|
||||
|
||||
if ( '' === $name || '' === $phone ) {
|
||||
wp_send_json_error( array( 'message' => __( 'Укажите имя и телефон.', 'twentytwentyfour' ) ), 422 );
|
||||
}
|
||||
|
||||
$sent = twentytwentyfour_send_telegram_message(
|
||||
'Новая заявка с главной страницы (контактная форма)',
|
||||
array(
|
||||
'Имя' => $name,
|
||||
'Телефон' => $phone,
|
||||
'Адрес' => $address,
|
||||
'Комментарий' => $comment,
|
||||
'Источник' => 'Главная страница (блок Контакты)',
|
||||
'URL' => $page_url,
|
||||
)
|
||||
);
|
||||
} else {
|
||||
$name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '';
|
||||
$phone = isset( $_POST['phone'] ) ? sanitize_text_field( wp_unslash( $_POST['phone'] ) ) : '';
|
||||
$address = isset( $_POST['address'] ) ? sanitize_text_field( wp_unslash( $_POST['address'] ) ) : '';
|
||||
$comment = isset( $_POST['comment'] ) ? sanitize_textarea_field( wp_unslash( $_POST['comment'] ) ) : '';
|
||||
|
||||
if ( '' === $name || '' === $phone ) {
|
||||
wp_send_json_error( array( 'message' => __( 'Укажите имя и телефон.', 'twentytwentyfour' ) ), 422 );
|
||||
}
|
||||
|
||||
$sent = twentytwentyfour_send_telegram_message(
|
||||
'Новая заявка со страницы Контакты',
|
||||
array(
|
||||
'Имя' => $name,
|
||||
'Телефон' => $phone,
|
||||
'Адрес' => $address,
|
||||
'Комментарий' => $comment,
|
||||
'Источник' => 'Страница Контакты',
|
||||
'URL' => $page_url,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if ( ! $sent ) {
|
||||
wp_send_json_error( array( 'message' => __( 'Не удалось отправить заявку. Проверьте настройки Telegram.', 'twentytwentyfour' ) ), 500 );
|
||||
}
|
||||
|
||||
wp_send_json_success( array( 'message' => __( 'Спасибо! Заявка принята.', 'twentytwentyfour' ) ) );
|
||||
}
|
||||
add_action( 'wp_ajax_twentytwentyfour_submit_lead', 'twentytwentyfour_submit_lead_ajax' );
|
||||
add_action( 'wp_ajax_nopriv_twentytwentyfour_submit_lead', 'twentytwentyfour_submit_lead_ajax' );
|
||||
|
||||
/**
|
||||
* Send WooCommerce checkout orders to Telegram.
|
||||
*
|
||||
* @param int $order_id Order ID.
|
||||
* @param array $posted_data Checkout payload.
|
||||
* @param WC_Order $order Order object.
|
||||
* @return void
|
||||
*/
|
||||
function twentytwentyfour_send_checkout_order_to_telegram( $order_id, $posted_data, $order ) {
|
||||
if ( ! $order instanceof WC_Order ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$items = array();
|
||||
|
||||
foreach ( $order->get_items() as $item ) {
|
||||
if ( ! $item instanceof WC_Order_Item_Product ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$items[] = sprintf( '%s x %d', $item->get_name(), (int) $item->get_quantity() );
|
||||
}
|
||||
|
||||
$address_parts = array_filter(
|
||||
array(
|
||||
$order->get_billing_city(),
|
||||
$order->get_billing_address_1(),
|
||||
(string) get_post_meta( $order_id, '_billing_house', true ),
|
||||
(string) get_post_meta( $order_id, '_billing_flat', true ),
|
||||
(string) get_post_meta( $order_id, '_billing_entrance_floor', true ),
|
||||
)
|
||||
);
|
||||
|
||||
$fields = array(
|
||||
'Заказ' => '#' . $order->get_order_number(),
|
||||
'Имя' => $order->get_billing_first_name(),
|
||||
'Телефон' => $order->get_billing_phone(),
|
||||
'Адрес' => implode( ', ', $address_parts ),
|
||||
'Когда доставить' => (string) get_post_meta( $order_id, '_tw_delivery_day', true ),
|
||||
'Интервал' => (string) get_post_meta( $order_id, '_tw_delivery_time', true ),
|
||||
'Обмен тары' => (string) get_post_meta( $order_id, '_tw_bottle_exchange', true ),
|
||||
'Комментарий' => $order->get_customer_note(),
|
||||
'Состав' => implode( '; ', $items ),
|
||||
'Сумма' => wp_strip_all_tags( $order->get_formatted_order_total() ),
|
||||
);
|
||||
|
||||
twentytwentyfour_send_telegram_message( 'Новый заказ с сайта', $fields );
|
||||
}
|
||||
add_action( 'woocommerce_checkout_order_processed', 'twentytwentyfour_send_checkout_order_to_telegram', 20, 3 );
|
||||
|
||||
/**
|
||||
* Enqueue shared test1 header/footer styles for cart and checkout.
|
||||
*
|
||||
@@ -549,13 +790,16 @@ function twentytwentyfour_test1_shared_shell_assets() {
|
||||
}
|
||||
|
||||
$shared_shell_css = get_theme_file_path( 'assets/css/test1-shared-shell.css' );
|
||||
$landing_fonts_file = get_theme_file_path( 'assets/css/fonts-manrope.css' );
|
||||
|
||||
wp_enqueue_style(
|
||||
'twentytwentyfour-test1-fonts',
|
||||
'https://fonts.googleapis.com/css2?family=Manrope:wght@400;500;700;800&display=swap',
|
||||
array(),
|
||||
null
|
||||
);
|
||||
if ( file_exists( $landing_fonts_file ) ) {
|
||||
wp_enqueue_style(
|
||||
'twentytwentyfour-test1-fonts',
|
||||
get_theme_file_uri( 'assets/css/fonts-manrope.css' ),
|
||||
array(),
|
||||
(string) filemtime( $landing_fonts_file )
|
||||
);
|
||||
}
|
||||
|
||||
if ( file_exists( $shared_shell_css ) ) {
|
||||
wp_enqueue_style(
|
||||
|
||||
Reference in New Issue
Block a user