PluginProbe
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more / 3.7.2
StockPack – Stock photos and AI images from Unsplash, Adobe Stock, Freepik and more v3.7.2
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.7.2, at src/class-stockpackcaptions.php

323 lines 12.7 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 add_filter( 'attachment_fields_to_edit', array( $this, 'add_license_history_link' ), 20, 2 );
64 }
65
66 /**
67 *
68 */
69 public function actions() {
70
71 }
72
73 public function add_caption_to_premium( $caption, $image ) {
74 if ( ! $this->query->is_provider_premium( $image->data->provider ) ) {
75 return $caption;
76 }
77 if ( $this->settings->get_premium_providers_caption_setting() === 'yes' ) {
78 return $caption;
79 }
80
81 return '';
82
83 }
84
85 public function add_caption_fields( $form_fields, $post ) {
86 if ( $this->settings->get_standard_fields_caption_setting() !== 'yes' ) {
87 return $form_fields;
88 }
89
90 $checked = get_post_meta( $post->ID, 'stockpack_caption_generate', true );
91 $checked = ( $checked == 1 ) ? 'checked="checked"' : "";
92 $form_fields['stockpack_caption_generate'] = array(
93 'label' => __( 'Generate', 'stockpack' ),
94 'input' => 'html',
95 'html' => '<input type="checkbox" id="attachments-' . $post->ID . '-checkbox_field" name="attachments[' . $post->ID . '][stockpack_caption_generate]" value="1" ' . $checked . ' /> ',
96 'value' => 1,
97 'helps' => 'Changing fields will generate a new caption if this is checked. Uncheck if you want to prevent the caption being overwritten',
98 );
99
100 $form_fields['stockpack_author_name'] = array(
101 'label' => __( 'Author', 'stockpack' ),
102 'input' => 'text',
103 'value' => get_post_meta( $post->ID, 'stockpack_author_name', true ),
104 'helps' => 'The full name of the author (caption/credit)',
105 );
106
107 $form_fields['stockpack_author_url'] = array(
108 'label' => __( 'Author URL', 'stockpack' ),
109 'input' => 'text',
110 'value' => get_post_meta( $post->ID, 'stockpack_author_url', true ),
111 'helps' => 'The full URL of the author (caption/credit)',
112 );
113
114 $form_fields['stockpack_provider'] = array(
115 'label' => __( 'Source', 'stockpack' ),
116 'input' => 'text',
117 'value' => get_post_meta( $post->ID, 'stockpack_provider', true ),
118 'helps' => 'The full name of the image source. It can also be used as the image Title (caption/credit)',
119 );
120
121 $form_fields['stockpack_image_url'] = array(
122 'label' => __( 'Source URL', 'stockpack' ),
123 'input' => 'text',
124 'value' => get_post_meta( $post->ID, 'stockpack_image_url', true ),
125 'helps' => 'The full URL of the image (caption/credit)',
126 );
127
128 $form_fields['stockpack_license'] = array(
129 'label' => __( 'License', 'stockpack' ),
130 'input' => 'text',
131 'value' => get_post_meta( $post->ID, 'stockpack_license', true ),
132 'helps' => 'The full name of the image license source (caption/credit)',
133 );
134
135 $form_fields['stockpack_license_url'] = array(
136 'label' => __( 'License URL', 'stockpack' ),
137 'input' => 'text',
138 'value' => get_post_meta( $post->ID, 'stockpack_license_url', true ),
139 'helps' => 'The full URL of the image license source (caption/credit)',
140 );
141
142 $form_fields['stockpack_modification'] = array(
143 'label' => __( 'Mods', 'stockpack' ),
144 'input' => 'text',
145 'value' => get_post_meta( $post->ID, 'stockpack_modification', true ),
146 'helps' => 'State any alterations, eg:desaturated (caption/credit)',
147 );
148
149 $form_fields['stockpack_extra'] = array(
150 'label' => __( 'Extra', 'stockpack' ),
151 'input' => 'text',
152 'value' => get_post_meta( $post->ID, 'stockpack_extra', true ),
153 'helps' => 'You can use this field to add further description. It will show up first and create a new line',
154 );
155
156
157 return $form_fields;
158 }
159
160 public function add_license_history_link( $form_fields, $post ) {
161 if ( ! get_post_meta( $post->ID, 'stockpack_id', true ) ) {
162 return $form_fields;
163 }
164
165 $form_fields['stockpack_license_history'] = array(
166 'label' => 'StockPack',
167 'input' => 'html',
168 'html' => '<a href="' . esc_url( $this->query->dashboard_url( 'license-history?source=plugin' ) ) . '" target="_blank" rel="noopener">' . esc_html__( 'License history', 'stockpack' ) . '</a>',
169 );
170
171 return $form_fields;
172 }
173
174 function generate_license_link( $post, $attachment ) {
175 if ( ! isset( $attachment['stockpack_license'] ) ) {
176 return $post;
177 }
178
179 $autocomplete_options = array(
180 'ccby' => array(
181 'name' => 'CC BY 4.0',
182 'url' => 'https://creativecommons.org/licenses/by/4.0/',
183 ),
184 'cc0' => array(
185 'name' => 'CC0',
186 'url' => 'https://creativecommons.org/share-your-work/public-domain/cc0/'
187 ),
188 'public' => array(
189 'name' => 'Public Domain',
190 'url' => 'https://creativecommons.org/share-your-work/public-domain/'
191
192 )
193 );
194
195 $autocomplete_options = apply_filters( 'stockpack_autocomplete_licenses', $autocomplete_options, $attachment );
196
197
198 $key = str_replace( ' ', '', strtolower( $attachment['stockpack_license'] ) );
199 if ( isset( $autocomplete_options[ $key ] ) ) {
200 update_post_meta( $post['ID'], 'stockpack_license', $autocomplete_options[ $key ]['name'] );
201 update_post_meta( $post['ID'], 'stockpack_license_url', $autocomplete_options[ $key ]['url'] );
202 }
203
204 return $post;
205 }
206
207
208 function save_caption_fields( $post, $attachment ) {
209 if ( $this->settings->get_standard_fields_caption_setting() !== 'yes' ) {
210 return $post;
211 }
212
213 $fields = array(
214 'stockpack_author_name',
215 'stockpack_author_url',
216 'stockpack_provider',
217 'stockpack_image_url',
218 'stockpack_license',
219 'stockpack_license_url',
220 'stockpack_modification',
221 'stockpack_caption_generate',
222 'stockpack_modification'
223 );
224
225 foreach ( $fields as $field ) {
226 if ( isset( $attachment[ $field ] ) ) {
227 update_post_meta( $post['ID'], $field, $attachment[ $field ] );
228 } else {
229 delete_post_meta( $post['ID'], $field );
230 }
231 }
232
233
234 $post = $this->generate_license_link( $post, $attachment );
235 if ( isset( $attachment['stockpack_caption_generate'] ) ) {
236 $post['post_excerpt'] = $this->generate_caption( $attachment );
237 }
238
239 return $post;
240 }
241
242 public function generate_caption( $attachment ) {
243 /** Based on @https://wiki.creativecommons.org/wiki/best_practices_for_attribution
244 * Code left verbose to be simpler to modify and copy
245 */
246
247 $caption ='';
248 if ( isset( $attachment['stockpack_extra'] ) && $attachment['stockpack_extra']) {
249 $caption .= '<div class="stockpack-extra-caption">'.$attachment['stockpack_extra'].'</div>';
250 }
251
252 $caption .= __( 'Photo by', 'stockpack' ) . ' ';
253 /** Author */
254 if ( isset( $attachment['stockpack_author_url'] ) && $attachment['stockpack_author_url'] && ( isset( $attachment['stockpack_author_name'] ) ) && $attachment['stockpack_author_name'] ) {
255 $caption .= '<a href="' . $attachment['stockpack_author_url'] . '">';
256 }
257 if ( isset( $attachment['stockpack_author_name'] ) && $attachment['stockpack_author_name'] ) {
258 $caption .= $attachment['stockpack_author_name'];
259 }
260
261 if ( isset( $attachment['stockpack_author_url'] ) && $attachment['stockpack_author_url'] && ( isset( $attachment['stockpack_author_name'] ) ) && $attachment['stockpack_author_name'] ) {
262 $caption .= '</a>';
263 }
264 /** End Author */
265
266
267 /** Provider/Image title/Image Source */
268 if ( ( isset( $attachment['stockpack_provider'] ) && $attachment['stockpack_provider'] ) ) {
269 $caption .= ' ' . __( 'on', 'stockpack' ) . ' ';
270 }
271
272 if ( isset( $attachment['stockpack_image_url'] ) && $attachment['stockpack_image_url'] && ( isset( $attachment['stockpack_provider'] ) ) && $attachment['stockpack_provider'] ) {
273 $caption .= '<a href="' . $attachment['stockpack_image_url'] . '">';
274 }
275
276 if ( isset( $attachment['stockpack_provider'] ) && $attachment['stockpack_provider'] ) {
277 $caption .= $attachment['stockpack_provider'];
278 }
279
280 if ( isset( $attachment['stockpack_image_url'] ) && $attachment['stockpack_image_url'] && ( isset( $attachment['stockpack_provider'] ) ) && $attachment['stockpack_provider'] ) {
281 $caption .= '</a>';
282 }
283 /** End Provider/Image title/Image Source */
284
285
286 /** License */
287 if ( ( isset( $attachment['stockpack_license'] ) && $attachment['stockpack_license'] ) || ( isset( $attachment['stockpack_license_url'] ) && $attachment['stockpack_license_url'] ) ) {
288 $caption .= ' ' . __( 'used under', 'stockpack' ) . ' ';
289 }
290
291 if ( isset( $attachment['stockpack_license_url'] ) && $attachment['stockpack_license_url'] && ( isset( $attachment['stockpack_license'] ) && $attachment['stockpack_license'] ) ) {
292 $caption .= '<a href="' . $attachment['stockpack_license_url'] . '">';
293 }
294
295 if ( isset( $attachment['stockpack_license'] ) && $attachment['stockpack_license'] ) {
296 $caption .= $attachment['stockpack_license'];
297 }
298
299 if ( isset( $attachment['stockpack_license_url'] ) && $attachment['stockpack_license_url'] && ( isset( $attachment['stockpack_license'] ) && $attachment['stockpack_license'] ) ) {
300 $caption .= '</a>';
301 }
302 /** End License */
303
304 if ( isset( $attachment['stockpack_modification'] ) && $attachment['stockpack_modification'] ) {
305 $caption .= ' / ' . $attachment['stockpack_modification'];
306 }
307
308 return apply_filters( 'stockpack_generated_caption', $caption, $attachment );
309
310
311 }
312
313 public function featured_image_caption( $attachment_id ) {
314 return stockpack_fetch_caption( $attachment_id );
315 }
316
317
318 }
319
320 $GLOBALS['stockpack_captions'] = StockpackCaptions::get_instance();
321 }
322
323