PluginProbe
ElasticPress / 5.0.1
ElasticPress v5.0.1
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / includes / classes / Screen / Settings.php

Settings.php in ElasticPress 5.0.1, at includes/classes/Screen/Settings.php

177 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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'] ) ) {
101 $credentials = ( isset( $post['ep_credentials'] ) ) ? Utils\sanitize_credentials( $post['ep_credentials'] ) : [
102 'username' => '',
103 'token' => '',
104 ];
105
106 Utils\update_option( 'ep_credentials', $credentials );
107 }
108
109 if ( isset( $post['ep_bulk_setting'] ) ) {
110 Utils\update_option( 'ep_bulk_setting', $this->sanitize_bulk_settings( $post['ep_bulk_setting'] ) );
111 }
112
113 $es_info = \ElasticPress\Elasticsearch::factory()->get_elasticsearch_info( true );
114 if ( empty( $es_info['version'] ) ) {
115 add_action( 'admin_notices', [ $this, 'add_validation_notice' ] );
116
117 unset( $_POST['ep_host'] ); // Needed to prevent going to the next installation step
118 $this->reset_settings();
119 }
120 }
121
122 /**
123 * Add a notice to the Settings form when the host was not set yet
124 */
125 public function add_validation_notice() {
126 $target = ( Utils\is_epio() ) ?
127 _x( 'ElasticPress.io account', 'Settings validation message', 'elasticpress' ) :
128 _x( 'Elasticsearch server', 'Settings validation message', 'elasticpress' );
129
130 if ( empty( $this->prev_ep_host ) ) {
131 // Setting it for the first time -- probably during the install process.
132 $message = sprintf(
133 /* translators: EP.io account or ES server. */
134 __( 'It was not possible to connect to your %s. Please check your settings and try again.', 'elasticpress' ),
135 $target
136 );
137 } else {
138 $message = sprintf(
139 /* translators: EP.io account or ES server. */
140 __( 'It was not possible to connect to your %s. Your settings were reverted.', 'elasticpress' ),
141 $target
142 );
143 }
144 ?>
145 <div class="notice notice-error">
146 <p>
147 <?php echo wp_kses( $message, 'ep-html' ); ?>
148 </p>
149 </div>
150 <?php
151 }
152
153 /**
154 * Sanitize bulk settings.
155 *
156 * @param int $bulk_settings Number of bulk content items
157 * @return int
158 */
159 protected function sanitize_bulk_settings( $bulk_settings = 350 ) {
160 $bulk_settings = absint( $bulk_settings );
161
162 return ( 0 === $bulk_settings ) ? 350 : $bulk_settings;
163 }
164
165 /**
166 * Reset settings to their previous values
167 */
168 protected function reset_settings() {
169 Utils\update_option( 'ep_language', $this->prev_ep_language );
170 Utils\update_option( 'ep_host', $this->prev_ep_host );
171 Utils\update_option( 'ep_credentials', $this->prev_ep_credentials );
172 Utils\update_option( 'ep_bulk_setting', $this->prev_ep_bulk_setting );
173
174 \ElasticPress\Elasticsearch::factory()->get_elasticsearch_info( true );
175 }
176 }
177