Version Description
- FIX: Google search API issue
Download this release
Release Info
Developer | webtemyk |
Plugin | 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 +1 -1
- includes/class-post-images.php +260 -174
- includes/image-search/class-google-images.php +3 -2
- readme.txt +4 -1
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.
|
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 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 = '
|
47 |
|
48 |
$start = ( ( $page - 1 ) * 10 ) + 1;
|
49 |
$url = sprintf( '%s?%s', self::URL, http_build_query( [
|
50 |
'searchType' => 'image',
|
51 |
-
'start' => $start
|
|
|
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.
|
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
|