forked from 10up/ElasticPress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elasticpress.php
240 lines (199 loc) · 5.71 KB
/
elasticpress.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/**
* Plugin Name: ElasticPress
* Description: A fast and flexible search and query engine for WordPress.
* Version: 3.0a
* Author: Taylor Lovett, Matt Gross, Aaron Holbrook, 10up
* Author URI: http://10up.com
* License: GPLv2 or later
* Text Domain: elasticpress
* Domain Path: /lang/
* This program derives work from Alley Interactive's SearchPress
* and Automattic's VIP search plugin:
*
* Copyright (C) 2012-2013 Automattic
* Copyright (C) 2013 SearchPress
*
* @package elasticpress
*/
namespace ElasticPress;
use \WP_CLI as WP_CLI;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
define( 'EP_URL', plugin_dir_url( __FILE__ ) );
define( 'EP_PATH', plugin_dir_path( __FILE__ ) );
define( 'EP_VERSION', '3.0' );
/**
* PSR-4-ish autoloading
*
* @since 2.6
*/
spl_autoload_register(
function( $class ) {
// project-specific namespace prefix.
$prefix = 'ElasticPress\\';
// base directory for the namespace prefix.
$base_dir = __DIR__ . '/includes/classes/';
// does the class use the namespace prefix?
$len = strlen( $prefix );
if ( strncmp( $prefix, $class, $len ) !== 0 ) {
return;
}
$relative_class = substr( $class, $len );
$file = $base_dir . str_replace( '\\', '/', $relative_class ) . '.php';
// if the file exists, require it.
if ( file_exists( $file ) ) {
require $file;
}
}
);
/**
* We compare the current ES version to this compatibility version number. Compatibility is true when:
*
* EP_ES_VERSION_MIN <= YOUR ES VERSION <= EP_ES_VERSION_MAX
*
* We don't check minor releases so if your ES version if 5.1.1, we consider that 5.1 in our comparison.
*
* @since 2.2
*/
define( 'EP_ES_VERSION_MAX', '6.3' );
define( 'EP_ES_VERSION_MIN', '1.7' );
require_once __DIR__ . '/includes/compat.php';
require_once __DIR__ . '/includes/utils.php';
// Define a constant if we're network activated to allow plugin to respond accordingly.
$network_activated = Utils\is_network_activated( plugin_basename( __FILE__ ) );
if ( $network_activated ) {
define( 'EP_IS_NETWORK', true );
}
global $wp_version;
/**
* Handle indexables
*/
Indexables::factory()->register( new Indexable\Post\Post() );
/**
* Handle features
*/
Features::factory()->register_feature(
new Feature\Search\Search()
);
Features::factory()->register_feature(
new Feature\ProtectedContent\ProtectedContent()
);
Features::factory()->register_feature(
new Feature\Autosuggest\Autosuggest()
);
Features::factory()->register_feature(
new Feature\RelatedPosts\RelatedPosts()
);
Features::factory()->register_feature(
new Feature\WooCommerce\WooCommerce()
);
Features::factory()->register_feature(
new Feature\Facets\Facets()
);
Features::factory()->register_feature(
new Feature\Documents\Documents()
);
if ( version_compare( $wp_version, '5.1', '>=' ) || 0 === stripos( $wp_version, '5.1-' ) ) {
Features::factory()->register_feature(
new Feature\Users\Users()
);
}
/**
* Set the availability of dashboard sync functionality. Defaults to true (enabled).
*
* Sync can be disabled by defining EP_DASHBOARD_SYNC as false in wp-config.php.
* NOTE: Must be defined BEFORE `require_once(ABSPATH . 'wp-settings.php');` in wp-config.php.
*
* @since 2.3
*/
if ( ! defined( 'EP_DASHBOARD_SYNC' ) ) {
define( 'EP_DASHBOARD_SYNC', true );
}
/**
* Setup dashboard
*/
require_once __DIR__ . '/includes/dashboard.php';
Dashboard\setup();
/**
* WP CLI Commands
*/
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'elasticpress', __NAMESPACE__ . '\Command' );
}
/**
* Handle upgrades. Certain version require a re-sync on upgrade.
*
* @since 2.2
*/
function handle_upgrades() {
if ( ! is_admin() || defined( 'DOING_AJAX' ) ) {
return;
}
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
$last_sync = get_site_option( 'ep_last_sync', 'never' );
} else {
$last_sync = get_option( 'ep_last_sync', 'never' );
}
// No need to upgrade since we've never synced
if ( empty( $last_sync ) || 'never' === $last_sync ) {
return;
}
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
$old_version = get_site_option( 'ep_version', false );
} else {
$old_version = get_option( 'ep_version', false );
}
/**
* Reindex if we cross a reindex version in the upgrade
*/
$reindex_versions = apply_filters(
'ep_reindex_versions',
array(
'2.2',
'2.3.1',
'2.4',
'2.5.1',
'2.6',
'2.7',
'3.0',
)
);
$need_upgrade_sync = false;
if ( false === $old_version ) {
$need_upgrade_sync = true;
} else {
$last_reindex_version = $reindex_versions[ count( $reindex_versions ) - 1 ];
if ( -1 === version_compare( $old_version, $last_reindex_version ) && 0 <= version_compare( EP_VERSION, $last_reindex_version ) ) {
$need_upgrade_sync = true;
}
}
if ( $need_upgrade_sync ) {
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
update_site_option( 'ep_need_upgrade_sync', true );
} else {
update_option( 'ep_need_upgrade_sync', true );
}
}
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) {
update_site_option( 'ep_version', sanitize_text_field( EP_VERSION ) );
} else {
update_option( 'ep_version', sanitize_text_field( EP_VERSION ) );
}
}
add_action( 'plugins_loaded', __NAMESPACE__ . '\handle_upgrades', 5 );
/**
* Load text domain and handle debugging
*
* @since 2.2
*/
function setup_misc() {
load_plugin_textdomain( 'elasticpress', false, basename( __DIR__ ) . '/lang' ); // Load any available translations first.
if ( is_user_logged_in() && ! defined( 'WP_EP_DEBUG' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
define( 'WP_EP_DEBUG', is_plugin_active( 'debug-bar-elasticpress/debug-bar-elasticpress.php' ) );
}
}
add_action( 'plugins_loaded', __NAMESPACE__ . '\setup_misc' );
do_action( 'elasticpress_loaded' );