Featured Image From URL - Version 2.2.6

Version Description

  • Bug fixes: menu settings, product category and default external image.
Download this release

Release Info

Developer marceljm
Plugin Icon 128x128 Featured Image From URL
Version 2.2.6
Comparing to
See all releases

Code changes from version 2.2.5 to 2.2.6

admin/category.php CHANGED
@@ -44,16 +44,17 @@ function fifu_ctgr_add_box() {
44
  include 'html/category.html';
45
  }
46
 
47
- add_action('edited_product_cat', 'fifu_ctgr_save_properties', 10, 2);
48
- add_action('create_product_cat', 'fifu_ctgr_save_properties', 10, 2);
49
 
50
  function fifu_ctgr_save_properties($term_id) {
51
- if (isset($_POST['fifu_input_url']))
52
- update_term_meta($term_id, 'fifu_image_url', fifu_convert(esc_url($_POST['fifu_input_url'])));
53
-
54
  if (isset($_POST['fifu_input_alt']))
55
  update_term_meta($term_id, 'fifu_image_alt', wp_strip_all_tags($_POST['fifu_input_alt']));
56
 
57
- fifu_ctgr_update_fake_attach_id($term_id);
 
 
 
 
58
  }
59
 
44
  include 'html/category.html';
45
  }
46
 
47
+ add_action('edited_product_cat', 'fifu_ctgr_save_properties', 10, 1);
48
+ add_action('created_product_cat', 'fifu_ctgr_save_properties', 10, 1);
49
 
50
  function fifu_ctgr_save_properties($term_id) {
 
 
 
51
  if (isset($_POST['fifu_input_alt']))
52
  update_term_meta($term_id, 'fifu_image_alt', wp_strip_all_tags($_POST['fifu_input_alt']));
53
 
54
+ if (isset($_POST['fifu_input_url'])) {
55
+ $url = esc_url($_POST['fifu_input_url']);
56
+ update_term_meta($term_id, 'fifu_image_url', fifu_convert($url));
57
+ fifu_db_ctgr_update_fake_attach_id($term_id);
58
+ }
59
  }
60
 
admin/db.php CHANGED
@@ -22,6 +22,12 @@ class FifuDb {
22
  $this->author = 77777;
23
  $this->MAX_INSERT = 100;
24
  $this->MAX_URL_LENGTH = 2048;
 
 
 
 
 
 
25
  }
26
 
27
  /* alter table */
@@ -47,7 +53,7 @@ class FifuDb {
47
  AND NOT EXISTS (
48
  SELECT 1
49
  FROM " . $this->postmeta . "
50
- WHERE post_id = p.post_parent
51
  AND meta_key = '_wp_attached_file'
52
  )
53
  )"
@@ -82,7 +88,7 @@ class FifuDb {
82
  AND NOT EXISTS (
83
  SELECT 1
84
  FROM " . $this->postmeta . "
85
- WHERE post_id = p.post_parent
86
  AND meta_key = '_wp_attachment_image_alt'
87
  )
88
  )"
@@ -108,6 +114,16 @@ class FifuDb {
108
  );
109
  }
110
 
 
 
 
 
 
 
 
 
 
 
111
  // get ids from categories with external media and no thumbnail_id
112
  function get_categories_without_meta() {
113
  return $this->wpdb->get_results("
@@ -173,6 +189,84 @@ class FifuDb {
173
  );
174
  }
175
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
  // clean meta data
177
 
178
  function delete_thumbnail_ids($ids) {
@@ -231,7 +325,17 @@ class FifuDb {
231
  function delete_attachments($ids) {
232
  $this->wpdb->get_results("
233
  DELETE FROM " . $this->posts . "
234
- WHERE id IN (" . $ids . ")"
 
 
 
 
 
 
 
 
 
 
235
  );
236
  }
237
 
@@ -298,7 +402,7 @@ class FifuDb {
298
  }
299
 
300
  function get_formatted_value($url, $alt, $post_parent) {
301
- return "(" . $this->author . ", '" . $url . "', '" . $alt . "', 'image/jpeg', 'attachment', 'inherit', '" . $post_parent . "', now(), now(), now(), now(), '', '', '', '', '')";
302
  }
303
 
304
  /* insert fake internal featured image */
@@ -311,6 +415,10 @@ class FifuDb {
311
  foreach ($this->get_categories_without_meta() as $res) {
312
  $ids = ($i++ == 0) ? $res->term_id : ($ids . "," . $res->term_id);
313
  $url = get_term_meta($res->term_id, 'fifu_image_url', true);
 
 
 
 
314
  $value = $this->get_formatted_value($url, get_term_meta($res->term_id, 'fifu_image_alt', true), $res->term_id);
315
  $this->insert_attachment_by($value);
316
  $att_id = $this->wpdb->insert_id;
@@ -391,6 +499,150 @@ class FifuDb {
391
  $this->delete_empty_urls_category();
392
  }
393
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
394
  }
395
 
396
  /* fake internal featured image */
@@ -420,3 +672,46 @@ function fifu_db_change_url_length() {
420
  $db->change_url_length();
421
  }
422
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  $this->author = 77777;
23
  $this->MAX_INSERT = 100;
24
  $this->MAX_URL_LENGTH = 2048;
25
+ $this->types = $this->get_types();
26
+ }
27
+
28
+ function get_types() {
29
+ $post_types = fifu_get_post_types();
30
+ return join("','", $post_types);
31
  }
32
 
33
  /* alter table */
53
  AND NOT EXISTS (
54
  SELECT 1
55
  FROM " . $this->postmeta . "
56
+ WHERE post_id = id
57
  AND meta_key = '_wp_attached_file'
58
  )
59
  )"
88
  AND NOT EXISTS (
89
  SELECT 1
90
  FROM " . $this->postmeta . "
91
+ WHERE post_id = id
92
  AND meta_key = '_wp_attachment_image_alt'
93
  )
94
  )"
114
  );
115
  }
116
 
117
+ // has attachment created bu FIFU
118
+ function is_fifu_attachment($att_id) {
119
+ return $this->wpdb->get_row("
120
+ SELECT 1
121
+ FROM " . $this->posts . "
122
+ WHERE id = " . $att_id . "
123
+ AND post_author = " . $this->author
124
+ ) != null;
125
+ }
126
+
127
  // get ids from categories with external media and no thumbnail_id
128
  function get_categories_without_meta() {
129
  return $this->wpdb->get_results("
189
  );
190
  }
191
 
192
+ function get_attachments_without_post($post_id) {
193
+ $result = $this->wpdb->get_results("
194
+ SELECT GROUP_CONCAT(id) AS ids
195
+ FROM " . $this->posts . "
196
+ WHERE post_parent = " . $post_id . "
197
+ AND post_type = 'attachment'
198
+ AND post_author = " . $this->author . "
199
+ AND NOT EXISTS (
200
+ SELECT 1
201
+ FROM " . $this->postmeta . "
202
+ WHERE post_id = post_parent
203
+ AND meta_key = '_thumbnail_id'
204
+ AND meta_value = id
205
+ )
206
+ GROUP BY post_parent"
207
+ );
208
+ return $result ? $result[0]->ids : null;
209
+ }
210
+
211
+ function get_ctgr_attachments_without_post($term_id) {
212
+ $result = $this->wpdb->get_results("
213
+ SELECT GROUP_CONCAT(id) AS ids
214
+ FROM " . $this->posts . "
215
+ WHERE post_parent = " . $term_id . "
216
+ AND post_type = 'attachment'
217
+ AND post_author = " . $this->author . "
218
+ AND NOT EXISTS (
219
+ SELECT 1
220
+ FROM " . $this->termmeta . "
221
+ WHERE term_id = post_parent
222
+ AND meta_key = 'thumbnail_id'
223
+ AND meta_value = id
224
+ )
225
+ GROUP BY post_parent"
226
+ );
227
+ return $result ? $result[0]->ids : null;
228
+ }
229
+
230
+ function get_posts_without_featured_image() {
231
+ return $this->wpdb->get_results("
232
+ SELECT id
233
+ FROM " . $this->posts . "
234
+ WHERE post_type IN ('$this->types')
235
+ AND post_status = 'publish'
236
+ AND NOT EXISTS (
237
+ SELECT 1
238
+ FROM " . $this->postmeta . "
239
+ WHERE post_id = id
240
+ AND meta_key IN ('_thumbnail_id', 'fifu_image_url')
241
+ )"
242
+ );
243
+ }
244
+
245
+ function get_category_image_url($term_id) {
246
+ return $this->wpdb->get_results("
247
+ SELECT meta_value
248
+ FROM " . $this->termmeta . "
249
+ WHERE meta_key = 'fifu_image_url'
250
+ AND term_id = " . $term_id
251
+ );
252
+ }
253
+
254
+ function get_featured_and_gallery_ids($post_id) {
255
+ return $this->wpdb->get_results("
256
+ SELECT GROUP_CONCAT(meta_value SEPARATOR ',') as 'ids'
257
+ FROM " . $this->postmeta . "
258
+ WHERE post_id = " . $post_id . "
259
+ AND meta_key IN ('_thumbnail_id')"
260
+ );
261
+ }
262
+
263
+ function insert_default_thumbnail_id($value) {
264
+ $this->wpdb->get_results("
265
+ INSERT INTO " . $this->postmeta . " (post_id, meta_key, meta_value)
266
+ VALUES " . $value
267
+ );
268
+ }
269
+
270
  // clean meta data
271
 
272
  function delete_thumbnail_ids($ids) {
325
  function delete_attachments($ids) {
326
  $this->wpdb->get_results("
327
  DELETE FROM " . $this->posts . "
328
+ WHERE id IN (" . $ids . ")
329
+ AND post_type = 'attachment'
330
+ AND post_author = " . $this->author
331
+ );
332
+ }
333
+
334
+ function delete_attachment_meta_url_and_alt($ids) {
335
+ $this->wpdb->get_results("
336
+ DELETE FROM " . $this->postmeta . "
337
+ WHERE meta_key IN ('_wp_attached_file','_wp_attachment_image_alt')
338
+ AND post_id IN (" . $ids . ")"
339
  );
340
  }
341
 
402
  }
403
 
404
  function get_formatted_value($url, $alt, $post_parent) {
405
+ return "(" . $this->author . ", '" . $url . "', '" . str_replace("'", "", $alt) . "', 'image/jpeg', 'attachment', 'inherit', '" . $post_parent . "', now(), now(), now(), now(), '', '', '', '', '')";
406
  }
407
 
408
  /* insert fake internal featured image */
415
  foreach ($this->get_categories_without_meta() as $res) {
416
  $ids = ($i++ == 0) ? $res->term_id : ($ids . "," . $res->term_id);
417
  $url = get_term_meta($res->term_id, 'fifu_image_url', true);
418
+ if (!$url) {
419
+ $result = $this->get_category_image_url($res->term_id);
420
+ $url = $result[0]->meta_value;
421
+ }
422
  $value = $this->get_formatted_value($url, get_term_meta($res->term_id, 'fifu_image_alt', true), $res->term_id);
423
  $this->insert_attachment_by($value);
424
  $att_id = $this->wpdb->insert_id;
499
  $this->delete_empty_urls_category();
500
  }
501
 
502
+ /* save 1 post */
503
+
504
+ function update_fake_attach_id($post_id) {
505
+ $att_id = get_post_thumbnail_id($post_id);
506
+ $url = fifu_main_image_url($post_id);
507
+ $has_fifu_attachment = $att_id ? ($this->is_fifu_attachment($att_id) && get_option('fifu_default_attach_id') != $att_id) : false;
508
+ // delete
509
+ if (!$url) {
510
+ if ($has_fifu_attachment) {
511
+ wp_delete_attachment($att_id);
512
+ delete_post_thumbnail($post_id);
513
+ if (fifu_get_default_url())
514
+ set_post_thumbnail($post_id, get_option('fifu_default_attach_id'));
515
+ } else {
516
+ if (fifu_get_default_url())
517
+ set_post_thumbnail($post_id, get_option('fifu_default_attach_id'));
518
+ }
519
+ }
520
+ else {
521
+ // update
522
+ $alt = get_post_meta($post_id, 'fifu_image_alt', true);
523
+ if ($has_fifu_attachment) {
524
+ update_post_meta($att_id, '_wp_attached_file', ';' . $url);
525
+ update_post_meta($att_id, '_wp_attachment_image_alt', $alt);
526
+ $this->wpdb->update($this->posts, $set = array('post_title' => $alt, 'guid' => $url), $where = array('id' => $att_id), null, null);
527
+ }
528
+ // insert
529
+ else {
530
+ $value = $this->get_formatted_value($url, $alt, $post_id);
531
+ $this->insert_attachment_by($value);
532
+ $att_id = $this->wpdb->insert_id;
533
+ update_post_meta($post_id, '_thumbnail_id', $att_id);
534
+ update_post_meta($att_id, '_wp_attached_file', ';' . $url);
535
+ update_post_meta($att_id, '_wp_attachment_image_alt', $alt);
536
+ $attachments = $this->get_attachments_without_post($post_id);
537
+ if ($attachments) {
538
+ $this->delete_attachments($attachments);
539
+ $this->delete_attachment_meta_url_and_alt($attachments);
540
+ }
541
+ }
542
+ }
543
+ }
544
+
545
+ /* save 1 category */
546
+
547
+ function ctgr_update_fake_attach_id($term_id) {
548
+ if (fifu_is_on('fifu_data_generation'))
549
+ return;
550
+
551
+ $att_id = get_term_meta($term_id, 'thumbnail_id');
552
+ $att_id = $att_id ? $att_id[0] : null;
553
+ $has_fifu_attachment = $att_id ? $this->is_fifu_attachment($att_id) : false;
554
+
555
+ $url = get_term_meta($term_id, 'fifu_image_url', true);
556
+
557
+ // delete
558
+ if (!$url) {
559
+ if ($has_fifu_attachment) {
560
+ wp_delete_attachment($att_id);
561
+ update_term_meta($term_id, 'thumbnail_id', 0);
562
+ }
563
+ } else {
564
+ // update
565
+ $alt = get_term_meta($term_id, 'fifu_image_alt', true);
566
+ if ($has_fifu_attachment) {
567
+ update_post_meta($att_id, '_wp_attached_file', ';' . $url);
568
+ update_post_meta($att_id, '_wp_attachment_image_alt', $alt);
569
+ $this->wpdb->update($this->posts, $set = array('guid' => $url, 'post_title' => $alt), $where = array('id' => $att_id), null, null);
570
+ }
571
+ // insert
572
+ else {
573
+ $value = $this->get_formatted_value($url, $alt, $term_id);
574
+ $this->insert_attachment_by($value);
575
+ $att_id = $this->wpdb->insert_id;
576
+ update_term_meta($term_id, 'thumbnail_id', $att_id);
577
+ update_post_meta($att_id, '_wp_attached_file', ';' . $url);
578
+ update_post_meta($att_id, '_wp_attachment_image_alt', $alt);
579
+ $attachments = $this->get_ctgr_attachments_without_post($term_id);
580
+ if ($attachments) {
581
+ $this->delete_attachments($attachments);
582
+ $this->delete_attachment_meta_url_and_alt($attachments);
583
+ }
584
+ }
585
+ }
586
+ }
587
+
588
+ /* default url */
589
+
590
+ function create_attachment($url) {
591
+ $value = $this->get_formatted_value($url, null, null);
592
+ $this->insert_attachment_by($value);
593
+ return $this->wpdb->insert_id;
594
+ }
595
+
596
+ function set_default_url() {
597
+ $att_id = get_option('fifu_default_attach_id');
598
+ if (!$att_id)
599
+ return;
600
+ $value = null;
601
+ foreach ($this->get_posts_without_featured_image() as $res) {
602
+ $aux = "(" . $res->id . ", '_thumbnail_id', " . $att_id . ")";
603
+ $value = $value ? $value . ',' . $aux : $aux;
604
+ }
605
+ if ($value) {
606
+ $this->insert_default_thumbnail_id($value);
607
+ update_post_meta($att_id, '_wp_attached_file', ';' . get_option('fifu_default_url'));
608
+ }
609
+ }
610
+
611
+ function update_default_url($url) {
612
+ $att_id = get_option('fifu_default_attach_id');
613
+ if ($url != wp_get_attachment_url($att_id)) {
614
+ $this->wpdb->update($this->posts, $set = array('guid' => $url), $where = array('id' => $att_id), null, null);
615
+ update_post_meta($att_id, '_wp_attached_file', ';' . $url);
616
+ }
617
+ }
618
+
619
+ function delete_default_url() {
620
+ $att_id = get_option('fifu_default_attach_id');
621
+ wp_delete_attachment($att_id);
622
+ delete_option('fifu_default_attach_id');
623
+ $this->wpdb->delete($this->postmeta, array('meta_key' => '_thumbnail_id', 'meta_value' => $att_id));
624
+ }
625
+
626
+ /* delete post */
627
+
628
+ function before_delete_post($post_id) {
629
+ $default_url_enabled = fifu_is_on('fifu_enable_default_url');
630
+ $default_att_id = $default_url_enabled ? get_option('fifu_default_attach_id') : null;
631
+ $result = $this->get_featured_and_gallery_ids($post_id);
632
+ if ($result) {
633
+ $ids = explode(',', $result[0]->ids);
634
+ $value = null;
635
+ foreach ($ids as $id) {
636
+ if ($id && $id != $default_att_id)
637
+ $value = ($value == null) ? $id : $value . ',' . $id;
638
+ }
639
+ if ($value) {
640
+ $this->delete_attachments($value);
641
+ $this->delete_attachment_meta_url_and_alt($value);
642
+ }
643
+ }
644
+ }
645
+
646
  }
647
 
648
  /* fake internal featured image */
672
  $db->change_url_length();
673
  }
674
 
675
+ /* save post */
676
+
677
+ function fifu_db_update_fake_attach_id($post_id) {
678
+ $db = new FifuDb();
679
+ $db->update_fake_attach_id($post_id);
680
+ }
681
+
682
+ /* save category */
683
+
684
+ function fifu_db_ctgr_update_fake_attach_id($term_id) {
685
+ $db = new FifuDb();
686
+ $db->ctgr_update_fake_attach_id($term_id);
687
+ }
688
+
689
+ /* default url */
690
+
691
+ function fifu_db_create_attachment($url) {
692
+ $db = new FifuDb();
693
+ return $db->create_attachment($url);
694
+ }
695
+
696
+ function fifu_db_set_default_url() {
697
+ $db = new FifuDb();
698
+ return $db->set_default_url();
699
+ }
700
+
701
+ function fifu_db_update_default_url($url) {
702
+ $db = new FifuDb();
703
+ return $db->update_default_url($url);
704
+ }
705
+
706
+ function fifu_db_delete_default_url() {
707
+ $db = new FifuDb();
708
+ return $db->delete_default_url();
709
+ }
710
+
711
+ /* delete post */
712
+
713
+ function fifu_db_before_delete_post($post_id) {
714
+ $db = new FifuDb();
715
+ $db->before_delete_post($post_id);
716
+ }
717
+
admin/html/category.html CHANGED
@@ -1,7 +1,7 @@
1
  <style><?php include 'css/category.css' ?></style>
2
 
3
  <tr>
4
- <th>Featured Image from URL</th>
5
  <td>
6
  <div class="box">
7
  <?php include 'meta-box.html' ?>
1
  <style><?php include 'css/category.css' ?></style>
2
 
3
  <tr>
4
+ <th><b>Featured Image from URL</b></th>
5
  <td>
6
  <div class="box">
7
  <?php include 'meta-box.html' ?>
admin/html/css/menu.css CHANGED
@@ -3,10 +3,10 @@
3
  margin: 20px 20px 20px 15px;
4
 
5
  display: block;
6
- -webkit-animation: fadeInFromNone 0.25s step-end;
7
- -moz-animation: fadeInFromNone 0.25s step-end;
8
- -o-animation: fadeInFromNone 0.25s step-end;
9
- animation: fadeInFromNone 0.25s step-end;
10
  }
11
 
12
  .header-box {
@@ -44,12 +44,12 @@
44
  overflow: hidden;
45
  width: 72px;
46
  height: 24px;
47
- background: url("<?php echo $image_button ?>") 0 0 no-repeat;
48
  text-align: center !important;
49
  }
50
 
51
  .toggleoff {
52
- background: url("<?php echo $image_button ?>") -72px 0 no-repeat;
53
  }
54
 
55
  th, td {
3
  margin: 20px 20px 20px 15px;
4
 
5
  display: block;
6
+ -webkit-animation: fadeInFromNone 0.5s step-end;
7
+ -moz-animation: fadeInFromNone 0.5s step-end;
8
+ -o-animation: fadeInFromNone 0.5s step-end;
9
+ animation: fadeInFromNone 0.5s step-end;
10
  }
11
 
12
  .header-box {
44
  overflow: hidden;
45
  width: 72px;
46
  height: 24px;
47
+ background: url("../../images/onoff.jpg") 0 0 no-repeat;
48
  text-align: center !important;
49
  }
50
 
51
  .toggleoff {
52
+ background: url("../../images/onoff.jpg") -72px 0 no-repeat;
53
  }
54
 
55
  th, td {
admin/html/menu.html CHANGED
@@ -1,7 +1,3 @@
1
- <style><?php include 'css/menu.css' ?></style>
2
- <script><?php include 'js/menu.js' ?></script>
3
- <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.min.css">
4
- <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
5
  <div class="wrap">
6
 
7
  <div class="header-box" style="background-color:#23282d">
 
 
 
 
1
  <div class="wrap">
2
 
3
  <div class="header-box" style="background-color:#23282d">
admin/html/meta-box.html CHANGED
@@ -48,7 +48,7 @@
48
 
49
  <div style="<?php echo $show_news ?>">
50
  <p style="font-size: 12px; padding: 10px; border-left: 3px solid red; color: black;
51
- background-color: whitesmoke; border-radius: 100px 5px; position: relative; top: 10px">Please report any problem to <a href="mailto:marcel@featuredimagefromurl.com">marcel@featuredimagefromurl.com</a><br>It should be fixed in 24 hours.</p>
52
  <!--p style="font-size: 12px; padding: 5px; border-left: 6px solid red; color: black;
53
  background-color: #eee; border-radius: 25px 5px;">Please report any problem to <a href="mailto:marcel@featuredimagefromurl.com">marcel@featuredimagefromurl.com</a></p-->
54
  <!--p style="font-size: 12px; padding: 5px; border-left: 6px solid #b20ab8; color: black;
48
 
49
  <div style="<?php echo $show_news ?>">
50
  <p style="font-size: 12px; padding: 10px; border-left: 3px solid red; color: black;
51
+ background-color: whitesmoke; border-radius: 100px 5px; position: relative; top: 10px">Please report any problem to <b>marcel@featuredimagefromurl.com</b><br>It should be fixed in 24 hours.</p>
52
  <!--p style="font-size: 12px; padding: 5px; border-left: 6px solid red; color: black;
53
  background-color: #eee; border-radius: 25px 5px;">Please report any problem to <a href="mailto:marcel@featuredimagefromurl.com">marcel@featuredimagefromurl.com</a></p-->
54
  <!--p style="font-size: 12px; padding: 5px; border-left: 6px solid #b20ab8; color: black;
admin/menu.php CHANGED
@@ -3,16 +3,22 @@
3
  add_action('admin_menu', 'fifu_insert_menu');
4
 
5
  function fifu_insert_menu() {
6
- add_menu_page(
7
- 'Featured Image from URL', 'Featured Image from URL', 'administrator', 'featured-image-from-url', 'fifu_get_menu_html', plugins_url() . '/featured-image-from-url/admin/images/favicon.png', 57
8
- );
 
 
 
9
 
10
  add_action('admin_init', 'fifu_get_menu_settings');
11
  }
12
 
13
  function fifu_get_menu_html() {
14
  flush();
15
- $image_button = plugins_url() . '/featured-image-from-url/admin/images/onoff.jpg';
 
 
 
16
 
17
  $enable_social = get_option('fifu_social');
18
  $enable_original = get_option('fifu_original');
@@ -58,17 +64,15 @@ function fifu_get_menu_html() {
58
  } else
59
  update_option('fifu_fake_created', null, 'no');
60
 
61
- if ($default_url && fifu_is_on('fifu_enable_default_url') && fifu_is_on('fifu_fake2')) {
62
  if (!wp_get_attachment_url(get_option('fifu_default_attach_id'))) {
63
- $attach_id = fifu_create_attachment_url($default_url);
64
- update_option('fifu_default_attach_id', $attach_id);
65
- fifu_set_default_url($attach_id);
66
- } else {
67
- fifu_update_default_url($default_url);
68
- }
69
- } else {
70
- fifu_delete_default_url();
71
- }
72
 
73
  if (fifu_is_on('fifu_data_clean')) {
74
  fifu_enable_clean();
@@ -256,28 +260,6 @@ function fifu_disable_fake() {
256
  delete_option('fifu_fake_attach_id');
257
  }
258
 
259
- function fifu_create_attachment_url($url) {
260
- // create attachment
261
- $filename = ';' . $url;
262
- $parent_post_id = null;
263
- $filetype = wp_check_filetype('fifu.png', null);
264
- $attachment = array(
265
- 'guid' => $url,
266
- 'post_mime_type' => $filetype['type'],
267
- 'post_author' => 77777,
268
- 'post_title' => '',
269
- 'post_excerpt' => '',
270
- 'post_content' => 'Please don\'t remove me. I\'m just an empty symbolic file that keeps the attached field filled ' .
271
- '(some themes/plugins depend on having an attached file to work). Anyway you are free to use any internal image you want instead of me.',
272
- 'post_status' => 'inherit'
273
- );
274
- $attach_id = wp_insert_attachment($attachment, $filename, $parent_post_id);
275
- require_once( ABSPATH . 'wp-admin/includes/image.php' );
276
- $attach_data = wp_generate_attachment_metadata($attach_id, $filename);
277
- wp_update_attachment_metadata($attach_id, $attach_data);
278
- return $attach_id;
279
- }
280
-
281
  function fifu_enable_clean() {
282
  global $wpdb;
283
 
@@ -318,49 +300,6 @@ function fifu_enable_clean() {
318
  update_option('fifu_fake_created', false, 'no');
319
  }
320
 
321
- function fifu_set_default_url($attach_id) {
322
- global $wpdb;
323
- $table_postmeta = $wpdb->prefix . 'postmeta';
324
- $table_posts = $wpdb->prefix . 'posts';
325
- $query = "
326
- SELECT id
327
- FROM " . $table_posts . " p
328
- INNER JOIN " . $table_postmeta . " pm ON p.id = pm.post_id
329
- WHERE post_type IN ('post','page','product')
330
- AND post_status = 'publish'
331
- AND NOT EXISTS (
332
- SELECT 1 FROM " . $table_postmeta . " pm2
333
- WHERE pm2.post_id = pm.post_id
334
- AND pm2.meta_key IN ('_thumbnail_id', 'fifu_image_url', 'fifu_video_url', 'fifu_slider_image_url_0', 'fifu_shortcode')
335
- AND pm2.meta_value <> -1
336
- )";
337
- $result = $wpdb->get_results($query);
338
- foreach ($result as $i) {
339
- set_post_thumbnail($i->id, $attach_id);
340
- }
341
- }
342
-
343
- function fifu_update_default_url($url) {
344
- global $wpdb;
345
- $table_posts = $wpdb->prefix . 'posts';
346
- $attach_id = get_option('fifu_default_attach_id');
347
- if ($url != wp_get_attachment_url($attach_id)) {
348
- $wpdb->update($table_posts, $set = array('guid' => $url), $where = array('id' => $attach_id), null, null);
349
- update_post_meta($attach_id, '_wp_attached_file', ';' . $url);
350
- }
351
- }
352
-
353
- function fifu_delete_default_url() {
354
- $attach_id = get_option('fifu_default_attach_id');
355
- wp_delete_attachment($attach_id);
356
- delete_option('fifu_default_attach_id');
357
-
358
- global $wpdb;
359
- $table_postmeta = $wpdb->prefix . 'postmeta';
360
- $where = array('meta_key' => '_thumbnail_id', 'meta_value' => $attach_id);
361
- $wpdb->delete($table_postmeta, $where);
362
- }
363
-
364
  function fifu_version() {
365
  $plugin_data = get_plugin_data(FIFU_PLUGIN_DIR . 'featured-image-from-url.php');
366
  return $plugin_data ? $plugin_data['Name'] . ':' . $plugin_data['Version'] : '';
3
  add_action('admin_menu', 'fifu_insert_menu');
4
 
5
  function fifu_insert_menu() {
6
+ // jquery
7
+ wp_enqueue_style('jquery-css', '//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.min.css');
8
+ wp_enqueue_script('jquery-ui', 'https://code.jquery.com/ui/1.11.4/jquery-ui.min.js');
9
+ wp_enqueue_script('jquery', 'https://code.jquery.com/jquery-1.11.3.min.js');
10
+
11
+ add_menu_page('Featured Image from URL', 'Featured Image from URL', 'administrator', 'featured-image-from-url', 'fifu_get_menu_html', plugins_url() . '/featured-image-from-url/admin/images/favicon.png', 57);
12
 
13
  add_action('admin_init', 'fifu_get_menu_settings');
14
  }
15
 
16
  function fifu_get_menu_html() {
17
  flush();
18
+
19
+ // css and js
20
+ wp_enqueue_style('fifu-menu-css', plugins_url('/html/css/menu.css', __FILE__));
21
+ wp_enqueue_script('fifu-menu-js', plugins_url('/html/js/menu.js', __FILE__));
22
 
23
  $enable_social = get_option('fifu_social');
24
  $enable_original = get_option('fifu_original');
64
  } else
65
  update_option('fifu_fake_created', null, 'no');
66
 
67
+ if (!empty($default_url) && fifu_is_on('fifu_enable_default_url') && fifu_is_on('fifu_fake2')) {
68
  if (!wp_get_attachment_url(get_option('fifu_default_attach_id'))) {
69
+ $att_id = fifu_db_create_attachment($default_url);
70
+ update_option('fifu_default_attach_id', $att_id);
71
+ fifu_db_set_default_url();
72
+ } else
73
+ fifu_db_update_default_url($default_url);
74
+ } else
75
+ fifu_db_delete_default_url();
 
 
76
 
77
  if (fifu_is_on('fifu_data_clean')) {
78
  fifu_enable_clean();
260
  delete_option('fifu_fake_attach_id');
261
  }
262
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
263
  function fifu_enable_clean() {
264
  global $wpdb;
265
 
300
  update_option('fifu_fake_created', false, 'no');
301
  }
302
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
303
  function fifu_version() {
304
  $plugin_data = get_plugin_data(FIFU_PLUGIN_DIR . 'featured-image-from-url.php');
305
  return $plugin_data ? $plugin_data['Name'] . ':' . $plugin_data['Version'] : '';
admin/meta-box.php CHANGED
@@ -66,6 +66,9 @@ function fifu_remove_first_image($data, $postarr) {
66
  add_action('save_post', 'fifu_save_properties');
67
 
68
  function fifu_save_properties($post_id) {
 
 
 
69
  /* image url */
70
  if (isset($_POST['fifu_input_url'])) {
71
  $url = esc_url($_POST['fifu_input_url']);
@@ -99,26 +102,5 @@ function fifu_update_or_delete_alt($post_id, $field, $value) {
99
  delete_post_meta($post_id, $field, $value);
100
  }
101
 
102
- add_action('before_delete_post', 'fifu_remove_product_gallery');
103
-
104
- function fifu_remove_product_gallery($post_id) {
105
- global $wpdb;
106
- $table_postmeta = $wpdb->prefix . 'postmeta';
107
- $table_posts = $wpdb->prefix . 'posts';
108
- $query = "
109
- SELECT GROUP_CONCAT(meta_value SEPARATOR ',') as 'ids'
110
- FROM " . $table_postmeta . " pm
111
- WHERE pm.post_id = " . $post_id . "
112
- AND pm.meta_key IN ('_thumbnail_id', '_product_image_gallery')";
113
- $result = $wpdb->get_results($query);
114
- if ($result) {
115
- $ids = explode(',', $result[0]->ids);
116
- foreach ($ids as $id) {
117
- if ($id) {
118
- $where = array('id' => $id, 'post_author' => 77777, 'post_type' => 'attachment');
119
- $wpdb->delete($table_posts, $where);
120
- }
121
- }
122
- }
123
- }
124
 
66
  add_action('save_post', 'fifu_save_properties');
67
 
68
  function fifu_save_properties($post_id) {
69
+ if (!$_POST)
70
+ return;
71
+
72
  /* image url */
73
  if (isset($_POST['fifu_input_url'])) {
74
  $url = esc_url($_POST['fifu_input_url']);
102
  delete_post_meta($post_id, $field, $value);
103
  }
104
 
105
+ add_action('before_delete_post', 'fifu_db_before_delete_post');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
 
featured-image-from-url.php CHANGED
@@ -4,7 +4,7 @@
4
  * Plugin Name: Featured Image from URL
5
  * Plugin URI: https://featuredimagefromurl.com/
6
  * Description: Use an external image as Featured Image of your post/page/custom post type (WooCommerce). Includes Auto Set (External Post), Product Gallery, Social Tags and more.
7
- * Version: 2.2.5
8
  * Author: Marcel Jacques Machado
9
  * Author URI: https://www.linkedin.com/in/marceljm/
10
  */
4
  * Plugin Name: Featured Image from URL
5
  * Plugin URI: https://featuredimagefromurl.com/
6
  * Description: Use an external image as Featured Image of your post/page/custom post type (WooCommerce). Includes Auto Set (External Post), Product Gallery, Social Tags and more.
7
+ * Version: 2.2.6
8
  * Author: Marcel Jacques Machado
9
  * Author URI: https://www.linkedin.com/in/marceljm/
10
  */
includes/external-post.php CHANGED
@@ -93,121 +93,6 @@ function fifu_update_fake_attach_id($post_id) {
93
  set_post_thumbnail($post_id, get_option('fifu_fake_attach_id'));
94
  return;
95
  }
96
-
97
- global $wpdb;
98
- $posts_table = $wpdb->prefix . 'posts';
99
- $postmeta_table = $wpdb->prefix . 'postmeta';
100
- $att_id = get_post_thumbnail_id($post_id);
101
- $url = fifu_main_image_url($post_id);
102
- $AUTHOR = 77777;
103
- if ($att_id && $att_id != null)
104
- $has_fifu_attachment = $wpdb->get_row('SELECT 1 FROM ' . $posts_table . ' WHERE ID = ' . $att_id . ' AND POST_AUTHOR = ' . $AUTHOR) != null;
105
- else
106
- $has_fifu_attachment = false;
107
-
108
- // delete
109
- if (!fifu_main_image_url($post_id)) {
110
- if ($has_fifu_attachment) {
111
- if (fifu_get_default_url()) {
112
- set_post_thumbnail($post_id, get_option('fifu_default_attach_id'));
113
- } else {
114
- wp_delete_attachment($att_id);
115
- delete_post_thumbnail($post_id);
116
- }
117
- } else {
118
- if (fifu_get_default_url())
119
- set_post_thumbnail($post_id, get_option('fifu_default_attach_id'));
120
- }
121
- }
122
- // update
123
- else {
124
- $alt = get_post_meta($post_id, 'fifu_image_alt', true);
125
- if ($has_fifu_attachment) {
126
- update_post_meta($att_id, '_wp_attached_file', ';' . $url);
127
- update_post_meta($att_id, '_wp_attachment_image_alt', $alt);
128
- $wpdb->update($posts_table, $set = array('post_title' => $alt), $where = array('id' => $att_id), null, null);
129
- $wpdb->update($posts_table, $set = array('guid' => $url), $where = array('id' => $att_id), null, null);
130
- }
131
- // insert
132
- else {
133
- $filename = ';' . fifu_main_image_url($post_id);
134
- $parent_post_id = $post_id;
135
- $filetype = wp_check_filetype('fifu.png', null);
136
- $attachment = array(
137
- 'guid' => fifu_main_image_url($post_id),
138
- 'post_mime_type' => $filetype['type'],
139
- 'post_author' => $AUTHOR,
140
- 'post_title' => $alt,
141
- 'post_excerpt' => '',
142
- 'post_content' => 'Please don\'t remove that. It\'s just an empty symbolic file',
143
- 'post_status' => 'inherit'
144
- );
145
- $attach_id = wp_insert_attachment($attachment, $filename, $parent_post_id);
146
- require_once(ABSPATH . 'wp-admin/includes/image.php');
147
- $attach_data = wp_generate_attachment_metadata($attach_id, $filename);
148
- wp_update_attachment_metadata($attach_id, $attach_data);
149
- set_post_thumbnail($post_id, $attach_id);
150
- update_post_meta($attach_id, '_wp_attachment_image_alt', $alt);
151
- }
152
- }
153
- }
154
-
155
- function fifu_ctgr_update_fake_attach_id($term_id) {
156
- if (fifu_is_on('fifu_data_generation'))
157
- return;
158
-
159
- global $wpdb;
160
- $posts_table = $wpdb->prefix . 'posts';
161
- $postmeta_table = $wpdb->prefix . 'postmeta';
162
- $termmeta_table = $wpdb->prefix . 'termmeta';
163
- $aux = $wpdb->get_row('SELECT meta_value FROM ' . $termmeta_table . ' WHERE term_id = ' . $term_id . ' AND meta_key = "thumbnail_id"');
164
- $att_id = $aux ? $aux->meta_value : null;
165
-
166
- $url = get_term_meta($term_id, 'fifu_image_url', true);
167
-
168
- $AUTHOR = 77777;
169
- if ($att_id && $att_id != null && $att_id != 0)
170
- $has_fifu_attachment = $wpdb->get_row('SELECT 1 FROM ' . $posts_table . ' WHERE ID = ' . $att_id . ' AND POST_AUTHOR = ' . $AUTHOR) != null;
171
- else
172
- $has_fifu_attachment = false;
173
-
174
- // delete
175
- if (!$url) {
176
- if ($has_fifu_attachment) {
177
- wp_delete_attachment($att_id);
178
- update_term_meta($term_id, 'thumbnail_id', 0);
179
- }
180
- }
181
- // update
182
- else {
183
- $alt = get_term_meta($term_id, 'fifu_image_alt', true);
184
- if ($has_fifu_attachment) {
185
- update_post_meta($att_id, '_wp_attached_file', ';' . $url);
186
- update_post_meta($att_id, '_wp_attachment_image_alt', $alt);
187
- $wpdb->update($posts_table, $set = array('post_title' => $alt), $where = array('id' => $att_id), null, null);
188
- $wpdb->update($posts_table, $set = array('guid' => $url), $where = array('id' => $att_id), null, null);
189
- }
190
- // insert
191
- else {
192
- $filename = ';' . $url;
193
- $parent_post_id = 0;
194
- $filetype = wp_check_filetype('fifu.png', null);
195
- $attachment = array(
196
- 'guid' => $url,
197
- 'post_mime_type' => $filetype['type'],
198
- 'post_author' => $AUTHOR,
199
- 'post_title' => $alt,
200
- 'post_excerpt' => '',
201
- 'post_content' => 'Please don\'t remove that. It\'s just an empty symbolic file',
202
- 'post_status' => 'inherit'
203
- );
204
- $attach_id = wp_insert_attachment($attachment, $filename, $parent_post_id);
205
- require_once(ABSPATH . 'wp-admin/includes/image.php');
206
- $attach_data = wp_generate_attachment_metadata($attach_id, $filename);
207
- wp_update_attachment_metadata($attach_id, $attach_data);
208
- update_term_meta($term_id, 'thumbnail_id', $attach_id);
209
- update_post_meta($attach_id, '_wp_attachment_image_alt', $alt);
210
- }
211
- }
212
  }
213
 
93
  set_post_thumbnail($post_id, get_option('fifu_fake_attach_id'));
94
  return;
95
  }
96
+ fifu_db_update_fake_attach_id($post_id);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  }
98
 
readme.txt CHANGED
@@ -157,6 +157,9 @@ Features:
157
 
158
  == Changelog ==
159
 
 
 
 
160
  = 2.2.5 =
161
  * New feature: settings > featured image > original size.
162
 
@@ -502,6 +505,9 @@ was removed. To finish, a Premium version is now been presented.
502
 
503
  == Upgrade Notice ==
504
 
 
 
 
505
  = 2.2.5 =
506
  * New feature: settings > featured image > original size.
507
 
157
 
158
  == Changelog ==
159
 
160
+ = 2.2.6 =
161
+ * Bug fixes: menu settings, product category and default external image.
162
+
163
  = 2.2.5 =
164
  * New feature: settings > featured image > original size.
165
 
505
 
506
  == Upgrade Notice ==
507
 
508
+ = 2.2.6 =
509
+ * Bug fixes: menu settings, product category and default external image.
510
+
511
  = 2.2.5 =
512
  * New feature: settings > featured image > original size.
513