This repository was archived by the owner on Feb 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhsph-multi-search.php
More file actions
155 lines (116 loc) · 4.8 KB
/
hsph-multi-search.php
File metadata and controls
155 lines (116 loc) · 4.8 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
<?php
/*
Plugin Name: HSPH Mutlisite Search
Plugin URI: http://sph.harvard.edu
Description: Perform a string search on titles and content of all the posts and pages of a network
Version: 1.0.0
Author: GMolter/HSPH Web Team
Author URI: http://sph.harvard.edu
Network: true
*/
/**
* Add a widget to the network dashboard.
*
*/
function hsph_multi_search_add_dashboard_widgets() {
wp_add_dashboard_widget(
'hsph_multi_search_dashboard_widget', // Widget slug.
'Multisite Search', // Title.
'hsph_multi_search_display_dashboard_widget' // Display function.
);
}
add_action( 'wp_network_dashboard_setup', 'hsph_multi_search_add_dashboard_widgets' );
/**
* Display the content of our Dashboard Widget.
*/
function hsph_multi_search_display_dashboard_widget() {
// Display whatever it is you want to show.
echo '<form role="search" method="post" class="hsph-multi-search-form" action="'.admin_url( 'admin-ajax.php' ).'">';
echo ' <input type="hidden" name="hsph_wp_nounce" value="'.wp_create_nonce( "hsph_ajax_security" ).'">';
echo ' <input type="hidden" name="action" value="hsph_multi_search">';
echo ' <label>';
echo ' <span class="screen-reader-text">'.__("Search network").'</span>';
echo ' <input required="required" type="search" class="search-field" placeholder="'.esc_attr_x( 'Search …', 'placeholder' ).'" name="multisearch" title="'.esc_attr_x( 'Search for:', 'label' ).'" />';
echo ' </label>';
echo ' <input type="submit" class="search-submit" value="'.esc_attr_x( 'Search', 'submit button' ).'" />';
echo '</form>';
}
/**
* We add an ajax action to perform the search
*/
add_action( 'wp_ajax_hsph_multi_search', 'hsph_multi_search_ajax_search' );
function hsph_multi_search_ajax_search() {
//we check that user is allowed to perform the search and that the ajax request look valid
if(current_user_can('manage_network') && check_ajax_referer( 'hsph_ajax_security', 'hsph_wp_nounce',false)){
global $wpdb;
//We store the initial blogID value for reseting at the end
$initialBlogID = $wpdb->blogid;
//Get the list of public blogs
$blogs = wp_get_sites(array(
"network_id" => $wpdb->siteid,
"public" => 1,
"archived" => null,
"deleted" => null,
"limit" => 10000
));
//Settings headers to return a file
header("Content-Type: plain/text");
header("Content-Disposition: Attachment; filename=hsph-web-multi-search-".date("Y-m-d-H-i-s").".txt");
header("Pragma: no-cache");
//vars to keep track of the number of occurences and blogs
$i = 0;
$j = 0;
$multisearch = sanitize_text_field($_POST["multisearch"]);
echo "#########################################################################"."\n";
echo "Searching for occurences of: \"".$multisearch."\"\n";
echo "#########################################################################"."\n\n";
//for each existing public blog
foreach ($blogs as $blog){
$wpdb->set_blog_id($blog["blog_id"]);
$wpdb->set_prefix($wpdb->base_prefix);
//we get the list of posts containing our search string
$query = $wpdb->prepare( " SELECT ID, post_title
FROM $wpdb->posts
WHERE post_status='publish'
AND ( post_title LIKE %s OR post_content LIKE %s )
AND post_type IN ('post','page','research_project','multimedia-article','press-release','magazine','hsph-in-the-news','featured-news-story')"
,"%".$multisearch."%","%".$multisearch."%");
$results = $wpdb->get_results( $query, ARRAY_A );
//we display each result
if(is_array($results) && !empty($results)){
$j++;
echo "#########################################################################"."\n";
echo "http://".$blog["domain"].$blog["path"]." - Blog ID: ".$blog["blog_id"]."\n";
echo "#########################################################################"."\n\n";
foreach ($results as $result){
echo "\"".$result["post_title"]."\" (ID: ".$result["ID"].") - http://".$blog["domain"].$blog["path"]."?p=".$result["ID"]."\n\r";
$i++;
}
}
}
//Displaying a few stats because who doesn't like stats?!
echo "#########################################################################"."\n";
echo $i ." occurrence";
if($i>1) {
echo "s ";
}
if($i>1){
echo "found on ".$j." blog";
if($j>1){
echo "s ";
}
}
else{
echo "found";
}
echo "\n";
echo "#########################################################################"."\n\n";
// If the initial blogId has been changed we reset it
if($wpdb->blogid != $initialBlogID){
$wpdb->set_blog_id($initialBlogID);
$wpdb->set_prefix($wpdb->base_prefix);
}
}
wp_die(); // this is required to terminate immediately and return a proper response
}
?>