isDir() || $template_file->getExtension() !== 'php') { continue; } // Получаем относительный путь (например, "pages/template-landing.php") $relative_path = str_replace($custom_templates_dir, '', $template_file->getPathname()); // Формируем имя шаблона (убираем .php и заменяем слэши на дефисы) $template_name = str_replace(['/', '.php'], [' - ', ''], $relative_path); // Добавляем в список шаблонов $templates['templates/' . $relative_path] = $template_name; } return $templates; }); includeFilesFromFolder(get_template_directory() . '/global-functions'); // Add the function to the Timber context add_filter('timber/context', function($context) { $context['template_path'] = get_template_directory_uri(); $current_url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $context['current_url'] = htmlspecialchars($current_url); return $context; }); /** * Подключает статические CSS и JS файлы из указанной директории * * @param string $dir_name Название директории в папке /static/ * @param array $options Дополнительные параметры: * - 'css' => false - отключить подключение CSS * - 'js' => false - отключить подключение JS * - 'version' => string - версия файлов (по умолчанию использует время модификации файла) */ function enqueue_static_assets($dir_name, $options = []) { // Устанавливаем пути к директориям $static_dir = get_template_directory() . '/static/' . $dir_name; $static_uri = get_template_directory_uri() . '/static/' . $dir_name; // Подключаем CSS файлы if (!isset($options['css']) || $options['css'] !== false) { $css_dir = $static_dir . '/css/'; if (file_exists($css_dir)) { $css_files = scandir($css_dir); foreach ($css_files as $file) { if (pathinfo($file, PATHINFO_EXTENSION) === 'css') { $handle = $dir_name . '-' . pathinfo($file, PATHINFO_FILENAME); $src = $static_uri . '/css/' . $file; $ver = isset($options['version']) ? $options['version'] : filemtime($css_dir . $file); wp_enqueue_style( $handle, $src, array(), $ver ); } } } } // Подключаем JS файлы if (!isset($options['js']) || $options['js'] !== false) { $js_dir = $static_dir . '/js/'; if (file_exists($js_dir)) { $js_files = scandir($js_dir); foreach ($js_files as $file) { if (pathinfo($file, PATHINFO_EXTENSION) === 'js') { $handle = $dir_name . '-' . pathinfo($file, PATHINFO_FILENAME); $src = $static_uri . '/js/' . $file; $ver = isset($options['version']) ? $options['version'] : filemtime($js_dir . $file); $in_footer = strpos($file, 'admin') === 0 ? false : true; wp_enqueue_script( $handle, $src, array(), $ver, $in_footer ); } } } } } add_action('init', function() { if (function_exists('wpseo_load_textdomain')) { wpseo_load_textdomain(); } if (function_exists('pll_load_textdomain')) { pll_load_textdomain(); } add_filter('timber/context', function($context) { $context['current_lang'] = pll_current_language(); return $context; }); }, 1); $modules_path = get_template_directory() . '/modules/*/editor-blocks/*/editor-block-controller.php'; foreach (glob($modules_path) as $file) { require_once $file; // Подключаем каждый найденный файл } requireShortcodes(get_template_directory() . '/modules'); require_once('modules/blog/module-ajax-controller.php'); require_once('modules/forms/module-ajax-controller.php'); include_module('forms'); include_module('layout'); class WooProduct extends Timber\Post { protected $wc_product; public function __construct ($pid = null) { parent::__construct($pid); $this->wc_product = wc_get_product($this->ID); } public function price() { return $this->wc_product->get_price(); } public function get_price_html() { return $this->wc_product->get_price_html(); } public function get_attr() { return $this->wc_product->get_attribute('pa_compound'); } public function get_test() { return 'test'; } }; add_filter('timber/post/classmap', function ($classmap) { $custom_classmap = [ 'product' => WooProduct::class, ]; return array_merge($classmap, $custom_classmap); }); //Ajax подгрузка товаров в архиве add_action( 'wp_ajax_nopriv_get_products', 'get_products' ); add_action( 'wp_ajax_get_products', 'get_products' ); function get_products() { global $post; if (function_exists('WC')) { WC(); } // Потом Timber $context = Timber::context(); $context['get_page'] = empty($_POST['get_page']) ? 1 : $_POST['get_page']; $context['criteria_for_new_product'] = date('Y-m-d', strtotime('-30 days')); $context['get_category'] = isset($_POST['get_category']) ? $_POST['get_category'] : NULL; $context['get_category_type'] = isset($_POST['get_category_type']) ? $_POST['get_category_type'] : NULL; // Задаем количество постов для подзагрузки Ajax $posts_per_page = 1; $args = array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => $posts_per_page, 'paged' => $context['get_page'], 'has_password' => FALSE ); $count_args = array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => -1, 'has_password' => FALSE ); if ($context['get_category'] != NULL) { $categories = [ 'tax_query' => array( array( 'taxonomy' => $context['get_category_type'], 'field' => 'term_id', 'terms' => array($context['get_category']), 'operator' => 'IN' ), array( 'taxonomy' => 'product_visibility', 'field' => 'slug', 'terms' => 'exclude-from-catalog', 'operator' => 'NOT IN' ) ) ]; $args = array_merge($args, $categories); $count_args = array_merge($count_args, $categories); } $products = new WP_Query($args); $products = new Timber\PostQuery($products, 'Timber\Integrations\WooCommerce\Product'); $context['posts'] = $products; $context['count'] = count(Timber::get_posts($count_args)); if ($context['count'] <= $context['get_page'] * 1) $context['ended'] = true; Timber::render( 'woocommerce/archive-product/archive-product-ajaxload.twig', $context ); die(); } function get_product_info ($id, $type) { $product = wc_get_product( $id ); if ($type == 'price') { return $product->get_price(); } elseif ($type == 'weight') { return $product->get_weight() . ' кг'; } } function get_add_to_cart_button ($id) { $product = wc_get_product( $id ); return ''. pll__('Добавить в корзину') .''; } function get_collection_siblings ($term) { $args = array( 'posts_per_page' => -1, 'post_type' => 'product', 'order' => 'ASC', 'order_by' => 'name', 'tax_query' => [ [ 'taxonomy' => 'pa_collection', 'terms' => $term, 'field' => 'id', ] ], ); $siblings = get_posts($args); $sibl_arr = []; foreach( $siblings as $sibling ) { $sibl_arr [] = $sibling; } return $sibl_arr; } register_sidebar( array( 'name' => 'Сайдбар для фильтров товаров', 'id' => 'sidebar_filters', 'before_widget' => '
', 'after_widget' => '
', 'before_title' => '

', 'after_title' => '

', ) ); // Define encryption secret key define('SECRET', 'fT54ehYtt87@q1A'); /** * Email Validation * Validates the format of the provided email via AJAX. */ add_action('wp_ajax_email_validate', 'email_validate'); add_action('wp_ajax_nopriv_email_validate', 'email_validate'); function email_validate() { if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) { header("Content-Type: application/json"); echo json_encode(array( 'status' => 'error', 'text' => esc_html__('Invalid email format', 'woodmart') )); } wp_die(); } /** * Send Verification Code * Generates a random code, encrypts it, stores it in a cookie, and sends it via email. */ add_action('wp_ajax_send_code', 'send_code'); add_action('wp_ajax_nopriv_send_code', 'send_code'); function send_code() { $email = $_POST['email']; // Generate a random 4-digit code $string = rand(1234, 9999); $ciphering = "AES-128-CTR"; $options = 0; $iv = '1234567891011121'; $encryption = openssl_encrypt($string, $ciphering, SECRET, $options, $iv); // Store encrypted code in a cookie for 5 minutes setcookie('login_code', $encryption, time() + 60 * 5, '/'); // Prepare email content based on language if (function_exists('pll_current_language') && pll_current_language() === 'ru') { $subject = "Проверочный код Cosmopet -" . $string; $message = "Привет, это Cosmopet.\n Держите проверочный код!\n " . $string; } else { $subject = "Cosmopet Verification Code -" . $string; $message = "Hello, this is CosmoPet.\n Here's your verification code!\n " . $string; } // Remove email filters for consistent sending remove_all_filters('wp_mail_from'); remove_all_filters('wp_mail_from_name'); $headers = array( 'From: Cosmopet ', 'content-type: text/html; charset=utf-8', ); // Send email wp_mail($email, $subject, $message, $headers); wp_die(); } /** * Check Verification Code * Validates the user-entered code, logs in or registers the user. */ add_action('wp_ajax_check_code', 'check_code'); add_action('wp_ajax_nopriv_check_code', 'check_code'); function check_code() { header("Content-Type: application/json"); $code = $_POST['code']; $email = $_POST['email']; // Generate a random 12-character password $alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890@#!()'; $pass = array(); $alphaLength = strlen($alphabet) - 1; for ($i = 0; $i < 12; $i++) { $n = rand(0, $alphaLength); $pass[] = $alphabet[$n]; } $pass = implode($pass); // Check if verification code cookie exists if (isset($_COOKIE['login_code'])) { $string = $_COOKIE['login_code']; $ciphering = "AES-128-CTR"; $options = 0; $iv = '1234567891011121'; $decryption = openssl_decrypt($string, $ciphering, SECRET, $options, $iv); if ($decryption === $code) { if (email_exists($email)) { // Log in existing user $user_id = get_user_by('login', $email)->ID; if (empty($user_id)) { $user_id = get_user_by('email', $email)->ID; } wp_set_password($pass, $user_id); wp_signon( array( 'user_login' => $email, 'user_password' => $pass, 'remember' => 'on', ) ); echo json_encode(array( 'status' => 'success_auth' )); if (function_exists('update_field')) { update_field('activated', true, 'user_' . $user_id); // Requires ACF } exit(); } else { // Register new user $user_id = wp_create_user($email, $pass, $email); wp_update_user([ 'ID' => $user_id, 'user_email' => $email ]); wp_set_auth_cookie($user_id, true); echo json_encode(array( 'status' => 'success_reg' )); if (function_exists('update_field')) { update_field('activated', true, 'user_' . $user_id); // Requires ACF } exit(); } } else { echo json_encode(array( 'status' => 'error', 'text' => esc_html__('Invalid code', 'woodmart') )); exit(); } } else { echo json_encode(array( 'status' => 'error', 'text' => esc_html__('The code hasexpired', 'woodmart') )); exit(); } wp_die(); } /** * Logout Redirect * Redirects to the homepage after user logout. */ add_action('wp_logout', 'logout_redirect'); function logout_redirect() { wp_redirect('/'); exit(); }