types
10 months ago
class-ad-content.php
1 week ago
class-ad-dummy.php
1 year ago
class-ad-factory.php
1 day ago
class-ad-group-relation.php
1 year ago
class-ad-group.php
5 months ago
class-ad-image.php
1 year ago
class-ad-plain.php
1 week ago
class-ad-repository.php
1 day ago
class-ad-types.php
1 day ago
class-ads.php
1 day ago
class-ad-image.php
89 lines
| 1 | <?php |
| 2 | /** |
| 3 | * This class is responsible to model image ads. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.48.0 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Ads; |
| 11 | |
| 12 | use Advanced_Ads; |
| 13 | use AdvancedAds\Abstracts\Ad; |
| 14 | use AdvancedAds\Interfaces\Ad_Interface; |
| 15 | |
| 16 | defined( 'ABSPATH' ) || exit; |
| 17 | |
| 18 | /** |
| 19 | * Image ad. |
| 20 | */ |
| 21 | class Ad_Image extends Ad implements Ad_Interface { |
| 22 | |
| 23 | /** |
| 24 | * Get the image id for the ad. |
| 25 | * |
| 26 | * @param string $context What the value is for. Valid values are view and edit. |
| 27 | * |
| 28 | * @return int |
| 29 | */ |
| 30 | public function get_image_id( $context = 'view' ): int { |
| 31 | return $this->get_prop( 'image_id', $context ) ?? 0; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Set the image id. |
| 36 | * |
| 37 | * @param int|string $image_id Image id for ad. |
| 38 | */ |
| 39 | public function set_image_id( $image_id ) { |
| 40 | $this->set_prop( 'image_id', absint( $image_id ) ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Prepare output for frontend. |
| 45 | * |
| 46 | * @return string |
| 47 | */ |
| 48 | public function prepare_frontend_output(): string { |
| 49 | $url = $this->get_url(); |
| 50 | $image_id = $this->get_image_id(); |
| 51 | |
| 52 | ob_start(); |
| 53 | $this->get_type_object()->create_image_tag( $image_id, $this ); |
| 54 | $img = ob_get_clean(); |
| 55 | |
| 56 | if ( ! defined( 'AAT_VERSION' ) && $url ) { |
| 57 | $alt = trim( esc_textarea( get_post_meta( $image_id, '_wp_attachment_image_alt', true ) ) ); |
| 58 | $aria_label = ! empty( $alt ) ? $alt : wp_basename( get_the_title( $image_id ) ); |
| 59 | $options = Advanced_Ads::get_instance()->options(); |
| 60 | $target_blank = ! empty( $options['target-blank'] ) ? ' target="_blank"' : ''; |
| 61 | $img = sprintf( '<a href="%s"%s aria-label="%s">%s</a>', esc_url( $url ), $target_blank, $aria_label, $img ); |
| 62 | } |
| 63 | |
| 64 | return $img; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Pre save |
| 69 | * |
| 70 | * @param array $post_data Post data. |
| 71 | * |
| 72 | * @return void |
| 73 | */ |
| 74 | public function pre_save( $post_data ): void { |
| 75 | $image_id = absint( $post_data['image_id'] ?? 0 ); |
| 76 | if ( $image_id ) { |
| 77 | $attachment = get_post( $image_id ); |
| 78 | if ( $attachment && 0 === $attachment->post_parent ) { |
| 79 | wp_update_post( |
| 80 | [ |
| 81 | 'ID' => $image_id, |
| 82 | 'post_parent' => $this->get_id(), |
| 83 | ] |
| 84 | ); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 |