Complex classes like Dynamic_Featured_Image often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Dynamic_Featured_Image, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
48 | class Dynamic_Featured_Image { |
||
49 | /** |
||
50 | * Current version of the plugin. |
||
51 | * |
||
52 | * @since 3.0.0 |
||
53 | */ |
||
54 | const VERSION = '3.5.2'; |
||
55 | |||
56 | /** |
||
57 | * Text domain. |
||
58 | * |
||
59 | * @since 3.6.0 |
||
60 | */ |
||
61 | const TEXT_DOMAIN = 'dynamic-featured-image'; |
||
62 | |||
63 | /** |
||
64 | * Image upload directory. |
||
65 | * |
||
66 | * @var $__upload_dir string |
||
67 | */ |
||
68 | private $__upload_dir; |
||
69 | |||
70 | /** |
||
71 | * Image upload URL. |
||
72 | * |
||
73 | * @var $__upload_url string |
||
74 | */ |
||
75 | private $__upload_url; |
||
76 | |||
77 | /** |
||
78 | * Database object. |
||
79 | * |
||
80 | * @var $__db wpdb |
||
81 | */ |
||
82 | private $__db; |
||
83 | |||
84 | /** |
||
85 | * Title for dfi metabox. |
||
86 | * |
||
87 | * @var $metabox_title string |
||
88 | */ |
||
89 | protected $metabox_title; |
||
90 | |||
91 | /** |
||
92 | * Users post type filter for dfi metabox. |
||
93 | * |
||
94 | * @var $user_filter array |
||
95 | */ |
||
96 | protected $user_filter; |
||
97 | |||
98 | /** |
||
99 | * Constructor. Hooks all interactions to initialize the class. |
||
100 | * |
||
101 | * @since 1.0.0 |
||
102 | * @access public |
||
103 | * @global object $wpdb |
||
104 | * |
||
105 | * @see add_action() |
||
106 | */ |
||
107 | public function __construct() { |
||
108 | // plugin update warning. |
||
109 | add_action( 'in_plugin_update_message-' . plugin_basename( __FILE__ ), array( $this, 'update_notice' ) ); |
||
110 | |||
111 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); |
||
112 | add_action( 'add_meta_boxes', array( $this, 'initialize_featured_box' ) ); |
||
113 | add_action( 'save_post', array( $this, 'save_meta' ) ); |
||
114 | add_action( 'plugins_loaded', array( $this, 'load_plugin_textdomain' ) ); |
||
115 | |||
116 | // handle ajax request. |
||
117 | add_action( 'wp_ajax_dfiMetaBox_callback', array( $this, 'ajax_callback' ) ); |
||
118 | |||
119 | // add action links. |
||
120 | add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'dfi_action_links' ) ); |
||
121 | |||
122 | // media uploader custom fields. |
||
123 | add_filter( 'attachment_fields_to_edit', array( $this, 'media_attachment_custom_fields' ), 10, 2 ); |
||
124 | add_filter( 'attachment_fields_to_save', array( $this, 'media_attachment_custom_fields_save' ), 10, 2 ); |
||
125 | |||
126 | // get the site protocol. |
||
127 | $protocol = $this->get_protocol(); |
||
128 | |||
129 | $this->__upload_dir = wp_upload_dir(); |
||
130 | $this->__upload_url = preg_replace( '#^https?://#', '', $this->__upload_dir['baseurl'] ); |
||
131 | |||
132 | // add protocol to the upload url. |
||
133 | $this->__upload_url = $protocol . $this->__upload_url; |
||
134 | |||
135 | // post type filter added by user. |
||
136 | $this->user_filter = array(); |
||
137 | |||
138 | global $wpdb; |
||
139 | $this->__db = $wpdb; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Return site protocol. |
||
144 | * |
||
145 | * @since 3.5.1 |
||
146 | * @access public |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | private function get_protocol() { |
||
|
|||
151 | return ( ( ! empty( $_SERVER['HTTPS'] ) && 'off' !== $_SERVER['HTTPS'] ) || |
||
152 | ( ! empty( $_SERVER['SERVER_PORT'] ) && 443 === $_SERVER['SERVER_PORT'] ) ) ? 'https://' : 'http://'; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Add required admin scripts. |
||
157 | * |
||
158 | * @since 1.0.0 |
||
159 | * @access public |
||
160 | * |
||
161 | * @see wp_enque_style() |
||
162 | * @see wp_register_script() |
||
163 | * @see wp_enqueue_script() |
||
164 | * |
||
165 | * @return void |
||
166 | */ |
||
167 | public function enqueue_admin_scripts() { |
||
168 | // enqueue styles. |
||
169 | wp_enqueue_style( 'style-dfi', plugins_url( '/css/style-dfi.css', __FILE__ ), array(), self::VERSION ); |
||
170 | wp_enqueue_style( 'dashicons', plugins_url( '/css/dashicons.css', __FILE__ ), array(), self::VERSION ); |
||
171 | |||
172 | // register script. |
||
173 | wp_register_script( 'scripts-dfi', plugins_url( '/js/script-dfi.js', __FILE__ ), array( 'jquery' ), self::VERSION ); |
||
174 | |||
175 | // localize the script with required data. |
||
176 | wp_localize_script( |
||
177 | 'scripts-dfi', |
||
178 | 'WP_SPECIFIC', |
||
179 | array( |
||
180 | 'upload_url' => $this->__upload_url, |
||
181 | 'metabox_title' => __( $this->metabox_title, self::TEXT_DOMAIN ), |
||
182 | 'mediaSelector_title' => __( 'Dynamic Featured Image - Media Selector', self::TEXT_DOMAIN ), |
||
183 | 'mediaSelector_buttonText' => __( 'Set Featured Image', self::TEXT_DOMAIN ), |
||
184 | ) |
||
185 | ); |
||
186 | |||
187 | // enqueue scripts. |
||
188 | wp_enqueue_script( 'scripts-dfi' ); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Add upgrade link. |
||
193 | * |
||
194 | * @access public |
||
195 | * @since 3.5.1 |
||
196 | * @action plugin_action_links |
||
197 | * |
||
198 | * @codeCoverageIgnore |
||
199 | * |
||
200 | * @param array $links Action links. |
||
201 | * |
||
202 | * @return array |
||
203 | */ |
||
204 | public function dfi_action_links( $links ) { |
||
205 | $upgrade_link = array( |
||
206 | '<a href="https://ankitpokhrel.com/explore/downloads/dynamic-featured-image-pro/" target="_blank">Upgrade to Premium</a>' |
||
207 | ); |
||
208 | |||
209 | return array_merge( $links, $upgrade_link ); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Add featured meta boxes dynamically. |
||
214 | * |
||
215 | * @since 1.0.0 |
||
216 | * @access public |
||
217 | * @global object $post |
||
218 | * |
||
219 | * @see get_post_meta() |
||
220 | * @see get_post_types() |
||
221 | * @see add_meta_box() |
||
222 | * @see add_filter() |
||
223 | * |
||
224 | * @return void |
||
225 | */ |
||
226 | public function initialize_featured_box() { |
||
227 | global $post; |
||
228 | |||
229 | // make metabox title dynamic. |
||
230 | $this->metabox_title = apply_filters( 'dfi_set_metabox_title', __( 'Featured Image', self::TEXT_DOMAIN ) ); |
||
231 | |||
232 | $featured_data = get_post_meta( $post->ID, 'dfiFeatured', true ); |
||
233 | $total_featured = count( $featured_data ); |
||
234 | |||
235 | $default_filter = array( 'attachment', 'revision', 'nav_menu_item' ); |
||
236 | $this->user_filter = apply_filters( 'dfi_post_type_user_filter', $this->user_filter ); |
||
237 | $filter = array_merge( $default_filter, $this->user_filter ); |
||
238 | |||
239 | $post_types = get_post_types(); |
||
240 | $post_types = array_diff( $post_types, $filter ); |
||
241 | |||
242 | $post_types = apply_filters( 'dfi_post_types', $post_types ); |
||
243 | |||
244 | if ( ! empty( $featured_data ) && $total_featured >= 1 ) { |
||
245 | $i = 2; |
||
246 | foreach ( $featured_data as $featured ) { |
||
247 | self::_dfi_add_meta_box( $post_types, $featured, $i ); |
||
248 | $i ++; |
||
249 | } |
||
250 | } else { |
||
251 | self::_dfi_add_meta_box( $post_types ); |
||
252 | } |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * Translates more than one digit number digit by digit. |
||
257 | * |
||
258 | * @param int $number Integer to be translated. |
||
259 | * |
||
260 | * @return string Translated number |
||
261 | */ |
||
262 | protected function _get_number_translation( $number ) { |
||
263 | if ( $number <= 9 ) { |
||
264 | return __( $number, self::TEXT_DOMAIN ); |
||
265 | } else { |
||
266 | $pieces = str_split( $number, 1 ); |
||
267 | $buffer = ''; |
||
268 | foreach ( $pieces as $piece ) { |
||
269 | $buffer .= __( $piece, self::TEXT_DOMAIN ); |
||
270 | } |
||
271 | |||
272 | return $buffer; |
||
273 | } |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Adds meta boxes. |
||
278 | * |
||
279 | * @param array $post_types Post types to show featured image box. |
||
280 | * @param object $featured Callback arguments. |
||
281 | * @param int $i Index of the featured image. |
||
282 | * |
||
283 | * @return void |
||
284 | */ |
||
285 | private function _dfi_add_meta_box( $post_types, $featured = null, $i = null ) { |
||
286 | if ( ! is_null( $i ) ) { |
||
287 | foreach ( $post_types as $type ) { |
||
288 | add_meta_box( |
||
289 | 'dfiFeaturedMetaBox-' . $i, |
||
290 | __( $this->metabox_title, self::TEXT_DOMAIN ) . ' ' . self::_get_number_translation( $i ), |
||
291 | array( $this, 'featured_meta_box' ), |
||
292 | $type, |
||
293 | 'side', |
||
294 | 'low', |
||
295 | array( $featured, $i + 1 ) |
||
296 | ); |
||
297 | add_filter( "postbox_classes_{$type}_dfiFeaturedMetaBox-" . $i, array( $this, 'add_metabox_classes' ) ); |
||
298 | } |
||
299 | } else { |
||
300 | foreach ( $post_types as $type ) { |
||
301 | add_meta_box( |
||
302 | 'dfiFeaturedMetaBox', |
||
303 | __( $this->metabox_title, self::TEXT_DOMAIN ) . ' ' . __( 2, self::TEXT_DOMAIN ), |
||
304 | array( $this, 'featured_meta_box' ), |
||
305 | $type, |
||
306 | 'side', |
||
307 | 'low', |
||
308 | array( null, null ) |
||
309 | ); |
||
310 | add_filter( "postbox_classes_{$type}_dfiFeaturedMetaBox", array( $this, 'add_metabox_classes' ) ); |
||
311 | } |
||
312 | } |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Separate thumb and full image url from given URL string. |
||
317 | * |
||
318 | * @since 3.3.1 |
||
319 | * |
||
320 | * @param string $url_string Url string. |
||
321 | * @param string $state Thumb or full. |
||
322 | * |
||
323 | * @return string|null |
||
324 | */ |
||
325 | private function _separate( $url_string, $state = 'thumb' ) { |
||
326 | $image_piece = explode( ',', $url_string ); |
||
327 | |||
328 | if ( 'thumb' === $state ) { |
||
329 | return isset( $image_piece[0] ) ? $image_piece[0] : null; |
||
330 | } |
||
331 | |||
332 | return isset( $image_piece[1] ) ? $image_piece[1] : null; |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * Create a nonce field |
||
337 | * |
||
338 | * @since 3.5.0 |
||
339 | * |
||
340 | * @see wp_nonce_field() |
||
341 | * @see plugin_basename() |
||
342 | * |
||
343 | * @codeCoverageIgnore |
||
344 | * |
||
345 | * @param string $key Nonce key. |
||
346 | * |
||
347 | * @return string |
||
348 | */ |
||
349 | protected function _nonce_field( $key ) { |
||
350 | return wp_nonce_field( plugin_basename( __FILE__ ), $key, true, false ); |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Featured meta box as seen in the admin |
||
355 | * |
||
356 | * @since 1.0.0 |
||
357 | * @access public |
||
358 | * |
||
359 | * @param object $post Global post object. |
||
360 | * @param array $featured Array containing featured image count. |
||
361 | * |
||
362 | * @throws Exception Medium size image not found. |
||
363 | * @return void |
||
364 | */ |
||
365 | public function featured_meta_box( $post, $featured ) { |
||
366 | $featured_img = $featured['args'][0]; |
||
367 | $featured_id = is_null( $featured['args'][1] ) ? 2 : --$featured['args'][1]; |
||
368 | $featured_img_full = $featured_img; |
||
369 | $featured_img_trimmed = $featured_img; |
||
370 | |||
371 | if ( ! is_null( $featured_img ) ) { |
||
372 | $featured_img_trimmed = self::_separate( $featured_img ); |
||
373 | $featured_img_full = self::_separate( $featured_img, 'full' ); |
||
374 | } |
||
375 | |||
376 | try { |
||
377 | $thumbnail = $this->get_image_thumb( $this->__upload_url . $featured_img_full, 'medium' ); |
||
378 | if ( is_null( $thumbnail ) ) { |
||
379 | // medium sized thumbnail image is missing. |
||
380 | throw new Exception( 'Medium size image not found', 1 ); |
||
381 | } |
||
382 | } catch ( Exception $e ) { |
||
383 | // since medium sized thumbnail image was not found, |
||
384 | // let's set full image url as thumbnail. |
||
385 | $thumbnail = $featured_img_full; |
||
386 | } |
||
387 | |||
388 | // Add a nonce field. |
||
389 | echo $this->_nonce_field( 'dfi_fimageplug-' . $featured_id ); |
||
390 | echo self::_get_featured_box( $featured_img_trimmed, $featured_img, $featured_id, $thumbnail, $post->ID ); |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * Returns featured box html content. |
||
395 | * |
||
396 | * @since 3.1.0 |
||
397 | * @access private |
||
398 | * |
||
399 | * @param string $featured_img_trimmed Medium sized image. |
||
400 | * @param string $featured_img Full sized image. |
||
401 | * @param string $featured_id Attachment Id. |
||
402 | * @param string $thumbnail Thumb sized image. |
||
403 | * @param int $post_id Post id. |
||
404 | * |
||
405 | * @return string Html content |
||
406 | */ |
||
407 | private function _get_featured_box( $featured_img_trimmed, $featured_img, $featured_id, $thumbnail, $post_id ) { |
||
408 | $has_featured_image = ! empty( $featured_img_trimmed ) ? 'hasFeaturedImage' : ''; |
||
409 | $thumbnail = ! is_null( $thumbnail ) ? $thumbnail : ''; |
||
410 | $dfi_empty = is_null( $featured_img_trimmed ) ? 'dfiImgEmpty' : ''; |
||
411 | |||
412 | return "<a href='javascript:void(0)' class='dfiFeaturedImage {$has_featured_image}' title='" . __( 'Set Featured Image', self::TEXT_DOMAIN ) . "' data-post-id='" . $post_id . "'><span class='dashicons dashicons-camera'></span></a><br/> |
||
413 | <img src='" . $thumbnail . "' class='dfiImg {$dfi_empty}'/> |
||
414 | <div class='dfiLinks'> |
||
415 | <a href='javascript:void(0)' data-id='{$featured_id}' data-id-local='" . $this->_get_number_translation( ( $featured_id + 1 ) ) . "' class='dfiAddNew dashicons dashicons-plus' title='" . __( 'Add New', self::TEXT_DOMAIN ) . "'></a> |
||
416 | <a href='javascript:void(0)' class='dfiRemove dashicons dashicons-minus' title='" . __( 'Remove', self::TEXT_DOMAIN ) . "'></a> |
||
417 | </div> |
||
418 | <div class='dfiClearFloat'></div> |
||
419 | <input type='hidden' name='dfiFeatured[]' value='{$featured_img}' class='dfiImageHolder' />"; |
||
420 | } |
||
421 | |||
422 | /** |
||
423 | * Load new featured meta box via ajax |
||
424 | * |
||
425 | * @since 1.0.0 |
||
426 | * @access public |
||
427 | * |
||
428 | * @return void |
||
429 | */ |
||
430 | public function ajax_callback() { |
||
431 | $featured_id = isset( $_POST['id'] ) ? (int) strip_tags( trim( $_POST['id'] ) ) : null; |
||
432 | |||
433 | if ( is_null( $featured_id ) ) { |
||
434 | return; |
||
435 | } |
||
436 | |||
437 | echo $this->_nonce_field( 'dfi_fimageplug-' . $featured_id ); |
||
438 | ?> |
||
439 | <a href="javascript:void(0)" class="dfiFeaturedImage" |
||
440 | title="<?php echo __( 'Set Featured Image', self::TEXT_DOMAIN ) ?>"><span |
||
441 | class="dashicons dashicons-camera"></span></a><br/> |
||
442 | <img src="" class="dfiImg dfiImgEmpty"/> |
||
443 | <div class="dfiLinks"> |
||
444 | <a href="javascript:void(0)" data-id="<?php echo $featured_id ?>" |
||
445 | data-id-local="<?php echo self::_get_number_translation( ( $featured_id + 1 ) ) ?>" |
||
446 | class="dfiAddNew dashicons dashicons-plus" title="<?php echo __( 'Add New', self::TEXT_DOMAIN ) ?>"></a> |
||
447 | <a href="javascript:void(0)" class="dfiRemove dashicons dashicons-minus" |
||
448 | title="<?php echo __( 'Remove', self::TEXT_DOMAIN ) ?>"></a> |
||
449 | </div> |
||
450 | <div class="dfiClearFloat"></div> |
||
451 | <input type="hidden" name="dfiFeatured[]" value="" class="dfiImageHolder"/> |
||
452 | <?php |
||
453 | wp_die( '' ); |
||
454 | } |
||
455 | |||
456 | /** |
||
457 | * Add custom class 'featured-meta-box' to meta box. |
||
458 | * |
||
459 | * @since 1.0.0 |
||
460 | * @access public |
||
461 | * |
||
462 | * @see add_metabox_classes |
||
463 | * |
||
464 | * @param array $classes Classes to add in the meta box. |
||
465 | * |
||
466 | * @return array |
||
467 | */ |
||
468 | public function add_metabox_classes( $classes ) { |
||
469 | array_push( $classes, 'featured-meta-box' ); |
||
470 | |||
471 | return $classes; |
||
472 | } |
||
473 | |||
474 | /** |
||
475 | * Add custom fields in media uploader. |
||
476 | * |
||
477 | * @since 3.4.0 |
||
478 | * |
||
479 | * @param array $form_fields Fields to include in media attachment form. |
||
480 | * @param array $post Post data. |
||
481 | * |
||
482 | * @return array |
||
483 | */ |
||
484 | public function media_attachment_custom_fields( $form_fields, $post ) { |
||
485 | $form_fields['dfi-link-to-image'] = array( |
||
486 | 'label' => __( 'Link to Image', self::TEXT_DOMAIN ), |
||
487 | 'input' => 'text', |
||
488 | 'value' => get_post_meta( $post->ID, '_dfi_link_to_image', true ), |
||
489 | ); |
||
490 | |||
491 | return $form_fields; |
||
492 | } |
||
493 | |||
494 | /** |
||
495 | * Save values of media uploader custom fields. |
||
496 | * |
||
497 | * @since 3.4.0 |
||
498 | * |
||
499 | * @param array $post Post data for database. |
||
500 | * @param array $attachment Attachment fields from $_POST form. |
||
501 | * |
||
502 | * @return array |
||
503 | */ |
||
504 | public function media_attachment_custom_fields_save( $post, $attachment ) { |
||
505 | if ( isset( $attachment['dfi-link-to-image'] ) ) { |
||
506 | update_post_meta( $post['ID'], '_dfi_link_to_image', $attachment['dfi-link-to-image'] ); |
||
507 | } |
||
508 | |||
509 | return $post; |
||
510 | } |
||
511 | |||
512 | /** |
||
513 | * Update featured images in the database. |
||
514 | * |
||
515 | * @since 1.0.0 |
||
516 | * @access public |
||
517 | * |
||
518 | * @see plugin_basename() |
||
519 | * @see update_post_meta() |
||
520 | * @see current_user_can() |
||
521 | * |
||
522 | * @param int $post_id Current post id. |
||
523 | * |
||
524 | * @return bool |
||
525 | */ |
||
526 | public function save_meta( $post_id ) { |
||
527 | // Check auto save. |
||
528 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
||
529 | return false; |
||
530 | } |
||
531 | |||
532 | if ( $this->_verify_nonces() ) { |
||
533 | // Check permission before saving data. |
||
534 | if ( current_user_can( 'edit_posts', $post_id ) && isset( $_POST['dfiFeatured'] ) ) { |
||
535 | update_post_meta( $post_id, 'dfiFeatured', $_POST['dfiFeatured'] ); |
||
536 | } |
||
537 | } |
||
538 | |||
539 | return false; |
||
540 | } |
||
541 | |||
542 | /** |
||
543 | * Verify metabox nonces. |
||
544 | * |
||
545 | * @access protected |
||
546 | * @see wp_verify_nonce() |
||
547 | * |
||
548 | * @return bool |
||
549 | */ |
||
550 | protected function _verify_nonces() { |
||
551 | $keys = array_keys( $_POST ); |
||
552 | foreach ( $keys as $key ) { |
||
553 | if ( preg_match( '/dfi_fimageplug-\d+$/', $key ) ) { |
||
554 | // Verify nonce. |
||
555 | if ( ! wp_verify_nonce( $_POST[ $key ], plugin_basename( __FILE__ ) ) ) { |
||
556 | return false; |
||
557 | } |
||
558 | } |
||
559 | } |
||
560 | |||
561 | return true; |
||
562 | } |
||
563 | |||
564 | /** |
||
565 | * Add update notice. Displayed in plugin update page. |
||
566 | * |
||
567 | * @since 2.0.0 |
||
568 | * @access public |
||
569 | * |
||
570 | * @return void |
||
571 | */ |
||
572 | public function update_notice() { |
||
573 | $info = __( 'ATTENTION! Please read the <a href="https://github.com/ankitpokhrel/Dynamic-Featured-Image/wiki" target="_blank">DOCUMENTATION</a> properly before update.', self::TEXT_DOMAIN ); |
||
574 | echo '<div style="color:red; padding:7px 0;">' . strip_tags( $info, '<a><b><i><span>' ) . '</div>'; |
||
575 | } |
||
576 | |||
577 | /** |
||
578 | * Execute query. |
||
579 | * |
||
580 | * @param string $query Query to execute. |
||
581 | * |
||
582 | * @return null|string |
||
583 | */ |
||
584 | private function execute_query( $query ) { |
||
585 | return $this->__db->get_var( $query ); |
||
586 | } |
||
587 | |||
588 | /** |
||
589 | * Get attachment id of the image by image url. |
||
590 | * |
||
591 | * @since 3.1.7 |
||
592 | * @access protected |
||
593 | * @global object $wpdb |
||
594 | * |
||
595 | * @param string $image_url URL of an image. |
||
596 | * |
||
597 | * @return string |
||
598 | */ |
||
599 | protected function _get_attachment_id( $image_url ) { |
||
600 | return self::execute_query( $this->__db->prepare( 'SELECT ID FROM ' . $this->__db->posts . ' WHERE guid = %s', $image_url ) ); |
||
601 | } |
||
602 | |||
603 | /** |
||
604 | * Get image url of the image by attachment id. |
||
605 | * |
||
606 | * @since 2.0.0 |
||
607 | * @access public |
||
608 | * |
||
609 | * @see wp_get_attachment_image_src() |
||
610 | * |
||
611 | * @param int $attachment_id attachment id of an image. |
||
612 | * @param string $size size of the image to fetch (thumbnail, medium, full). |
||
613 | * |
||
614 | * @return string |
||
615 | */ |
||
616 | public function get_image_url( $attachment_id, $size = 'full' ) { |
||
617 | $image_thumb = wp_get_attachment_image_src( $attachment_id, $size ); |
||
618 | |||
619 | return empty( $image_thumb ) ? null : $image_thumb[0]; |
||
620 | } |
||
621 | |||
622 | /** |
||
623 | * Get image thumbnail url of specific size by image url. |
||
624 | * |
||
625 | * @since 2.0.0 |
||
626 | * @access public |
||
627 | * |
||
628 | * @see get_image_id() |
||
629 | * @see wp_get_attachment_image_src() |
||
630 | * |
||
631 | * @param string $image_url url of an image. |
||
632 | * @param string $size size of the image to fetch (thumbnail, medium, full). |
||
633 | * |
||
634 | * @return string |
||
635 | */ |
||
636 | public function get_image_thumb( $image_url, $size = 'thumbnail' ) { |
||
637 | $attachment_id = $this->get_image_id( $image_url ); |
||
638 | $image_thumb = wp_get_attachment_image_src( $attachment_id, $size ); |
||
639 | |||
640 | return empty( $image_thumb ) ? null : $image_thumb[0]; |
||
641 | } |
||
642 | |||
643 | /** |
||
644 | * Gets attachment id from given image url. |
||
645 | * |
||
646 | * @param string $image_url url of an image. |
||
647 | * |
||
648 | * @since 2.0.0 |
||
649 | * @access public |
||
650 | * |
||
651 | * @return int|null attachment id of an image |
||
652 | */ |
||
653 | public function get_image_id( $image_url ) { |
||
654 | $attachment_id = $this->_get_attachment_id( $image_url ); |
||
655 | |||
656 | if ( is_null( $attachment_id ) ) { |
||
657 | // check if the image is edited image. |
||
658 | // and try to get the attachment id. |
||
659 | $image_url = str_replace( $this->__upload_url . '/', '', $image_url ); |
||
660 | $row = self::execute_query( $this->__db->prepare( 'SELECT post_id FROM ' . $this->__db->postmeta . ' WHERE meta_value = %s', $image_url ) ); |
||
661 | if ( ! is_null( $row ) ) { |
||
662 | $attachment_id = $row; |
||
663 | } |
||
664 | } |
||
665 | |||
666 | return $attachment_id; |
||
667 | } |
||
668 | |||
669 | /** |
||
670 | * Get image title. |
||
671 | * |
||
672 | * @since 2.0.0 |
||
673 | * @access public |
||
674 | * |
||
675 | * @param string $image_url URL of an image. |
||
676 | * |
||
677 | * @return string |
||
678 | */ |
||
679 | public function get_image_title( $image_url ) { |
||
682 | |||
683 | /** |
||
684 | * Get image title by id. |
||
685 | * |
||
686 | * @since 2.0.0 |
||
687 | * @access public |
||
688 | * |
||
689 | * @param int $attachment_id Attachment id of an image. |
||
690 | * |
||
691 | * @return string |
||
692 | */ |
||
693 | public function get_image_title_by_id( $attachment_id ) { |
||
696 | |||
697 | /** |
||
698 | * Get image caption. |
||
699 | * |
||
700 | * @since 2.0.0 |
||
701 | * @access public |
||
702 | * |
||
703 | * @param string $image_url URL of an image. |
||
704 | * |
||
705 | * @return string |
||
706 | */ |
||
707 | public function get_image_caption( $image_url ) { |
||
710 | |||
711 | /** |
||
712 | * Get image caption by id. |
||
713 | * |
||
714 | * @since 2.0.0 |
||
715 | * @access public |
||
716 | * |
||
717 | * @param int $attachment_id Attachment id of an image. |
||
718 | * |
||
719 | * @return string |
||
720 | */ |
||
721 | public function get_image_caption_by_id( $attachment_id ) { |
||
724 | |||
725 | /** |
||
726 | * Get image alternate text. |
||
727 | * |
||
728 | * @since 2.0.0 |
||
729 | * @access public |
||
730 | * |
||
731 | * @see get_post_meta() |
||
732 | * |
||
733 | * @param string $image_url URL of an image. |
||
734 | * |
||
735 | * @return string |
||
736 | */ |
||
737 | public function get_image_alt( $image_url ) { |
||
747 | |||
748 | /** |
||
749 | * Get image alternate text by attachment id. |
||
750 | * |
||
751 | * @since 2.0.0 |
||
752 | * @access public |
||
753 | * |
||
754 | * @see get_post_meta() |
||
755 | * |
||
756 | * @param int $attachment_id Attachment id of an image. |
||
757 | * |
||
758 | * @return string |
||
759 | */ |
||
760 | public function get_image_alt_by_id( $attachment_id ) { |
||
765 | |||
766 | /** |
||
767 | * Get image description. |
||
768 | * |
||
769 | * @since 3.0.0 |
||
770 | * @access public |
||
771 | * |
||
772 | * @param string $image_url URL of an image. |
||
773 | * |
||
774 | * @return string |
||
775 | */ |
||
776 | public function get_image_description( $image_url ) { |
||
779 | |||
780 | /** |
||
781 | * Get image description by id. |
||
782 | * |
||
783 | * @since 3.0.0 |
||
784 | * @access public |
||
785 | * |
||
786 | * @param int $attachment_id attachment id of an image. |
||
787 | * |
||
788 | * @return string |
||
789 | */ |
||
790 | public function get_image_description_by_id( $attachment_id ) { |
||
793 | |||
794 | /** |
||
795 | * Get link to image. |
||
796 | * |
||
797 | * @since 3.4.0 |
||
798 | * @access public |
||
799 | * |
||
800 | * @param int $attachment_id Attachment id of an image. |
||
801 | * |
||
802 | * @return string|null |
||
803 | */ |
||
804 | public function get_link_to_image( $attachment_id ) { |
||
807 | |||
808 | /** |
||
809 | * Get all attachment ids of the post. |
||
810 | * |
||
811 | * @since 2.0.0 |
||
812 | * @access public |
||
813 | * |
||
814 | * @see get_post_meta() |
||
815 | * |
||
816 | * @param int $post_id id of the current post. |
||
817 | * |
||
818 | * @return array |
||
819 | */ |
||
820 | public function get_post_attachment_ids( $post_id ) { |
||
833 | |||
834 | /** |
||
835 | * Fetches featured image data of nth position. |
||
836 | * |
||
837 | * @since 3.0.0 |
||
838 | * @access public |
||
839 | * |
||
840 | * @see get_featured_images() |
||
841 | * |
||
842 | * @param int $position Position of the featured image. |
||
843 | * @param int $post_id Current post id. |
||
844 | * |
||
845 | * @return array if found, null otherwise. |
||
846 | */ |
||
847 | public function get_nth_featured_image( $position, $post_id = null ) { |
||
857 | |||
858 | /** |
||
859 | * Check if the image is attached with the particular post. |
||
860 | * |
||
861 | * @since 2.0.0 |
||
862 | * @access public |
||
863 | * |
||
864 | * @see get_post_attachment_ids() |
||
865 | * |
||
866 | * @param int $attachment_id Attachment id of an image. |
||
867 | * @param int $post_id Current post id. |
||
868 | * |
||
869 | * @return bool |
||
870 | */ |
||
871 | public function is_attached( $attachment_id, $post_id ) { |
||
880 | |||
881 | /** |
||
882 | * Retrieve featured images for specific post(s). |
||
883 | * |
||
884 | * @since 2.0.0 |
||
885 | * @access public |
||
886 | * |
||
887 | * @see get_post_meta() |
||
888 | * |
||
889 | * @param integer $post_id id of the current post. |
||
890 | * |
||
891 | * @return array |
||
892 | */ |
||
893 | public function get_featured_images( $post_id = null ) { |
||
894 | if ( is_null( $post_id ) ) { |
||
895 | global $post; |
||
896 | |||
897 | $post_id = $post->ID; |
||
898 | } |
||
899 | |||
900 | $dfi_images = get_post_meta( $post_id, 'dfiFeatured', true ); |
||
901 | $ret_images = array(); |
||
902 | |||
926 | |||
927 | /** |
||
928 | * Check to see if the upload url is already available in path. |
||
929 | * |
||
930 | * @since 3.1.14 |
||
931 | * @access protected |
||
932 | * |
||
933 | * @param string $img Uploaded image. |
||
934 | * |
||
935 | * @return string |
||
936 | */ |
||
937 | protected function _get_real_upload_path( $img ) { |
||
945 | |||
946 | /** |
||
947 | * Retrieve featured images for specific post(s) including the default Featured Image. |
||
948 | * |
||
949 | * @since 3.1.7 |
||
950 | * @access public |
||
951 | * |
||
952 | * @see $this->get_featured_images() |
||
953 | * |
||
954 | * @param int $post_id Current post id. |
||
955 | * |
||
956 | * @return array An array of images or an empty array on failure |
||
957 | */ |
||
958 | public function get_all_featured_images( $post_id = null ) { |
||
980 | |||
981 | /** |
||
982 | * Load the plugin's textdomain hooked to 'plugins_loaded'. |
||
983 | * |
||
984 | * @since 1.0.0 |
||
985 | * @access public |
||
986 | * |
||
987 | * @see load_plugin_textdomain() |
||
988 | * @see plugin_basename() |
||
989 | * @action plugins_loaded |
||
990 | * |
||
991 | * @codeCoverageIgnore |
||
992 | * |
||
993 | * @return void |
||
994 | */ |
||
995 | public function load_plugin_textdomain() { |
||
1002 | } |
||
1003 | |||
1014 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: