| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: YMM Search |
| 4 |
* Description: Customer can search for replacment parts by vehicle make, model and year. |
| 5 |
* Version: 1.0.12 |
| 6 |
* Author: Pektsekye |
| 7 |
* Author URI: http://hottons.com |
| 8 |
* License: GPLv2 |
| 9 |
* Requires at least: 4.7 |
| 10 |
* Tested up to: 6.9.1 |
| 11 |
* |
| 12 |
* Text Domain: ymm-search |
| 13 |
* Domain Path: /i18n/languages |
| 14 |
* |
| 15 |
* WC requires at least: 3.0 |
| 16 |
* WC tested up to: 10.5.2 |
| 17 |
* |
| 18 |
* @package Ymm |
| 19 |
* @author Pektsekye |
| 20 |
*/ |
| 21 |
if ( ! defined( 'ABSPATH' ) ) { |
| 22 |
exit; // Exit if accessed directly |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
final class Pektsekye_Ymm { |
| 27 |
|
| 28 |
|
| 29 |
protected static $_instance = null; |
| 30 |
|
| 31 |
protected $_pluginUrl; |
| 32 |
protected $_pluginPath; |
| 33 |
protected $_registry; |
| 34 |
|
| 35 |
public $_message = array(); |
| 36 |
|
| 37 |
protected $_lastShortcodeInd = 0; |
| 38 |
|
| 39 |
|
| 40 |
public static function instance() { |
| 41 |
if ( is_null( self::$_instance ) ) { |
| 42 |
self::$_instance = new self(); |
| 43 |
self::$_instance->initApp(); |
| 44 |
} |
| 45 |
return self::$_instance; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
public function __construct() { |
| 50 |
|
| 51 |
$this->_pluginPath = plugin_dir_path( __FILE__ ); |
| 52 |
$this->_pluginUrl = plugins_url('/', __FILE__ ); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
public function initApp() { |
| 57 |
$this->includes(); |
| 58 |
$this->init_hooks(); |
| 59 |
$this->init_controllers(); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
public function includes() { |
| 64 |
include_once( 'Widget/Selector.php' ); |
| 65 |
include_once( 'Widget/HorizontalSelector.php' ); |
| 66 |
|
| 67 |
if ( $this->is_request( 'admin' ) ) { |
| 68 |
include_once( 'Block/Adminhtml/Ymm/Selector.php' ); |
| 69 |
|
| 70 |
include_once( 'Block/Adminhtml/Product/Edit/Restriction.php' ); |
| 71 |
new Pektsekye_Ymm_Block_Adminhtml_Product_Edit_Restriction(); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
private function init_hooks() { |
| 77 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts') ); |
| 78 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts') ); |
| 79 |
|
| 80 |
add_action( 'plugins_loaded', array( $this, 'load_textdomain') ); |
| 81 |
add_action( 'widgets_init', array( $this, 'register_widgets') ); |
| 82 |
add_action( 'admin_menu', array( $this, 'set_admin_menu' ), 70 ); |
| 83 |
|
| 84 |
add_shortcode( 'ymm_selector', array( $this, 'show_selector_by_shortcode' ) ); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
private function init_controllers() { |
| 89 |
|
| 90 |
add_filter('woocommerce_product_tabs', array( $this, 'add_product_tabs' ) ); |
| 91 |
|
| 92 |
if ($this->is_request('frontend')){ |
| 93 |
include_once( 'Controller/Product.php' ); |
| 94 |
$controller = new Pektsekye_Ymm_Controller_Product(); |
| 95 |
add_filter('pre_get_posts', array( $controller, 'product_query'), 9);//call it before wc pre_get_posts |
| 96 |
} |
| 97 |
|
| 98 |
if ( $this->is_request( 'admin' )){ |
| 99 |
if ( isset($_GET['page']) && $_GET['page'] == 'ymm') { |
| 100 |
include_once( 'Controller/Adminhtml/Ymm/Selector.php' ); |
| 101 |
add_action( 'init', array( new Pektsekye_Ymm_Controller_Adminhtml_Ymm_Selector(), 'execute' ) ); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
if ( $this->is_request( 'ajax' ) && isset($_GET['action'])){ |
| 106 |
if ($_GET['action'] == 'ymm_selector_fetch') { |
| 107 |
include_once( 'Controller/Selector.php' ); |
| 108 |
$controller = new Pektsekye_Ymm_Controller_Selector(); |
| 109 |
add_action( 'wp_ajax_ymm_selector_fetch', array( $controller, 'fetch') ); |
| 110 |
add_action( 'wp_ajax_nopriv_ymm_selector_fetch', array( $controller, 'fetch') ); |
| 111 |
|
| 112 |
} elseif ($_GET['action'] == 'ymm_selector_get_categories') { |
| 113 |
include_once( 'Controller/Selector.php' ); |
| 114 |
$controller = new Pektsekye_Ymm_Controller_Selector(); |
| 115 |
add_action( 'wp_ajax_ymm_selector_get_categories', array( $controller, 'getCategories') ); |
| 116 |
add_action( 'wp_ajax_nopriv_ymm_selector_get_categories', array( $controller, 'getCategories') ); |
| 117 |
|
| 118 |
} elseif ($_GET['action'] == 'ymm_restriction_search') { |
| 119 |
include_once( 'Controller/Adminhtml/Ymm/Selector.php' ); |
| 120 |
$controller = new Pektsekye_Ymm_Controller_Adminhtml_Ymm_Selector(); |
| 121 |
add_action( 'wp_ajax_ymm_restriction_search', array( $controller, 'searchRestrictions') ); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
public function add_product_tabs($tabs) { |
| 128 |
if ($this->is_request( 'admin' )){ //WooCommerce Tab Manager |
| 129 |
return $tabs; |
| 130 |
} |
| 131 |
|
| 132 |
if (get_option('ymm_display_vehicle_fitment') != 'yes'){ |
| 133 |
return $tabs; |
| 134 |
} |
| 135 |
|
| 136 |
include_once('Block/Product/View/Tabs/Restriction.php'); |
| 137 |
$block = new Pektsekye_Ymm_Block_Product_View_Tabs_Restriction(); |
| 138 |
|
| 139 |
if (count($block->getRestrictions()) == 0){ |
| 140 |
return $tabs; |
| 141 |
} |
| 142 |
|
| 143 |
$tabs['ymm'] = array( |
| 144 |
'title' => __( 'Vehicle Fitment', 'ymm-search'), |
| 145 |
'priority' => 40, |
| 146 |
'callback' => array($block, 'page_init') |
| 147 |
); |
| 148 |
|
| 149 |
return $tabs; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
public function enqueue_admin_scripts() { |
| 154 |
global $pagenow; |
| 155 |
|
| 156 |
if (isset($_GET['page']) && $_GET['page'] == 'ymm') { |
| 157 |
wp_enqueue_style('ymm-manage-selector', $this->_pluginUrl . 'view/adminhtml/web/ymm/main.css'); |
| 158 |
} elseif ((isset($_GET['post']) && isset($_GET['action']) && $_GET['action'] == 'edit') |
| 159 |
|| ('post-new.php' == $pagenow && isset($_GET['post_type']) && $_GET['post_type'] == 'product')){ |
| 160 |
wp_enqueue_script('ymm_search_restriction', $this->_pluginUrl . 'view/adminhtml/web/product/edit/main.js', array('jquery', 'jquery-ui-widget')); |
| 161 |
wp_enqueue_style('ymm_search_restriction', $this->_pluginUrl . 'view/adminhtml/web/product/edit/main.css'); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
public function enqueue_frontend_scripts() { |
| 167 |
wp_enqueue_script('ymm', $this->_pluginUrl . 'view/frontend/web/main.js', array('jquery', 'jquery-ui-widget', 'jquery-cookie')); |
| 168 |
wp_enqueue_style( 'ymm', $this->_pluginUrl . 'view/frontend/web/main.css' ); |
| 169 |
if (get_post_type() == 'product') { |
| 170 |
wp_enqueue_style( 'ymm_product_restriction', $this->_pluginUrl . 'view/frontend/web/product/restriction.css' ); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
public function load_textdomain() { |
| 176 |
load_plugin_textdomain( 'ymm-search', false, basename( dirname( __FILE__ ) ) . '/i18n/languages' ); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
public function register_widgets() { |
| 181 |
register_widget('Pektsekye_Ymm_Widget_Selector'); |
| 182 |
register_widget('Pektsekye_Ymm_Widget_HorizontalSelector'); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
public function set_admin_menu() { |
| 187 |
add_menu_page( _x( 'YMM Search', 'Admin menu', 'ymm-search'), _x( 'YMM Search', 'Admin menu', 'ymm-search'), 'manage_woocommerce', 'ymm', array( new Pektsekye_Ymm_Block_Adminhtml_Ymm_Selector(), 'page_init' ) ); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
public function show_selector_by_shortcode($atts) { |
| 192 |
|
| 193 |
include_once($this->getPluginPath() . 'Block/Selector.php'); |
| 194 |
|
| 195 |
$block = new Pektsekye_Ymm_Block_Selector(); |
| 196 |
|
| 197 |
$this->_lastShortcodeInd++; |
| 198 |
|
| 199 |
$block->setWidgetId('content_' . $this->_lastShortcodeInd); |
| 200 |
|
| 201 |
if (isset($atts['template'])){ |
| 202 |
$template = str_replace('/', '', trim($atts['template'])); // do not allow to change directory |
| 203 |
$block->setTemplate($template); |
| 204 |
} |
| 205 |
|
| 206 |
$garageEnabled = isset($atts['garage']) && $atts['garage'] == 1 ? 1 : 0; |
| 207 |
$block->setGarageEnabled($garageEnabled); |
| 208 |
|
| 209 |
$removeFromGarage = isset($atts['remove_from_garage_enabled']) && $atts['remove_from_garage_enabled'] == 1 ? 1 : 0; |
| 210 |
$block->setRemoveFromGarageEnabled($removeFromGarage); |
| 211 |
|
| 212 |
$filterCategoryPage = isset($atts['filter_category_page']) && $atts['filter_category_page'] == 0 ? 0 : 1; |
| 213 |
$block->setFilterCategoryPage($filterCategoryPage); |
| 214 |
|
| 215 |
ob_start(); |
| 216 |
|
| 217 |
$block->page_init(); |
| 218 |
|
| 219 |
$contents = ob_get_clean(); |
| 220 |
|
| 221 |
return $contents; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
private function is_request( $type ) { |
| 226 |
switch ( $type ) { |
| 227 |
case 'admin' : |
| 228 |
return is_admin(); |
| 229 |
case 'ajax' : |
| 230 |
return defined( 'DOING_AJAX' ); |
| 231 |
case 'cron' : |
| 232 |
return defined( 'DOING_CRON' ); |
| 233 |
case 'frontend' : |
| 234 |
return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' ); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
public function getPluginUrl() { |
| 240 |
return $this->_pluginUrl; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
public function getPluginPath() { |
| 245 |
return $this->_pluginPath; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
public function register($key, $value) { |
| 250 |
return $this->_registry[$key] = $value; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
public function registry($key) { |
| 255 |
return isset($this->_registry[$key]) ? $this->_registry[$key] : null; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
public function setMessage($message, $type = 'text') { |
| 260 |
if ($type == 'error_lines'){ |
| 261 |
$this->_message[$type][] = $message; |
| 262 |
} else { |
| 263 |
$this->_message[$type] = $message; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
public function getMessage() { |
| 269 |
return $this->_message; |
| 270 |
} |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
function Pektsekye_YMM() { |
| 276 |
return Pektsekye_Ymm::instance(); |
| 277 |
} |
| 278 |
|
| 279 |
include_once('Setup/Install.php'); |
| 280 |
register_activation_hook(__FILE__, array('Pektsekye_Ymm_Setup_Install', 'install')); |
| 281 |
|
| 282 |
// If WooCommerce plugin is installed and active. |
| 283 |
if (in_array('woocommerce/woocommerce.php', (array) get_option('active_plugins', array())) || in_array('woocommerce/woocommerce.php', array_keys((array) get_site_option('active_sitewide_plugins', array())))){ |
| 284 |
Pektsekye_YMM(); |
| 285 |
} |
| 286 |
|
| 287 |
// define compatibility with WooCommerce HPOS (High-Performance Order Storage) |
| 288 |
add_action( 'before_woocommerce_init', function() { |
| 289 |
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) { |
| 290 |
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true ); |
| 291 |
} |
| 292 |
} ); |
| 293 |
|
| 294 |
// disable adding <p> tags into the HTML code rendered from shortcode |
| 295 |
if (function_exists('wp_is_block_theme') && wp_is_block_theme()){ |
| 296 |
remove_action('init', 'register_block_core_shortcode'); |
| 297 |
} |
| 298 |
|
| 299 |
|