Синхронизация файлов - Rsync
Популярное
Rsync — очень мощная утилита, которая позволяет:
- синхронизировать данные между между компьютерами в сети, дисками, локальными директориями или виртуальными машинами;
- копировать как отдельные файлы, так и целую структуру каталогов;
- сохранять данные о владельцах файлов, права доступа, время правки и другую служебную информацию;
- cохранять символические и жёсткие ссылки.
Синтаксис
rsync {параметры} {источник} {приемник}
Источник/Приемник
Источник/приемник может быть как локальным там и удаленным.
Ключевое правило
Слеш в конце пути к источнику означает "копировать СОДЕРЖИМОЕ этой директории". Отсутствие слеша означает "копировать САМУ директорию и её содержимое".
Локальный путь
Обычный путь к файлу или директории на текущей машине:
/www/site.com/public_html/ - путь от корня сервера
./wp-content/uploads/ - путь от текущей папки
Удаленный путь
Находится на удаленном сервере.
Формат - {пользователь}@{сервер}:{путь}
Пример - user@255.255.255.255:/data/www/site.com/
Параметры
Основные параметры
-a, --archive - архивный режим, флаг объединяющий сразу несколько других параметров (-rlptgoD).
-n, --dry-run - имитирует работу команды НЕ ВЫПОЛНЯЯ никаких реальных изменений на стороне приемника и демонстрирует в выводе что было бы при реальном выполнение команды без данного параметра.
-v, -vv, -vvv, --verbose - включает подробный вывод результата выполнения. При увеличение v увеличивается уровень детализации. Без данного параметра команды не выведет никаких результатов. Отлично работает с параметром --progress.
--progress - будет отображен процесс для каждого передаваемого файла в реальном времени.
-z, --compress - включает сжатие данных перед отправкой файла по сети. Файлы сжимаются "на лету" на стороне отправителя и распаковываются на стороне получателя.
Не стоит использовать:
при локальном копировании
при передаче уже сжатых файлов
--partial - по умолчанию rsync при прерывании передачи удаляет не до конца переданный файл на "приёмнике", чтобы в следующий раз начать его заново с нуля. С этим параметром "говорит" не удалять частично переданные файлы. В следующий раз он попытается докачать их с того места, где закончил.
--partial-dir={DIR} - более продвинутый и безопасный аналог. rsync будет сохранять частичные файлы не в целевую директорию, а в указанную временную {DIR}.
--bwlimit={RATE} - Жёсткое ограничение скорости передачи данных. Значение указывается в килобайтах в секунду.
--delete - Этот параметр заставляет rsync сделать целевую директорию - идеальной зеркальной копией исходной. Она УДАЛЯЕТ на приёмнике все файлы и папки, которых в источнике нет.
--ignore-existing - rsync будет копировать только те файлы, которых ещё нет на приёмнике. Если файл с данным именем уже существует в цели, он будет проигнорирован, даже если содержимое или метаданные отличаются.
-u, --update - rsync будет копировать файл только в том случае, если файл в источнике новее (по дате изменения), чем файл на приёмнике. Если файл на приёмнике новее или такой же, он будет проигнорирован.
Дополнительные параметры
Входят в параметр -a
-r, --recursive - рекурсивное копирование, без этого параметра будут скопированы только файлы из указанной директории.
-l, --links - сохраняет симлинки как симлинки.
-p, --perms - сохраняет права доступа для файлов и папок.
-t, --times - сохраняет временные метка файлов и папок.
-g, --group - сохранять группу. Файлы будут принадлежать той же группе, что и на источнике.
-o, --owner - сохраняет владельца.
-D - сохранять device files (специальные файлы устройств). Это эквивалентно одновременному использованию --devices --specials.
Примеры
Копировать содержимое папки uploads на удаленный сервер с сохранением исходных данных файлов, сжатием для отправки и подробным отображением процесса
rsync -avz --progress ./uploads/ user@255.255.255.255:/home/data/www/site.com/public_html/wp-content/uploads/
Удаление файлов из истории GIT
Делаем бэкап файлов
# Создаем zip-архив с картинками zip -r uploads_backup.zip wp-content/uploads/ # Или используем tar (более эффективно) tar -czf uploads_backup.tar.gz wp-content/uploads/
# Удаляем папку из индекса
git rm -r --cached wp-content/uploads/
# Очищаем историю от этих файлов
git filter-branch --tree-filter 'rm -rf wp-content/uploads' --prune-empty HEAD
или
git filter-branch -f --tree-filter 'rm -f wp-content/uploads' --prune-empty HEAD
Добавить в .gitignore
echo "wp-content/uploads/" >> .gitignore
git add .gitignore
Принудительный пуш
git push origin stage --force
лучше через
git rev-list --objects --all | git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | awk '/^blob/ {print substr($0,6)}' | sort --numeric-sort --key=2 | tail -10
git filter-repo --path wp-content/uploads/ --invert-paths --force
git filter-repo --path-glob '*.log' --path-glob '*.zip' --path-glob '*.rar' --path-glob '*.tar' --path-glob '*.gz' --path-glob '*.mp4' --path-glob '*.avi' --path-glob '*.mov' --path-glob '*.webm' --invert-paths --force
Очистка Docker от ненужных данных: образы, контейнеры, тома и логи
docker volume prune
docker container prune
docker image prune
docker network prune
docker system prune
docker system prune -af
Поиск больших файлов на сервере
find . -type f -size +200M -exec du -h {} + 2>/dev/null | sort -hr
Смена ip серевра в Fastpanel
egrep -rl "10.10.10.10" /etc/ | xargs -i sed -r -i 's/10.10.10.10/20.20.20.20/g' '{}'
2. далее нужно сменить в базе sqllite , но самое простое это с помощью встроенной утилиты:
mogwai change_ip --from=10.10.10.10 --to=20.20.20.20
проверяем конфиги и ребутим
Certbot SSL выпуск Let's Encrypt в Docker
docker-compose run --entrypoint "/bin/sh -c 'certbot certonly --webroot -w /var/www/certbot --email tech@domain.ru --agree-tos --no-eff-email -d domain.ru'" certbot
WP 1C - hook атрибутов
add_action(
'itglx_wc1c_product_option_custom_processing_2f582177-ae22-11ec-babb-d8bbc10dcaeb',
function (int $productId, SimpleXMLElement $property) {
$value = html_entity_decode((string)$property->Значение);
update_field('composition-table', $value, $productId);
},
10,
2
);
gitbash alias
// открываем
nano ~/.bashrc
// вписываем
alias 2dit-dev='ssh dev@43.12.712.42'
// применяем
source ~/.bashrc
Заявка в чат telegram
<form class="form" method="post" action="/send.php">
<div class="form__item">
<input class="form__input" type="text" name="name" required>
<label class="form__label">Ваше имя</label>
</div>
<div class="form__item">
<input class="form__input" type="text" name="phone" required>
<label class="form__label">Номер телефона</label>
</div>
<input class="form__input btn" type="submit" value="Отправить">
<input type="hidden" name="act" value="order">
</form>
Шаг 5: PHP-обработчик для отправки заявок в Telegram
Теперь нужно написать PHP-скрипт, который будет отправлять данные формы в Telegram-чат. Вот пример файла send.php:
<?php
// Токен, который дал @BotFather
$token = "1094153697:AAFiLXXXXXLl0hRDsxBij1lddKydKxSSsOg04";
// ID чата, в который бот будет отправлять заявки
$chat_id = "-40XXXX740";
// Проверяем, что форма была отправлена
if ($_POST['act'] == 'order') {
// Собираем данные из формы
$name = ($_POST['name']);
$phone = ($_POST['phone']);
// Формируем сообщение для отправки в Telegram
$arr = array(
'Имя:' => $name,
'Телефон:' => $phone
);
// Собираем текст сообщения
$txt = "";
foreach ($arr as $key => $value) {
$txt .= "<b>".$key."</b> ".$value."%0A";
}
// Отправляем запрос к API Telegram
$sendToTelegram = fopen("https://api.telegram.org/bot{$token}/sendMessage?chat_id={$chat_id}&parse_mode=html&text={$txt}","r");
// Обрабатываем результат отправки
if ($sendToTelegram) {
echo 'Спасибо! Ваша заявка принята.';
} else {
echo 'Ошибка отправки. Попробуйте снова.';
}
}
?>
Заключение
После выполнения всех шагов ваша форма на сайте будет отправлять заявки прямо в Telegram-чат. Теперь вы сможете оперативно получать и обрабатывать заявки, не теряя время на проверку электронной почты.
Если у вас возникнут трудности с настройкой, не стесняйтесь обращаться за помощью! Надеемся, что этот гайд был полезен для вас.
Проверка на вирусы
Язык WP программно
if( is_admin() )
switch_to_locale('en_US');
Можно через хук:
add_action('init', function(){
switch_to_locale('ru_RU');
});
больше инфы https://wp-kama.ru/function/switch_to_locale
Функция array_is_assoc($arr)
<?php
function array_is_assoc(array $arr) {
if (array() === $arr) return false;
return array_keys($arr) !== range(0, count($arr) - 1);
}
Функция pluck($arr, $propName)
<?php
function pluck($arr, $propName) {
return array_map(function($item) use ($propName) {
return is_array($item) ? $item[$propName] : $item->$propName;
}, $arr);
}
Функция group_by($array, $key)
<?php
function group_by($array, $key) {
$result = array();
foreach ($array as $element) {
if(is_callable($key)) {
$new_key = $key($element);
$result[$new_key][] = $element;
} else {
$result[$element[$key]][] = $element;
}
}
return $result;
}
rs_upload_from_url
<?php
function rs_upload_from_url( $url, $title = null ) {
require_once( ABSPATH . "/wp-load.php");
require_once( ABSPATH . "/wp-admin/includes/image.php");
require_once( ABSPATH . "/wp-admin/includes/file.php");
require_once( ABSPATH . "/wp-admin/includes/media.php");
// Download url to a temp file
$tmp = download_url( $url );
if ( is_wp_error( $tmp ) ) return false;
// Get the filename and extension ("photo.png" => "photo", "png")
$filename = pathinfo($url, PATHINFO_FILENAME);
$extension = pathinfo($url, PATHINFO_EXTENSION);
// An extension is required or else WordPress will reject the upload
if ( ! $extension ) {
// Look up mime type, example: "/photo.png" -> "image/png"
$mime = mime_content_type( $tmp );
$mime = is_string($mime) ? sanitize_mime_type( $mime ) : false;
// Only allow certain mime types because mime types do not always end in a valid extension (see the .doc example below)
$mime_extensions = array(
// mime_type => extension (no period)
'text/plain' => 'txt',
'text/csv' => 'csv',
'application/msword' => 'doc',
'image/jpg' => 'jpg',
'image/jpeg' => 'jpeg',
'image/gif' => 'gif',
'image/png' => 'png',
'video/mp4' => 'mp4',
);
if ( isset( $mime_extensions[$mime] ) ) {
// Use the mapped extension
$extension = $mime_extensions[$mime];
}else{
// Could not identify extension
@unlink($tmp);
return false;
}
}
// Upload by "sideloading": "the same way as an uploaded file is handled by media_handle_upload"
$args = array(
'name' => "$filename.$extension",
'tmp_name' => $tmp,
);
// Do the upload
$attachment_id = media_handle_sideload( $args, 0, $title);
// Cleanup temp file
@unlink($tmp);
// Error uploading
if ( is_wp_error($attachment_id) ) return false;
// Success, return attachment ID (int)
return (int) $attachment_id;
}
Класс Query
<?php
require $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php';
class Query {
public $query = [
'post_type' => 'post',
'meta_query' => [],
'tax_query' => [],
'posts_per_page' => -1,
'post__not_in' => []
];
function __construct($type = 'post')
{
$this->query['post_type'] = $type;
}
public function args($args = []) {
$this->query = $args;
return $this;
}
public function type($type = 'post') {
$this->query['post_type'] = $type;
return $this;
}
public function where($key, $value) {
$this->query[$key] = $value;
return $this;
}
public function meta($key, $value, $compare = '=') {
$this->query['meta_query'][] = [
'key' => $key,
'value' => $value,
'compare' => $compare
];
return $this;
}
public function tax($tax, $terms, $field = 'term_id') {
$this->query['tax_query'][] = [
'taxonomy' => $tax,
'field' => $field,
'terms' => $terms
];
return $this;
}
public function count($count = -1) {
$this->query['posts_per_page'] = $count;
return $this;
}
public function postNotIn($items = []) {
$this->query['post__not_in'] = $items;
return $this;
}
public function get() {
return get_posts($this->query);
}
public function wpGet() {
return (new WP_Query($this->query));
}
public function metaKey($key) {
$this->query['meta_key'] = $key;
return $this;
}
public function orderBy($by, $order = 'desc') {
$this->query['orderby'] = $by;
$this->query['order'] = $order;
return $this;
}
public function orderByMeta($by, $order = 'desc') {
$this->orderBy('meta_value', $order);
$this->metaKey($by);
return $this;
}
public function orderByMetaNum($by, $order = 'desc') {
$this->orderBy('meta_value_num', $order);
$this->metaKey($by);
return $this;
}
public function first() {
$this->query['posts_per_page'] = 1;
$posts = get_posts($this->query);
if(!empty($posts)) {
return $posts[0];
}
return null;
}
public function paged($page_number = 1) {
$this->query['paged'] = $page_number;
return $this;
}
}
Класс Request
<?php
class Request
{
private $request = [];
private $server = [];
private $files = [];
public function __get($name)
{
return $this->get($name);
}
public function equals($key, $value) {
return $this->get($key) == $value;
}
public function in($key, $value) {
return in_array($value, $this->get($key));
}
public function __construct()
{
$this->request = $_REQUEST;
$this->server = $_SERVER;
$this->files = $_FILES;
}
public function isEmpty() {
return empty($this->request);
}
public function isEmptyGet() {
return $this->isEmpty() && $this->isGet();
}
public function isEmptyPost() {
return $this->isEmpty() && $this->isPost();
}
public function isNotEmpty() {
return !empty($this->request);
}
public function isNotEmptyPost() {
return $this->isNotEmpty() && $this->isPost();
}
public function isNotEmptyGet() {
return $this->isNotEmpty() && $this->isGet();
}
public function get($name, $default = null)
{
if ($this->has($name)) {
return $this->request[$name];
}
return $default;
}
public function has($name)
{
return isset($this->request[$name]);
}
public function all()
{
return $this->request;
}
public function backUrl()
{
return $this->server['HTTP_REFERER'];
}
public function method()
{
return $this->server['REQUEST_METHOD'];
}
public function isPost() {
return $this->method() == 'POST';
}
public function isGet() {
return $this->method() == 'GET';
}
public function ip()
{
if (!empty($this->server['HTTP_CLIENT_IP'])) {
return $this->server['HTTP_CLIENT_IP'];
} elseif (!empty($this->get['HTTP_X_FORWARED_FOR'])) {
return $this->server['HTTP_X_FORWARED_FOR'];
} else {
return $this->server['REMOTE_ADDR'];
}
}
public function dd()
{
// $dumpArray = array_merge($this->request, ['files' => $this->files]);
echo '<pre>';
var_dump($this->request);
echo '</pre>';
die;
}
public function header($name) {
$name = trim(strtoupper($name));
return $this->server["HTTP_$name"];
}
public function allHeaders()
{
return apache_request_headers();
}
public function files() {
return $this->files;
}
public function file($name) {
return $this->files[$name];
}
public function notEmptyFiles()
{
return !empty($this->files);
}
}
XML to ARRAY
<?php
/**
* @param string $xml
*
* @return mixed
*/
function xml_to_array(string $xml): mixed
{
$xml = simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
return json_decode($json, true);
}
dd()
<?php
if ( ! function_exists( 'dd' ) ) {
/**
* @param $dump_element
*
* @return void
*/
function dd( $dump_element ): void {
ob_start();
echo '<div style="background: #202124; color: #15d515; max-width: 100%">';
var_dump( $dump_element );
echo '</div>';
$dump_string = ob_get_contents();
ob_end_clean();
$dump_string = preg_replace([
'/(\{)/',
'/(\})/',
"/\[(\".*\")\]/",
"/(.*\(\d+\.?\))/",
"/=>/",
"/(\()(\d+)(\))/",
], [
'<span style="color: #87a9bc">$1</span><div style="padding-left: 20px; margin: 0">',
'</div><span style="color: #87a9bc">$1</span>',
'<br><span style="color: #dc5e30;">$1</span>',
'<span style="color: #007eff;">$1</span>',
'<span style="color: #ff00eb;"> : </span>',
'<span style="color: #87bc95;">$1</span><span style="color: #cf8a5d;">$2</span><span style="color: #87bc95;">$3</span>',
], $dump_string);
echo $dump_string;
die();
}
}
is_post / is_get
function is_post($function = null) {
if ($function && !empty($_POST)) {
return $function($_POST) ?: null;
}
return !empty($_POST);
}
function is_get($function = null) {
if ($function && !empty($_GET)) {
return $function($_GET) ?: null;
}
return !empty($_GET);
}
is_dev
function is_dev() {
return isset($_GET['dev']) && $_GET['dev'] == 1;
}
VUE DEPLOY
try_files $uri $uri/ /index.html; // ручная настройка - NGIX fronend
// server.js
const express = require('express'); //npm download and introduce the express module npm -express -D
const app = express();
app.use(express.static('./dist')) // ./dist is the path of the dist folder after vue is packaged
app.listen(8080,function(err){ //8080 The port number of the project you want to monitor
if(err){
console.log(err)
}else {
console.log(‘Project started successfully’)
}
})
кастомный permalink для типа записей
<?php
add_filter(
'pre_post_link',
function ($permalink, $post) {
if ($post->post_type !== 'post') {
return $permalink;
}
return '/stati/%postname%/';
},10,2);
?>
Количество записей таксономии в произвольном типе
<?php
// Считать термины в типе данных
function get_term_post_count_by_type($term,$taxonomy,$type){
$args = array(
'fields' =>'ids',
'posts_per_page' => -1,
'post_type' => $type,
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term
)
)
);
$ps = get_posts( $args );
if (count($ps) > 0){
return count($ps);
} else {
return 0;
}
}
?>
Простой ajax запрос
<script>
let xhr = new XMLHttpRequest();
let formData = new FormData();
formData.append("type", type);
formData.append("slug", slug);
xhr.open("POST", '/wp-content/themes/help-consulting/ajax/post-tag.php', true);
xhr.onreadystatechange = function() {
if (this.readyState != 4) return;
console.log(this.responseText);
}
xhr.send(formData);
</script>
Debug в файл
<?php
//wp-config.php
define( 'WP_DEBUG', 0 );
define( 'WP_DEBUG_LOG', 0);
define( 'WP_DEBUG_DISPLAY', 0);
?>
Nuxt deploy
Шаг 1. Установка менеджера процессов pm2
Для того, чтобы управлять процессами и приложениями, запущенными в среде Node.js, а также автоматического их запуска после перезагрузки сервера, нам понадобится менеджер процессов Node.js, который мы установим глобально в систему через пакетный менеджер npm
Wordpress мультидомен
<?php
// wp-config.php в самый верх
define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST']);
define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST']);
$_SERVER['HTTPS'] = 'on';
define('FORCE_SSL_ADMIN', true);
define('FORCE_SSL_LOGIN', true);
?>
Редирект на c http на https
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP:X-HTTPS} !1
RewriteCond %{HTTP_HOST} ^domain\.ru$
RewriteRule ^(.*)$ https://domain.ru/$1 [R=301,L]
Excel to array
<?php
// Главное не забыть подключить IOFactory
use PhpOffice\PhpSpreadsheet\IOFactory;
$reader = IOFactory::createReader("Xlsx");
$spreadsheet = $reader->load("files/avc.xlsx");
foreach ($spreadsheet->getWorksheetIterator() as $worksheet) {
$arrayData[$worksheet->getTitle()] = $worksheet->toArray();
}
$result = array();
foreach ($arrayData as $key => $value) {
$keys = array_slice($value, 0, 1);
$values = array_slice($value, 1);
foreach ($values as $val) {
$result[$key][] = array_combine($keys[0], $val);
}
}
Загрузка одного файла Word Press
<?php
require($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php'); // Подключаем WordPress
if (!function_exists('wp_handle_upload')) // Проверяем, определена ли функция wp_handle_upload()
require_once(ABSPATH . 'wp-admin/includes/file.php'); // Если не определена, подключаем файл для ее определения
$file = $_FILES['file']; // Получаем информацию о загруженном файле из массива $_FILES
$overrides = ['test_form' => false]; // Определяем опции для обработки файла в функции wp_handle_upload()
$movefile = wp_handle_upload($file, $overrides); // Обрабатываем загруженный файл и получаем информацию о перемещенном файле или сообщение об ошибке
if ($movefile && empty($movefile['error'])) { // Проверяем, успешно ли был обработан загруженный файл
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename($movefile['file']), // Полный URL файла, включая имя файла
'post_mime_type' => $movefile['type'], // MIME-тип файла
'post_title' => preg_replace('/\.[^.]+$/', '', basename($movefile['file'])), // Имя файла без расширения
'post_content' => '', // Описание файла
'post_status' => 'publish' // Статус публикации вложения (опубликовано)
);
$attach_id = wp_insert_attachment($attachment, $movefile['file']); // Добавляем вложение в медиабиблиотеку WordPress и сохраняем его ID в $attach_id
}
Загрузка нескольких фалов WordPress
<?php
require($_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
if (!function_exists('wp_handle_upload'))
require_once(ABSPATH . 'wp-admin/includes/file.php');
$files = $_FILES['files'];
$overrides = ['test_form' => false];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$movefile = wp_handle_upload($file, $overrides);
if ($movefile && empty($movefile['error'])) {
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename($movefile['file']),
'post_mime_type' => $movefile['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($movefile['file'])),
'post_content' => '',
'post_status' => 'publish'
);
$attach_ids[] = wp_insert_attachment($attachment, $movefile['file']);
}
}
}
Редирект с заглавных на строчные в URL
RewriteCond %{REQUEST_URI} [A-Z]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule (.*) rewrite-strtolower.php?rewrite-strtolower-url=$1 [QSA,L]
<?
// rewrite-strtolower.php
if(isset($_GET['rewrite-strtolower-url'])) {
$url = $_GET['rewrite-strtolower-url'];
unset($_GET['rewrite-strtolower-url']);
$params = strtolower(http_build_query($_GET));
if(strlen($params)) {
$params = '?' . $params;
}
header('Location: https://' . $_SERVER['HTTP_HOST'] . '/' . strtolower($url) . $params, true, 301);
exit;
}
header("HTTP/1.0 404 Not Found");
die('Unable to convert the URL to lowercase. You must supply a URL to work upon.');
?>
full page scroll plugin
https://github.com/alvarotrigo/fullPage.js
https://github.com/peachananr/onepage-scroll
SSH Сортировка файлов
du --max-depth=1 . | sort -n -r
arrayChunks($array, $length)
<?php
function arrayChunks($array, $length)
{
$new = [];
if (is_array($length)) {
$lenIndex = 0;
$i = 0;
$j = 0;
foreach ($array as $item) {
$new[$i][] = $item;
$j++;
if ($j >= $length[$lenIndex]) {
$lenIndex++;
$i++;
$j = 0;
}
if (count($length) <= $lenIndex) {
$lenIndex = 0;
}
}
} else {
$new = array_chunk($array, $length);
}
return $new;
}
autoloader(string $dir)
<?php
function autoloader(string $dir)
{
$composer = json_decode(file_get_contents("$dir/autoloader.json"), 1);
$namespaces = $composer['autoload']['psr-4'];
// Foreach namespace specified in the composer, load the given classes
foreach ($namespaces as $namespace => $classpaths) {
if (!is_array($classpaths)) {
$classpaths = array($classpaths);
}
spl_autoload_register(function ($classname) use ($namespace, $classpaths, $dir) {
// Check if the namespace matches the class we are looking for
if (preg_match("#^" . preg_quote($namespace) . "#", $classname)) {
// Remove the namespace from the file path since it's psr4
$classname = str_replace($namespace, "", $classname);
$filename = preg_replace("#\\\\#", "/", $classname) . ".php";
foreach ($classpaths as $classpath) {
$fullpath = $dir . "/" . $classpath . "/$filename";
if (file_exists($fullpath)) {
require_once $fullpath;
}
}
}
});
}
}
acf_add_options_page
<?php
if (function_exists('acf_add_options_page')) {
acf_add_options_page(array(
'page_title' => 'Дополнительные настройки',
'menu_title' => 'Доп. настройки',
'menu_slug' => 'options',
'capability' => 'edit_posts',
'redirect' => false
));
}
num2word($num, $words)
<?php
function num2word($num, array $words):string
{
$num = $num % 100;
if ($num > 19) {
$num = $num % 10;
}
switch ($num) {
case 1: {
return strval($words[0]);
}
case 2:
case 3:
case 4: {
return strval($words[1]);
}
default: {
return strval($words[2]);
}
}
}
getPrev($date, $post_type = 'post')
<?php
/**
* Получает пердыдущую запись по Дате, в WordPress
* @param string $post_type
* @param string $date Дата в формате Y-m-d H:i:s
* @return integer
*/
function getPrev($date, $post_type = 'post')
{
global $wpdb;
$result = $wpdb->get_results(
"SELECT wp_posts.ID, wp_posts.post_name, wp_posts.post_title
FROM qydqk_posts AS wp_posts
WHERE wp_posts.post_date = (
SELECT max(wp_posts.post_date)
FROM qydqk_posts AS wp_posts
WHERE wp_posts.post_date < \"$date \"
AND wp_posts.post_type = '$post_type'
AND wp_posts.post_status = 'publish'
ORDER BY wp_posts.post_date DESC
)
AND wp_posts.post_type = '$post_type'
AND wp_posts.post_status = 'publish'
ORDER BY wp_posts.post_date DESC"
);
if (empty($result)) {
$result = $wpdb->get_results(
"SELECT wp_posts.ID, wp_posts.post_name, wp_posts.post_title
FROM qydqk_posts AS wp_posts
WHERE wp_posts.post_date = (
SELECT max(wp_posts.post_date)
FROM qydqk_posts AS wp_posts
WHERE wp_posts.post_type = '$post_type'
AND wp_posts.post_status = 'publish'
ORDER BY wp_posts.post_date ASC
)
AND wp_posts.post_type = '$post_type'
AND wp_posts.post_status = 'publish'
ORDER BY wp_posts.post_date DESC"
);
}
return intval($result[0]->ID);
}
getNext($date, $post_type = 'post')
<?php
/**
* Получает слудеющую запись по Дате, в WordPress
* @param string $post_type
* @param string $date Дата в формате Y-m-d H:i:s
* @return integer
*/
function getNext($date, $post_type = 'post')
{
global $wpdb;
$result = $wpdb->get_results(
"SELECT wp_posts.ID, wp_posts.post_name, wp_posts.post_title
FROM qydqk_posts AS wp_posts
WHERE wp_posts.post_date = (
SELECT min(wp_posts.post_date)
FROM qydqk_posts AS wp_posts
WHERE wp_posts.post_date > \"$date \"
AND wp_posts.post_type = '$post_type'
AND wp_posts.post_status = 'publish'
ORDER BY wp_posts.post_date DESC
)
AND wp_posts.post_type = '$post_type'
AND wp_posts.post_status = 'publish'
ORDER BY wp_posts.post_date DESC"
);
if (empty($result)) {
$result = $wpdb->get_results(
"SELECT wp_posts.ID, wp_posts.post_name, wp_posts.post_title
FROM qydqk_posts AS wp_posts
WHERE wp_posts.post_date = (
SELECT min(wp_posts.post_date)
FROM qydqk_posts AS wp_posts
WHERE wp_posts.post_type = '$post_type'
AND wp_posts.post_status = 'publish'
ORDER BY wp_posts.post_date ASC
)
AND wp_posts.post_type = '$post_type'
AND wp_posts.post_status = 'publish'
ORDER BY wp_posts.post_date DESC"
);
}
return intval($result[0]->ID);
}
Проверенный хостинг
Международный хостинг-провайдер, аккредитованный национальный и международный регистратор доменных имён.
Сервис, предоставляющий широкий спектр услуг в области веб-хостинга и виртуальных серверов.