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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265<?php
/*
Plugin Name: Equalify + UIC Network Utilities
Description: Scans content for PDF and Box.com links and exports CSV. Network-enabled.
Version: 1.0
Author: Your Name
Network: true
*/
/**
* Add the network admin menu page for Equalify + UIC Utilities
*/
add_action('network_admin_menu', 'uic_equalify_add_network_admin_menu');
function uic_equalify_add_network_admin_menu()
{
add_menu_page(
'Equalify + UIC Utilities',
'Equalify + UIC Utilities',
'manage_network_options',
'uic-equalify-utilities',
'render_link_scanner_page'
);
}
/**
* Render the main link scanner page in the network admin
*/
function render_link_scanner_page()
{
// Ensure this is accessed only from the Network Admin dashboard
if (!is_network_admin()) {
echo '<div class="notice notice-error"><p>This tool can only be used from the Network Admin dashboard.</p></div>';
return;
}
// Handle deletion of reports via nonce-verified request
if (isset($_GET['delete_report']) && isset($_GET['_wpnonce']) && wp_verify_nonce($_GET['_wpnonce'], 'delete_report')) {
$target_file = basename($_GET['delete_report']);
$deleted = false;
// Search all public sites for the file and delete it if found
foreach (get_sites(['public' => 1]) as $site) {
switch_to_blog($site->blog_id);
$upload_dir = wp_upload_dir();
$file_path = trailingslashit($upload_dir['basedir']) . $target_file;
if (file_exists($file_path)) {
error_log("Deleting from site ID: " . $site->blog_id . ", file: " . $file_path);
unlink($file_path);
$deleted = true;
restore_current_blog();
break;
}
restore_current_blog();
}
if (!$deleted) {
error_log("File not found in any site: " . $target_file);
}
}
$download_url = '';
$selected_site = isset($_POST['site_id']) ? (int) $_POST['site_id'] : get_current_blog_id();
// Process form submission to run link scans
if ($_SERVER['REQUEST_METHOD'] === 'POST' && check_admin_referer('run_link_scan')) {
switch_to_blog($selected_site);
if (isset($_POST['find_pdf_links'])) {
$download_url = scan_links_and_generate_csv('pdf');
} elseif (isset($_POST['find_box_links'])) {
$download_url = scan_links_and_generate_csv('box');
}
restore_current_blog();
}
// Retrieve all public sites for dropdown and report listing
$sites = get_sites(['public' => 1]);
?>
<div class="wrap">
<h1>UIC + Equalify Utilities</h1>
<h2>Generate Link Reports</h2>
<!-- Site selection and scan form -->
<form method="post" action="" name="link_report_form">
<label for="site_id">Select Site:</label>
<select name="site_id" id="site_id">
<?php foreach ($sites as $site) {
$blog_details = get_blog_details($site->blog_id);
$selected = ($site->blog_id === $selected_site) ? 'selected' : '';
echo "<option value='{$site->blog_id}' $selected>" . esc_html($blog_details->blogname) . "</option>";
} ?>
</select>
<?php wp_nonce_field('run_link_scan'); ?>
<br><br>
<input type="submit" name="find_pdf_links" class="button button-primary" value="Find PDF Links">
<input type="submit" name="find_box_links" class="button button-secondary" value="Find Box Links">
</form>
<h3>Reports</h3>
<ul>
<?php
// Aggregate all reports from all public sites
$all_reports = [];
foreach ($sites as $site) {
switch_to_blog($site->blog_id);
$upload_dir = wp_upload_dir();
$files = glob(trailingslashit($upload_dir['basedir']) . 'link-scan-*.csv') ?: [];
foreach ($files as $file_path) {
$all_reports[] = [
'site' => get_blog_details($site->blog_id)->blogname,
'file_name' => basename($file_path),
'file_url' => trailingslashit($upload_dir['baseurl']) . basename($file_path),
'delete_url' => wp_nonce_url(add_query_arg(['delete_report' => basename($file_path)]), 'delete_report'),
];
}
restore_current_blog();
}
// Sort reports by file name descending (newest first)
usort($all_reports, fn($a, $b) => strcmp($b['file_name'], $a['file_name']));
// Display each report with download and delete links
foreach ($all_reports as $report) {
echo '<li>' . esc_html($report['file_name']) . ' (' . esc_html($report['site']) . ') - <a href="' . esc_url($report['file_url']) . '">Download</a> | <a href="' . esc_url($report['delete_url']) . '">Delete</a></li>';
}
?>
</ul>
</div>
<?php
}
/**
* Scan posts and menus for links of a given type and generate a CSV report
*
* @param string $type 'pdf' or 'box'
* @return string URL to the generated CSV file
*/
function scan_links_and_generate_csv($type)
{
$results = [];
// Retrieve all published posts of any type
$args = [
'post_type' => 'any',
'post_status' => 'publish',
'posts_per_page' => -1,
];
$posts = get_posts($args);
error_log("Post count found: " . count($posts));
// Get current site details for naming the CSV
$site = get_blog_details();
error_log("Scanning site: " . $site->blogname . " (ID: " . get_current_blog_id() . ")");
// Search post content for matching links
foreach ($posts as $post) {
$content = $post->post_content;
// Extract all href links from post content
preg_match_all('/href=[\'"]([^\'"]+)[\'"]/i', $content, $matches);
foreach ($matches[1] as $link) {
$matches_type = ($type === 'pdf' && preg_match('/\.pdf(\?.*)?$/i', $link)) ||
($type === 'box' && strpos($link, 'box.com') !== false);
if ($matches_type) {
$results[] = [
'Location Type' => 'Post',
'Title' => get_the_title($post),
'Link' => $link,
'URL' => get_permalink($post),
];
}
}
}
// Search menu items for matching links
$menus = wp_get_nav_menus();
error_log("Menu count found: " . count($menus));
foreach ($menus as $menu) {
$items = wp_get_nav_menu_items($menu);
if (!empty($items)) {
foreach ($items as $item) {
$link = $item->url;
$matches_type = ($type === 'pdf' && preg_match('/\.pdf(\?.*)?$/i', $link)) ||
($type === 'box' && strpos($link, 'box.com') !== false);
if ($matches_type) {
$results[] = [
'Location Type' => 'Menu',
'Title' => $item->title,
'Link' => $link,
'URL' => '',
];
}
}
}
}
// Prepare CSV file path and URL
$upload_dir = wp_upload_dir();
$site_slug = sanitize_title($site->blogname);
$file_name = 'link-scan-' . $site_slug . '-' . $type . '-' . time() . '.csv';
$file_path = trailingslashit($upload_dir['basedir']) . $file_name;
$file_url = trailingslashit($upload_dir['baseurl']) . $file_name;
// Write results to CSV
$f = fopen($file_path, 'w');
fputcsv($f, ['Location Type', 'Title', 'Link', 'URL']);
if (!empty($results)) {
foreach ($results as $row) {
fputcsv($f, $row);
}
}
fclose($f);
error_log("CSV generated: " . $file_path);
return $file_url;
}
/**
* Remove the plugin menu and hide plugin from subsite plugin lists
* This prevents activation or usage on subsites
*/
if (!is_network_admin()) {
add_action('admin_menu', 'uic_equalify_remove_subsite_menu');
add_filter('all_plugins', 'uic_equalify_hide_plugin_on_subsites');
}
/**
* Remove the plugin menu page on subsites
*/
function uic_equalify_remove_subsite_menu()
{
remove_menu_page('uic-equalify-utilities');
}
/**
* Hide this plugin from plugin list on subsites
*
* @param array $plugins
* @return array
*/
function uic_equalify_hide_plugin_on_subsites($plugins)
{
if (!is_network_admin()) {
$plugin_file = plugin_basename(__FILE__);
if (isset($plugins[$plugin_file])) {
unset($plugins[$plugin_file]);
}
}
return $plugins;
}