plugin_name = 'What3wordsSearchbox';
if (is_admin()) {
$this->hook('plugins_loaded');
}
}
public static function get_instance() {
if (!isset(self::$instance)) {
$c = __CLASS__;
self::$instance = new $c();
}
return self::$instance;
}
/**
* "plugins_loaded" action hook; fired after active plugins and pluggable functions are
* loaded.
*/
public function plugins_loaded() {
$this->hook('admin_init');
$this->hook('admin_menu');
$this->hook('admin_enqueue_scripts');
}
public function admin_enqueue_scripts() {
$handle = 'what3words-expander-admin-js';
$src = WHAT3WORDS_SEARCHBOX_URL . 'assets/js/what3words-searchbox-admin.js';
$deps = ['jquery'];
$ver = NULL;
$in_footer = true;
wp_enqueue_script($handle, $src, $deps, $ver, $in_footer);
}
/**
* "plugin_action_links_[plugin-name]" filter hook; applied to the list of links to display
* on the plugins page (beside the activate/deactivate links)
*/
public function action_links($actions, $plugin_file, $plugin_data, $context) {
$settings = sprintf('%s', admin_url('options-general.php?page=3-word-address-validation-field/What3wordsSearchboxAdmin.php'), __('Settings', 'what3words-searchbox'));
array_unshift($actions, $settings);
return $actions;
}
/**
* "admin_init" action hook; called after the admin panel is initialised.
*/
public function admin_init () {
if (!empty($GLOBALS['pagenow']) && $GLOBALS['pagenow'] === 'plugins.php') {
$errors = $this->check_plugin_requirements();
if (!empty($errors)) {
unset($_GET['activate']);
$name = get_file_data(WHAT3WORDS_SEARCHBOX_FILE, ['Plugin Name'], 'plugin');
printf(
'
%1$s
%2$s has been deactivated.
',
join('', $errors),
$name[0]
);
deactivate_plugins(WHAT3WORDS_SEARCHBOX_NAME);
return;
}
}
$this->hook('plugin_action_links_' . WHAT3WORDS_SEARCHBOX_NAME, 'action_links');
$this->upgrade_settings();
$this->hook('admin_notices');
}
/**
* "admin_menu" action hook; called after the basic admin panel menu structure is in
* place.
*/
public function admin_menu() {
if (function_exists('add_options_page')) {
$page = __('What3words Searchbox - Settings', 'what3words-searchbox');
$menu = __('What3words Searchbox', 'what3words-searchbox');
add_options_page($page, $menu, 'manage_options', __FILE__, [$this, 'display_settings']);
}
}
/**
* "admin_notices" action hook; displays notices near the top of the admin pages.
*/
public function admin_notices() {
$meta = get_current_screen();
if (!empty($GLOBALS['pagenow']) && ($GLOBALS['pagenow'] === 'plugins.php' || ($GLOBALS['pagenow'] === 'options-general.php' && $meta->base !== 'settings_page_what3words-searchbox/What3wordsSearchboxAdmin'))) {
$settings = $this->get_option();
if (current_user_can('manage_options')) {
$notice = [];
if (empty($settings['api_key'])) {
$notice[] = __('You need to enter your what3words API key to use the What3words Searchbox plugin', 'what3words-searchbox');
}
if (empty($settings['input_selectors']) && !$settings['woocommerce_enabled']) {
$notice[] = __('You need to enter at least one jQuery input selector to use the What3words Searchbox plugin', 'what3words-searchbox');
}
if (!empty($notice)) {
$notice[] = sprintf('You can go to the What3words Searchbox Settings page to do this now', admin_url('options-general.php?page=3-word-address-validation-field/What3wordsSearchboxAdmin.php'));
echo '
' . implode('. ', $notice) . '
';
}
}
}
}
private function check_plugin_requirements() {
$min_version = '5.6';
$errors = [];
$curr_version = phpversion();
if (version_compare($min_version, $curr_version, '>')) {
$errors[] = sprintf(__('Cannot activate What3words Searchbox; your server is running PHP version %d but this plugin requires at least PHP %s', 'what3words-searchbox'), $curr_version, $min_version);
}
return $errors;
}
/**
* Called in response to the "admin_init" action hook; checks the current set of
* settings/options and upgrades them according to the new version of the plugin.
*/
private function upgrade_settings() {
$this->upgrade_v1_settings();
$settings = null;
$settings = $this->get_option();
if (is_array($settings) && !empty($settings['version']) && $settings['version'] === What3wordsSearchbox::VERSION) {
return;
}
if (!is_array($settings)) {
What3wordsSearchbox::get_instance()->add_settings();
}
}
private function upgrade_v1_settings() {
$legacy_settings = get_option('w3w_options');
if (is_array($legacy_settings) && !empty($legacy_settings)) {
$settings = $this->get_option();
if (!is_array($settings)) {
return;
}
if (isset($legacy_settings['w3w_field_api_key']) && !empty($legacy_settings['w3w_field_api_key'])) {
$settings['api_key'] = $legacy_settings['w3w_field_api_key'];
}
if (isset($legacy_settings['w3w_field_input']) && !empty($legacy_settings['w3w_field_input'])) {
$settings['input_selectors'] = $legacy_settings['w3w_field_input'];
}
if (isset($legacy_settings['w3w_field_lang_auto']) && !empty($legacy_settings['w3w_field_lang_auto'])) {
$settings['multilingual'] = $this->to_boolean($legacy_settings['w3w_field_lang_auto']);
}
if (isset($legacy_settings['w3w_field_lang']) && !empty($legacy_settings['w3w_field_lang'])) {
$settings['lang'] = $legacy_settings['w3w_field_lang'];
}
if (isset($legacy_settings['w3w_field_lang']) && !empty($legacy_settings['w3w_field_lang'])) {
$settings['lang'] = $legacy_settings['w3w_field_lang'];
}
if (isset($legacy_settings['w3w_field_items_to_show']) && !empty($legacy_settings['w3w_field_items_to_show'])) {
$settings['suggestions'] = intval($legacy_settings['w3w_field_items_to_show']);
}
if (isset($legacy_settings['w3w_field_placeholder']) && !empty($legacy_settings['w3w_field_placeholder'])) {
$settings['input_placeholder'] = $legacy_settings['w3w_field_placeholder'];
}
if (isset($legacy_settings['w3w_field_direction']) && !empty($legacy_settings['w3w_field_direction'])) {
$settings['text_direction'] = $legacy_settings['w3w_field_direction'];
}
if (isset($legacy_settings['w3w_field_woocommerce_fields']) && !empty($legacy_settings['w3w_field_woocommerce_fields'])) {
$settings['woocommerce_enabled'] = $this->to_boolean($legacy_settings['w3w_field_woocommerce_fields']);
}
// self::update_option($settings);
// delete_option('w3w_options');
}
}
public function display_settings() {
$settings = $this->save_settings();
$display = '';
$disabled = '';
if (!$settings['country_filter'] && !$settings['country_filter_selector']) {
$display = 'style="display: none;"';
}
if($settings['country_filter_selector'] !== '') {
$disabled = 'disabled=true';
}
if($settings['disabled_selector_bool'] != true) {
$disabled_selector_bool = 'style="display: none;"';
}
if (empty($settings['api_key'])) {
echo '' . __('You need to enter your what3words API key to use this plugin', 'what3words-searchbox') . '
';
}
if (empty($settings['input_selectors']) && !$settings['woocommerce_enabled']) {
echo '' . __('You need to enter at least one jQuery selector to use this plugin', 'what3words-searchbox') . '
';
}
?>
' . esc_html(get_admin_page_title()); ?>
get_option();
if (!empty($_POST['what3words-searchbox-submit'])) {
if (strstr($_GET['page'], '3-word-address-validation-field') && check_admin_referer('what3words-searchbox-save-settings', 'what3words-searchbox-nonce')) {
$settings['api_key'] = $this->option('what3words-searchbox-api-key');
$settings['input_selectors'] = $this->option('what3words-searchbox-input-selector');
$settings['input_placeholder'] = $this->option('what3words-searchbox-input-placeholder');
$settings['disabled_selector_bool'] = $this->boolean_option('what3words-disable-field-boolean');
$settings['disabled_selector'] = $this->option('what3words-disable-element-selector');
$settings['suggestions'] = $this->option('what3words-searchbox-suggestion-count');
$settings['text_direction'] = $this->option('what3words-searchbox-text-direction');
$settings['lang'] = $this->option('what3words-searchbox-language');
$settings['multilingual'] = $this->boolean_option('what3words-searchbox-multilingual');
$settings['country_filter'] = $this->boolean_option('what3words-searchbox-country-filter');
$settings['country_filter_selector'] = $this->option('what3words-searchbox-country-filter-selector');
$settings['country_code'] = $this->option('what3words-searchbox-country');
$settings['woocommerce_enabled'] = $this->boolean_option('what3words-searchbox-woocommerce');
$this->update_option($settings);
?>
to_boolean(sanitize_text_field($_POST[$field])) : false);
}
private function to_boolean($value) {
if (!is_string($value)) {
return (bool) $value;
}
switch (strtolower($value)) {
case '1':
case 'true':
case 'on':
case 'yes':
case 'y':
return true;
default:
return false;
}
}
}
} // end-if (!class_exists('What3wordsSearchboxAdmin'))
?>