-
Notifications
You must be signed in to change notification settings - Fork 20
/
wp-serverless-search.php
160 lines (125 loc) · 3.95 KB
/
wp-serverless-search.php
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
<?php
/**
* Plugin Name: WP Serverless Search
* Plugin URI: https://github.com/emaildano/wp-serverless-search
* Description: A static search plugin for WordPress.
* Version: 0.0.1
* Author: DigitalCube, Daniel Olson
* Author URI: https://digitalcube.jp
* License: GPL2
* Text Domain: wp-serverless-search
*/
/**
* On Plugin Activation
*/
function wp_sls_search_install()
{
// trigger our function that registers the custom post type
create_wp_sls_dir();
create_search_feed();
}
add_action('init', 'create_wp_sls_dir');
register_activation_hook(__FILE__, 'wp_sls_search_install');
/**
* Create WP SLS Dir
*/
function create_wp_sls_dir()
{
$upload_dir = wp_get_upload_dir();
$save_path = $upload_dir['basedir'] . '/wp-sls/.';
$dirname = dirname($save_path);
if (!is_dir($dirname)) {
mkdir($dirname, 0755, true);
}
}
/**
* Create Search Feed
*/
add_action('publish_post', 'create_search_feed');
function create_search_feed()
{
require_once(ABSPATH . 'wp-admin/includes/export.php');
ob_start();
$wpExportOptions = array(
'content' => 'post',
'status' => 'publish',
);
export_wp($wpExportOptions);
$xml = ob_get_clean();
$upload_dir = wp_get_upload_dir();
$save_path = $upload_dir['basedir'] . '/wp-sls/search-feed.xml';
file_put_contents($save_path, $xml);
}
/**
* Set Plugin Defaults
*/
function wp_sls_search_default_options()
{
$options = array(
'wp_sls_search_form' => '[role=search]',
'wp_sls_search_form_input' => 'input[type=search]',
);
foreach ($options as $key => $value) {
update_option($key, $value);
}
}
if (!get_option('wp_sls_search_form')) {
register_activation_hook(__FILE__, 'wp_sls_search_default_options');
}
/**
* Admin Settings Menu
*/
add_action('admin_menu', 'wp_sls_search');
function wp_sls_search()
{
add_options_page(
'WP Serverless Search',
'WP Serverless Search',
'manage_options',
'wp-sls-search',
'wp_sls_search_options'
);
}
require_once('lib/includes.php');
/*
* Scripts
*/
add_action('wp_enqueue_scripts', 'wp_sls_search_assets');
add_action('admin_enqueue_scripts', 'wp_sls_search_assets');
function wp_sls_search_assets()
{
$shifter_js = plugins_url('main/main.js', __FILE__);
$search_params = array(
'searchForm' => get_option('wp_sls_search_form'),
'searchFormInput' => get_option('wp_sls_search_form_input')
);
wp_register_script('wp-sls-search-js', $shifter_js, array('jquery', 'micromodal', 'fusejs'), null, true);
wp_localize_script('wp-sls-search-js', 'searchParams', $search_params);
wp_enqueue_script('wp-sls-search-js');
wp_register_script('fusejs', 'https://cdnjs.cloudflare.com/ajax/libs/fuse.js/3.2.1/fuse.min.js', null, null, true);
wp_enqueue_script('fusejs');
wp_register_script('micromodal', 'https://cdn.jsdelivr.net/npm/micromodal/dist/micromodal.min.js', null, null, true);
wp_enqueue_script('micromodal');
wp_register_style("wp-sls-search-css", plugins_url('/main/main.css', __FILE__));
wp_enqueue_style("wp-sls-search-css");
}
function wp_sls_search_modal()
{ ?>
<div class="wp-sls-search-modal" id="wp-sls-search-modal" aria-hidden="true">
<div class="wp-sls-search-modal__overlay" tabindex="-1" data-micromodal-overlay>
<div class="wp-sls-search-modal__container" role="dialog" aria-labelledby="modal__title" aria-describedby="modal__content">
<header class="wp-sls-search-modal__header">
<a href="#" aria-label="Close modal" data-micromodal-close></a>
</header>
<form role="search" method="get" class="search-form">
<label for="wp-sls-earch-field">
<span class="screen-reader-text">Search for:</span>
</label>
<input id="wp-sls-earch-field" class="wp-sls-search-field" type="search" autocomplete="off" class="search-field" placeholder="Search …" value="" name="s">
</form>
<div role="document"></div>
</div>
</div>
</div>
<?php }
add_action('wp_footer', 'wp_sls_search_modal');