AdvancedView.php
4.34 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* SearchWP AdvancedView.
*
* @package SearchWP
* @author Jon Christopher
*/
namespace SearchWP\Admin\Views;
use SearchWP\Utils;
use SearchWP\Settings;
use SearchWP\Admin\NavTab;
/**
* Class AdvancedView is responsible for providing the UI for Advanced.
*
* @since 4.0
*/
class AdvancedView {
private static $slug = 'advanced';
/**
* AdvancedView constructor.
*
* @since 4.0
*/
function __construct() {
if ( Utils::is_swp_admin_page( 'settings' ) ) {
new NavTab( [
'page' => 'settings',
'tab' => self::$slug,
'label' => __( 'Advanced', 'searchwp' ),
] );
}
if ( Utils::is_swp_admin_page( 'settings', self::$slug ) ) {
add_action( 'searchwp\settings\view', [ __CLASS__, 'render' ] );
add_action( 'searchwp\settings\after', [ __CLASS__, 'assets' ], 999 );
}
add_action( 'wp_ajax_' . SEARCHWP_PREFIX . 'import_settings', [ __CLASS__, 'import_settings' ] );
add_action( 'wp_ajax_' . SEARCHWP_PREFIX . 'update_setting', [ __CLASS__, 'update_setting' ] );
add_action( 'wp_ajax_' . SEARCHWP_PREFIX . 'wake_indexer', [ __CLASS__, 'wake_indexer' ] );
}
/**
* Outputs the assets needed for the AdvancedView UI.
*
* @since 4.0
* @return void
*/
public static function assets() {
$handle = SEARCHWP_PREFIX . self::$slug;
$debug = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG === true || isset( $_GET['script_debug'] ) ? '' : '.min';
wp_enqueue_script( $handle,
SEARCHWP_PLUGIN_URL . "assets/javascript/dist/advanced{$debug}.js",
[ 'jquery' ], SEARCHWP_VERSION, true );
wp_enqueue_style( $handle,
SEARCHWP_PLUGIN_URL . "assets/javascript/dist/advanced{$debug}.css",
[], SEARCHWP_VERSION );
// These are exportable.
$stopwords = new \SearchWP\Logic\Stopwords();
$synonyms = new \SearchWP\Logic\Synonyms();
Utils::localize_script( $handle, [
'engines' => Settings::_get_engines_settings(),
'settings' => call_user_func_array( 'array_merge', array_map( function( $key ) {
return [ $key => Settings::get_single( $key, 'boolean' ) ];
}, Settings::get_keys() ) ),
'stopwords' => $stopwords->get(),
'synonyms' => $synonyms->get(),
] );
}
/**
* Callback for the render of this view.
*
* @since 4.0
* @return void
*/
public static function render() {
// This node structure is as such to inherit WP-admin CSS.
?>
<div class="edit-post-meta-boxes-area">
<div id="poststuff">
<div class="meta-box-sortables">
<div id="searchwp-advanced"></div>
</div>
</div>
</div>
<?php
}
/**
* AJAX callback to update a setting.
*
* @since 4.0
* @return void
*/
public static function update_setting() {
Utils::check_ajax_permissions();
$setting = isset( $_REQUEST['setting'] ) ? Utils::decode_string( $_REQUEST['setting'] ) : null;
$value = isset( $_REQUEST['value'] ) ? json_decode( stripslashes( $_REQUEST['value'] ), true ) : null;
if ( is_null( $setting ) || is_null( $value ) ) {
wp_send_json_error();
}
Settings::update( $setting, $value );
wp_send_json_success();
}
/**
* AJAX callback to wake the indexer.
*
* @since 4.0
* @return void
*/
public static function wake_indexer() {
Utils::check_ajax_permissions();
$indexer = \SearchWP::$indexer;
$indexer->_wake_up();
wp_send_json_success();
}
/**
* AJAX callback to import engines.
*
* @since 4.0
* @return void
*/
public static function import_settings() {
Utils::check_ajax_permissions();
$settings = isset( $_REQUEST['settings'] ) ? json_decode( stripslashes( $_REQUEST['settings'] ), true ) : false;
if ( ! is_null( $settings['engines'] ) ) {
// Run these Engines through the saving process which validates and persists.
$engines_view = new \SearchWP\Admin\Views\EnginesView();
$engines_view->update_engines( $settings['engines'] );
}
if ( ! is_null( $settings['settings'] ) && is_array( $settings['settings'] ) ) {
foreach ( $settings['settings'] as $setting => $value ) {
Settings::update( $setting, $value );
}
}
if ( ! is_null( $settings['stopwords'] ) && is_array( $settings['stopwords'] ) ) {
$stopwords = new \SearchWP\Logic\Stopwords();
$stopwords->save( $settings['stopwords'] );
}
if ( ! is_null( $settings['synonyms'] ) && is_array( $settings['synonyms'] ) ) {
$synonyms = new \SearchWP\Logic\Synonyms();
$synonyms->save( $settings['synonyms'] );
}
wp_send_json_success();
}
}