| 1 |
<?php |
| 2 |
/** |
| 3 |
* Settings screen. |
| 4 |
* |
| 5 |
* @since 5.0.0 |
| 6 |
* @package ElasticPress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress\Screen; |
| 10 |
|
| 11 |
use ElasticPress\Screen; |
| 12 |
use ElasticPress\Utils; |
| 13 |
|
| 14 |
defined( 'ABSPATH' ) || exit; |
| 15 |
|
| 16 |
/** |
| 17 |
* Settings screen Class. |
| 18 |
*/ |
| 19 |
class Settings { |
| 20 |
/** |
| 21 |
* Previous language |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
protected $prev_ep_language = ''; |
| 26 |
|
| 27 |
/** |
| 28 |
* Previous URL host |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
protected $prev_ep_host = ''; |
| 33 |
|
| 34 |
/** |
| 35 |
* Previous credentials array |
| 36 |
* |
| 37 |
* @var array |
| 38 |
*/ |
| 39 |
protected $prev_ep_credentials = []; |
| 40 |
|
| 41 |
/** |
| 42 |
* Previous "Content Items per Index Cycle" setting |
| 43 |
* |
| 44 |
* @var int |
| 45 |
*/ |
| 46 |
protected $prev_ep_bulk_setting = 350; |
| 47 |
|
| 48 |
/** |
| 49 |
* Initialize class |
| 50 |
*/ |
| 51 |
public function setup() { |
| 52 |
add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); |
| 53 |
add_action( 'admin_init', [ $this, 'action_admin_init' ], 8 ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Enqueue script |
| 58 |
*/ |
| 59 |
public function admin_enqueue_scripts() { |
| 60 |
if ( 'settings' !== Screen::factory()->get_current_screen() ) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
wp_enqueue_script( |
| 65 |
'ep_settings_scripts', |
| 66 |
EP_URL . 'dist/js/settings-script.js', |
| 67 |
Utils\get_asset_info( 'settings-script', 'dependencies' ), |
| 68 |
Utils\get_asset_info( 'settings-script', 'version' ), |
| 69 |
true |
| 70 |
); |
| 71 |
|
| 72 |
wp_set_script_translations( 'ep_settings_scripts', 'elasticpress' ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Admin-init actions |
| 77 |
* |
| 78 |
* Sets up Settings API. |
| 79 |
*/ |
| 80 |
public function action_admin_init() { |
| 81 |
$post = wp_unslash( $_POST ); |
| 82 |
|
| 83 |
if ( empty( $post['ep_settings_nonce'] ) || ! wp_verify_nonce( $post['ep_settings_nonce'], 'elasticpress_settings' ) ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
$this->prev_ep_language = Utils\get_language(); |
| 88 |
$this->prev_ep_host = Utils\get_host(); |
| 89 |
$this->prev_ep_credentials = Utils\get_epio_credentials(); |
| 90 |
$this->prev_ep_bulk_setting = Utils\get_option( 'ep_bulk_setting', 350 ); |
| 91 |
|
| 92 |
$language = sanitize_text_field( $post['ep_language'] ); |
| 93 |
Utils\update_option( 'ep_language', $language ); |
| 94 |
|
| 95 |
if ( isset( $post['ep_host'] ) ) { |
| 96 |
$host = esc_url_raw( trim( $post['ep_host'] ) ); |
| 97 |
Utils\update_option( 'ep_host', $host ); |
| 98 |
} |
| 99 |
|
| 100 |
if ( isset( $post['ep_credentials'] ) && ( ! defined( 'EP_CREDENTIALS' ) || ! EP_CREDENTIALS ) ) { |
| 101 |
$credentials = Utils\sanitize_credentials( $post['ep_credentials'] ); |
| 102 |
|
| 103 |
// Preserve the existing token if the field was left empty (it is always empty on load). |
| 104 |
if ( empty( $credentials['token'] ) ) { |
| 105 |
$credentials['token'] = $this->prev_ep_credentials['token']; |
| 106 |
} |
| 107 |
|
| 108 |
Utils\update_option( 'ep_credentials', $credentials ); |
| 109 |
} |
| 110 |
|
| 111 |
if ( isset( $post['ep_bulk_setting'] ) ) { |
| 112 |
Utils\update_option( 'ep_bulk_setting', $this->sanitize_bulk_settings( $post['ep_bulk_setting'] ) ); |
| 113 |
} |
| 114 |
|
| 115 |
$es_info = \ElasticPress\Elasticsearch::factory()->get_elasticsearch_info( true ); |
| 116 |
if ( empty( $es_info['version'] ) ) { |
| 117 |
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { |
| 118 |
add_action( 'network_admin_notices', [ $this, 'add_validation_notice' ] ); |
| 119 |
} else { |
| 120 |
add_action( 'admin_notices', [ $this, 'add_validation_notice' ] ); |
| 121 |
} |
| 122 |
|
| 123 |
unset( $_POST['ep_host'] ); // Needed to prevent going to the next installation step |
| 124 |
$this->reset_settings(); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Add a notice to the Settings form when the host was not set yet |
| 130 |
*/ |
| 131 |
public function add_validation_notice() { |
| 132 |
$target = ( Utils\is_epio() ) ? |
| 133 |
_x( 'ElasticPress.io account', 'Settings validation message', 'elasticpress' ) : |
| 134 |
_x( 'Elasticsearch server', 'Settings validation message', 'elasticpress' ); |
| 135 |
|
| 136 |
$is_first_setup = empty( $this->prev_ep_host ); |
| 137 |
|
| 138 |
if ( $is_first_setup ) { |
| 139 |
// Setting it for the first time -- probably during the install process. |
| 140 |
$message = sprintf( |
| 141 |
/* translators: EP.io account or ES server. */ |
| 142 |
__( 'It was not possible to connect to your %s. Please check your settings and try again.', 'elasticpress' ), |
| 143 |
$target |
| 144 |
); |
| 145 |
} else { |
| 146 |
$message = sprintf( |
| 147 |
/* translators: EP.io account or ES server. */ |
| 148 |
__( 'It was not possible to connect to your %s. Your settings were reverted.', 'elasticpress' ), |
| 149 |
$target |
| 150 |
); |
| 151 |
} |
| 152 |
?> |
| 153 |
<div class="notice notice-error"> |
| 154 |
<p> |
| 155 |
<?php echo wp_kses( $message, 'ep-html' ); ?> |
| 156 |
</p> |
| 157 |
<?php if ( Utils\is_epio() && ! $is_first_setup ) : ?> |
| 158 |
<p> |
| 159 |
<?php esc_html_e( 'If you are trying to deactivate your site\'s communication with ElasticPress.io, temporarily disable the ElasticPress plugin instead.', 'elasticpress' ); ?> |
| 160 |
</p> |
| 161 |
<?php endif; ?> |
| 162 |
</div> |
| 163 |
<?php |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Sanitize bulk settings. |
| 168 |
* |
| 169 |
* @param int $bulk_settings Number of bulk content items |
| 170 |
* @return int |
| 171 |
*/ |
| 172 |
protected function sanitize_bulk_settings( $bulk_settings = 350 ) { |
| 173 |
$bulk_settings = absint( $bulk_settings ); |
| 174 |
|
| 175 |
return ( 0 === $bulk_settings ) ? 350 : $bulk_settings; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Reset settings to their previous values |
| 180 |
*/ |
| 181 |
protected function reset_settings() { |
| 182 |
Utils\update_option( 'ep_language', $this->prev_ep_language ); |
| 183 |
Utils\update_option( 'ep_host', $this->prev_ep_host ); |
| 184 |
Utils\update_option( 'ep_credentials', $this->prev_ep_credentials ); |
| 185 |
Utils\update_option( 'ep_bulk_setting', $this->prev_ep_bulk_setting ); |
| 186 |
|
| 187 |
\ElasticPress\Elasticsearch::factory()->get_elasticsearch_info( true ); |
| 188 |
} |
| 189 |
} |
| 190 |
|