Featured Image From URL - Version 1.9.4

Version Description

  • New: Bug fix.
Download this release

Release Info

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

Code changes from version 1.9.3 to 1.9.4

admin/html/js/menu.js CHANGED
@@ -42,3 +42,11 @@ function save(formName, url) {
42
  }
43
  });
44
  }
 
 
 
 
 
 
 
 
42
  }
43
  });
44
  }
45
+
46
+ jQuery(function () {
47
+ jQuery('#fifu_toggle_fake').click(function () {
48
+ setTimeout(function () {
49
+ window.location.replace('admin.php?page=featured-image-from-url&fake=on');
50
+ }, 250);
51
+ });
52
+ });
admin/html/menu.html CHANGED
@@ -8,6 +8,14 @@
8
  <h1>Featured Image From URL</h1>
9
  </div>
10
 
 
 
 
 
 
 
 
 
11
  <div class="box">
12
  <h2>Help</h2>
13
  <div class="greybox">
@@ -183,10 +191,12 @@
183
  </form>
184
  </div>
185
 
 
 
186
  <div class="box">
187
  <h2>Fake Internal Featured Image</h2>
188
  <div class="greybox" id="grad1">
189
- Even though FIFU works fine with the most of themes/plugins, some of them are not able to show the external featured image if there is no internal featured image associated to your post/page/product. Enabling the toggle below, a symbolic empty file will be added to your Media Library as an attempt to overcome that limitation.
190
  </div>
191
  <p/>
192
  <form
8
  <h1>Featured Image From URL</h1>
9
  </div>
10
 
11
+ <div class="box">
12
+ <h2 style="color:red">IMPORTANT!!!</h2>
13
+ <div class="greybox">
14
+ FIFU 2 is coming soon with some great improvements to solve problems as image size and image missing in some site areas.
15
+ <b>If your external images are not being shown after the last update</b>, try to disable/enable the toggle <a href="admin.php?page=featured-image-from-url#fake">Fake Internal Featured Image</a>. And if that doesn't solve your issue, please report that to the support email bellow. You can also go back to the <a href="https://downloads.wordpress.org/plugin/featured-image-from-url.1.9.1.zip">last stable version</a>.
16
+ </div>
17
+ </div>
18
+
19
  <div class="box">
20
  <h2>Help</h2>
21
  <div class="greybox">
191
  </form>
192
  </div>
193
 
194
+ <a name="fake"></a>
195
+
196
  <div class="box">
197
  <h2>Fake Internal Featured Image</h2>
198
  <div class="greybox" id="grad1">
199
+ Even though FIFU works fine with the most of themes/plugins, some of them are not able to show the external featured image if there is no internal featured image associated to your post/page/product. Enabling the toggle below, a symbolic empty file will be added to your Media Library as an attempt to overcome that limitation. <b>After enabled/disabled, it can take some minutes to finish.</b>.
200
  </div>
201
  <p/>
202
  <form
admin/menu.php CHANGED
@@ -37,10 +37,12 @@ function fifu_get_menu_html() {
37
 
38
  fifu_update_menu_options();
39
 
40
- if (get_option('fifu_fake') == 'toggleon')
41
- fifu_enable_fake();
42
- else
43
- fifu_disable_fake();
 
 
44
  }
45
 
46
  function fifu_get_menu_settings() {
@@ -114,32 +116,7 @@ function fifu_update_option($input, $type) {
114
  function fifu_enable_fake() {
115
  global $wpdb;
116
 
117
- // create attachment
118
- $filename = 'Featured Image from URL';
119
- $parent_post_id = null;
120
- $filetype = wp_check_filetype('fifu.png', null);
121
- $attachment = array(
122
- 'guid' => basename($filename),
123
- 'post_mime_type' => $filetype['type'],
124
- 'post_title' => '',
125
- 'post_excerpt' => '',
126
- 'post_content' => 'Please don\'t remove that. It\'s just an empty symbolic file that keeps the field filled ' .
127
- '(some themes/plugins depend on having an attached file to work). But you are free to use any image you want instead of this file.',
128
- 'post_status' => 'inherit'
129
- );
130
- $attach_id = wp_insert_attachment($attachment, $filename, $parent_post_id);
131
- require_once( ABSPATH . 'wp-admin/includes/image.php' );
132
- $attach_data = wp_generate_attachment_metadata($attach_id, $filename);
133
- wp_update_attachment_metadata($attach_id, $attach_data);
134
- update_option('fifu_fake_attach_id', $attach_id);
135
-
136
  $table = $wpdb->prefix . 'postmeta';
137
-
138
- // delete _thumbnail_id (new)
139
- $query = "DELETE FROM " . $table . " WHERE meta_key = '_thumbnail_id' AND meta_value LIKE 'fifu:%'";
140
- $wpdb->query($query);
141
-
142
- // insert _thumbnail_id
143
  $query = "
144
  SELECT DISTINCT post_id
145
  FROM " . $table . " a
@@ -158,46 +135,39 @@ function fifu_enable_fake() {
158
  )";
159
  $result = $wpdb->get_results($query);
160
  foreach ($result as $i) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
  $data = array('post_id' => $i->post_id, 'meta_key' => '_thumbnail_id', 'meta_value' => $attach_id);
162
  $wpdb->insert($table, $data);
163
  }
164
-
165
- // update _thumbnail_id (fake)
166
- $table_posts = $wpdb->prefix . 'posts';
167
- $query = "SELECT id FROM " . $table_posts . " WHERE post_type = 'attachment' AND guid = 'http://Featured%20Image%20from%20URL'";
168
- $result = $wpdb->get_results($query);
169
- foreach ($result as $i) {
170
- $data = array('meta_value' => $attach_id);
171
- $where = array('meta_key' => '_thumbnail_id', 'meta_value' => $i->id);
172
- $wpdb->update($table, $data, $where, null, null);
173
- }
174
-
175
- // update _thumbnail_id (-1)
176
- $query = "
177
- SELECT post_id
178
- FROM " . $table . " a
179
- WHERE a.meta_key IN ('fifu_image_url', 'fifu_video_url', 'fifu_slider_image_url_0', 'fifu_shortcode')
180
- AND a.meta_value IS NOT NULL
181
- AND a.meta_value <> ''";
182
- $result = $wpdb->get_results($query);
183
- foreach ($result as $i) {
184
- $data = array('meta_value' => $attach_id);
185
- $where = array('post_id' => $i->post_id, 'meta_key' => '_thumbnail_id', 'meta_value' => -1);
186
- $wpdb->update($table, $data, $where, null, null);
187
- }
188
  }
189
 
190
  function fifu_disable_fake() {
191
  global $wpdb;
192
- $table = $wpdb->prefix . 'postmeta';
193
 
194
- // delete (-1)
195
- $where = array('meta_key' => '_thumbnail_id', 'meta_value' => -1);
196
- $wpdb->delete($table, $where);
197
-
198
- // delete (fake)
199
  $table_posts = $wpdb->prefix . 'posts';
200
- $query = "SELECT id FROM " . $table_posts . " WHERE post_type = 'attachment' AND guid = 'http://Featured%20Image%20from%20URL'";
 
 
201
  $result = $wpdb->get_results($query);
202
  foreach ($result as $i) {
203
  $where = array('meta_key' => '_thumbnail_id', 'meta_value' => $i->id);
@@ -205,29 +175,43 @@ function fifu_disable_fake() {
205
  wp_delete_attachment($i->id);
206
  }
207
 
208
- // insert _thumbnail_id (new)
209
- $query = "
210
- SELECT DISTINCT post_id
211
- FROM " . $table . " a
212
- WHERE a.post_id in (
213
- SELECT post_id
214
- FROM " . $table . " b
215
- WHERE b.meta_key IN ('fifu_image_url', 'fifu_video_url', 'fifu_slider_image_url_0', 'fifu_shortcode')
216
- AND b.meta_value IS NOT NULL
217
- AND b.meta_value <> ''
218
- )
219
- AND NOT EXISTS (
220
- SELECT 1
221
- FROM " . $table . " c
222
- WHERE a.post_id = c.post_id
223
- AND c.meta_key = '_thumbnail_id'
224
- )";
225
  $result = $wpdb->get_results($query);
226
  foreach ($result as $i) {
227
- $data = array('post_id' => $i->post_id, 'meta_key' => '_thumbnail_id', 'meta_value' => 'fifu:' . $i->post_id);
228
- $wpdb->insert($table, $data);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229
  }
230
 
231
  delete_option('fifu_fake_attach_id');
 
 
 
 
 
 
 
 
 
232
  }
233
 
37
 
38
  fifu_update_menu_options();
39
 
40
+ if (isset($_GET['fake']) && isset($_GET['fake']) == 'on') {
41
+ if (get_option('fifu_fake') == 'toggleon')
42
+ fifu_enable_fake();
43
+ else if (get_option('fifu_fake') == 'toggleoff')
44
+ fifu_disable_fake();
45
+ }
46
  }
47
 
48
  function fifu_get_menu_settings() {
116
  function fifu_enable_fake() {
117
  global $wpdb;
118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  $table = $wpdb->prefix . 'postmeta';
 
 
 
 
 
 
120
  $query = "
121
  SELECT DISTINCT post_id
122
  FROM " . $table . " a
135
  )";
136
  $result = $wpdb->get_results($query);
137
  foreach ($result as $i) {
138
+ // create attachment
139
+ $filename = 'Featured Image from URL';
140
+ $parent_post_id = null;
141
+ $filetype = wp_check_filetype('fifu.png', null);
142
+ $attachment = array(
143
+ 'guid' => fifu_main_image_url($i->post_id),
144
+ 'post_mime_type' => $filetype['type'],
145
+ 'post_author' => 0,
146
+ 'post_title' => '',
147
+ 'post_excerpt' => '',
148
+ 'post_content' => 'Please don\'t remove that. It\'s just an empty symbolic file that keeps the field filled ' .
149
+ '(some themes/plugins depend on having an attached file to work). But you are free to use any image you want instead of this file.',
150
+ 'post_status' => 'inherit'
151
+ );
152
+ $attach_id = wp_insert_attachment($attachment, $filename, $parent_post_id);
153
+ require_once( ABSPATH . 'wp-admin/includes/image.php' );
154
+ $attach_data = wp_generate_attachment_metadata($attach_id, $filename);
155
+ wp_update_attachment_metadata($attach_id, $attach_data);
156
+
157
+ // create _thumbnail_id
158
  $data = array('post_id' => $i->post_id, 'meta_key' => '_thumbnail_id', 'meta_value' => $attach_id);
159
  $wpdb->insert($table, $data);
160
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
  }
162
 
163
  function fifu_disable_fake() {
164
  global $wpdb;
 
165
 
166
+ $table = $wpdb->prefix . 'postmeta';
 
 
 
 
167
  $table_posts = $wpdb->prefix . 'posts';
168
+
169
+ // delete attachment created by fifu
170
+ $query = "SELECT id FROM " . $table_posts . " WHERE post_type = 'attachment' AND post_content LIKE 'Please don%'";
171
  $result = $wpdb->get_results($query);
172
  foreach ($result as $i) {
173
  $where = array('meta_key' => '_thumbnail_id', 'meta_value' => $i->id);
175
  wp_delete_attachment($i->id);
176
  }
177
 
178
+ // delete meta values
179
+ $query = "SELECT post_id FROM " . $table . " WHERE meta_key = 'fifu_image_url'";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
  $result = $wpdb->get_results($query);
181
  foreach ($result as $i) {
182
+ $where = array('meta_key' => '_thumbnail_id', 'post_id' => $i->post_id, 'meta_value' => -1);
183
+ $wpdb->delete($table, $where);
184
+
185
+ $where = array('meta_key' => '_thumbnail_id', 'post_id' => $i->post_id, 'meta_value' => null);
186
+ $wpdb->delete($table, $where);
187
+
188
+ $where = array('meta_key' => '_thumbnail_id', 'post_id' => $i->post_id, 'meta_value' => 'fifu:' . $i->post_id);
189
+ $wpdb->delete($table, $where);
190
+
191
+ $where = array('meta_key' => '_thumbnail_id', 'post_id' => $i->post_id, 'meta_value' => get_option('fifu_fake_attach_id'));
192
+ $wpdb->delete($table, $where);
193
+
194
+ $where = array('meta_key' => '_wp_attached_file', 'post_id' => $i->post_id, 'meta_value' => 'Featured Image from URL');
195
+ $wpdb->delete($table, $where);
196
+
197
+ // delete _thumbnail_id withouth attachment
198
+ $query = "SELECT 1 FROM " . $table . " pm WHERE pm.meta_key = '_thumbnail_id' AND pm.post_id = " . $i->post_id . " AND NOT EXISTS (SELECT 1 FROM " . $table_posts . " p WHERE p.id = pm.meta_value)";
199
+ $result = $wpdb->get_results($query);
200
+ foreach ($result as $x) {
201
+ $where = array('meta_key' => '_thumbnail_id', 'post_id' => $i->post_id);
202
+ $wpdb->delete($table, $where);
203
+ }
204
  }
205
 
206
  delete_option('fifu_fake_attach_id');
207
+
208
+ // delete attachments
209
+ $query = "SELECT post_id FROM " . $table . " WHERE meta_key = '_wp_attached_file' AND meta_value = 'Featured Image from URL'";
210
+ $result = $wpdb->get_results($query);
211
+ foreach ($result as $i) {
212
+ $where = array('meta_key' => '_thumbnail_id', 'meta_value' => $i->post_id);
213
+ $wpdb->delete($table, $where);
214
+ wp_delete_attachment($i->post_id);
215
+ }
216
  }
217
 
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: 1.9.3
8
  * Author: Marcel Jacques Machado
9
  * Author URI: https://www.linkedin.com/in/marceljm/
10
  */
@@ -31,38 +31,8 @@ if (is_admin()) {
31
  register_deactivation_hook(__FILE__, 'fifu_deactivate');
32
 
33
  function fifu_deactivate($network_wide) {
34
- if (is_multisite() && $network_wide) {
35
- global $wpdb;
36
- foreach ($wpdb->get_col("SELECT blog_id FROM $wpdb->blogs") as $blog_id) {
37
- switch_to_blog($blog_id);
38
- fifu_delete_ids();
39
- }
40
- } else
41
- fifu_delete_ids();
42
- }
43
-
44
- function fifu_delete_ids() {
45
- global $wpdb;
46
- $table = $wpdb->prefix . 'postmeta';
47
-
48
- // new
49
- $query = "DELETE FROM " . $table . " WHERE meta_key = '_thumbnail_id' AND meta_value LIKE 'fifu:%'";
50
- $wpdb->query($query);
51
-
52
- // -1
53
- $where = array('meta_key' => '_thumbnail_id', 'meta_value' => -1);
54
- $wpdb->delete($table, $where);
55
-
56
- // fake
57
- $att_id = get_option('fifu_fake_attach_id');
58
- if ($att_id) {
59
- $where = array('meta_key' => '_thumbnail_id', 'meta_value' => $att_id);
60
- $wpdb->delete($table, $where);
61
- wp_delete_attachment($att_id);
62
- delete_option('fifu_fake_attach_id');
63
- }
64
-
65
- delete_option('fifu_migration');
66
  }
67
 
68
  /* activate */
@@ -70,95 +40,21 @@ function fifu_delete_ids() {
70
  register_activation_hook(__FILE__, 'fifu_activate');
71
 
72
  function fifu_activate($network_wide) {
 
 
 
 
 
 
 
 
73
  if (is_multisite() && $network_wide) {
74
  global $wpdb;
75
  foreach ($wpdb->get_col("SELECT blog_id FROM $wpdb->blogs") as $blog_id) {
76
  switch_to_blog($blog_id);
77
- fifu_run_migration();
78
  }
79
  } else
80
- fifu_run_migration();
81
- }
82
-
83
- /* the code below is to migrate _thumbnail_id to a new format "fifu:post_id" */
84
-
85
- if (!get_option('fifu_migration')) {
86
- fifu_run_migration();
87
- }
88
-
89
- function fifu_run_migration() {
90
- global $wpdb;
91
-
92
- update_option('fifu_fake', 'toggleoff');
93
-
94
- // insert _thumbnail_id (new)
95
- $table = $wpdb->prefix . 'postmeta';
96
- $query = "
97
- SELECT DISTINCT post_id
98
- FROM " . $table . " a
99
- WHERE a.post_id in (
100
- SELECT post_id
101
- FROM " . $table . " b
102
- WHERE b.meta_key IN ('fifu_image_url', 'fifu_video_url', 'fifu_slider_image_url_0', 'fifu_shortcode')
103
- AND b.meta_value IS NOT NULL
104
- AND b.meta_value <> ''
105
- )
106
- AND NOT EXISTS (
107
- SELECT 1
108
- FROM " . $table . " c
109
- WHERE a.post_id = c.post_id
110
- AND c.meta_key = '_thumbnail_id'
111
- )";
112
- $result = $wpdb->get_results($query);
113
- foreach ($result as $i) {
114
- $data = array('post_id' => $i->post_id, 'meta_key' => '_thumbnail_id', 'meta_value' => 'fifu:' . $i->post_id);
115
- $wpdb->insert($table, $data);
116
- }
117
-
118
- // update _thumbnail_id (fake)
119
- $att_id = get_option('fifu_fake_attach_id');
120
- if ($att_id) {
121
- $table = $wpdb->prefix . 'postmeta';
122
- $query = "
123
- SELECT DISTINCT post_id
124
- FROM " . $table . " a
125
- WHERE a.post_id in (
126
- SELECT post_id
127
- FROM " . $table . " b
128
- WHERE b.meta_key IN ('fifu_image_url', 'fifu_video_url', 'fifu_slider_image_url_0', 'fifu_shortcode')
129
- AND b.meta_value IS NOT NULL
130
- AND b.meta_value <> ''
131
- )
132
- AND EXISTS (
133
- SELECT 1
134
- FROM " . $table . " c
135
- WHERE a.post_id = c.post_id
136
- AND c.meta_key = '_thumbnail_id'
137
- AND c.meta_value = " . $att_id . "
138
- )";
139
- $result = $wpdb->get_results($query);
140
- foreach ($result as $i) {
141
- $data = array('meta_value' => 'fifu:' . $i->post_id);
142
- $where = array('post_id' => $i->post_id, 'meta_key' => '_thumbnail_id', 'meta_value' => $att_id);
143
- $wpdb->update($table, $data, $where, null, null);
144
- }
145
- }
146
-
147
- // update _thumbnail_id (-1)
148
- $query = "
149
- SELECT post_id
150
- FROM " . $table . " a
151
- WHERE a.meta_key IN ('fifu_image_url', 'fifu_video_url', 'fifu_slider_image_url_0', 'fifu_shortcode')
152
- AND a.meta_value IS NOT NULL
153
- AND a.meta_value <> ''";
154
- $result = $wpdb->get_results($query);
155
- foreach ($result as $i) {
156
- $data = array('meta_value' => 'fifu:' . $i->post_id);
157
- $where = array('post_id' => $i->post_id, 'meta_key' => '_thumbnail_id', 'meta_value' => -1);
158
- $wpdb->update($table, $data, $where, null, null);
159
- }
160
-
161
- // migration control
162
- update_option('fifu_migration', true);
163
  }
164
 
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: 1.9.4
8
  * Author: Marcel Jacques Machado
9
  * Author URI: https://www.linkedin.com/in/marceljm/
10
  */
31
  register_deactivation_hook(__FILE__, 'fifu_deactivate');
32
 
33
  function fifu_deactivate($network_wide) {
34
+ error_log('FIFU deactivated');
35
+ fifu_init(false, $network_wide);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  }
37
 
38
  /* activate */
40
  register_activation_hook(__FILE__, 'fifu_activate');
41
 
42
  function fifu_activate($network_wide) {
43
+ error_log('FIFU activated');
44
+ fifu_init(true, $network_wide);
45
+ }
46
+
47
+ /* init function */
48
+
49
+ function fifu_init($enabled, $network_wide) {
50
+ require_once( FIFU_ADMIN_DIR . '/menu.php' );
51
  if (is_multisite() && $network_wide) {
52
  global $wpdb;
53
  foreach ($wpdb->get_col("SELECT blog_id FROM $wpdb->blogs") as $blog_id) {
54
  switch_to_blog($blog_id);
55
+ $enabled ? fifu_enable_fake() : fifu_disable_fake();
56
  }
57
  } else
58
+ $enabled ? fifu_enable_fake() : fifu_disable_fake();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  }
60
 
includes/external-post.php CHANGED
@@ -88,23 +88,68 @@ function fifu_first_url_in_content($post_id) {
88
  }
89
 
90
  function fifu_update_fake_attach_id($post_id) {
91
- // already has attachment
92
- if (get_post_meta($post_id, '_thumbnail_id', true))
93
- return;
 
 
 
 
 
 
 
 
 
94
 
95
- // set fake attachment
96
- if (get_option('fifu_fake_attach_id')) {
97
- update_post_meta($post_id, '_thumbnail_id', get_option('fifu_fake_attach_id'));
98
- return;
 
 
99
  }
100
 
101
- // set new format
102
- if (fifu_main_image_url($post_id) || get_option('fifu_default_url')) {
103
- update_post_meta($post_id, '_thumbnail_id', 'fifu:' . $post_id);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  return;
105
- }
106
 
107
- // delete
108
- delete_post_meta($post_id, '_thumbnail_id');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  }
110
 
88
  }
89
 
90
  function fifu_update_fake_attach_id($post_id) {
91
+ global $wpdb;
92
+ $table = $wpdb->prefix . 'postmeta';
93
+ $table_posts = $wpdb->prefix . 'posts';
94
+
95
+ // delete attachment created by fifu
96
+ $query = "SELECT 1 FROM " . $table_posts . " WHERE post_type = 'attachment' AND post_content LIKE 'Please don%' AND id = " . $post_id;
97
+ $result = $wpdb->get_results($query);
98
+ foreach ($result as $i) {
99
+ $where = array('meta_key' => '_thumbnail_id', 'meta_value' => $post_id);
100
+ $wpdb->delete($table, $where);
101
+ wp_delete_attachment($post_id);
102
+ }
103
 
104
+ // delete _thumbnail_id withouth attachment
105
+ $query = "SELECT 1 FROM " . $table . " pm WHERE pm.meta_key = '_thumbnail_id' AND pm.post_id = " . $post_id . " AND NOT EXISTS (SELECT 1 FROM " . $table_posts . " p WHERE p.id = pm.meta_value)";
106
+ $result = $wpdb->get_results($query);
107
+ foreach ($result as $i) {
108
+ $where = array('meta_key' => '_thumbnail_id', 'post_id' => $post_id);
109
+ $wpdb->delete($table, $where);
110
  }
111
 
112
+ // delete meta values
113
+ $where = array('meta_key' => '_thumbnail_id', 'post_id' => $post_id, 'meta_value' => -1);
114
+ $wpdb->delete($table, $where);
115
+
116
+ $where = array('meta_key' => '_thumbnail_id', 'post_id' => $post_id, 'meta_value' => null);
117
+ $wpdb->delete($table, $where);
118
+
119
+ $where = array('meta_key' => '_thumbnail_id', 'post_id' => $post_id, 'meta_value' => 'fifu:' . $post_id);
120
+ $wpdb->delete($table, $where);
121
+
122
+ $where = array('meta_key' => '_thumbnail_id', 'post_id' => $post_id, 'meta_value' => get_option('fifu_fake_attach_id'));
123
+ $wpdb->delete($table, $where);
124
+
125
+ $where = array('meta_key' => '_wp_attached_file', 'post_id' => $post_id, 'meta_value' => 'Featured Image from URL');
126
+ $wpdb->delete($table, $where);
127
+
128
+ // check if has attachment (real)
129
+ if (get_post_meta($post_id, '_thumbnail_id', true))
130
  return;
 
131
 
132
+ // create attachment (fake)
133
+ $filename = 'Featured Image from URL';
134
+ $parent_post_id = null;
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' => 0,
140
+ 'post_title' => '',
141
+ 'post_excerpt' => '',
142
+ 'post_content' => 'Please don\'t remove that. It\'s just an empty symbolic file that keeps the field filled ' .
143
+ '(some themes/plugins depend on having an attached file to work). But you are free to use any image you want instead of this file.',
144
+ 'post_status' => 'inherit'
145
+ );
146
+ $attach_id = wp_insert_attachment($attachment, $filename, $parent_post_id);
147
+ require_once( ABSPATH . 'wp-admin/includes/image.php' );
148
+ $attach_data = wp_generate_attachment_metadata($attach_id, $filename);
149
+ wp_update_attachment_metadata($attach_id, $attach_data);
150
+
151
+ // add _thumbnail_id
152
+ $data = array('post_id' => $post_id, 'meta_key' => '_thumbnail_id', 'meta_value' => $attach_id);
153
+ $wpdb->insert($table, $data);
154
  }
155
 
includes/thumbnail.php CHANGED
@@ -33,6 +33,21 @@ function fifu_apply_css() {
33
  echo '<style>[class$="woocommerce-product-gallery__trigger"] {display:none !important;}</style>';
34
  }
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  add_filter('post_thumbnail_html', 'fifu_replace', 10, 2);
37
 
38
  function fifu_replace($html, $post_id) {
@@ -75,59 +90,35 @@ function fifu_add_to_content($content) {
75
  add_filter('wp_get_attachment_url', 'fifu_replace_attachment_url', 10, 2);
76
 
77
  function fifu_replace_attachment_url($att_url, $att_id) {
78
- if (get_option('fifu_fake_attach_id')) {
79
- if (fifu_show_internal_instead_of_external(get_the_ID()))
80
- return $att_url;
81
-
82
- if ($att_id == get_post_thumbnail_id(get_the_ID()))
83
- $url = fifu_main_image_url(get_the_ID());
84
-
85
- return $url ? $url : $att_url;
86
- }
87
-
88
- $post_id = strpos($att_id, 'fifu') !== false ? explode(':', $att_id)[1] : null;
89
 
90
- if (!$post_id)
91
  return $att_url;
92
 
93
- if (fifu_show_internal_instead_of_external($post_id))
94
- return $att_url;
 
95
 
96
- $url = fifu_main_image_url($post_id);
 
97
 
98
- return $url ? $url : $att_url;
 
 
 
 
 
99
  }
100
 
101
  add_filter('wp_get_attachment_image_src', 'fifu_replace_attachment_image_src', 10, 3);
102
 
103
  function fifu_replace_attachment_image_src($image, $att_id, $size) {
104
- if (get_option('fifu_fake_attach_id')) {
105
- if (fifu_show_internal_instead_of_external(get_the_ID()))
106
- return $image;
107
-
108
- if ($att_id == get_post_thumbnail_id(get_the_ID())) {
109
- $url = fifu_main_image_url(get_the_ID());
110
- if ($url) {
111
- return array(
112
- $url,
113
- 0,
114
- 0,
115
- false
116
- );
117
- }
118
- }
119
- return $image;
120
- }
121
-
122
- $post_id = strpos($att_id, 'fifu') !== false ? explode(':', $att_id)[1] : null;
123
 
124
- if (!$post_id)
125
  return $image;
126
 
127
- if (fifu_show_internal_instead_of_external($post_id))
128
- return $image;
129
-
130
- $url = fifu_main_image_url($post_id);
131
  if ($url) {
132
  $image_size = fifu_get_image_size($size);
133
  return array(
33
  echo '<style>[class$="woocommerce-product-gallery__trigger"] {display:none !important;}</style>';
34
  }
35
 
36
+ add_action('the_post', 'fifu_choose');
37
+
38
+ function fifu_choose($post) {
39
+ $post_id = $post->ID;
40
+
41
+ $image_url = fifu_main_image_url($post_id);
42
+
43
+ $att_id = get_post_meta($post_id, '_thumbnail_id', true);
44
+
45
+ if ($image_url || get_option('fifu_default_url')) {
46
+ if (!$att_id)
47
+ fifu_update_fake_attach_id($post_id);
48
+ }
49
+ }
50
+
51
  add_filter('post_thumbnail_html', 'fifu_replace', 10, 2);
52
 
53
  function fifu_replace($html, $post_id) {
90
  add_filter('wp_get_attachment_url', 'fifu_replace_attachment_url', 10, 2);
91
 
92
  function fifu_replace_attachment_url($att_url, $att_id) {
93
+ $post = get_post($att_id);
 
 
 
 
 
 
 
 
 
 
94
 
95
+ if (!$post || fifu_show_internal_instead_of_external($post->id))
96
  return $att_url;
97
 
98
+ $url = $post->guid;
99
+ if ($url)
100
+ return $url;
101
 
102
+ return $att_url;
103
+ }
104
 
105
+ add_filter('posts_where', 'fifu_query_attachments');
106
+
107
+ function fifu_query_attachments($where) {
108
+ if (isset($_POST['action']) && ($_POST['action'] == 'query-attachments'))
109
+ $where .= ' AND post_author <> 0 ';
110
+ return $where;
111
  }
112
 
113
  add_filter('wp_get_attachment_image_src', 'fifu_replace_attachment_image_src', 10, 3);
114
 
115
  function fifu_replace_attachment_image_src($image, $att_id, $size) {
116
+ $post = get_post($att_id);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
+ if (!$post || fifu_show_internal_instead_of_external($post->id))
119
  return $image;
120
 
121
+ $url = $post->guid;
 
 
 
122
  if ($url) {
123
  $image_size = fifu_get_image_size($size);
124
  return array(
readme.txt CHANGED
@@ -159,6 +159,9 @@ Features:
159
 
160
  == Changelog ==
161
 
 
 
 
162
  = 1.9.3 =
163
  * New: Bug fix.
164
 
@@ -396,6 +399,9 @@ was removed. To finish, a Premium version is now been presented.
396
 
397
  == Upgrade Notice ==
398
 
 
 
 
399
  = 1.9.3 =
400
  * New: Bug fix.
401
 
159
 
160
  == Changelog ==
161
 
162
+ = 1.9.4 =
163
+ * New: Bug fix.
164
+
165
  = 1.9.3 =
166
  * New: Bug fix.
167
 
399
 
400
  == Upgrade Notice ==
401
 
402
+ = 1.9.4 =
403
+ * New: Bug fix.
404
+
405
  = 1.9.3 =
406
  * New: Bug fix.
407