-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp-civitai.php
178 lines (143 loc) · 5.81 KB
/
wp-civitai.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/*
Plugin Name: WP Civitai
Plugin URI: http://ostris.com/wp-civitai
Description: A plugin to integrate Civitai with WordPress. Allow you to cache and display Civitai content on your site.
Version: 0.1
Author: Ostris (Jaret Burkett)
Author URI: http://ostris.com
License: GNU GPLv3
*/
define('WP_CIVITAI_FILE', __FILE__);
define('WP_CIVITAI_PATH', plugin_dir_path(__FILE__));
define('WP_CIVITAI_URL', plugin_dir_url(__FILE__));
require_once WP_CIVITAI_PATH . 'civitai-settings.php';
require_once WP_CIVITAI_PATH . 'wp-civitai-page.php';
if (!class_exists('WpCivitai')) {
class WpCivitai {
public function __construct() {
register_activation_hook(WP_CIVITAI_FILE, array($this, 'install'));
add_shortcode('civitai', array($this, 'shortcode_output'));
add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts_styles'));
}
public function enqueue_scripts_styles() {
wp_enqueue_style('wp-civitai-style', plugin_dir_url(__FILE__) . 'build/index.css');
wp_enqueue_script('wp-civitai-script', plugin_dir_url(__FILE__) . 'build/index.js', array('wp-element'), '1.0.0', true);
}
public function shortcode_output($atts) {
$atts = shortcode_atts(array(
'type' => 'model',
'id' => '',
), $atts, 'civitai');
$type = $atts['type'];
if ($atts['type'] == 'model') {
// Construct the endpoint URL based on the model id
$endpoint = 'https://civitai.com/api/v1/models/' . $atts['id'];
} else {
// Print an error message
return 'Invalid shortcode. Please specify a valid type';
}
$data = $this->get_data($endpoint);
$page = new WpCivitaiPage($type, $data);
return $page->process_and_render();
}
private function api_call($endpoint) {
// Get API key and username from options
$options = get_option('wp_civitai_options');
$api_key = $options['api-key'];
$username = $options['civitai-username'];
// Prepare the API request
$args = array(
'headers' => array(// 'Authorization' => 'Basic ' . base64_encode($username . ':' . $api_key)
)
);
// Make the request
$response = wp_remote_get($endpoint, $args);
// Check for errors
if (is_wp_error($response)) {
// Log the error and return null
error_log($response->get_error_message());
return null;
}
// Decode the response body
$data = json_decode(wp_remote_retrieve_body($response), true);
return $data;
}
public function install(): void {
global $wpdb;
$table_name = $wpdb->prefix . "civitai";
if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
endpoint varchar(255) NOT NULL,
date_added datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
response text NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
}
public function get_data($endpoint) {
global $wpdb;
$table_name = $wpdb->prefix . "civitai";
$result = $wpdb->get_row(
$wpdb->prepare(
"
SELECT * FROM $table_name
WHERE endpoint = %s
",
$endpoint
)
);
if ($result) {
$data = maybe_unserialize($result->response);
if ($this->is_cache_expired($result->date_added)) {
// Cache is expired, flag for update
return $this->update_data($endpoint, $data);
}
// Return cached data
return $data;
} else {
// No data found, flag for update
return $this->update_data($endpoint, null);
}
}
public function update_data($endpoint, $data) {
global $wpdb;
$table_name = $wpdb->prefix . "civitai";
// Make API call
$new_data = $this->api_call($endpoint);
if ($new_data) {
// If API call was successful, save new data to DB
$wpdb->replace(
$table_name,
array(
'endpoint' => $endpoint,
'date_added' => current_time('mysql'),
'response' => maybe_serialize($new_data)
),
array('%s', '%s', '%s')
);
// Return new data
return $new_data;
} else {
// If API call failed, return old data if available
return $data;
}
}
private function is_cache_expired($date): bool {
$cache_minutes = get_option('wp_civitai_options')['cache-minutes'];
$expiration_date = strtotime($date . ' + ' . $cache_minutes . ' minutes');
// If the current time is past the expiration date, cache is expired
return current_time('timestamp') > $expiration_date;
}
}
}
if (class_exists('WpCivitai')) {
$wpCivitai = new WpCivitai();
}
if (class_exists('WpCivitaiSettings')) {
$wpCivitaiSettings = new WpCivitaiSettings();
}