-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimmich-wordpress.php
More file actions
224 lines (198 loc) · 7.48 KB
/
Copy pathimmich-wordpress.php
File metadata and controls
224 lines (198 loc) · 7.48 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
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
<?php
/*
Plugin Name: Immich Server Integration
Description: Connects WordPress to an Immich server to browse and import your photo library.
Version: 2.0.0
Author: Example
*/
if (!defined('ABSPATH')) {
exit;
}
// Basic plugin constants
define('IMMICH_WP_VERSION', '2.0.0');
define('IMMICH_WP_URL', plugin_dir_url(__FILE__));
// Option keys
const IMMICH_OPTION = 'immich_wp_settings';
/**
* Retrieve plugin settings with defaults.
*/
function immich_wp_get_settings() {
$defaults = array(
'server_url' => '',
'email' => '',
'password' => '',
);
$settings = get_option(IMMICH_OPTION, array());
return wp_parse_args($settings, $defaults);
}
/**
* Store plugin settings.
*/
function immich_wp_save_settings($settings) {
update_option(IMMICH_OPTION, $settings);
}
/**
* Get an authentication token from Immich using the stored credentials.
*/
function immich_wp_get_token($force = false) {
$token = get_transient('immich_wp_token');
if (!$force && $token) {
return $token;
}
$settings = immich_wp_get_settings();
if (empty($settings['server_url']) || empty($settings['email']) || empty($settings['password'])) {
return '';
}
$url = rtrim($settings['server_url'], '/') . '/api/auth/login';
$response = wp_remote_post($url, array(
'headers' => array('Content-Type' => 'application/json'),
'body' => wp_json_encode(array(
'email' => $settings['email'],
'password' => $settings['password'],
)),
'timeout' => 15,
));
if (is_wp_error($response)) {
return '';
}
$code = wp_remote_retrieve_response_code($response);
$body = json_decode(wp_remote_retrieve_body($response), true);
if ($code !== 200 || empty($body['accessToken'])) {
return '';
}
set_transient('immich_wp_token', $body['accessToken'], HOUR_IN_SECONDS);
return $body['accessToken'];
}
/**
* Fetch a list of assets from Immich.
*/
function immich_wp_get_assets() {
$token = immich_wp_get_token();
if (!$token) return array();
$settings = immich_wp_get_settings();
$url = rtrim($settings['server_url'], '/') . '/api/assets';
$response = wp_remote_get($url, array(
'headers' => array('Authorization' => 'Bearer ' . $token),
'timeout' => 15,
));
if (is_wp_error($response)) {
return array();
}
$code = wp_remote_retrieve_response_code($response);
$body = json_decode(wp_remote_retrieve_body($response), true);
if ($code !== 200 || !is_array($body)) {
return array();
}
return $body;
}
/**
* Render the settings page.
*/
function immich_wp_settings_page() {
if (!current_user_can('manage_options')) {
return;
}
if (isset($_POST['immich_save'])) {
check_admin_referer('immich_save_settings');
$settings = array(
'server_url' => esc_url_raw($_POST['server_url']),
'email' => sanitize_email($_POST['email']),
'password' => sanitize_text_field($_POST['password']),
);
immich_wp_save_settings($settings);
echo '<div class="updated"><p>Settings saved.</p></div>';
}
$settings = immich_wp_get_settings();
?>
<div class="wrap immich-admin-page">
<h1>Immich Server Integration</h1>
<h2 class="nav-tab-wrapper">
<a href="#basic" class="nav-tab nav-tab-active">Basic Settings</a>
</h2>
<div id="basic" class="tab-content">
<form method="post">
<?php wp_nonce_field('immich_save_settings'); ?>
<table class="form-table" role="presentation">
<tr>
<th scope="row"><label for="server_url">Server URL</label></th>
<td><input name="server_url" type="url" id="server_url" value="<?php echo esc_attr($settings['server_url']); ?>" class="regular-text" placeholder="https://example.com" required></td>
</tr>
<tr>
<th scope="row"><label for="email">Email</label></th>
<td><input name="email" type="email" id="email" value="<?php echo esc_attr($settings['email']); ?>" class="regular-text" required></td>
</tr>
<tr>
<th scope="row"><label for="password">Password</label></th>
<td><input name="password" type="password" id="password" value="<?php echo esc_attr($settings['password']); ?>" class="regular-text" required></td>
</tr>
</table>
<?php submit_button('Save Changes', 'primary', 'immich_save'); ?>
</form>
<?php if (!empty($settings['server_url']) && !empty($settings['email']) && !empty($settings['password'])) : ?>
<div class="card immich-connection-test">
<h2>Connection Test</h2>
<p>Verify your settings by connecting to the Immich server.</p>
<button id="immich-test-connection" class="button button-secondary">Test Connection</button>
<div id="immich-connection-result" class="notice" style="display:none"></div>
</div>
<?php endif; ?>
<div class="card immich-info">
<h2>Plugin Information</h2>
<p>This plugin connects WordPress to your Immich server so you can browse and import your media library.</p>
</div>
</div>
</div>
<?php
}
/**
* Display a simple library page with thumbnails.
*/
function immich_wp_library_page() {
if (!current_user_can('upload_files')) {
return;
}
$assets = immich_wp_get_assets();
echo '<div class="wrap"><h1>Immich Library</h1><div style="display:flex;flex-wrap:wrap">';
foreach ($assets as $asset) {
if (empty($asset['id'])) continue;
$thumb = rtrim(immich_wp_get_settings()['server_url'], '/') . '/api/assets/' . $asset['id'] . '/thumbnail?key=' . immich_wp_get_token();
echo '<img src="' . esc_url($thumb) . '" style="width:150px;height:150px;object-fit:cover;margin:5px" />';
}
echo '</div></div>';
}
/**
* Enqueue assets for the settings page.
*/
function immich_wp_admin_assets($hook) {
if ($hook !== 'settings_page_immich-settings') {
return;
}
wp_enqueue_style('immich-admin', IMMICH_WP_URL . 'assets/css/admin.css', array(), IMMICH_WP_VERSION);
wp_enqueue_script('immich-admin', IMMICH_WP_URL . 'assets/js/admin.js', array('jquery'), IMMICH_WP_VERSION, true);
wp_localize_script('immich-admin', 'immich_admin', array(
'nonce' => wp_create_nonce('immich_test_connection'),
));
}
add_action('admin_enqueue_scripts', 'immich_wp_admin_assets');
/**
* AJAX handler to test Immich connection.
*/
function immich_wp_ajax_test_connection() {
check_ajax_referer('immich_test_connection', 'nonce');
$token = immich_wp_get_token(true);
if ($token) {
wp_send_json_success(array('message' => 'Connection successful!'));
} else {
wp_send_json_error(array('message' => 'Connection failed.'));
}
}
add_action('wp_ajax_immich_test_connection', 'immich_wp_ajax_test_connection');
/**
* Add menu items.
*/
function immich_wp_admin_menu() {
add_options_page('Immich Settings', 'Immich', 'manage_options', 'immich-settings', 'immich_wp_settings_page');
add_media_page('Immich Library', 'Immich Library', 'upload_files', 'immich-library', 'immich_wp_library_page');
}
add_action('admin_menu', 'immich_wp_admin_menu');
?>