-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjPlugin-RSSFeed.php
More file actions
873 lines (737 loc) · 36.5 KB
/
jPlugin-RSSFeed.php
File metadata and controls
873 lines (737 loc) · 36.5 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
<?php
/**
* Plugin Name: jPlugin-RSSFeed
* Plugin URI: https://dev.jirath.com/
* Description: Plugin สำหรับดึงข้อมูลบทความในรูปแบบ RSS Feed มาแสดงบน WordPress ของคุณ
* Version: 1.0.0
* Author: JIRATH BURAPARATH
* Author URI: https://www.jirath.com
* Text Domain: jrssfeed
*/
// หากมีการเรียกไฟล์โดยตรง ให้ออกจากระบบ
if (!defined('ABSPATH')) {
exit;
}
// กำหนดค่าคงที่สำหรับ Plugin
define('JRSSFEED_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('JRSSFEED_PLUGIN_URL', plugin_dir_url(__FILE__));
define('DEFAULT_URL', 'https://feeds.feedburner.com/blogspot/dailydeliciousthai');
/**
* ฟังก์ชันที่ทำงานเมื่อเปิดใช้งานปลั๊กอินครั้งแรก
* - สร้างไฟล์ CSS
* - ตั้งค่าเริ่มต้นสำหรับ Feed URLs
*/
function jrssfeed_plugin_activation() {
// สร้างไดเรกทอรี CSS ถ้ายังไม่มี
$css_dir = JRSSFEED_PLUGIN_DIR . 'css';
if (!file_exists($css_dir)) {
mkdir($css_dir, 0755, true);
}
// สร้างไฟล์ CSS
$css_file = $css_dir . '/styles.css';
$css_content = '
/* jrssfeed Styles - Modern Classic Theme-Agnostic Design */
.jrssfeed-rsscontent-container {
margin: 2rem 0;
line-height: 1.6;
}
/* List Layout */
.jrssfeed-list {
list-style: none;
padding: 0;
margin: 0;
}
.jrssfeed-list-item {
margin-bottom: 2.5rem;
padding-bottom: 2rem;
border-bottom: 1px solid rgba(0, 0, 0, 0.08);
display: flex;
flex-wrap: wrap;
transition: transform 0.2s ease;
}
.jrssfeed-list-item:hover {
transform: translateY(-3px);
}
.jrssfeed-list-item:last-child {
border-bottom: none;
}
.jrssfeed-thumbnail {
flex: 0 0 28%;
max-width: 28%;
margin-right: 2rem;
border-radius: 4px;
overflow: hidden;
box-shadow: 0 3px 10px rgba(0, 0, 0, 0.08);
}
.jrssfeed-thumbnail img {
max-width: 100%;
height: auto;
display: block;
transition: transform 0.3s ease;
}
.jrssfeed-thumbnail img:hover {
transform: scale(1.03);
}
.jrssfeed-content {
flex: 0 0 calc(72% - 2rem);
max-width: calc(72% - 2rem);
}
.jrssfeed-title {
margin-top: 0;
margin-bottom: 0.8rem;
transition: color 0.2s ease;
}
.jrssfeed-title a {
text-decoration: none;
}
.jrssfeed-date {
opacity: 0.7;
font-size: 0.9em;
margin-bottom: 1rem;
}
.jrssfeed-excerpt {
margin-bottom: 1.2rem;
line-height: 1.7;
}
.jrssfeed-source {
font-size: 0.85em;
opacity: 0.7;
margin-top: 0.8rem;
font-style: italic;
}
.jrssfeed-read-more {
display: inline-block;
padding: 0.5rem 1.2rem;
text-decoration: none;
border-radius: 4px;
transition: all 0.2s ease;
background-color: rgba(0, 0, 0, 0.05);
}
.jrssfeed-read-more:hover {
background-color: rgba(0, 0, 0, 0.1);
transform: translateY(-2px);
box-shadow: 0 3px 8px rgba(0, 0, 0, 0.08);
}
/* Grid Layout */
.jrssfeed-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap: 2rem;
margin: 1.5rem 0;
}
.jrssfeed-grid-item {
border-radius: 6px;
overflow: hidden;
transition: all 0.25s ease;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
background-color: rgba(255, 255, 255, 0.02);
backdrop-filter: blur(5px);
}
.jrssfeed-grid-item:hover {
transform: translateY(-5px);
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
}
.jrssfeed-grid-item .jrssfeed-thumbnail {
max-width: 100%;
margin-right: 0;
margin-bottom: 0;
border-radius: 6px 6px 0 0;
box-shadow: none;
}
.jrssfeed-grid-item .jrssfeed-content {
max-width: 100%;
padding: 1.5rem;
}
.jrssfeed-grid-item .jrssfeed-title {
font-size: 1.2em;
line-height: 1.4;
}
/* Admin Settings */
.jrssfeed-feed-url-row {
margin-bottom: 1rem;
padding: 0.8rem;
border-radius: 4px;
background-color: rgba(0, 0, 0, 0.03);
}
.jrssfeed-remove-url {
margin-left: 0.8rem;
opacity: 0.7;
transition: opacity 0.2s ease;
}
.jrssfeed-remove-url:hover {
opacity: 1;
}
.jrssfeed-banner {
display: flex;
align-items: center;
background: linear-gradient(to right, #f8f9fa, #e9ecef);
padding: 20px;
margin-bottom: 30px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
border-left: 4px solid rgb(146, 9, 9); /* สีน้ำเงินของ Google */
border-radius: 5px;
margin-top: 20px;
}
.jrssfeed-banner .jrssfeed-banner-logo {
margin-right: 20px;
flex-shrink: 0;
}
.jrssfeed-banner .jrssfeed-banner-logo img {
border-radius: 10px;
box-shadow: 0 3px 6px rgba(0, 0, 0, 0.1);
transition: transform 0.3s ease;
}
.jrssfeed-banner .jrssfeed-banner-logo img:hover {
transform: scale(1.05);
}
.jrssfeed-banner .jrssfeed-banner-banner-content {
flex-grow: 1;
}
.jrssfeed-banner .jrssfeed-banner-banner-content h1 {
margin: 0 0 8px 0;
color: #202124;
font-size: 24px;
font-weight: 600;
line-height: 1.2;
}
.jrssfeed-banner .jrssfeed-banner-banner-content .jrssfeed-banner-description {
margin: 0;
color: #5f6368;
font-size: 14px;
line-height: 1.5;
}
.jrssfeed-banner .jrssfeed-banner-version {
background-color:rgb(146, 9, 9);
color: white;
padding: 5px 10px;
border-radius: 15px;
font-size: 12px;
font-weight: 500;
margin-left: 10px;
box-shadow: 0 2px 4px rgba(66, 133, 244, 0.3);
}
/* Responsive */
@media (max-width: 992px) {
.jrssfeed-grid {
grid-template-columns: repeat(2, 1fr);
}
}
@media (max-width: 768px) {
.jrssfeed-thumbnail {
flex: 0 0 100%;
max-width: 100%;
margin-right: 0;
margin-bottom: 1.5rem;
}
.jrssfeed-content {
flex: 0 0 100%;
max-width: 100%;
}
}
@media (max-width: 576px) {
.jrssfeed-grid {
grid-template-columns: 1fr;
grid-gap: 1.5rem;
}
.jrssfeed-list-item {
margin-bottom: 2rem;
padding-bottom: 1.5rem;
}
}';
if (!file_exists($css_file)) {
file_put_contents($css_file, $css_content);
}
// ตั้งค่าเริ่มต้นสำหรับ feed URLs
$default_feed_urls = array(
DEFAULT_URL
);
if (!get_option('jrssfeed_feed_urls')) {
update_option('jrssfeed_feed_urls', $default_feed_urls);
}
}
register_activation_hook(__FILE__, 'jrssfeed_plugin_activation');
/**
* ฟังก์ชันสำหรับล้างแคชเมื่อคลิกปุ่ม "ล้างแคช" ในหน้าตั้งค่า
*/
function jrssfeed_handle_clear_cache() {
if (isset($_GET['action']) && $_GET['action'] === 'clear_cache' && isset($_GET['_wpnonce'])) {
if (wp_verify_nonce($_GET['_wpnonce'], 'jrssfeed_clear_cache')) {
delete_transient('jrssfeed_rsscontent_feed_array');
wp_redirect(admin_url('admin.php?page=jrssfeed-settings&cache_cleared=1'));
exit;
}
}
}
add_action('admin_init', 'jrssfeed_handle_clear_cache');
/**
* เพิ่มเมนูในส่วนผู้ดูแลระบบ
*/
function jrssfeed_add_admin_menu() {
add_menu_page(
'jrssfeed-dashboard', # ชื่อหน้า
'jRSSFeed', # ขื่อที่แสดงในเมนู
'manage_options', # สิทธิ์การเข้าถึง ต้องเป็นแอดมินที่จัดการตัวเลือกได้
'jrssfeed-admin-page', # slug
'jrssfeed_admin_page', # views function
'dashicons-rss', # ICON
30 # ลำดับแสดง
);
// เพิ่ม submenu แรกที่มีชื่อต่างจากเมนูหลัก (ต้องใช้ slug เดียวกับเมนูหลัก)
add_submenu_page(
'jrssfeed-admin-page', # parent slug (ต้องตรงกับ slug ของเมนูหลัก)
'jrssfeed-dashboad-submenu',# ชื่อหน้า
'แดชบอร์ด', # ชื่อที่แสดงในเมนู (เปลี่ยนเป็นชื่อที่ต้องการ)
'manage_options', # สิทธิ์การเข้าถึง
'jrssfeed-admin-page', # slug (ต้องตรงกับ slug ของเมนูหลัก)
'jrssfeed_admin_page' # views function (ต้องเหมือนกับในเมนูหลัก)
);
add_submenu_page(
'jrssfeed-admin-page', # parent slug (ต้องตรงกับ slug ของเมนูหลัก)
'jrssfeed-settings', # ชื่อหน้า
'การตั้งค่า', # ชื่อที่แสดงในเมนู
'manage_options', # สิทธิ์การเข้าถึง
'jrssfeed-settings', # slug ของเมนูย่อย
'jrssfeed_settings_page' # views function
);
}
add_action('admin_menu', 'jrssfeed_add_admin_menu');
/**
* หน้าแสดงผลหลักในส่วนผู้ดูแลระบบ
*/
function jrssfeed_admin_page() {
?>
<?php include plugin_dir_path(__FILE__) . 'banner.php'; ?>
<div class="wrap">
<!-- <h1>jRSSFeed</h1>
<p>ยินดีต้อนรับสู่ Plugin สำหรับดึงข้อมูลบทความจากเว็บบล็อกต่างๆ มาแสดงบน WordPress</p> -->
<h2>บทความจาก RSS Feed ล่าสุด</h2>
<?php
$feed_items = jrssfeed_get_feed_items();
if (is_wp_error($feed_items)) {
echo '<div class="error"><p>ไม่สามารถดึงข้อมูลได้: ' . esc_html($feed_items->get_error_message()) . '</p></div>';
} else {
echo '<table class="wp-list-table widefat fixed striped">';
echo '<thead><tr><th>หัวข้อ</th><th>วันที่</th><th>แหล่งที่มา</th></tr></thead>';
echo '<tbody>';
foreach ($feed_items as $item) {
$title = esc_html($item['title']);
$date = $item['date'];
$permalink = esc_url($item['permalink']);
$source = isset($item['source_domain']) ? esc_html($item['source_domain']) : '';
echo '<tr>';
echo '<td><a href="' . $permalink . '" target="_blank">' . $title . '</a></td>';
echo '<td>' . $date . '</td>';
echo '<td>' . $source . '</td>';
echo '</tr>';
}
echo '</tbody></table>';
}
?>
<hr>
<h2>วิธีใช้งาน Shortcode</h2>
<p>คุณสามารถใช้ shortcode ต่อไปนี้เพื่อแสดงบทความจากเว็บบล็อกต่างๆ:</p>
<ul>
<li><code>[jrssfeed_rsscontent_posts count="5"]</code> - แสดงบทความล่าสุด 5 บทความ</li>
<li><code>[jrssfeed_rsscontent_posts count="10" category="wordpress"]</code> - แสดงบทความล่าสุด 10 บทความในหมวดหมู่ wordpress</li>
<li><code>[jrssfeed_rsscontent_posts display="grid" show_source="yes"]</code> - แสดงเป็นกริด พร้อมแสดงแหล่งที่มา</li>
</ul>
</div>
<?php
}
/**
* หน้าตั้งค่าในส่วนผู้ดูแลระบบ
* - เพิ่มการจัดการหลาย Feed URLs
*/
function jrssfeed_settings_page() {
// บันทึกการตั้งค่า
if (isset($_POST['jrssfeed_save_settings'])) {
check_admin_referer('jrssfeed_settings_nonce');
$cache_time = intval($_POST['jrssfeed_cache_time']);
update_option('jrssfeed_cache_time', $cache_time);
// บังคับลบ transient เมื่อมีการเปลี่ยนแปลงค่า cache_time
delete_transient('jrssfeed_rsscontent_feed_array');
$posts_per_page = intval($_POST['jrssfeed_posts_per_page']);
update_option('jrssfeed_posts_per_page', $posts_per_page);
// บันทึก feed URLs
$feed_urls = array();
if (isset($_POST['jrssfeed_feed_urls']) && is_array($_POST['jrssfeed_feed_urls'])) {
foreach ($_POST['jrssfeed_feed_urls'] as $url) {
$url = trim($url);
if (!empty($url)) {
$feed_urls[] = esc_url_raw($url);
}
}
}
// ตรวจสอบว่ามี URL อย่างน้อย 1 URL
if (empty($feed_urls)) {
$feed_urls = array(DEFAULT_URL);
}
update_option('jrssfeed_feed_urls', $feed_urls);
echo '<div class="updated"><p>บันทึกการตั้งค่าเรียบร้อยแล้ว</p></div>';
}
if (isset($_POST['jrssfeed_reset_all'])) {
check_admin_referer('jrssfeed_settings_nonce');
// ลบการตั้งค่าทั้งหมด
delete_option('jrssfeed_cache_time');
delete_option('jrssfeed_posts_per_page');
delete_option('jrssfeed_feed_urls');
// ลบข้อมูล Widget (ถ้าต้องการลบจากหน้าตั้งค่าด้วย)
delete_option('widget_jrssfeed_rsscontent_widget');
// ลบ transient cache
delete_transient('jrssfeed_rsscontent_feed_array');
// ตั้งค่าใหม่เป็นค่าเริ่มต้น
update_option('jrssfeed_cache_time', 3600);
update_option('jrssfeed_posts_per_page', 5);
update_option('jrssfeed_feed_urls', array(DEFAULT_URL));
echo '<div class="updated"><p>รีเซ็ตการตั้งค่าทั้งหมดเรียบร้อยแล้ว</p></div>';
}
// ดึงค่าการตั้งค่า
$cache_time = get_option('jrssfeed_cache_time', 3600); // ค่าเริ่มต้น 1 ชั่วโมง
$posts_per_page = get_option('jrssfeed_posts_per_page', 5); // ค่าเริ่มต้น 5 บทความ
$feed_urls = get_option('jrssfeed_feed_urls', array(DEFAULT_URL));
?>
<?php include plugin_dir_path(__FILE__) . 'banner.php'; ?>
<div class="wrap">
<!-- <h1>การตั้งค่า jrssfeed rsscontent</h1> -->
<h2>การตั้งค่า</h2>
<form method="post" action="">
<?php wp_nonce_field('jrssfeed_settings_nonce'); ?>
<table class="form-table">
<tr>
<th><label for="jrssfeed_cache_time">เวลาในการแคช (วินาที)</label></th>
<td>
<input type="number" id="jrssfeed_cache_time" name="jrssfeed_cache_time" value="<?php echo esc_attr($cache_time); ?>" min="60" step="60">
<p class="description">ระยะเวลาที่จะเก็บแคชข้อมูลก่อนดึงข้อมูลใหม่ (ขั้นต่ำ 60 วินาที)</p>
</td>
</tr>
<tr>
<th><label for="jrssfeed_posts_per_page">จำนวนบทความต่อหน้า</label></th>
<td>
<input type="number" id="jrssfeed_posts_per_page" name="jrssfeed_posts_per_page" value="<?php echo esc_attr($posts_per_page); ?>" min="1" max="50">
<p class="description">จำนวนบทความที่จะแสดงเมื่อใช้ shortcode โดยไม่ระบุจำนวน</p>
</td>
</tr>
<tr>
<th><label>RSS Feed URLs</label></th>
<td>
<div id="jrssfeed-feed-urls-container">
<?php foreach ($feed_urls as $index => $url): ?>
<div class="jrssfeed-feed-url-row">
<input type="url" name="jrssfeed_feed_urls[]" value="<?php echo esc_attr($url); ?>" class="regular-text" placeholder="https://example.com/feed">
<?php if ($index > 0): ?>
<button type="button" class="button jrssfeed-remove-url">ลบ</button>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
<button type="button" class="button jrssfeed-add-url">เพิ่ม URL</button>
<p class="description">ใส่ URL ของ RSS feed ที่ต้องการดึงข้อมูล</p>
</td>
</tr>
</table>
<p>
<input type="submit" name="jrssfeed_save_settings" class="button button-primary" value="บันทึกการตั้งค่า">
<input type="submit" name="jrssfeed_reset_all" class="button button-secondary" value="รีเซ็ตการตั้งค่าทั้งหมด" onclick="return confirm('คุณแน่ใจหรือไม่ว่าต้องการรีเซ็ตการตั้งค่าทั้งหมดเป็นค่าเริ่มต้น?');">
</p>
</form>
<hr>
<h2>ล้างแคช</h2>
<p>คลิกปุ่มด้านล่างเพื่อล้างแคชและดึงข้อมูลใหม่จากทุก Feed URLs</p>
<p><a href="<?php echo wp_nonce_url(admin_url('admin.php?page=jrssfeed-settings&action=clear_cache'), 'jrssfeed_clear_cache'); ?>" class="button">ล้างแคช</a></p>
</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
// เพิ่ม URL
$('.jrssfeed-add-url').on('click', function() {
var newRow = $('<div class="jrssfeed-feed-url-row"></div>');
newRow.append('<input type="url" name="jrssfeed_feed_urls[]" value="" class="regular-text" placeholder="https://example.com/feed"> ');
newRow.append('<button type="button" class="button jrssfeed-remove-url">ลบ</button>');
$('#jrssfeed-feed-urls-container').append(newRow);
});
// ลบ URL
$(document).on('click', '.jrssfeed-remove-url', function() {
$(this).parent('.jrssfeed-feed-url-row').remove();
});
});
</script>
<?php
}
/**
* แสดงข้อความแจ้งเตือนเมื่อล้างแคช
*/
function jrssfeed_admin_notices() {
if (isset($_GET['cache_cleared']) && $_GET['cache_cleared'] === '1' && isset($_GET['page']) && $_GET['page'] === 'jrssfeed-settings') {
echo '<div class="updated"><p>ล้างแคชเรียบร้อยแล้ว</p></div>';
}
}
add_action('admin_notices', 'jrssfeed_admin_notices');
/**
* ดึงข้อมูลจากหลาย RSS feeds
*
* @param int $count จำนวนรายการที่ต้องการ
* @param string $category หมวดหมู่ที่ต้องการกรอง (ถ้ามี)
* @return array|WP_Error รายการบทความหรือข้อผิดพลาด
*/
function jrssfeed_get_feed_items($count = 10, $category = '') {
$cache_time = get_option('jrssfeed_cache_time', 3600);
// ตรวจสอบแคชจาก Transient API
$cached_items = get_transient('jrssfeed_rsscontent_feed_array');
if (false !== $cached_items) {
// กรองตามหมวดหมู่ถ้ามีการระบุ
if (!empty($category)) {
$filtered_items = array_filter($cached_items, function($item) use ($category) {
return in_array(strtolower($category), array_map('strtolower', $item['categories']));
});
return array_slice($filtered_items, 0, $count);
}
// ส่งคืนเฉพาะจำนวนที่ต้องการ
return array_slice($cached_items, 0, $count);
}
// ไม่มีข้อมูลในแคช ดึงข้อมูลใหม่
if (!class_exists('SimplePie')) {
require_once(ABSPATH . WPINC . '/class-simplepie.php');
}
// ดึง URLs จากการตั้งค่า
$feed_urls = get_option('jrssfeed_feed_urls', array(DEFAULT_URL));
$all_items = array();
$errors = array();
// วนลูปดึงข้อมูลจากแต่ละ feed URL
foreach ($feed_urls as $feed_url) {
$feed = new SimplePie();
$feed->set_feed_url($feed_url);
$feed->enable_cache(false); // ปิดแคชของ SimplePie เพราะเราใช้ Transient แล้ว
$feed->init();
if ($feed->error()) {
$errors[] = 'Feed URL: ' . $feed_url . ' - Error: ' . $feed->error();
continue; // ข้ามไปยัง URL ถัดไปหากมีข้อผิดพลาด
}
$items = $feed->get_items(0, 10); // ดึง 50 รายการล่าสุดจากแต่ละ feed
// แปลง SimplePie items เป็น array ธรรมดา
foreach ($items as $item) {
// ดึงโดเมนจาก URL สำหรับแสดงแหล่งที่มา
$source_domain = parse_url($feed_url, PHP_URL_HOST);
$all_items[] = array(
'title' => $item->get_title(),
'permalink' => $item->get_permalink(),
'date' => $item->get_date('Y-m-d H:i:s'),
'content' => $item->get_content(),
'excerpt' => wp_trim_words($item->get_content(), 120, '...'),
'author' => $item->get_author() ? $item->get_author()->get_name() : '',
'categories' => $item->get_categories() ? array_map(function ($cat) {
return $cat->get_term();
}, $item->get_categories()) : array(),
'source_url' => $feed_url,
'source_domain' => $source_domain
);
}
}
// มีข้อผิดพลาดทั้งหมดและไม่มีข้อมูล
if (empty($all_items) && !empty($errors)) {
return new WP_Error('simplepie_error', implode('<br>', $errors));
}
// เรียงลำดับรายการตามวันที่ล่าสุด
usort($all_items, function($a, $b) {
return strtotime($b['date']) - strtotime($a['date']);
});
// เก็บข้อมูลในแคชด้วย Transient API
// set_transient('jrssfeed_rsscontent_feed_array', $all_items, $cache_time);
// แก้ไขเป็น set_transient แบบบังคับ
set_transient('jrssfeed_rsscontent_feed_array', $all_items, $cache_time);
// var_dump($all_items);
// var_dump($cache_time);
// กรองตามหมวดหมู่ถ้ามีการระบุ
if (!empty($category)) {
$filtered_items = array_filter($all_items, function($item) use ($category) {
return in_array(strtolower($category), array_map('strtolower', $item['categories']));
});
return array_slice($filtered_items, 0, $count);
}
// ส่งคืนเฉพาะจำนวนที่ต้องการ
return array_slice($all_items, 0, $count);
}
/**
* Shortcode สำหรับแสดงบทความ
*
* @param array $atts ตัวเลือกของ shortcode
* @return string HTML สำหรับแสดงผล
*/
function jrssfeed_rsscontent_posts_shortcode($atts) {
$atts = shortcode_atts(array(
'count' => get_option('jrssfeed_posts_per_page', 5),
'category' => '',
'display' => 'list', // list, grid
'show_thumbnail' => 'yes',
'show_excerpt' => 'yes',
'show_date' => 'yes',
'show_source' => 'no', // เพิ่มตัวเลือกแสดงแหล่งที่มา
), $atts);
$count = intval($atts['count']);
$feed_items = jrssfeed_get_feed_items($count, $atts['category']);
if (is_wp_error($feed_items)) {
return '<div class="jrssfeed-error">ไม่สามารถดึงข้อมูลได้: ' . esc_html($feed_items->get_error_message()) . '</div>';
}
$output = '<div class="jrssfeed-rsscontent-container">';
if ($atts['display'] === 'grid') {
$output .= '<div class="jrssfeed-grid">';
} else {
$output .= '<ul class="jrssfeed-list">';
}
foreach ($feed_items as $item) {
$title = esc_html($item['title']);
$permalink = esc_url($item['permalink']);
$date = $item['date'];
$content = $item['content'];
$excerpt = $item['excerpt'];
$source_domain = isset($item['source_domain']) ? esc_html($item['source_domain']) : '';
// ค้นหา thumbnail
$thumbnail = '';
if ($atts['show_thumbnail'] === 'yes') {
preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $content, $matches);
if (!empty($matches['src'])) {
$thumbnail = $matches['src'];
}
}
if ($atts['display'] === 'grid') {
$output .= '<div class="jrssfeed-grid-item">';
if ($thumbnail && $atts['show_thumbnail'] === 'yes') {
$output .= '<div class="jrssfeed-thumbnail"><a href="' . $permalink . '" target="_blank"><img src="' . esc_url($thumbnail) . '" alt="' . $title . '"></a></div>';
}
$output .= '<h3 class="jrssfeed-title"><a href="' . $permalink . '" target="_blank">' . $title . '</a></h3>';
if ($atts['show_date'] === 'yes') {
$output .= '<div class="jrssfeed-date">' . $date . '</div>';
}
if ($atts['show_source'] === 'yes' && !empty($source_domain)) {
$output .= '<div class="jrssfeed-source">แหล่งที่มา: ' . $source_domain . '</div>';
}
if ($atts['show_excerpt'] === 'yes') {
$output .= '<div class="jrssfeed-excerpt">' . $excerpt . '</div>';
}
$output .= '<a href="' . $permalink . '" target="_blank" class="jrssfeed-read-more">อ่านเพิ่มเติม</a>';
$output .= '</div>';
} else {
$output .= '<li class="jrssfeed-list-item">';
if ($thumbnail && $atts['show_thumbnail'] === 'yes') {
$output .= '<div class="jrssfeed-thumbnail"><a href="' . $permalink . '" target="_blank"><img src="' . esc_url($thumbnail) . '" alt="' . $title . '"></a></div>';
}
$output .= '<div class="jrssfeed-content">';
$output .= '<h3 class="jrssfeed-title"><a href="' . $permalink . '" target="_blank">' . $title . '</a></h3>';
if ($atts['show_date'] === 'yes') {
$output .= '<div class="jrssfeed-date">' . $date . '</div>';
}
if ($atts['show_source'] === 'yes' && !empty($source_domain)) {
$output .= '<div class="jrssfeed-source">แหล่งที่มา: ' . $source_domain . '</div>';
}
if ($atts['show_excerpt'] === 'yes') {
$output .= '<div class="jrssfeed-excerpt">' . $excerpt . '</div>';
}
$output .= '<a href="' . $permalink . '" target="_blank" class="jrssfeed-read-more">อ่านเพิ่มเติม</a>';
$output .= '</div>';
$output .= '</li>';
}
}
if ($atts['display'] === 'grid') {
$output .= '</div>';
} else {
$output .= '</ul>';
}
$output .= '</div>';
return $output;
}
add_shortcode('jrssfeed_rsscontent_posts', 'jrssfeed_rsscontent_posts_shortcode');
/**
* เพิ่ม CSS สำหรับแสดงผลด้านหน้าเว็บไซต์
*/
function jrssfeed_enqueue_styles() {
wp_enqueue_style('jrssfeed-styles', JRSSFEED_PLUGIN_URL . 'css/styles.css', array(), '1.0.0');
}
add_action('wp_enqueue_scripts', 'jrssfeed_enqueue_styles');
/**
* เพิ่ม jQuery สำหรับหน้าตั้งค่าในแอดมิน
*/
function jrssfeed_admin_scripts($hook) {
// โหลด CSS สำหรับทุกหน้าที่เกี่ยวข้องกับปลั๊กอินของคุณ
if (strpos($hook, 'jrssfeed') !== false) {
wp_enqueue_style('jrssfeed-styles', JRSSFEED_PLUGIN_URL . 'css/styles.css', array(), '1.0.0');
}
if ($hook == 'jrssfeed_page_jrssfeed-settings') {
wp_enqueue_script('jquery');
}
}
add_action('admin_enqueue_scripts', 'jrssfeed_admin_scripts');
/**
* สร้าง Widget สำหรับแสดงผลบทความในธีม
*/
class jrssfeed_rsscontent_Widget extends WP_Widget {
public function __construct() {
parent::__construct(
'jrssfeed_rsscontent_widget',
'jrssfeed rsscontent',
array('description' => 'แสดงบทความล่าสุดจากเว็บบล็อกต่างๆ')
);
}
public function widget($args, $instance) {
$title = isset($instance['title']) ? apply_filters('widget_title', $instance['title']) : 'บทความล่าสุด';
$count = isset($instance['count']) ? intval($instance['count']) : 5;
$show_thumbnail = isset($instance['show_thumbnail']) ? $instance['show_thumbnail'] : 'yes';
$show_excerpt = isset($instance['show_excerpt']) ? $instance['show_excerpt'] : 'yes';
$show_source = isset($instance['show_source']) ? $instance['show_source'] : 'no';
echo $args['before_widget'];
echo $args['before_title'] . $title . $args['after_title'];
echo do_shortcode('[jrssfeed_rsscontent_posts count="' . $count . '" show_thumbnail="' . $show_thumbnail . '" show_excerpt="' . $show_excerpt . '" show_source="' . $show_source . '"]');
echo $args['after_widget'];
}
public function form($instance) {
$title = isset($instance['title']) ? $instance['title'] : 'บทความล่าสุด';
$count = isset($instance['count']) ? intval($instance['count']) : 5;
$show_thumbnail = isset($instance['show_thumbnail']) ? $instance['show_thumbnail'] : 'yes';
$show_excerpt = isset($instance['show_excerpt']) ? $instance['show_excerpt'] : 'yes';
$show_source = isset($instance['show_source']) ? $instance['show_source'] : 'no';
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">ชื่อ Widget:</label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id('count'); ?>">จำนวนบทความ:</label>
<input class="widefat" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" type="number" min="1" max="10" value="<?php echo esc_attr($count); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id('show_thumbnail'); ?>">แสดงรูปภาพ:</label>
<select class="widefat" id="<?php echo $this->get_field_id('show_thumbnail'); ?>" name="<?php echo $this->get_field_name('show_thumbnail'); ?>">
<option value="yes" <?php selected($show_thumbnail, 'yes'); ?>>ใช่</option>
<option value="no" <?php selected($show_thumbnail, 'no'); ?>>ไม่</option>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id('show_excerpt'); ?>">แสดงเนื้อหาย่อ:</label>
<select class="widefat" id="<?php echo $this->get_field_id('show_excerpt'); ?>" name="<?php echo $this->get_field_name('show_excerpt'); ?>">
<option value="yes" <?php selected($show_excerpt, 'yes'); ?>>ใช่</option>
<option value="no" <?php selected($show_excerpt, 'no'); ?>>ไม่</option>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id('show_source'); ?>">แสดงแหล่งที่มา:</label>
<select class="widefat" id="<?php echo $this->get_field_id('show_source'); ?>" name="<?php echo $this->get_field_name('show_source'); ?>">
<option value="yes" <?php selected($show_source, 'yes'); ?>>ใช่</option>
<option value="no" <?php selected($show_source, 'no'); ?>>ไม่</option>
</select>
</p>
<?php
}
public function update($new_instance, $old_instance) {
$instance = array();
$instance['title'] = !empty($new_instance['title']) ? sanitize_text_field($new_instance['title']) : '';
$instance['count'] = !empty($new_instance['count']) ? intval($new_instance['count']) : 5;
$instance['show_thumbnail'] = !empty($new_instance['show_thumbnail']) ? $new_instance['show_thumbnail'] : 'yes';
$instance['show_excerpt'] = !empty($new_instance['show_excerpt']) ? $new_instance['show_excerpt'] : 'yes';
$instance['show_source'] = !empty($new_instance['show_source']) ? $new_instance['show_source'] : 'no';
return $instance;
}
}
/**
* ลงทะเบียน Widget
*/
function jrssfeed_register_widget() {
register_widget('jrssfeed_rsscontent_Widget');
}
add_action('widgets_init', 'jrssfeed_register_widget');