Skip to content

Commit a8eecee

Browse files
committed
v4.2
A custom plugin to scan a directory structure and create a filebase with category structure, file viewer, and download functionality in a single shortcode, supporting multiple file types and search functionality.
1 parent 3d317d2 commit a8eecee

5 files changed

+562
-0
lines changed

admin-settings.php

+179
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<?php
2+
// admin-settings.php
3+
4+
// Add settings page to define the directory path
5+
function filebase_add_settings_page() {
6+
add_options_page(
7+
'Filebase Settings',
8+
'Filebase',
9+
'manage_options',
10+
'filebase-settings',
11+
'filebase_settings_page'
12+
);
13+
}
14+
add_action('admin_menu', 'filebase_add_settings_page');
15+
16+
function filebase_settings_page() {
17+
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
18+
if (isset($_POST['filebase_path'])) {
19+
update_option('filebase_path', sanitize_text_field($_POST['filebase_path']));
20+
echo '<div class="updated"><p>Path updated successfully!</p></div>';
21+
}
22+
if (isset($_POST['filebase_default_thumbnail'])) {
23+
update_option('filebase_default_thumbnail', esc_url_raw($_POST['filebase_default_thumbnail']));
24+
echo '<div class="updated"><p>Default thumbnail updated successfully!</p></div>';
25+
}
26+
}
27+
28+
$path = get_option('filebase_path', '');
29+
$default_thumbnail = get_option('filebase_default_thumbnail', '');
30+
31+
echo '<div class="wrap">';
32+
echo '<h1>Filebase Settings</h1>';
33+
echo '<form method="post">';
34+
echo '<table class="form-table">';
35+
36+
// Path setting
37+
echo '<tr valign="top">';
38+
echo '<th scope="row"><label for="filebase_path">Base Directory Path</label></th>';
39+
echo '<td><input type="text" id="filebase_path" name="filebase_path" value="' . esc_attr($path) . '" class="regular-text"></td>';
40+
echo '</tr>';
41+
42+
// Default thumbnail setting
43+
echo '<tr valign="top">';
44+
echo '<th scope="row"><label for="filebase_default_thumbnail">Default Thumbnail URL</label></th>';
45+
echo '<td><input type="text" id="filebase_default_thumbnail" name="filebase_default_thumbnail" value="' . esc_attr($default_thumbnail) . '" class="regular-text">';
46+
echo '<p class="description">Enter the URL of the default thumbnail image to be used when no specific thumbnail is set for a file.</p></td>';
47+
echo '</tr>';
48+
49+
echo '</table>';
50+
echo '<p class="submit"><input type="submit" class="button-primary" value="Save Changes"></p>';
51+
echo '</form>';
52+
echo '</div>';
53+
}
54+
55+
// Add Sync Files submenu under Filebase Comments
56+
function filebase_add_sync_button_page() {
57+
add_submenu_page(
58+
'edit.php?post_type=filebase_comment', // Parent menu slug
59+
'Sync Files', // Page title
60+
'Sync Files', // Menu title
61+
'manage_options', // Capability
62+
'sync-files', // Menu slug
63+
'filebase_render_sync_button_page' // Callback function
64+
);
65+
}
66+
add_action('admin_menu', 'filebase_add_sync_button_page');
67+
68+
// Render the Sync Files page
69+
function filebase_render_sync_button_page() {
70+
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
71+
$sync_result = filebase_sync_files_to_admin();
72+
if ($sync_result) {
73+
echo '<div class="updated"><p>Files synchronized successfully!</p></div>';
74+
} else {
75+
echo '<div class="error"><p>No new files were added or an error occurred.</p></div>';
76+
}
77+
}
78+
79+
echo '<div class="wrap">';
80+
echo '<h1>Synchronize Files</h1>';
81+
echo '<form method="post">';
82+
echo '<p>Click the button below to scan the filebase directory and add any new files to the admin interface.</p>';
83+
echo '<button type="submit" class="button button-primary">Sync Files</button>';
84+
echo '</form>';
85+
echo '</div>';
86+
}
87+
88+
// Sync files to admin
89+
function filebase_sync_files_to_admin() {
90+
$base_path = ABSPATH . get_option('filebase_path', '');
91+
92+
if (empty($base_path) || !is_dir($base_path)) {
93+
error_log('Filebase sync failed: Invalid base path: ' . $base_path);
94+
return false; // Exit if the path is invalid
95+
}
96+
97+
if (!function_exists('filebase_get_all_files')) {
98+
require_once plugin_dir_path(__FILE__) . 'shared-functions.php';
99+
}
100+
101+
$files = filebase_get_all_files($base_path);
102+
$added_count = 0;
103+
104+
foreach ($files as $file) {
105+
$relative_path = str_replace($base_path . DIRECTORY_SEPARATOR, '', $file);
106+
107+
// Check if a post for this file already exists
108+
$existing_post = get_posts([
109+
'post_type' => 'filebase_comment',
110+
'meta_query' => [
111+
[
112+
'key' => '_filebase_file',
113+
'value' => $relative_path,
114+
'compare' => '=',
115+
],
116+
],
117+
'posts_per_page' => 1,
118+
]);
119+
120+
if (empty($existing_post)) {
121+
error_log('Filebase: Adding file: ' . $relative_path);
122+
// Create a new post
123+
$result = wp_insert_post([
124+
'post_title' => basename($relative_path),
125+
'post_type' => 'filebase_comment',
126+
'post_status' => 'publish',
127+
'meta_input' => [
128+
'_filebase_file' => $relative_path,
129+
],
130+
]);
131+
132+
if ($result) {
133+
error_log('Filebase: File added successfully: ' . $relative_path);
134+
$added_count++;
135+
} else {
136+
error_log('Filebase: Failed to add file: ' . $relative_path);
137+
}
138+
} else {
139+
error_log('Filebase: File already exists: ' . $relative_path);
140+
}
141+
}
142+
143+
return $added_count > 0;
144+
}
145+
146+
// Add thumbnail meta box for filebase_comment post type
147+
function filebase_add_thumbnail_meta_box() {
148+
add_meta_box(
149+
'filebase_thumbnail_meta',
150+
__('File Thumbnail', 'filebase'),
151+
'filebase_thumbnail_meta_box_callback',
152+
'filebase_comment', // Post type
153+
'side',
154+
'default'
155+
);
156+
}
157+
add_action('add_meta_boxes', 'filebase_add_thumbnail_meta_box');
158+
159+
// Callback for rendering the thumbnail meta box
160+
function filebase_thumbnail_meta_box_callback($post) {
161+
// Retrieve the current thumbnail URL
162+
$thumbnail = get_post_meta($post->ID, '_filebase_thumbnail', true);
163+
164+
echo '<label for="filebase_thumbnail">' . __('Thumbnail URL:', 'filebase') . '</label>';
165+
echo '<input type="text" id="filebase_thumbnail" name="filebase_thumbnail" value="' . esc_attr($thumbnail) . '" style="width:100%;">';
166+
echo '<p class="description">' . __('Enter the URL of the thumbnail image for this file.', 'filebase') . '</p>';
167+
}
168+
169+
// Save the thumbnail URL when the post is saved
170+
function filebase_save_thumbnail_meta_box($post_id) {
171+
if (array_key_exists('filebase_thumbnail', $_POST)) {
172+
update_post_meta(
173+
$post_id,
174+
'_filebase_thumbnail',
175+
esc_url_raw($_POST['filebase_thumbnail'])
176+
);
177+
}
178+
}
179+
add_action('save_post', 'filebase_save_thumbnail_meta_box');

0 commit comments

Comments
 (0)