158 lines
4.3 KiB
PHP
158 lines
4.3 KiB
PHP
<?php
|
|
|
|
/**
|
|
* plugin controller.
|
|
*
|
|
* @author Ivan Filipov <ivan@web-lab.solutions>
|
|
*
|
|
* @version 1.0
|
|
* @date updated Feb 26, 2023
|
|
*/
|
|
namespace wlurep;
|
|
|
|
class Plugin {
|
|
private static $_instance = null;
|
|
|
|
/**
|
|
* Plugin constructor.
|
|
*
|
|
* Initializing WL update real estates plugin.
|
|
*
|
|
* @since 1.0.0
|
|
* @access private
|
|
*/
|
|
|
|
private function __construct() {
|
|
$this->register_autoloader();
|
|
$this->register_json_api();
|
|
if(is_admin()){
|
|
$this->register_admin_menu();
|
|
global $pagenow;
|
|
if ( $pagenow === 'options.php' || $pagenow === 'options-general.php' && isset($_GET['page']) && $_GET['page'] == 'wl-urep-settings') {
|
|
// WL settings page
|
|
$this->register_settings_page();
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Register autoloader.
|
|
*
|
|
* WL update real estate autoloader, that loads all the classes needed to run the plugin.
|
|
*
|
|
* @since 1.6.0
|
|
* @access private
|
|
*/
|
|
private function register_autoloader() {
|
|
require_once WL_UREP_PATH . '/Loader.php';
|
|
|
|
Loader::registerAutoLoad();
|
|
}
|
|
/**
|
|
* Register json api class.
|
|
*
|
|
*
|
|
* @since 1.6.0
|
|
* @access private
|
|
*/
|
|
private function register_json_api() {
|
|
$jsonAPI = \wlurep\JSONAPI::getInstance();
|
|
}
|
|
|
|
/**
|
|
* Add function to admin_menu hook
|
|
*/
|
|
private function register_admin_menu() {
|
|
add_action('admin_menu', array($this, 'addSettingsMenu'));
|
|
}
|
|
/**
|
|
* Add function to admin_init hook
|
|
*/
|
|
private function register_settings_page() {
|
|
add_action('admin_init', array($this, 'settingsPage'));
|
|
}
|
|
|
|
/**
|
|
* Add menu
|
|
*/
|
|
public function addSettingsMenu() {
|
|
// Add Role management page to user menu
|
|
add_options_page( __('Automatic update real estates', 'wlurep'), __('Automatic update real estates', 'wlurep'), 'manage_options', 'wl-urep-settings', array($this, 'page'));
|
|
}
|
|
|
|
/*
|
|
* Function that generate REST API settings page
|
|
*/
|
|
public function page() {
|
|
if (!current_user_can('manage_options')) {
|
|
return;
|
|
}
|
|
?>
|
|
<form method="POST" action="options.php">
|
|
<?php
|
|
settings_fields( 'wl_options' );
|
|
do_settings_sections( 'wl_urep_section' );
|
|
submit_button();
|
|
|
|
?>
|
|
</form>
|
|
<?php
|
|
}
|
|
/**
|
|
* function that generate settings page
|
|
*/
|
|
public function settingsPage() {
|
|
register_setting('wl_options', 'wl_options', array($this, 'wl_urep_settings_validate'));
|
|
add_settings_section('wl_urep_api_settings', __('WL REST API Settings', 'wlurep'), array($this, 'wl_urep_section_text'), 'wl_urep_section');
|
|
|
|
add_settings_field('wl_urep_setting_api_url', __('API url', 'wlurep'), array($this, 'wl_urep_setting_api_url'), 'wl_urep_section', 'wl_urep_api_settings');
|
|
add_settings_field('wl_urep_setting_api_key', __('API Token', 'wlurep'), array($this, 'wl_urep_setting_api_key'), 'wl_urep_section', 'wl_urep_api_settings');
|
|
}
|
|
/**
|
|
* function that generate section text
|
|
*/
|
|
public function wl_urep_section_text() {
|
|
echo '<p>'.__('Here you can set all the options for using the REST API that automatic update real estate properties', 'wlurep').'</p>';
|
|
}
|
|
/**
|
|
* function that generate API token field
|
|
*/
|
|
public function wl_urep_setting_api_key() {
|
|
$apiKey = wl_get_option('urep_api_key', '');
|
|
echo "<input id='wl_urep_setting_api_key' name='wl_options[urep_api_key]' type='text' value='" . $apiKey. "' class='regular-text' />";
|
|
}
|
|
/**
|
|
* function that generate API url field
|
|
*/
|
|
public function wl_urep_setting_api_url() {
|
|
$apiUrl = wl_get_option('urep_api_url', '');
|
|
echo "<input id='wl_urep_setting_api_url' name='wl_options[urep_api_url]' type='text' value='" . $apiUrl . "' class='regular-text' />";
|
|
}
|
|
/**
|
|
* function that validate saved fields
|
|
*/
|
|
public function wl_urep_settings_validate($input) {
|
|
$api_key = trim($input['urep_api_key']);
|
|
if (!preg_match('/^[a-z0-9-]+$/i', $api_key)) {
|
|
$input['urep_api_key'] = '';
|
|
add_settings_error('urep_error', 'wl_urep_setting_api_key', __('The API key can contain lower letters, numbers and dash', 'wlurep'), 'error');
|
|
}
|
|
$input['urep_api_url'] = sanitize_url($input['urep_api_url']);
|
|
|
|
return $input;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return \wlurep\Plugin
|
|
*/
|
|
public static function getInstance() {
|
|
if (self::$_instance == null) {
|
|
self::$_instance = new self;
|
|
}
|
|
return self::$_instance;
|
|
}
|
|
}
|
|
|
|
Plugin::getInstance();
|
|
|