-
Notifications
You must be signed in to change notification settings - Fork 0
/
search-attributes-for-woocommerce.php
191 lines (164 loc) · 5.53 KB
/
search-attributes-for-woocommerce.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/**
* Plugin Name: Search Attributes for WooCommerce
* Plugin URI: https://wordpress.org/plugins/search-attributes-for-woocommerce/
* Description: This WordPress plugin allows you to extend WordPress search feature by searching into Woocommerce product attributes
* Version: 1.3.4
* Author: Aslam Doctor
* Author URI: https://aslamdoctor.com/
* Developer: Aslam Doctor
* Developer URI: https://aslamdoctor.com/
* Text Domain: wsatt
* Domain Path: /languages
* Requires at least: 4.6
*
* WC requires at least: 4.3
* WC tested up to: 8.4.0
*
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*
* @package wsatt
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Check if woocommerce is installed and activated before activating plugin.
*/
function wsatt_check_required_plugin() {
if ( is_admin() && current_user_can( 'activate_plugins' ) && ! class_exists( 'WooCommerce' ) ) {
add_action( 'admin_notices', 'wsatt_plugin_notice' );
deactivate_plugins( plugin_basename( __FILE__ ) );
if ( isset( $_GET['activate'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
unset( $_GET['activate'] ); // phpcs:ignore WordPress.Security.NonceVerification
}
}
register_setting( 'wsatt-settings-group', 'wsatt_status' );
register_setting( 'wsatt-settings-group', 'wsatt_attributes' );
}
add_action( 'admin_init', 'wsatt_check_required_plugin' );
/**
* Show plugin activation notice
*/
function wsatt_plugin_notice() { ?><div class="error"><p><?php echo esc_html_e( 'Please activate Woocommerce plugin before using', 'wsatt' ); ?> <strong><?php echo esc_html_e( 'Search Attributes for WooCommerce', 'wsatt' ); ?></strong> <?php echo esc_html_e( 'plugin.', 'wsatt' ); ?></p></div>
<?php
}
/**
* Add Admin Menu.
*/
function wsatt_register_my_custom_submenu_page() {
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
$role = (array) $user->roles;
if ( in_array( 'administrator', $role, true ) ) {
wsatt_add_pages( 'manage_options' );
} elseif ( in_array( 'shop_manager', $role, true ) ) {
wsatt_add_pages( 'shop_manager' );
} elseif ( current_user_can( 'manage_woocommerce' ) ) {
wsatt_add_pages( 'shop_manager' );
}
}
}
add_action( 'admin_menu', 'wsatt_register_my_custom_submenu_page', 99 );
/**
* Add page to admin menu
*
* @param [String] $role Current User role.
*/
function wsatt_add_pages( $role ) {
add_submenu_page(
'woocommerce',
__( 'Search Attributes for WooCommerce', 'wsatt' ),
__( 'Search Attributes for WooCommerce', 'wsatt' ),
$role,
'wsatt-page',
'wsatt_callback'
);
}
/**
* Add Plugin Settings link on Plugins page.
*
* @param [Array] $links All links related to specific plugin under Admin>Plugins section.
*/
function wsatt_plugin_settings_link( $links ) {
$settings_link = admin_url( 'admin.php?page=wsatt-page' );
array_unshift( $links, '<a href="' . esc_url( $settings_link ) . '">' . __( 'Settings', 'wsatt' ) . '</a>' );
return $links;
}
$wsatt_plugin = plugin_basename( __FILE__ );
add_filter( 'plugin_action_links_' . esc_html( $wsatt_plugin ), 'wsatt_plugin_settings_link' );
/**
* Load Admin Page View.
*/
function wsatt_callback() {
include 'inc/functions.php';
include 'view.php';
}
/**
* Add Scripts and Styles.
*
* @param [String] $hook The hook to enqueue stylesheet and scripts.
*/
function wsatt_enqueue( $hook ) {
if ( 'woocommerce_page_wsatt-page' !== $hook ) {
return;
}
// Stylesheets.
wp_register_style( 'wsatt_app_css', ( plugin_dir_url( __FILE__ ) . '/css/app.css' ), false, '1.3.2' );
wp_enqueue_style( 'wsatt_app_css' );
}
add_action( 'admin_enqueue_scripts', 'wsatt_enqueue' );
/**
* Execute Hook if settings Enabled.
*/
$wsatt_status = get_option( 'wsatt_status' );
if ( isset( $wsatt_status ) && ! empty( $wsatt_status ) ) {
/**
* Apply search filters
*
* @param [Object] $query Global wp_query to modify.
*/
function wsatt_search_filter( $query ) {
if ( is_admin() ) {
return $query;
}
if ( $query->is_search() && get_search_query() ) {
add_filter( 'posts_where', 'wsatt_brands_where' );
}
return $query;
}
add_filter( 'pre_get_posts', 'wsatt_search_filter' );
/**
* Modify sql query for wp_query
*
* @param [String] $where Where condition for SQL query.
*/
function wsatt_brands_where( $where = '' ) {
global $wpdb;
$wsatt_attributes = get_option( 'wsatt_attributes' );
if ( isset( $wsatt_attributes ) && is_array( $wsatt_attributes ) && count( $wsatt_attributes ) > 0 ) {
foreach ( $wsatt_attributes as $attribute ) {
$where .= " OR $wpdb->posts.ID IN (SELECT $wpdb->posts.ID
FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE $wpdb->posts.post_type = 'product'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = '" . esc_sql( $attribute ) . "'
AND $wpdb->terms.name LIKE '%" . get_search_query() . "%')";
}
}
return $where;
}
}
/**
* Declare HPOS compatibility for the plugin
*/
function wsatt_declare_hpos_compatibility() {
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
}
}
add_action( 'before_woocommerce_init', 'wsatt_declare_hpos_compatibility' );