PluginProbe
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more / 3.6.1
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more v3.6.1
3.7.2 3.7.1 3.7.0 3.6.0 3.6.1 3.5.2 trunk 2.0 2.1 2.2 2.3 2.4 2.4.1 2.4.2 2.4.3 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.6.0 2.6.1 All 73 releases
stockpack / src / class-stockpackcaptions.php

class-stockpackcaptions.php in StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more 3.6.1, at src/class-stockpackcaptions.php

308 lines 12.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6 if ( ! class_exists( 'StockpackCaptions' ) ) {
7 class StockpackCaptions {
8 /**
9 * @var Singleton The reference the *Singleton* instance of this class
10 */
11 private static $instance;
12
13 /** @var StockpackAdmin */
14 public $admin;
15
16 /** @var StockpackQuery */
17 public $query;
18
19 /** @var StockpackSettings */
20 public $settings;
21
22 public $initialized = false;
23
24 public static function get_instance() {
25 if ( null === self::$instance ) {
26 self::$instance = new self();
27 }
28
29 return self::$instance;
30 }
31
32 /**
33 * StockPack constructor.
34 */
35 protected function __construct() {
36 add_action( 'init', array( $this, 'init' ) );
37 }
38
39 /**
40 *
41 */
42 public function init() {
43 if ( $this->initialized ) {
44 return $this;
45 }
46 $this->initialized = true;
47 $this->admin = \StockpackAdmin::get_instance();
48 $this->query = \StockpackQuery::get_instance();
49 $this->settings = \StockpackSettings::get_instance();
50 $this->actions();
51 $this->filters();
52
53 return $this;
54 }
55
56 /**
57 *
58 */
59 public function filters() {
60 add_filter( 'stockpack_caption', array( $this, 'add_caption_to_premium' ), 10, 2 );
61 add_filter( 'attachment_fields_to_edit', array( $this, 'add_caption_fields' ), 10, 2 );
62 add_filter( 'attachment_fields_to_save', array( $this, 'save_caption_fields' ), 10, 2 );
63 }
64
65 /**
66 *
67 */
68 public function actions() {
69
70 }
71
72 public function add_caption_to_premium( $caption, $image ) {
73 if ( ! $this->query->is_provider_premium( $image->data->provider ) ) {
74 return $caption;
75 }
76 if ( $this->settings->get_premium_providers_caption_setting() === 'yes' ) {
77 return $caption;
78 }
79
80 return '';
81
82 }
83
84 public function add_caption_fields( $form_fields, $post ) {
85 if ( $this->settings->get_standard_fields_caption_setting() !== 'yes' ) {
86 return $form_fields;
87 }
88
89 $checked = get_post_meta( $post->ID, 'stockpack_caption_generate', true );
90 $checked = ( $checked == 1 ) ? 'checked="checked"' : "";
91 $form_fields['stockpack_caption_generate'] = array(
92 'label' => __( 'Generate', 'stockpack' ),
93 'input' => 'html',
94 'html' => '<input type="checkbox" id="attachments-' . $post->ID . '-checkbox_field" name="attachments[' . $post->ID . '][stockpack_caption_generate]" value="1" ' . $checked . ' /> ',
95 'value' => 1,
96 'helps' => 'Changing fields will generate a new caption if this is checked. Uncheck if you want to prevent the caption being overwritten',
97 );
98
99 $form_fields['stockpack_author_name'] = array(
100 'label' => __( 'Author', 'stockpack' ),
101 'input' => 'text',
102 'value' => get_post_meta( $post->ID, 'stockpack_author_name', true ),
103 'helps' => 'The full name of the author (caption/credit)',
104 );
105
106 $form_fields['stockpack_author_url'] = array(
107 'label' => __( 'Author URL', 'stockpack' ),
108 'input' => 'text',
109 'value' => get_post_meta( $post->ID, 'stockpack_author_url', true ),
110 'helps' => 'The full URL of the author (caption/credit)',
111 );
112
113 $form_fields['stockpack_provider'] = array(
114 'label' => __( 'Source', 'stockpack' ),
115 'input' => 'text',
116 'value' => get_post_meta( $post->ID, 'stockpack_provider', true ),
117 'helps' => 'The full name of the image source. It can also be used as the image Title (caption/credit)',
118 );
119
120 $form_fields['stockpack_image_url'] = array(
121 'label' => __( 'Source URL', 'stockpack' ),
122 'input' => 'text',
123 'value' => get_post_meta( $post->ID, 'stockpack_image_url', true ),
124 'helps' => 'The full URL of the image (caption/credit)',
125 );
126
127 $form_fields['stockpack_license'] = array(
128 'label' => __( 'License', 'stockpack' ),
129 'input' => 'text',
130 'value' => get_post_meta( $post->ID, 'stockpack_license', true ),
131 'helps' => 'The full name of the image license source (caption/credit)',
132 );
133
134 $form_fields['stockpack_license_url'] = array(
135 'label' => __( 'License URL', 'stockpack' ),
136 'input' => 'text',
137 'value' => get_post_meta( $post->ID, 'stockpack_license_url', true ),
138 'helps' => 'The full URL of the image license source (caption/credit)',
139 );
140
141 $form_fields['stockpack_modification'] = array(
142 'label' => __( 'Mods', 'stockpack' ),
143 'input' => 'text',
144 'value' => get_post_meta( $post->ID, 'stockpack_modification', true ),
145 'helps' => 'State any alterations, eg:desaturated (caption/credit)',
146 );
147
148 $form_fields['stockpack_extra'] = array(
149 'label' => __( 'Extra', 'stockpack' ),
150 'input' => 'text',
151 'value' => get_post_meta( $post->ID, 'stockpack_extra', true ),
152 'helps' => 'You can use this field to add further description. It will show up first and create a new line',
153 );
154
155
156 return $form_fields;
157 }
158
159 function generate_license_link( $post, $attachment ) {
160 if ( ! isset( $attachment['stockpack_license'] ) ) {
161 return $post;
162 }
163
164 $autocomplete_options = array(
165 'ccby' => array(
166 'name' => 'CC BY 4.0',
167 'url' => 'https://creativecommons.org/licenses/by/4.0/',
168 ),
169 'cc0' => array(
170 'name' => 'CC0',
171 'url' => 'https://creativecommons.org/share-your-work/public-domain/cc0/'
172 ),
173 'public' => array(
174 'name' => 'Public Domain',
175 'url' => 'https://creativecommons.org/share-your-work/public-domain/'
176
177 )
178 );
179
180 $autocomplete_options = apply_filters( 'stockpack_autocomplete_licenses', $autocomplete_options, $attachment );
181
182
183 $key = str_replace( ' ', '', strtolower( $attachment['stockpack_license'] ) );
184 if ( isset( $autocomplete_options[ $key ] ) ) {
185 update_post_meta( $post['ID'], 'stockpack_license', $autocomplete_options[ $key ]['name'] );
186 update_post_meta( $post['ID'], 'stockpack_license_url', $autocomplete_options[ $key ]['url'] );
187 }
188
189 return $post;
190 }
191
192
193 function save_caption_fields( $post, $attachment ) {
194 if ( $this->settings->get_standard_fields_caption_setting() !== 'yes' ) {
195 return $post;
196 }
197
198 $fields = array(
199 'stockpack_author_name',
200 'stockpack_author_url',
201 'stockpack_provider',
202 'stockpack_image_url',
203 'stockpack_license',
204 'stockpack_license_url',
205 'stockpack_modification',
206 'stockpack_caption_generate',
207 'stockpack_modification'
208 );
209
210 foreach ( $fields as $field ) {
211 if ( isset( $attachment[ $field ] ) ) {
212 update_post_meta( $post['ID'], $field, $attachment[ $field ] );
213 } else {
214 delete_post_meta( $post['ID'], $field );
215 }
216 }
217
218
219 $post = $this->generate_license_link( $post, $attachment );
220 if ( isset( $attachment['stockpack_caption_generate'] ) ) {
221 $post['post_excerpt'] = $this->generate_caption( $attachment );
222 }
223
224 return $post;
225 }
226
227 public function generate_caption( $attachment ) {
228 /** Based on @https://wiki.creativecommons.org/wiki/best_practices_for_attribution
229 * Code left verbose to be simpler to modify and copy
230 */
231
232 $caption ='';
233 if ( isset( $attachment['stockpack_extra'] ) && $attachment['stockpack_extra']) {
234 $caption .= '<div class="stockpack-extra-caption">'.$attachment['stockpack_extra'].'</div>';
235 }
236
237 $caption .= __( 'Photo by', 'stockpack' ) . ' ';
238 /** Author */
239 if ( isset( $attachment['stockpack_author_url'] ) && $attachment['stockpack_author_url'] && ( isset( $attachment['stockpack_author_name'] ) ) && $attachment['stockpack_author_name'] ) {
240 $caption .= '<a href="' . $attachment['stockpack_author_url'] . '">';
241 }
242 if ( isset( $attachment['stockpack_author_name'] ) && $attachment['stockpack_author_name'] ) {
243 $caption .= $attachment['stockpack_author_name'];
244 }
245
246 if ( isset( $attachment['stockpack_author_url'] ) && $attachment['stockpack_author_url'] && ( isset( $attachment['stockpack_author_name'] ) ) && $attachment['stockpack_author_name'] ) {
247 $caption .= '</a>';
248 }
249 /** End Author */
250
251
252 /** Provider/Image title/Image Source */
253 if ( ( isset( $attachment['stockpack_provider'] ) && $attachment['stockpack_provider'] ) ) {
254 $caption .= ' ' . __( 'on', 'stockpack' ) . ' ';
255 }
256
257 if ( isset( $attachment['stockpack_image_url'] ) && $attachment['stockpack_image_url'] && ( isset( $attachment['stockpack_provider'] ) ) && $attachment['stockpack_provider'] ) {
258 $caption .= '<a href="' . $attachment['stockpack_image_url'] . '">';
259 }
260
261 if ( isset( $attachment['stockpack_provider'] ) && $attachment['stockpack_provider'] ) {
262 $caption .= $attachment['stockpack_provider'];
263 }
264
265 if ( isset( $attachment['stockpack_image_url'] ) && $attachment['stockpack_image_url'] && ( isset( $attachment['stockpack_provider'] ) ) && $attachment['stockpack_provider'] ) {
266 $caption .= '</a>';
267 }
268 /** End Provider/Image title/Image Source */
269
270
271 /** License */
272 if ( ( isset( $attachment['stockpack_license'] ) && $attachment['stockpack_license'] ) || ( isset( $attachment['stockpack_license_url'] ) && $attachment['stockpack_license_url'] ) ) {
273 $caption .= ' ' . __( 'used under', 'stockpack' ) . ' ';
274 }
275
276 if ( isset( $attachment['stockpack_license_url'] ) && $attachment['stockpack_license_url'] && ( isset( $attachment['stockpack_license'] ) && $attachment['stockpack_license'] ) ) {
277 $caption .= '<a href="' . $attachment['stockpack_license_url'] . '">';
278 }
279
280 if ( isset( $attachment['stockpack_license'] ) && $attachment['stockpack_license'] ) {
281 $caption .= $attachment['stockpack_license'];
282 }
283
284 if ( isset( $attachment['stockpack_license_url'] ) && $attachment['stockpack_license_url'] && ( isset( $attachment['stockpack_license'] ) && $attachment['stockpack_license'] ) ) {
285 $caption .= '</a>';
286 }
287 /** End License */
288
289 if ( isset( $attachment['stockpack_modification'] ) && $attachment['stockpack_modification'] ) {
290 $caption .= ' / ' . $attachment['stockpack_modification'];
291 }
292
293 return apply_filters( 'stockpack_generated_caption', $caption, $attachment );
294
295
296 }
297
298 public function featured_image_caption( $attachment_id ) {
299 return stockpack_fetch_caption( $attachment_id );
300 }
301
302
303 }
304
305 $GLOBALS['stockpack_captions'] = StockpackCaptions::get_instance();
306 }
307
308