PluginProbe
Image SEO – AI-Driven Image SEO Optimizer / trunk
Image SEO – AI-Driven Image SEO Optimizer vtrunk
3.2.16 3.2.15 3.2.13 3.2.14 3.2.12 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 3.2.6 trunk 2.0.9 3.0.0 3.0.1 3.0.2 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.2.0 3.2.1 3.2.2 3.2.3 All 71 releases
imageseo / src / Async / GenerateImageBackgroundProcess.php

GenerateImageBackgroundProcess.php in Image SEO – AI-Driven Image SEO Optimizer trunk, at src/Async/GenerateImageBackgroundProcess.php

193 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImageSeoWP\Async;
4
5 defined( 'ABSPATH' ) or die( 'Cheatin&#8217; uh?' );
6
7 use ImageSeoWP\Helpers\SocialMedia;
8
9 class GenerateImageBackgroundProcess extends WPBackgroundProcess {
10
11 protected $action = 'imageseo_generate_image_background_process';
12
13
14 protected function getMinutes( $content ) {
15 $word = str_word_count( strip_tags( $content ) );
16 $minutes = floor( $word / 200 );
17 if ( $minutes < 1 ) {
18 $minutes = 1;
19 }
20
21 return $minutes;
22 }
23
24 /**
25 * @param \WP_Post $post
26 *
27 * @return string
28 */
29 protected function getSubTitle( \WP_Post $post ): string {
30 switch ( $post->post_type ) {
31 case 'product':
32 if ( ! function_exists( 'wc_get_product' ) ) {
33 return '';
34 }
35
36 $product = wc_get_product( $post->ID );
37
38 $subTitle = html_entity_decode( sprintf( '%s%s', $product->get_price(), get_woocommerce_currency_symbol() ) );
39 break;
40 default:
41 $subTitle = get_the_author_meta( 'display_name', $post->post_author );
42 break;
43 }
44
45 return apply_filters( 'imageseo_get_sub_title_social_media', $subTitle, $post );
46 }
47
48 /**
49 * @param \WP_Post $post
50 *
51 * @return string
52 */
53 protected function getSubTitleTwo( \WP_Post $post ): string {
54 switch ( $post->post_type ) {
55 case 'product':
56 if ( ! function_exists( 'wc_get_product' ) ) {
57 return '';
58 }
59
60 $product = wc_get_product( $post->ID );
61
62 $subTitle = $product->get_review_count();
63 break;
64 default:
65 $subTitle = sprintf( __( '%s min read', 'imageseo' ), $this->getMinutes( $post->post_content ) );
66 break;
67 }
68
69 return apply_filters( 'imageseo_get_sub_title_two_social_media', $subTitle, $post );
70 }
71
72 protected function getVisibilityRating( $post, $settings ) {
73 switch ( $post->post_type ) {
74 case 'product':
75 $visibility = $settings['visibilityRating'];
76 break;
77 default:
78 $visibility = false;
79 break;
80 }
81
82 return apply_filters( 'imageseo_get_visibility_rating', $visibility, $post );
83 }
84
85 protected function getAvatarUrl( $post ) {
86 return apply_filters( 'imageseo_get_avatar_url', get_avatar_url( $post->post_author ) );
87 }
88
89 /**
90 * Task.
91 *
92 * @param mixed $item Queue item to iterate over
93 *
94 * @return mixed
95 */
96 protected function task( $item ) {
97 $limitExcedeed = imageseo_get_service( 'UserInfo' )->hasLimitExcedeed();
98 if ( $limitExcedeed ) {
99 return false;
100 }
101
102 if ( ! imageseo_get_api_key() ) {
103 return false;
104 }
105
106 $post = get_post( $item['id'] );
107 if ( ! $post ) {
108 return false;
109 }
110
111 $medias = array(
112 SocialMedia::OPEN_GRAPH['name'],
113 );
114 $settings = imageseo_get_service( 'Option' )->getOption( 'social_media_settings' );
115
116 $siteTitle = get_bloginfo( 'name' );
117 $subTitle = $this->getSubTitle( $post );
118 $subTitleTwo = $this->getSubTitleTwo( $post );
119
120 $visibilityRating = $this->getVisibilityRating( $post, $settings );
121
122 $nbGoodStars = 0;
123 if ( $visibilityRating && function_exists( 'wc_get_product' ) ) {
124 $product = wc_get_product( $post->ID );
125 $nbGoodStars = ceil( $product->get_average_rating() );
126 }
127 $avatarUrl = $this->getAvatarUrl( $post );
128
129 $featuredImgUrl = get_the_post_thumbnail_url( $item['id'], 'full' );
130
131 if ( ! $featuredImgUrl ) {
132 $featuredImgUrl = $settings['defaultBgImg'];
133 }
134
135 $isAlreadyGenerate = get_post_meta( $item['id'], '_imageseo_social_media_image_is_generate', true );
136 $transientCurrentProcess = get_transient( '_imageseo_filename_social_process' );
137 if ( ! $transientCurrentProcess ) {
138 $transientCurrentProcess = array();
139 }
140 foreach ( $medias as $media ) {
141 $formatFilename = sanitize_title( sprintf( '%s-%s-%s', $siteTitle, $post->post_name, $media ) );
142 $filename = apply_filters( 'imageseo_filename_social_media_image', $formatFilename, $item['id'], $media );
143
144 if ( $isAlreadyGenerate ) {
145 $currentAttachmentId = get_post_meta( $item['id'], sprintf( '_imageseo_social_media_image_%s', $media ), true );
146 wp_delete_attachment( $currentAttachmentId, true );
147 }
148
149 $data = array(
150 'title' => $post->post_title,
151 'subTitle' => $subTitle,
152 'subTitleTwo' => $subTitleTwo,
153 'layout' => $settings['layout'],
154 'textColor' => str_replace( '#', '', $settings['textColor'] ),
155 'contentBackgroundColor' => str_replace( '#', '', $settings['contentBackgroundColor'] ),
156 'starColor' => str_replace( '#', '', $settings['starColor'] ),
157 'visibilitySubTitle' => $settings['visibilitySubTitle'],
158 'visibilitySubTitleTwo' => $settings['visibilitySubTitleTwo'],
159 'visibilityAvatar' => $settings['visibilityAvatar'],
160 'visibilityRating' => $visibilityRating,
161 'layout' => $settings['layout'],
162 'textAlignment' => $settings['textAlignment'],
163 'logoUrl' => $settings['logoUrl'],
164 'avatarUrl' => $avatarUrl,
165 'bgImgUrl' => $featuredImgUrl,
166 'nbGoodStars' => (string) $nbGoodStars,
167 'media' => $media,
168 );
169
170 $result = imageseo_get_service( 'GenerateImageSocial' )->generate(
171 $filename,
172 $data
173 );
174
175 if ( isset( $result['attachment_id'] ) ) {
176 update_post_meta( $item['id'], sprintf( '_imageseo_social_media_image_%s', $media ), $result['attachment_id'] );
177 unset( $transientCurrentProcess[ $item['id'] ] );
178 set_transient( '_imageseo_filename_social_process', $transientCurrentProcess, 20 );
179 }
180
181 if ( ! $isAlreadyGenerate ) {
182 update_post_meta( $item['id'], '_imageseo_social_media_image_is_generate', true );
183 }
184 }
185
186 return false;
187 }
188
189 protected function complete() {
190 parent::complete();
191 }
192 }
193