/** * Plugin Name: Ajax поиск * Author URI: https://t.me/devnullik * Author: under water * Version: 1.0 * * Requires at least: 2.5 * Requires PHP: 7.2 * * License: GPL2 * License URI: https://www.gnu.org/licenses/gpl-2.0.html **/ class AjaxSearch { public function __construct() { add_action('init', [$this, 'init']); add_action('add_meta_boxes', array($this, 'addMetaBox')); add_action('save_post', array($this, 'savePost'), 10, 3); add_action('wp_enqueue_scripts', array($this, 'enqueueScripts')); add_shortcode('edit-currencies', [$this, 'editCurrencies']); add_shortcode('ajax-search-variations', [$this, 'ajaxSearchVariations']); add_shortcode('ajax-search-variation-active', [$this, 'ajaxSearchVariationActive']); } public function init() { register_post_type('search-variations', [ 'label' => null, 'labels' => [ 'name' => 'Вариации поиска', 'singular_name' => 'Вариации поиска', 'add_new' => 'Добавить', 'add_new_item' => 'Добавить', 'edit_item' => 'Редактировать', 'new_item' => 'Вариации поиска', 'view_item' => 'Просмотреть', 'search_items' => 'Поиск', 'not_found' => 'Не найдено', 'not_found_in_trash' => 'Не найдено', 'parent_item_colon' => '', 'menu_name' => 'Вариации поиска', ], 'description' => '', 'menu_icon' => 'dashicons-media-document', 'publicly_queryable' => false, 'public' => true, 'exclude_from_search' => null, 'show_ui' => true, 'show_in_nav_menus' => false, 'show_in_menu' => true, 'rest_base' => null, 'menu_position' => 49, 'hierarchical' => true, 'supports' => ['title'], 'show_in_rest' => true, 'taxonomies' => [], 'has_archive' => true, 'rewrite' => true, 'query_var' => true ]); register_post_type('currencies', [ 'label' => null, 'labels' => [ 'name' => 'Валюты', 'singular_name' => 'Валюты', 'add_new' => 'Добавить', 'add_new_item' => 'Добавить', 'edit_item' => 'Редактировать', 'new_item' => 'Валюты', 'view_item' => 'Просмотреть', 'search_items' => 'Поиск', 'not_found' => 'Не найдено', 'not_found_in_trash' => 'Не найдено', 'parent_item_colon' => '', 'menu_name' => 'Валюты', ], 'description' => '', 'menu_icon' => 'dashicons-money-alt', 'publicly_queryable' => false, 'public' => true, 'exclude_from_search' => null, 'show_ui' => true, 'show_in_nav_menus' => false, 'show_in_menu' => true, 'rest_base' => null, 'menu_position' => 49, 'hierarchical' => true, 'supports' => ['title'], 'show_in_rest' => true, 'taxonomies' => [], 'has_archive' => true, 'rewrite' => true, 'query_var' => true ]); } public function enqueueScripts() { $directory = plugins_url().'/ajax-search/'; wp_enqueue_style("AjaxSearch-style", "{$directory}assets/style.css"); wp_enqueue_script("AjaxSearch-cookie", "{$directory}assets/cookie.js", ['jquery'], null, true); wp_enqueue_script("AjaxSearch-script", "{$directory}assets/script.js", ['AjaxSearch-cookie'], null, true); global $wpdb; $ress = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}postmeta WHERE meta_key = 'estate-price-object' AND meta_value != ''"); $curs = $arr = []; $currencies = get_posts([ 'numberposts' => -1, 'post_status' => 'publish', 'post_type' => 'currencies' ]); foreach( $currencies as $cur ){ $curs[$cur->ID] = get_post_meta($cur->ID, '_currencies_course', true)*1; } foreach( $ress as $res ){ $prc = $res->meta_value*1; $arr[$res->post_id] = [0 => $prc]; foreach( $curs as $id => $cur ){ $arr[$res->post_id][$id] = $cur * $prc; } } wp_localize_script("AjaxSearch-script", 'currencies', $arr); } public function addMetaBox() { add_meta_box("AjaxSearch-box", 'AjaxSearch', [$this, 'metaBox'], 'search-variations', 'normal', 'high'); add_meta_box("AjaxSearch-box", 'AjaxSearch', [$this, 'metaBoxСurrencies'], 'currencies', 'normal', 'high'); } public function savePost( $ID = null, $post = null, $update = null ) { if( isset($_POST['AjaxSearch']) && is_array($_POST['AjaxSearch']) && $post ){ foreach( $_POST['AjaxSearch'] as $key => $val ){ update_post_meta($post->ID, $key, $val); } } } public function metaBox() { $postID = $_GET['post']; include __DIR__ . '/meta-box.php'; } public function metaBoxСurrencies() { $postID = $_GET['post']; include __DIR__ . '/currencies.php'; } public function ajaxSearchVariations() { echo '
USD / $
{$item['icon']}{$item['title']}
"; if( count($item['vars']) > 0 ){ $i = 0; foreach( $item['vars'] as $val => $vars ){ $vars[] = $val; $ul .= "'.$active.'