SettingsView.php
3.6 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
<?php
/**
* SearchWP SettingsView.
*
* @package SearchWP
* @author Jon Christopher
*/
namespace SearchWP\Admin\Views;
use SearchWP\Utils;
use SearchWP\Admin\NavTab;
use SearchWP\Logic\Synonyms;
use SearchWP\Logic\Stopwords;
/**
* Class SettingsView is responsible for providing the UI for Settings.
*
* @since 4.0
*/
class SettingsView {
private static $slug = 'settings';
/**
* SettingsView constructor.
*
* @since 4.0
*/
function __construct() {
if ( Utils::is_swp_admin_page( 'settings' ) ) {
new NavTab([
'page' => 'settings',
'tab' => self::$slug,
'label' => __( 'Settings', '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 . 'stopwords_suggestions', [ __CLASS__ , 'get_stopwords_suggestions' ] );
add_action( 'wp_ajax_' . SEARCHWP_PREFIX . 'stopwords_update', [ __CLASS__ , 'update_stopwords' ] );
add_action( 'wp_ajax_' . SEARCHWP_PREFIX . 'synonyms_update', [ __CLASS__ , 'update_synonyms' ] );
}
/**
* AJAX callback to update saved synonyms.
*
* @since 4.0
*/
public static function update_synonyms() {
Utils::check_ajax_permissions();
$update = isset( $_REQUEST['synonyms'] ) ? json_decode( stripslashes( $_REQUEST['synonyms'] ), true ) : false;
$synonyms = new Synonyms();
$update = $synonyms->save( $update );
wp_send_json_success( $update );
}
/**
* AJAX callback to update saved stopwords.
*
* @since 4.0
*/
public static function update_stopwords() {
Utils::check_ajax_permissions();
$update = isset( $_REQUEST['stopwords'] ) ? json_decode( stripslashes( $_REQUEST['stopwords'] ), true ) : false;
$stopwords = new Stopwords();
$update = $stopwords->save( $update );
wp_send_json_success( $update );
}
/**
* AJAX callback to get suggested stopwords.
*
* @since 4.0
*/
public static function get_stopwords_suggestions() {
Utils::check_ajax_permissions();
$stopwords = new Stopwords();
wp_send_json_success( $stopwords->get_suggestions( [
'limit' => absint( apply_filters( 'searchwp\stopwords\suggestions\limit', 20 ) ),
'threshold' => floatval( apply_filters( 'searchwp\stopwords\suggestions\threshold', 0.3 ) ),
] ) );
}
/**
* Outputs the assets needed for the Settings 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/settings{$debug}.js",
[ 'jquery' ], SEARCHWP_VERSION, true );
wp_enqueue_style( $handle,
SEARCHWP_PLUGIN_URL . "assets/javascript/dist/settings{$debug}.css",
[], SEARCHWP_VERSION );
$stopwords = new Stopwords();
$synonyms = new Synonyms();
Utils::localize_script( $handle, [
'stopwords' => [
'list' => $stopwords->get(),
'defaults' => $stopwords->get_default(),
'suggest' => (bool) apply_filters( 'searchwp\stopwords\suggestions', true ),
],
'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-settings"></div>
</div>
</div>
</div>
<?php
}
}