Auto Post Thumbnail - Version 3.9.11

Version Description

  • FIX: Google search API issue
Download this release

Release Info

Developer webtemyk
Plugin Icon 128x128 Auto Post Thumbnail
Version 3.9.11
Comparing to
See all releases

Code changes from version 3.9.10 to 3.9.11

auto-post-thumbnail.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Auto Featured Image (Auto Post Thumbnail)
4
  Plugin URI: https://cm-wp.com/apt
5
  Description: Automatically generate the Featured Image from the first image in post or any custom post type only if Featured Image is not set manually. Featured Image Generation From Title. Native image search for Elementor, Gutenberg, Classic Editor.
6
- Version: 3.9.10
7
  Author: Creative Motion <support@cm-wp.com>
8
  Author URI: https://cm-wp.com
9
  Text Domain: apt
3
  Plugin Name: Auto Featured Image (Auto Post Thumbnail)
4
  Plugin URI: https://cm-wp.com/apt
5
  Description: Automatically generate the Featured Image from the first image in post or any custom post type only if Featured Image is not set manually. Featured Image Generation From Title. Native image search for Elementor, Gutenberg, Classic Editor.
6
+ Version: 3.9.11
7
  Author: Creative Motion <support@cm-wp.com>
8
  Author URI: https://cm-wp.com
9
  Text Domain: apt
includes/class-post-images.php CHANGED
@@ -1,174 +1,260 @@
1
- <?php
2
-
3
- namespace WBCR\APT;
4
-
5
- use WP_Post, WP_Error;
6
-
7
- // Exit if accessed directly
8
- if ( ! defined( 'ABSPATH' ) ) {
9
- exit;
10
- }
11
-
12
- /**
13
- * Post images class
14
- */
15
- class PostImages {
16
-
17
- /**
18
- * @var \WAPT_Plugin
19
- */
20
- private $plugin;
21
-
22
- /**
23
- * @var WP_Post
24
- */
25
- public $post;
26
-
27
- /**
28
- * @var array
29
- */
30
- private $images = [];
31
-
32
- /**
33
- * Post Images constructor.
34
- *
35
- * @param WP_Post|int|string $post Post object or post ID or post content
36
- */
37
- public function __construct( $post = null ) {
38
- $this->plugin = \WAPT_Plugin::app();
39
-
40
- if ( is_numeric( $post ) ) {
41
- $post = get_post( $post, 'OBJECT' );
42
- $this->post = $post;
43
- } elseif ( is_object( $post ) ) {
44
- $this->post = $post;
45
- } elseif ( is_string( $post ) ) {
46
- $new_post = new \stdClass();
47
- $new_post->post_content = $post;
48
-
49
- $this->post = new WP_Post( $new_post );
50
- }
51
-
52
- $this->find_images();
53
- }
54
-
55
- /**
56
- * Get an array of images url, contained in the post
57
- */
58
- private function find_images() {
59
- $matches = [];
60
- $images = [];
61
-
62
- //do shortcodes before search images
63
- $post_content = do_shortcode( $this->post->post_content ?? '' );
64
-
65
- // Get all images from post's body
66
- preg_match_all( '/<\s*img .*?src\s*=\s*[\"\']?([^\"\'> ]*).*?>/i', $post_content, $matches );
67
-
68
- if ( count( $matches ) ) {
69
- //$this->plugin->logger->debug( "Found from regex: " . var_export( $matches[0], true ) );
70
-
71
- foreach ( $matches[0] as $key => $image ) {
72
- $title = '';
73
- preg_match_all( '/<\s*img [^\>]*title\s*=\s*[\"\']?([^\"\'> ]*)/i', $image, $matches_title );
74
-
75
- if ( count( $matches_title ) && isset( $matches_title[1] ) && isset( $matches_title[1][ $key ] ) ) {
76
- $title = $matches[1][ $key ];
77
- }
78
-
79
- $images[] = [
80
- 'tag' => $image,
81
- 'url' => $matches[1][ $key ],
82
- 'title' => $title,
83
- ];
84
- }
85
- }
86
-
87
- $this->images = $images;
88
- $this->plugin->logger->debug( 'Found images: ' . var_export( $images, true ) );
89
- }
90
-
91
- /**
92
- * Get the post object
93
- *
94
- * @return WP_Post
95
- */
96
- public function get_post() {
97
- return $this->post;
98
- }
99
-
100
- /**
101
- * Get an array of images url, contained in the post
102
- *
103
- * @return array
104
- */
105
- public function get_images() {
106
- return $this->images;
107
- }
108
-
109
- /**
110
- * Get count of images url, contained in the post
111
- *
112
- * @return int
113
- */
114
- public function count_images() {
115
- return count( $this->images );
116
- }
117
-
118
- /**
119
- * If images is founded in post
120
- *
121
- * @return bool
122
- */
123
- public function is_images() {
124
- return (bool) $this->count_images();
125
- }
126
-
127
- /**
128
- * @param string $image Image path
129
- * @param string $suffix Slug suffix
130
- * @param WP_Post $post Post object
131
- *
132
- * @return string
133
- */
134
- public function unique_filepath( $image, $suffix = 'image', $post = null ) {
135
- if ( ! $post ) {
136
- $post = $this->get_post();
137
- }
138
-
139
- $uploads = wp_upload_dir( current_time( 'mysql' ) );
140
- $extension = pathinfo( $image, PATHINFO_EXTENSION );
141
-
142
- //$slug = wp_unique_post_slug( $post->post_title, $post->ID, $post->post_status, $post->post_type, $post->post_parent );
143
- $slug = "wapt_{$suffix}";
144
- $file_path = wp_unique_filename( $uploads['path'], "{$slug}_{$post->post_type}_{$post->ID}.{$extension}" );
145
- $file_path = "{$uploads['path']}/{$file_path}";
146
-
147
- return $file_path;
148
- }
149
-
150
- /**
151
- * @param string $url URL
152
- * @param string $path_to Path to download
153
- *
154
- * @return bool
155
- */
156
- public function download( $url, $path_to ) {
157
- $response = wp_remote_get( $url );
158
- if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
159
- $body = wp_remote_retrieve_body( $response );
160
-
161
- global $wp_filesystem;
162
- if ( ! $wp_filesystem ) {
163
- if ( ! function_exists( 'WP_Filesystem' ) ) {
164
- require_once ABSPATH . 'wp-admin/includes/file.php';
165
- }
166
- WP_Filesystem();
167
- }
168
-
169
- $downloaded = $path_to ? $wp_filesystem->put_contents( $path_to, $body ) : false;
170
- }
171
-
172
- return isset( $downloaded ) ? (bool) $downloaded : false;
173
- }
174
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ namespace WBCR\APT;
4
+
5
+ use WP_Post, WP_Error;
6
+
7
+ // Exit if accessed directly
8
+ if ( ! defined( 'ABSPATH' ) ) {
9
+ exit;
10
+ }
11
+
12
+ /**
13
+ * Post images class
14
+ */
15
+ class PostImages {
16
+
17
+ /**
18
+ * @var \WAPT_Plugin
19
+ */
20
+ private $plugin;
21
+
22
+ /**
23
+ * @var WP_Post
24
+ */
25
+ public $post;
26
+
27
+ /**
28
+ * @var array
29
+ */
30
+ private $images = [];
31
+
32
+ /**
33
+ * Post Images constructor.
34
+ *
35
+ * @param WP_Post|int|string $post Post object or post ID or post content
36
+ */
37
+ public function __construct( $post = null ) {
38
+ $this->plugin = \WAPT_Plugin::app();
39
+
40
+ if ( is_numeric( $post ) ) {
41
+ $post = get_post( $post, 'OBJECT' );
42
+ $this->post = $post;
43
+ } elseif ( is_object( $post ) ) {
44
+ $this->post = $post;
45
+ } elseif ( is_string( $post ) ) {
46
+ $new_post = new \stdClass();
47
+ $new_post->post_content = $post;
48
+
49
+ $this->post = new WP_Post( $new_post );
50
+ }
51
+
52
+ $this->find_images();
53
+
54
+ }
55
+
56
+ /**
57
+ * Get an array of images url, contained in the post
58
+ */
59
+ private function find_images() {
60
+ $matches = [];
61
+ $images = [];
62
+
63
+ //do shortcodes before search images
64
+ $post_content = do_shortcode( $this->post->post_content ?? '' );
65
+
66
+ // Get all images from post's body
67
+ preg_match_all( '/<\s*img .*?src\s*=\s*[\"\']?([^\"\'> ]*).*?>/i', $post_content, $matches );
68
+
69
+ if ( count( $matches ) ) {
70
+ //$this->plugin->logger->debug( "Found from regex: " . var_export( $matches[0], true ) );
71
+
72
+ foreach ( $matches[0] as $key => $image ) {
73
+ $title = '';
74
+ preg_match_all( '/<\s*img [^\>]*title\s*=\s*[\"\']?([^\"\'> ]*)/i', $image, $matches_title );
75
+
76
+
77
+ if ( count( $matches_title ) && isset( $matches_title[1] ) && isset( $matches_title[1][ $key ] ) ) {
78
+ $title = $matches[1][ $key ];
79
+ }
80
+
81
+ $images[] = [
82
+ 'tag' => $image,
83
+ 'url' => $matches[1][ $key ],
84
+ 'title' => $title,
85
+ ];
86
+ }
87
+ } else {
88
+ // find all matches youtube links :
89
+
90
+ // youtube.com/v/vidid
91
+ //youtube.com/vi/vidid
92
+ //youtube.com/?v=vidid
93
+ //youtube.com/?vi=vidid
94
+ //youtube.com/watch?v=vidid
95
+ //youtube.com/watch?vi=vidid
96
+ //youtu.be/vidid
97
+ //youtube.com/embed/vidid
98
+ //http://youtube.com/v/vidid
99
+ //http://www.youtube.com/v/vidid
100
+ //https://www.youtube.com/v/vidid
101
+ //youtube.com/watch?v=vidid&wtv=wtv
102
+ //http://www.youtube.com/watch?dev=inprogress&v=vidid&feature=related
103
+ //https://m.youtube.com/watch?v=vidid
104
+
105
+ preg_match_all("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $post_content, $matches);
106
+
107
+ if (count($matches)) {
108
+
109
+ foreach ($matches[0] as $key=> $image) {
110
+ $image = '<img src="//img.youtube.com/vi/'.$image.'/maxresdefault.jpg">';
111
+ $title = '';
112
+ preg_match_all( '/<\s*img [^\>]*title\s*=\s*[\"\']?([^\"\'> ]*)/i', $image, $matches_title );
113
+
114
+ if ( count( $matches_title ) && isset( $matches_title[1] ) && isset( $matches_title[1][ $key ] ) ) {
115
+ $title = $matches[1][ $key ];
116
+ }
117
+
118
+ $images[] = [
119
+ 'tag' => $image,
120
+ 'url' => $matches[1][ $key ],
121
+ 'title' => $title,
122
+ ];
123
+ }
124
+ }
125
+ }
126
+
127
+ $this->images = $images;
128
+ $this->plugin->logger->debug( 'Found images: ' . var_export( $images, true ) );
129
+ }
130
+
131
+ private function find_videos() {
132
+ $matches = [];
133
+ $videos = [];
134
+
135
+ //do shortcodes before search images
136
+ $post_content = do_shortcode( $this->post->post_content ?? '' );
137
+
138
+ // Get all youtube videos from post's body
139
+ preg_match_all( "#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $post_content, $matches );
140
+
141
+ if ( count( $matches ) ) {
142
+ //$this->plugin->logger->debug( "Found from regex: " . var_export( $matches[0], true ) );
143
+
144
+ foreach ( $matches[0] as $key => $video ) {
145
+ $title = '';
146
+ preg_match_all( '/<\s*img [^\>]*title\s*=\s*[\"\']?([^\"\'> ]*)/i', $video, $matches_title );
147
+
148
+ if ( count( $matches_title ) && isset( $matches_title[1] ) && isset( $matches_title[1][ $key ] ) ) {
149
+ $title = $matches[1][ $key ];
150
+ }
151
+
152
+ $videos[] = [
153
+ 'tag' => $video,
154
+ 'url' => $matches[1][ $key ],
155
+ 'title' => $title,
156
+ ];
157
+ }
158
+ }
159
+
160
+ $this->images = $videos;
161
+ $this->plugin->logger->debug( 'Found videos: ' . var_export( $videos, true ) );
162
+ }
163
+
164
+
165
+ /**
166
+ * Get the post object
167
+ *
168
+ * @return WP_Post
169
+ */
170
+ public function get_post() {
171
+ return $this->post;
172
+ }
173
+
174
+ /**
175
+ * Get an array of images url, contained in the post
176
+ *
177
+ * @return array
178
+ */
179
+ public function get_images() {
180
+ return $this->images;
181
+ }
182
+
183
+ public function get_videos() {
184
+ return $this->videos;
185
+ }
186
+
187
+ /**
188
+ * Get count of images url, contained in the post
189
+ *
190
+ * @return int
191
+ */
192
+ public function count_images() {
193
+ return count( $this->images );
194
+ }
195
+
196
+ public function count_videos() {
197
+ return count( $this->videos );
198
+ }
199
+
200
+ /**
201
+ * If images is founded in post
202
+ *
203
+ * @return bool
204
+ */
205
+ public function is_images() {
206
+ return (bool) $this->count_images();
207
+ }
208
+
209
+ public function is_videos() {
210
+ return (bool) $this->count_videos();
211
+ }
212
+
213
+ /**
214
+ * @param string $image Image path
215
+ * @param string $suffix Slug suffix
216
+ * @param WP_Post $post Post object
217
+ *
218
+ * @return string
219
+ */
220
+ public function unique_filepath( $image, $suffix = 'image', $post = null ) {
221
+ if ( ! $post ) {
222
+ $post = $this->get_post();
223
+ }
224
+
225
+ $uploads = wp_upload_dir( current_time( 'mysql' ) );
226
+ $extension = pathinfo( $image, PATHINFO_EXTENSION );
227
+
228
+ //$slug = wp_unique_post_slug( $post->post_title, $post->ID, $post->post_status, $post->post_type, $post->post_parent );
229
+ $slug = "wapt_{$suffix}";
230
+ $file_path = wp_unique_filename( $uploads['path'], "{$slug}_{$post->post_type}_{$post->ID}.{$extension}" );
231
+ $file_path = "{$uploads['path']}/{$file_path}";
232
+
233
+ return $file_path;
234
+ }
235
+
236
+ /**
237
+ * @param string $url URL
238
+ * @param string $path_to Path to download
239
+ *
240
+ * @return bool
241
+ */
242
+ public function download( $url, $path_to ) {
243
+ $response = wp_remote_get( $url );
244
+ if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
245
+ $body = wp_remote_retrieve_body( $response );
246
+
247
+ global $wp_filesystem;
248
+ if ( ! $wp_filesystem ) {
249
+ if ( ! function_exists( 'WP_Filesystem' ) ) {
250
+ require_once ABSPATH . 'wp-admin/includes/file.php';
251
+ }
252
+ WP_Filesystem();
253
+ }
254
+
255
+ $downloaded = $path_to ? $wp_filesystem->put_contents( $path_to, $body ) : false;
256
+ }
257
+
258
+ return isset( $downloaded ) ? (bool) $downloaded : false;
259
+ }
260
+ }
includes/image-search/class-google-images.php CHANGED
@@ -43,12 +43,13 @@ class GoogleImages implements ImageSearch {
43
  $rights = '';
44
  }
45
  */
46
- $rights = '&rights=(cc_publicdomain%7Ccc_attribute%7Ccc_sharealike).-(cc_noncommercial%7Ccc_nonderived)';
47
 
48
  $start = ( ( $page - 1 ) * 10 ) + 1;
49
  $url = sprintf( '%s?%s', self::URL, http_build_query( [
50
  'searchType' => 'image',
51
- 'start' => $start . $rights,
 
52
  'q' => $query,
53
  'key' => $this->key,
54
  'cx' => $this->cse,
43
  $rights = '';
44
  }
45
  */
46
+ $rights = '(cc_publicdomain%7Ccc_attribute%7Ccc_sharealike).-(cc_noncommercial%7Ccc_nonderived)';
47
 
48
  $start = ( ( $page - 1 ) * 10 ) + 1;
49
  $url = sprintf( '%s?%s', self::URL, http_build_query( [
50
  'searchType' => 'image',
51
+ 'start' => $start,
52
+ 'rights' => $rights,
53
  'q' => $query,
54
  'key' => $this->key,
55
  'cx' => $this->cse,
readme.txt CHANGED
@@ -4,7 +4,7 @@ Tags: post thumbnails, featured image, elementor, thumbnails, featured image fro
4
  Requires at least: 4.8
5
  Tested up to: 6.0
6
  Requires PHP: 7.0
7
- Stable tag: 3.9.10
8
  License: GPLv2
9
  License URI: https://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -113,6 +113,9 @@ This section describes how to install the plugin and get it working.
113
 
114
  == Changelog ==
115
 
 
 
 
116
  = 3.9.10 =
117
  * ADD: Default image for generation
118
  * FIX: Memory allocate error on Generation page
4
  Requires at least: 4.8
5
  Tested up to: 6.0
6
  Requires PHP: 7.0
7
+ Stable tag: 3.9.11
8
  License: GPLv2
9
  License URI: https://www.gnu.org/licenses/gpl-2.0.html
10
 
113
 
114
  == Changelog ==
115
 
116
+ = 3.9.11 =
117
+ * FIX: Google search API issue
118
+
119
  = 3.9.10 =
120
  * ADD: Default image for generation
121
  * FIX: Memory allocate error on Generation page