Files
Roman Pyrih 232bc86866 Save
2025-09-09 13:12:36 +02:00

466 lines
13 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* skysafe functions and definitions
*
* @link https://developer.wordpress.org/themes/basics/theme-functions/
*
* @package skysafe
*/
if (! defined('_S_VERSION'))
{
// Replace the version number of the theme on each release.
define('_S_VERSION', '1.0.0');
}
/**
* Sets up theme defaults and registers support for various WordPress features.
*
* Note that this function is hooked into the after_setup_theme hook, which
* runs before the init hook. The init hook is too late for some features, such
* as indicating support for post thumbnails.
*/
function skysafe_setup()
{
/*
* Make theme available for translation.
* Translations can be filed in the /languages/ directory.
* If you're building a theme based on skysafe, use a find and replace
* to change 'skysafe' to the name of your theme in all the template files.
*/
load_theme_textdomain('skysafe', get_template_directory() . '/languages');
// Add default posts and comments RSS feed links to head.
add_theme_support('automatic-feed-links');
/*
* Let WordPress manage the document title.
* By adding theme support, we declare that this theme does not use a
* hard-coded <title> tag in the document head, and expect WordPress to
* provide it for us.
*/
add_theme_support('title-tag');
/*
* Enable support for Post Thumbnails on posts and pages.
*
* @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
*/
add_theme_support('post-thumbnails');
// This theme uses wp_nav_menu() in one location.
register_nav_menus(
array(
'menu-1' => esc_html__('Primary', 'skysafe'),
)
);
/*
* Switch default core markup for search form, comment form, and comments
* to output valid HTML5.
*/
add_theme_support(
'html5',
array(
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption',
'style',
'script',
)
);
// Set up the WordPress core custom background feature.
add_theme_support(
'custom-background',
apply_filters(
'skysafe_custom_background_args',
array(
'default-color' => 'ffffff',
'default-image' => '',
)
)
);
// Add theme support for selective refresh for widgets.
add_theme_support('customize-selective-refresh-widgets');
/**
* Add support for core custom logo.
*
* @link https://codex.wordpress.org/Theme_Logo
*/
add_theme_support(
'custom-logo',
array(
'height' => 250,
'width' => 250,
'flex-width' => true,
'flex-height' => true,
)
);
}
add_action('after_setup_theme', 'skysafe_setup');
/**
* Set the content width in pixels, based on the theme's design and stylesheet.
*
* Priority 0 to make it available to lower priority callbacks.
*
* @global int $content_width
*/
function skysafe_content_width()
{
$GLOBALS['content_width'] = apply_filters('skysafe_content_width', 640);
}
add_action('after_setup_theme', 'skysafe_content_width', 0);
/**
* Register widget area.
*
* @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
*/
function skysafe_widgets_init()
{
register_sidebar(
array(
'name' => esc_html__('Sidebar', 'skysafe'),
'id' => 'sidebar-1',
'description' => esc_html__('Add widgets here.', 'skysafe'),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
)
);
}
add_action('widgets_init', 'skysafe_widgets_init');
/**
* Enqueue scripts and styles.
*/
function skysafe_scripts()
{
wp_enqueue_style('skysafe-style', get_stylesheet_uri(), array(), _S_VERSION);
wp_style_add_data('skysafe-style', 'rtl', 'replace');
wp_enqueue_script('skysafe-navigation', get_template_directory_uri() . '/js/navigation.js', array(), _S_VERSION, true);
if (is_singular() && comments_open() && get_option('thread_comments'))
{
wp_enqueue_script('comment-reply');
}
}
add_action('wp_enqueue_scripts', 'skysafe_scripts');
/**
* Implement the Custom Header feature.
*/
require get_template_directory() . '/inc/custom-header.php';
/**
* Custom template tags for this theme.
*/
require get_template_directory() . '/inc/template-tags.php';
/**
* Functions which enhance the theme by hooking into WordPress.
*/
require get_template_directory() . '/inc/template-functions.php';
/**
* Customizer additions.
*/
require get_template_directory() . '/inc/customizer.php';
/**
* Load Jetpack compatibility file.
*/
if (defined('JETPACK__VERSION'))
{
require get_template_directory() . '/inc/jetpack.php';
}
function skysafe_enqueue_assets()
{
wp_enqueue_style('skysafe-style', get_stylesheet_uri());
wp_enqueue_style(
'skysafe-custom',
get_template_directory_uri() . '/css/custom.css',
array('skysafe-style'),
filemtime(get_template_directory() . '/css/custom.css')
);
}
add_action('wp_enqueue_scripts', 'skysafe_enqueue_assets');
function cf7_date_validation_script()
{
?>
<script>
document.addEventListener('DOMContentLoaded', function() {
const arrival = document.getElementById("arrival");
const departure = document.getElementById("departure");
const updateHasValue = (input) => {
if (input.value) {
input.classList.add("has-value");
} else {
input.classList.remove("has-value");
}
};
if (arrival && departure) {
arrival.addEventListener("input", function() {
updateHasValue(arrival);
departure.min = arrival.value;
if (departure.value && departure.value < arrival.value) {
departure.value = arrival.value;
updateHasValue(departure);
}
});
departure.addEventListener("input", function() {
updateHasValue(departure);
});
updateHasValue(arrival);
updateHasValue(departure);
}
});
</script>
<?php
}
add_action('wp_footer', 'cf7_date_validation_script');
add_filter('wpcf7_autop_or_not', '__return_false');
add_action('wp_enqueue_scripts', function ()
{
wp_register_script(
'cookie-notice-pro',
get_template_directory_uri() . '/assets/CookieNoticePro/cookienoticepro.script.js',
array('jquery'),
null,
true
);
wp_enqueue_script('cookie-notice-pro');
wp_enqueue_style(
'cookie-notice-pro-style',
get_template_directory_uri() . '/assets/CookieNoticePro/cookienoticepro.style.css',
array(),
null
);
});
require_once get_template_directory() . '/assets/CookieNoticePro/cookies.php';
define('SMSAPI_TOKEN', 'HWC5PZqXdeedBkmPklz20Tj12KEVgID7pPtVWX5R');
define('SMSAPI_NOTIFY_TO', '+48884546620'); // domyślny numer powiadomień
add_action( "wpcf7_mail_sent", "wpcf7_send_sms" );
function wpcf7_send_sms( $cf7 )
{
$allowed_form_id = 33;
if ( (int)$cf7 -> id() !== $allowed_form_id ) {
return;
}
// pobierz obiekt submission (zawiera dane POST, pliki itd.)
$submission = WPCF7_Submission::get_instance();
if ( ! $submission ) {
die('[CF7] Brak submission');
return;
}
$data = $submission -> get_posted_data();
$your_name = isset( $data['your-name'] ) ? sanitize_text_field( $data['your-name'] ) : '';
$your_plate = isset( $data['your-plate'] ) ? sanitize_text_field( $data['your-plate'] ) : '';
$arrival = isset( $data['arrival'] ) ? sanitize_text_field( $data['arrival'] ) : '';
$departure = isset( $data['departure'] ) ? sanitize_text_field( $data['departure'] ) : '';
$your_email = isset( $data['your-email'] ) ? sanitize_email( $data['your-email'] ) : '';
$your_phone = isset( $data['your-phone'] ) ? preg_replace( '/\D+/', '', $data['your-phone'] ) : '';
// Zbuduj treść SMS krótko i konkretnie
$sms_text = implode("\n", [
"Nowe zapytanie:",
"Imię i nazwisko: {$your_name}",
"Numer rejestracyjny: {$your_plate}",
"Przyjazd: {$arrival}",
"Wyjazd: {$departure}",
"Email: {$your_email}",
"Telefon: " . ($your_phone !== '—' ? '+'.ltrim($your_phone, '0') : '—'),
]);
// Opcjonalna transliteracja jak w Twoim przykładzie (uniknie problemów z 7-bit GSM)
$sms_text = smsapi_no_pl( $sms_text );
smsapi_send( SMSAPI_NOTIFY_TO, $sms_text, null ); // wysyłka
// smsapi_send( '+48530755774', $sms_text, 'SkySafe' ); // wysyłka
// smsapi_send( '+48533605006', $sms_text, null ); // wysyłka
}
function smsapi_send(string $to, string $message, ?string $from = null): array
{
if (!defined('SMSAPI_TOKEN') || !SMSAPI_TOKEN)
{
throw new RuntimeException('Brak SMSAPI_TOKEN w wp-config.php');
}
// Uporządkuj numer: dodaj +48 jeśli podano PL bez prefiksu
$to = preg_replace('/\D+/', '', $to);
if (strpos($to, '48') !== 0 && strpos($to, '+48') !== 0 && strlen($to) === 9)
{
$to = '48' . $to;
}
if ($to[0] !== '+')
{
$to = '+' . $to;
}
$body = [
'to' => $to,
'message' => $message,
'format' => 'json', // jak w Twoim przykładzie
// 'encoding' => 'utf-8', // opcjonalnie; przy transliteracji zwykle zbędne
];
if (!empty($from))
{
$body['from'] = $from; // wymaga aktywnego nadawcy w SMSAPI
}
$endpoints = [
'https://api.smsapi.pl/sms.do',
'https://api2.smsapi.pl/sms.do', // fallback jak w Twoim kodzie
];
$last_error = null;
foreach ($endpoints as $url)
{
$resp = wp_remote_post($url, [
'timeout' => 15,
'headers' => [
'Authorization' => 'Bearer ' . SMSAPI_TOKEN,
],
'body' => $body, // application/x-www-form-urlencoded
]);
if (is_wp_error($resp))
{
$last_error = $resp->get_error_message();
continue;
}
$code = wp_remote_retrieve_response_code($resp);
$raw = wp_remote_retrieve_body($resp);
if ($code >= 200 && $code < 300)
{
// Odpowiedź w formacie JSON (bo format=json) spróbuj zdekodować
$json = json_decode($raw, true);
if (json_last_error() === JSON_ERROR_NONE)
{
// możesz tu zweryfikować status wg dokumentacji SMSAPI (np. "count", "list", "error")
if (!empty($json['error']))
{
// Gdy API zwróci błąd semantyczny (np. zły nadawca)
throw new RuntimeException('[SMSAPI] ' . (is_array($json['error']) ? wp_json_encode($json['error']) : $json['error']));
}
return [
'endpoint' => $url,
'http' => $code,
'data' => $json,
];
}
else
{
// Gdyby przyszła odpowiedź nie-JSON mimo format=json
return [
'endpoint' => $url,
'http' => $code,
'raw' => $raw,
];
}
}
else
{
// Nietypowy kod HTTP próbuj kolejnego endpointu
$last_error = 'HTTP ' . $code . ' body: ' . $raw;
}
}
throw new RuntimeException('SMSAPI: nieudana wysyłka. Ostatni błąd: ' . ($last_error ?: 'brak danych'));
}
function smsapi_no_pl(string $s): string
{
// prosta transliteracja PL → ASCII (jak w Twoim kodzie, ale krócej)
$map = [
'ą' => 'a',
'ć' => 'c',
'ę' => 'e',
'ł' => 'l',
'ń' => 'n',
'ó' => 'o',
'ś' => 's',
'ż' => 'z',
'ź' => 'z',
'Ą' => 'A',
'Ć' => 'C',
'Ę' => 'E',
'Ł' => 'L',
'Ń' => 'N',
'Ó' => 'O',
'Ś' => 'S',
'Ż' => 'Z',
'Ź' => 'Z',
];
$s = strtr($s, $map);
// awaryjnie:
$t = @iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $s);
return $t !== false ? $t : $s;
}
add_action( 'wp_enqueue_scripts', function() {
wp_add_inline_script( 'contact-form-7', "
document.addEventListener('wpcf7invalid', function(event) {
let output = event.target.querySelector('.wpcf7-response-output');
if (output) {
output.classList.remove('success');
output.classList.add('error');
}
}, false);
document.addEventListener('wpcf7spam', function(event) {
let output = event.target.querySelector('.wpcf7-response-output');
if (output) {
output.classList.remove('success');
output.classList.add('error');
}
}, false);
document.addEventListener('wpcf7mailfailed', function(event) {
let output = event.target.querySelector('.wpcf7-response-output');
if (output) {
output.classList.remove('success');
output.classList.add('error');
}
}, false);
document.addEventListener('wpcf7mailsent', function(event) {
let output = event.target.querySelector('.wpcf7-response-output');
if (output) {
output.classList.remove('error');
output.classList.add('success');
}
}, false);
");
});