-
Notifications
You must be signed in to change notification settings - Fork 54
/
algolia.php
151 lines (127 loc) · 4.13 KB
/
algolia.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
<?php
/**
* Plugin Name: WP Search with Algolia
* Plugin URI: https://github.com/WebDevStudios/wp-search-with-algolia
* Description: Integrate the powerful Algolia search service with WordPress
* Version: 2.8.1
* Requires at least: 5.0
* Requires PHP: 7.4
* Author: WebDevStudios
* Author URI: https://webdevstudios.com
* License: GNU General Public License v2.0 / MIT License
* Text Domain: wp-search-with-algolia
* Domain Path: /languages
*
* @since 1.0.0
* @package WebDevStudios\WPSWA
*/
// The following code is a derivative work of the code from the
// Algolia Search plugin for WordPress, which is licensed GPLv2.
// This code therefore is also licensed under the terms of the GNU Public License v2.0.
// Nothing to see here if not loaded in WP context.
if ( ! defined( 'WPINC' ) ) {
die;
}
// The Algolia Search plugin version.
define( 'ALGOLIA_VERSION', '2.8.1' );
// The minmum required PHP version.
define( 'ALGOLIA_MIN_PHP_VERSION', '7.4' );
// The minimum required WordPress version.
define( 'ALGOLIA_MIN_WP_VERSION', '5.0' );
define( 'ALGOLIA_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
define( 'ALGOLIA_PLUGIN_URL', plugins_url( '/', __FILE__ ) );
if ( ! defined( 'ALGOLIA_PATH' ) ) {
define( 'ALGOLIA_PATH', __DIR__ . '/' );
}
/**
* Check for required PHP version.
*
* @author WebDevStudios <contact@webdevstudios.com>
* @since 1.1.0
*
* @return bool
*/
function algolia_php_version_check() {
if ( version_compare( PHP_VERSION, ALGOLIA_MIN_PHP_VERSION, '<' ) ) {
return false;
}
return true;
}
/**
* Check for required WordPress version.
*
* @author WebDevStudios <contact@webdevstudios.com>
* @since 1.1.0
*
* @return bool
*/
function algolia_wp_version_check() {
if ( version_compare( $GLOBALS['wp_version'], ALGOLIA_MIN_WP_VERSION, '<' ) ) {
return false;
}
return true;
}
/**
* Check if WP Search with Algolia Pro is active.
*
* @author Webdevstudios <contact@webdevstudios.com>
* @since 2.5.0
*
* @return bool
*/
function algolia_is_pro_active() {
if ( ! defined( 'WPSWA_PRO_VERSION' ) ) {
return false;
}
return true;
}
/**
* Admin notices if requirements aren't met.
*
* @author WebDevStudios <contact@webdevstudios.com>
* @since 1.1.0
*/
function algolia_requirements_error_notice() {
$notices = [];
if ( ! algolia_php_version_check() ) {
$notices[] = sprintf(
// translators: placeholder 1 is minimum required PHP version, placeholder 2 is installed PHP version.
esc_html__( 'Algolia plugin requires PHP %1$s or higher. You’re still on %2$s.', 'wp-search-with-algolia' ),
esc_html( ALGOLIA_MIN_PHP_VERSION ),
esc_html( PHP_VERSION )
);
}
if ( ! algolia_wp_version_check() ) {
$notices[] = sprintf(
// translators: placeholder 1 is minimum required WordPress version, placeholder 2 is installed WordPress version.
esc_html__( 'Algolia plugin requires at least WordPress in version %1$s, You are on %2$s.', 'wp-search-with-algolia' ),
esc_html( ALGOLIA_MIN_WP_VERSION ),
esc_html( $GLOBALS['wp_version'] )
);
}
foreach ( $notices as $notice ) {
echo '<div class="notice notice-error"><p>' . esc_html( $notice ) . '</p></div>';
}
}
/**
* I18n.
*
* @author WebDevStudios <contact@webdevstudios.com>
* @since 1.0.0
*/
function algolia_load_textdomain() {
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- This is a legitimate use of a global filter.
$locale = apply_filters( 'plugin_locale', get_locale(), 'wp-search-with-algolia' );
load_textdomain( 'wp-search-with-algolia', WP_LANG_DIR . '/wp-search-with-algolia/wp-search-with-algolia-' . $locale . '.mo' );
load_plugin_textdomain( 'wp-search-with-algolia', false, plugin_basename( dirname( __FILE__ ) ) . '/languages/' );
}
add_action( 'init', 'algolia_load_textdomain' );
if ( algolia_php_version_check() && algolia_wp_version_check() ) {
require_once ALGOLIA_PATH . 'classmap.php';
$algolia = Algolia_Plugin_Factory::create();
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'algolia', new Algolia_CLI() );
}
} else {
add_action( 'admin_notices', 'algolia_requirements_error_notice' );
}