-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbddb-main.php
276 lines (247 loc) · 9.18 KB
/
bddb-main.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
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
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* Plugin Name: Boycott Douban Database
* Plugin URI: http://pewae.com
* GitHub Plugin URI: https://github.com/lifishake/wp-boycott-douban-db
* Description: 抵制源于喜爱。既然无法改变它,那就自己创造一个。
* Author: lifishake
* Author URI: http://pewae.com
* Version: 1.0.0
* Date: 2025-01-02
* License: GNU General Public License 3.0+ http://www.gnu.org/licenses/gpl.html
*/
/*宏定义*/
define('BDDB_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define('BDDB_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ) ;
define ('BDDB_PLUGIN_BASE_NAME', plugin_basename(__FILE__));
define('BDDB_GALLERY_URL',home_url('/',is_ssl()?'https':'http').'wp-content/poster_gallery/');
//define('BDDB_GALLERY_DIR', ABSPATH.'wp-content/poster_gallery/');
define('BDDB_TAX_VER', '20220101');
define('BDDB_META_VER', '20230210');
register_activation_hook( __FILE__, 'bddb_plugin_activation' );
register_deactivation_hook( __FILE__,'bddb_plugin_deactivation' );
register_uninstall_hook(__FILE__, 'bddb_plugin_uninstall');
require_once( BDDB_PLUGIN_DIR . '/bddb-funcs.php');
require_once( BDDB_PLUGIN_DIR . '/class/class-bddb-editor.php');
require_once( BDDB_PLUGIN_DIR . '/class/class-bddb-douban-fecther.php');
require_once( BDDB_PLUGIN_DIR . '/class/class-bddb-fecther.php');
require_once( BDDB_PLUGIN_DIR . '/class/class-bddb-image.php');
require_once( BDDB_PLUGIN_DIR . '/class/class-bddb-templates.php');
require_once( BDDB_PLUGIN_DIR . '/class/class-bddb-settings.php');
require_once( BDDB_PLUGIN_DIR . '/class/class-bddb-statics.php');
require_once( BDDB_PLUGIN_DIR . '/class/class-bddb-types-list-table.php');
function bddb_is_debug_mode()
{
if (isset( $_SERVER['PHPRC'] ) && strpos($_SERVER['PHPRC'], "xampp" ) > 0)
{
return 1;
}
return 0;
}
/* 打log用 */
function bddb_log()
{
//echo '<pre>'.$any.'</pre>';
print_r(debug_backtrace());
}
/*创建目录*/
function bddb_create_dir($dir) {
if (file_exists ($dir)) {
if (! is_writeable ( $dir )) {
@chmod ( $dir, '511' );
}
} else {
@mkdir ( $dir, '511', true );
}
}
/*创建必须文件*/
function bddb_create_nopic($width, $height) {
if ($width == $height){
$src = sprintf("%s/img/nocover_square.png", BDDB_PLUGIN_DIR);
}else{
$src = sprintf("%s/img/nocover_oblone.png", BDDB_PLUGIN_DIR);
}
$dest = sprintf("%s/img/nocover_%s_%s.png", BDDB_PLUGIN_DIR, $width, $height);
if (file_exists ($dest)) {
return;
}
$image = new Bddb_SimpleImage();
$image->load($src);
$image->resize($width,$height);
$image->save($dest);
}
/*插件激活*/
function bddb_plugin_activation() {
}
/*检查路径,检查默认文件*/
function bddb_check_paths(){
$dir_o = BDDB_Settings::get_default_folder();
$gallery_dir= ABSPATH.$dir_o;
$thumb_dir = $gallery_dir."thumbnails/";
bddb_create_dir($gallery_dir);
bddb_create_dir($thumb_dir);
foreach (BDDB_Statics::get_valid_types() as $type) {
bddb_create_nopic(BDDB_Settings::get_poster_width($type),BDDB_Settings::get_poster_height($type));
bddb_create_nopic(BDDB_Settings::get_thumbnail_width($type),BDDB_Settings::get_thumbnail_height($type));
}
}
/*插件反激活*/
function bddb_plugin_deactivation()
{
}
/*插件卸载*/
function bddb_plugin_uninstall()
{
}
/*配置画面*/
if (is_admin())
{
require_once( BDDB_PLUGIN_DIR . '/bddb-options.php');
}
/*变量初期化, 更早*/
add_action('plugins_loaded', 'bddb_init', 11);
function bddb_init()
{
bddb_check_paths();
add_action('admin_init','bddb_admin_init');
add_action('init', 'bddb_init_actions', 11);
}
/* Plugin页面追加配置选项 */
function bddb_init_actions()
{
BDDB_Statics::check_db();
BDDB_Statics::check_taxonomies();
BDDB_Statics::check_types();
//js和css加载
add_action( 'wp_enqueue_scripts', 'bddb_scripts' );
//Quick Tag追加
add_shortcode('bddbr', 'qt_show_record');
//ajax 显示 page 回调
add_action('wp_ajax_bddb_next_gallery_page', 'ajax_get_gallery_page');
add_action('wp_ajax_nopriv_bddb_next_gallery_page', 'ajax_get_gallery_page');
//修改主题对应的模板名
add_filter('page_template_hierarchy', 'bddb_add_theme_template_supported');
}
/**
* @brief 解析豆瓣页面内容。
* @param string $pic_mass 批量图片地址
* @param string $default 默认图片地址
* @return array
* @since 0.6.1
*/
function bddb_add_theme_template_supported($templates) {
$bfound = false;
foreach($templates as $template) {
foreach(BDDB_Statics::get_valid_types() as $bddb_type) {
$imaged_name = sprintf("page-%ssgallery.php", $bddb_type);
if ($imaged_name === $template) {
$bfound = true;
break;
}
}
if ($bfound) {
break;
}
}
if ($bfound) {
array_unshift($templates, "page-bddbgallery.php");
}
return $templates;
}
function qt_show_record($atts, $content = null) {
extract( $atts );
$post_type = get_post_type($id);
$ret = '';
if ('book' == $post_type) {
$ret = BDDB_Book::getInstance()->show_record($atts, $content);
} elseif('movie' == $post_type) {
$ret = BDDB_Movie::getInstance()->show_record($atts, $content);
} elseif('game' == $post_type) {
$ret = BDDB_Game::getInstance()->show_record($atts, $content);
} elseif('album' == $post_type) {
$ret = BDDB_Album::getInstance()->show_record($atts, $content);
}
return $ret;
}
/*取下一页的ajax回调函数*/
function ajax_get_gallery_page() {
if (!isset($_POST['nonce']) || !isset($_POST['pid']) || !isset($_POST['type']) || !isset($_POST['nobj']) ) {
wp_die();
}
if ('book' == $_POST['type']) {
BDDB_Book::getInstance()->ajax_get_gallery_page();
} elseif('movie' == $_POST['type']) {
BDDB_Movie::getInstance()->ajax_get_gallery_page();
} elseif('game' == $_POST['type']) {
BDDB_Game::getInstance()->ajax_get_gallery_page();
} elseif('album' == $_POST['type']) {
BDDB_Album::getInstance()->ajax_get_gallery_page();
}
}
/*取豆瓣信息的ajax回调函数*/
//TODO 放进类中
function bddb_ajax_douban_fetch() {
$resp = array('title' => 'here is the title', 'content' => 'finished') ;
if (!isset($_GET['nonce']) || !isset($_GET['id']) || !isset($_GET['ptype']) || !isset($_GET['doulink']) ) {
wp_die();
}
if ( !wp_verify_nonce($_GET['nonce'],"douban-spider-".$_GET['id'])) {
wp_die();
}
if (!BDDB_Statics::is_valid_type($_GET['ptype'])){
wp_die();
}
$post_id = $_GET['id'];
$link = $_GET['doulink'];
$got = BDDB_Fetcher::fetch($link, $_GET['ptype']);
$resp['result'] = $got['content'];
wp_send_json($resp) ;
wp_die();
}
//后台初始化
function bddb_admin_init() {
add_action( 'admin_enqueue_scripts', 'bddb_admin_scripts' );
add_action( 'wp_ajax_bddb_douban_fetch', 'bddb_ajax_douban_fetch' );
BDDB_Statics::admin_init();
BDDB_Typed_List::admin_init();
BDDB_Editor_Factory::admin_init();
}
//js和css初始化
function bddb_scripts() {
if (is_page(array('moviesgallery','booksgallery','gamesgallery','albumsgallery'))) {
remove_action( 'wp_head','print_emoji_detection_script',7);
remove_action( 'wp_print_styles', 'print_emoji_styles');
wp_enqueue_script( 'bddb-fancy', BDDB_PLUGIN_URL . 'js/fancybox.umd.js', array(), '20211123', true );
wp_enqueue_script( 'bddb-color-thief', BDDB_PLUGIN_URL . 'js/color-thief.js', array(), '20221128', true );
wp_enqueue_script( 'bddb-fancy-func', BDDB_PLUGIN_URL . 'js/fancygallery.js', array(), '20230608', true );
wp_localize_script( 'bddb-fancy-func', 'ajaxurl', admin_url('admin-ajax.php'));
wp_enqueue_style( 'bddb-boxstyle', BDDB_PLUGIN_URL . 'css/fancybox.css', array(), '20220829' );
$css = '';
$rate = floatval(BDDB_Settings::get_poster_height(false)/BDDB_Settings::get_poster_width(false));
if (is_page('booksgallery')) {
$rate = floatval(BDDB_Settings::get_poster_height('book')/BDDB_Settings::get_poster_width('book'));
}
elseif (is_page('moviesgallery')) {
$rate = floatval(BDDB_Settings::get_poster_height('movie')/BDDB_Settings::get_poster_width('movie'));
}
elseif (is_page('gamesgallery')) {
$rate = floatval(BDDB_Settings::get_poster_height('game')/BDDB_Settings::get_poster_width('game'));
}
elseif (is_page('albumsgallery')) {
$rate = floatval(BDDB_Settings::get_poster_height('album')/BDDB_Settings::get_poster_width('album'));
}
$thumbs_height = intval(80 * $rate);
$css = ".fancybox__container { --fancybox-thumbs-width: 80px; --fancybox-thumbs-height: {$thumbs_height}px; }";
wp_add_inline_style('bddb-boxstyle', $css);
wp_enqueue_style( 'bddb-gallery-boxstyle', BDDB_PLUGIN_URL . 'css/bddb-fancy-gallery.css', array(), '20241010' );
}
wp_enqueue_style( 'bddb-style-front', BDDB_PLUGIN_URL . 'css/bddb.css', array(), '20230608' );
}
/* 统一处理后台相关的脚本 */
function bddb_admin_scripts() {
wp_enqueue_script('bddb-js-admin', BDDB_PLUGIN_URL . 'js/bddb-admin.js', array(), '20230608', true);
wp_localize_script( 'bddb-js-admin', 'nomouse_names', array('nothing'));
wp_enqueue_style( 'bddb-adminstyle', BDDB_PLUGIN_URL . 'css/bddb-admin.css', array(), '20220526' );
wp_deregister_style( 'open-sans' );
wp_register_style( 'open-sans', false );
}