Advanced_Ads_Modal.php
3 years ago
EDD_SL_Plugin_Updater.php
4 years ago
ad-ajax.php
3 years ago
ad-debug.php
3 years ago
ad-expiration.php
3 years ago
ad-health-notices.php
3 years ago
ad-model.php
3 years ago
ad-select.php
3 years ago
ad.php
3 years ago
ad_ajax_callbacks.php
3 years ago
ad_group.php
3 years ago
ad_placements.php
3 years ago
ad_type_abstract.php
3 years ago
ad_type_content.php
3 years ago
ad_type_dummy.php
3 years ago
ad_type_group.php
3 years ago
ad_type_image.php
3 years ago
ad_type_plain.php
3 years ago
checks.php
3 years ago
compatibility.php
3 years ago
display-conditions.php
3 years ago
filesystem.php
3 years ago
frontend-notices.php
3 years ago
frontend_checks.php
3 years ago
in-content-injector.php
3 years ago
inline-css.php
3 years ago
plugin.php
3 years ago
upgrades.php
6 years ago
utils.php
3 years ago
visitor-conditions.php
3 years ago
widget.php
3 years ago
ad_type_image.php
260 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Advanced Ads Image Ad Type |
| 4 | * |
| 5 | * @package Advanced_Ads |
| 6 | * @author Thomas Maier <support@wpadvancedads.com> |
| 7 | * @license GPL-2.0+ |
| 8 | * @link https://wpadvancedads.com |
| 9 | * @copyright 2015 Thomas Maier, Advanced Ads GmbH |
| 10 | * |
| 11 | * Class containing information about the content ad type |
| 12 | * this should also work as an example for other ad types |
| 13 | */ |
| 14 | class Advanced_Ads_Ad_Type_Image extends Advanced_Ads_Ad_Type_Abstract { |
| 15 | |
| 16 | /** |
| 17 | * ID - internal type of the ad type |
| 18 | * must be static so set your own ad type ID here |
| 19 | * use slug like format, only lower case, underscores and hyphens |
| 20 | * |
| 21 | * @var string $ID ad type ID. |
| 22 | */ |
| 23 | public $ID = 'image'; |
| 24 | |
| 25 | /** |
| 26 | * Set basic attributes |
| 27 | */ |
| 28 | public function __construct() { |
| 29 | $this->title = __( 'Image Ad', 'advanced-ads' ); |
| 30 | $this->description = __( 'Ads in various image formats.', 'advanced-ads' ); |
| 31 | $this->parameters = [ |
| 32 | 'image_url' => '', |
| 33 | 'image_title' => '', |
| 34 | 'image_alt' => '', |
| 35 | ]; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Output for the ad parameters metabox |
| 40 | * |
| 41 | * @param Advanced_Ads_Ad $ad ad object. |
| 42 | * |
| 43 | * @return void |
| 44 | */ |
| 45 | public function render_parameters( Advanced_Ads_Ad $ad ) { |
| 46 | $id = isset( $ad->output['image_id'] ) ? $ad->output['image_id'] : ''; |
| 47 | $url = isset( $ad->url ) ? esc_attr( $ad->url ) : ''; |
| 48 | $edit_link = $id ? get_edit_post_link( $id ) : ''; |
| 49 | |
| 50 | ?><span class="label"> |
| 51 | <button href="#" class="advads_image_upload button advads-button-secondary" type="button" |
| 52 | data-uploader-title="<?php esc_attr_e( 'Insert File', 'advanced-ads' ); ?>" |
| 53 | data-uploader-button-text="<?php esc_attr_e( 'Insert', 'advanced-ads' ); ?>" |
| 54 | onclick="return false;"> |
| 55 | <?php esc_html_e( 'Select image', 'advanced-ads' ); ?> |
| 56 | </button> |
| 57 | </span> |
| 58 | <div> |
| 59 | <input type="hidden" name="advanced_ad[output][image_id]" value="<?php echo absint( $id ); ?>" id="advads-image-id"/> |
| 60 | <div id="advads-image-preview"> |
| 61 | <?php $this->create_image_tag( $id, $ad ); ?> |
| 62 | </div> |
| 63 | <a id="advads-image-edit-link" class="<?php echo ! $edit_link ? 'hidden' : ''; ?>" href="<?php echo esc_url( $edit_link ); ?>"><span class="dashicons dashicons-edit"></span></a> |
| 64 | </div> |
| 65 | <hr/> |
| 66 | <?php |
| 67 | // don’t show if tracking plugin enabled |
| 68 | if ( ! defined( 'AAT_VERSION' ) ) : |
| 69 | ?> |
| 70 | <label for="advads-url" class="label"><?php esc_html_e( 'URL', 'advanced-ads' ); ?></label> |
| 71 | <div> |
| 72 | <input type="url" name="advanced_ad[url]" id="advads-url" class="advads-ad-url" value="<?php echo esc_url( $url ); ?>" placeholder="https://www.example.com/"/> |
| 73 | <p class="description"> |
| 74 | <?php esc_html_e( 'Link to target site including http(s)', 'advanced-ads' ); ?> |
| 75 | </p> |
| 76 | </div> |
| 77 | <hr/> |
| 78 | <?php |
| 79 | endif; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Render image tag |
| 84 | * |
| 85 | * @param int $attachment_id post id of the image. |
| 86 | * @param Advanced_Ads_Ad $ad ad object. |
| 87 | */ |
| 88 | public function create_image_tag( $attachment_id, $ad ) { |
| 89 | $image = wp_get_attachment_image_src( $attachment_id, 'full' ); |
| 90 | $style = ''; |
| 91 | |
| 92 | // if we don't have an image, bail early. |
| 93 | if ( ! $image ) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | list( $src, $width, $height ) = $image; |
| 98 | // override image sizes with the sizes given in ad options, but in frontend only |
| 99 | if ( |
| 100 | ! is_admin() |
| 101 | || wp_doing_ajax() |
| 102 | ) { |
| 103 | $width = isset( $ad->width ) ? absint( $ad->width ) : $width; |
| 104 | $height = isset( $ad->height ) ? absint( $ad->height ) : $height; |
| 105 | } |
| 106 | $hwstring = image_hwstring( $width, $height ); |
| 107 | $alt = trim( esc_textarea( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ); |
| 108 | |
| 109 | global $wp_current_filter; |
| 110 | |
| 111 | // TODO: use an array for attributes so they are simpler to extend |
| 112 | $sizes = ''; |
| 113 | $srcset = ''; |
| 114 | $more_attributes = $srcset; |
| 115 | // create srcset and sizes attributes if we are in the the_content filter and in WordPress 4.4 |
| 116 | if ( |
| 117 | isset( $wp_current_filter ) |
| 118 | && in_array( 'the_content', $wp_current_filter, true ) |
| 119 | && ! defined( 'ADVADS_DISABLE_RESPONSIVE_IMAGES' ) |
| 120 | ) { |
| 121 | if ( function_exists( 'wp_get_attachment_image_srcset' ) ) { |
| 122 | $srcset = wp_get_attachment_image_srcset( $attachment_id, 'full' ); |
| 123 | } |
| 124 | if ( function_exists( 'wp_get_attachment_image_sizes' ) ) { |
| 125 | $sizes = wp_get_attachment_image_sizes( $attachment_id, 'full' ); |
| 126 | } |
| 127 | if ( $srcset && $sizes ) { |
| 128 | $more_attributes .= ' srcset="' . $srcset . '" sizes="' . $sizes . '"'; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // TODO: move to classes/compabtility.php when we have a simpler filter for additional attributes |
| 133 | // compabitility with WP Smush. Disables their lazy load for image ads because it caused them to not show up in certain positions at all. |
| 134 | $wp_smush_settings = get_option( 'wp-smush-settings' ); |
| 135 | if ( isset( $wp_smush_settings['lazy_load'] ) && $wp_smush_settings['lazy_load'] ) { |
| 136 | // Lazy load is enabled. |
| 137 | $more_attributes .= ' class="no-lazyload"'; |
| 138 | } |
| 139 | |
| 140 | // add css rule to be able to center the ad. |
| 141 | if ( isset( $ad->output['position'] ) && strpos( $ad->output['position'], 'center' ) === 0 ) { |
| 142 | $style .= 'display: inline-block;'; |
| 143 | } |
| 144 | |
| 145 | $style = apply_filters( 'advanced-ads-ad-image-tag-style', $style ); |
| 146 | $style = '' !== $style ? 'style="' . $style . '"' : ''; |
| 147 | |
| 148 | $more_attributes = apply_filters( 'advanced-ads-ad-image-tag-attributes', $more_attributes ); |
| 149 | $more_attributes .= ' ' . $hwstring . ' ' . $style; |
| 150 | $img = sprintf( '<img src="%s" alt="%s" %s />', esc_url( $src ), esc_attr( $alt ), $more_attributes ); |
| 151 | |
| 152 | // Add 'loading' attribute if applicable, available from WP 5.5. |
| 153 | if ( |
| 154 | $wp_current_filter |
| 155 | && function_exists( 'wp_lazy_loading_enabled' ) |
| 156 | && wp_lazy_loading_enabled( 'img', $wp_current_filter ) |
| 157 | && ! strpos( $more_attributes, 'loading=' ) |
| 158 | ) { |
| 159 | $img = wp_img_tag_add_loading_attr( $img, $wp_current_filter ); |
| 160 | } |
| 161 | |
| 162 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- use unescaped image tag here |
| 163 | echo $img; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Render preview on the ad overview list |
| 168 | * |
| 169 | * @param Advanced_Ads_Ad $ad ad object. |
| 170 | */ |
| 171 | public function render_preview( Advanced_Ads_Ad $ad ) { |
| 172 | if ( empty( $ad->output['image_id'] ) ) { |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | list( $src, $width, $height ) = wp_get_attachment_image_src( $ad->output['image_id'], 'medium', true ); |
| 177 | $preview_size_small = 50; |
| 178 | $preview_size_large = 200; |
| 179 | |
| 180 | // scale down width or height for the preview |
| 181 | if ( $width > $height ) { |
| 182 | $preview_height = ceil( $height / ( $width / $preview_size_small ) ); |
| 183 | $preview_width = $preview_size_small; |
| 184 | $tooltip_height = ceil( $height / ( $width / $preview_size_large ) ); |
| 185 | $tooltip_width = $preview_size_large; |
| 186 | } else { |
| 187 | $preview_width = ceil( $width / ( $height / $preview_size_small ) ); |
| 188 | $preview_height = $preview_size_small; |
| 189 | $tooltip_width = ceil( $width / ( $height / $preview_size_large ) ); |
| 190 | $tooltip_height = $preview_size_large; |
| 191 | } |
| 192 | |
| 193 | $preview_hwstring = image_hwstring( $preview_width, $preview_height ); |
| 194 | $tooltip_hwstring = image_hwstring( $tooltip_width, $tooltip_height ); |
| 195 | $alt = wp_strip_all_tags( get_post_meta( $ad->output['image_id'], '_wp_attachment_image_alt', true ) ); |
| 196 | |
| 197 | include ADVADS_BASE_PATH . 'admin/views/ad-list/preview-image.php'; |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Prepare the ads frontend output by adding <object> tags |
| 202 | * |
| 203 | * @param Advanced_Ads_Ad $ad ad object. |
| 204 | * @return string $content ad content prepared for frontend output |
| 205 | */ |
| 206 | public function prepare_output( $ad ) { |
| 207 | $id = ( isset( $ad->output['image_id'] ) ) ? absint( $ad->output['image_id'] ) : ''; |
| 208 | $url = ( isset( $ad->url ) ) ? esc_url( $ad->url ) : ''; |
| 209 | |
| 210 | ob_start(); |
| 211 | $this->create_image_tag( $id, $ad ); |
| 212 | $img = ob_get_clean(); |
| 213 | if ( ! defined( 'AAT_VERSION' ) && $url ) { |
| 214 | // get general target setting |
| 215 | $options = Advanced_Ads::get_instance()->options(); |
| 216 | $target_blank = ! empty( $options['target-blank'] ) ? ' target="_blank"' : ''; |
| 217 | $img = sprintf( '<a href="%s"%s>%s</a>', esc_url( $url ), $target_blank, $img ); |
| 218 | } |
| 219 | |
| 220 | return $img; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Generate a string with the original image size for output in the backend |
| 225 | * Only show, if different from entered image sizes |
| 226 | * |
| 227 | * @param Advanced_Ads_Ad $ad ad object. |
| 228 | * @return string empty, if the entered size is the same as the original size |
| 229 | */ |
| 230 | public static function show_original_image_size( Advanced_Ads_Ad $ad ) { |
| 231 | $attachment_id = ( isset( $ad->output['image_id'] ) ) ? absint( $ad->output['image_id'] ) : ''; |
| 232 | |
| 233 | $image = wp_get_attachment_image_src( $attachment_id, 'full' ); |
| 234 | |
| 235 | if ( $image ) { |
| 236 | list( $src, $width, $height ) = $image; |
| 237 | ?> |
| 238 | <p class="description"> |
| 239 | <?php |
| 240 | if ( ( isset( $ad->width ) && $ad->width !== $width ) |
| 241 | || ( isset( $ad->height ) && $ad->height !== $height ) ) { |
| 242 | printf( |
| 243 | /** |
| 244 | * This string shows up on the ad edit page of image ads if the size entered for the ad is different from the size of the uploaded image. |
| 245 | */ |
| 246 | // translators: $s is a size string like "728 x 90". |
| 247 | esc_attr__( 'Original size: %s', 'advanced-ads' ), |
| 248 | esc_html( $width ) . ' x ' . esc_html( $height ) |
| 249 | ); |
| 250 | ?> |
| 251 | </p> |
| 252 | <?php |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | return ''; |
| 257 | } |
| 258 | |
| 259 | } |
| 260 |