Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-conte.../plugins/advanced.../classes
File: in-content-injector.php
<?php
[0] Fix | Delete
[1] Fix | Delete
use AdvancedAds\Utilities\WordPress;
[2] Fix | Delete
[3] Fix | Delete
/**
[4] Fix | Delete
* Injects ads in the content based on an XPath expression.
[5] Fix | Delete
*/
[6] Fix | Delete
class Advanced_Ads_In_Content_Injector {
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* Gather placeholders which later are replaced by the ads
[10] Fix | Delete
*
[11] Fix | Delete
* @var array $ads_for_placeholders
[12] Fix | Delete
*/
[13] Fix | Delete
private static $ads_for_placeholders = [];
[14] Fix | Delete
[15] Fix | Delete
/**
[16] Fix | Delete
* Inject ads directly into the content
[17] Fix | Delete
*
[18] Fix | Delete
* @param string $placement_id Id of the placement.
[19] Fix | Delete
* @param array $placement_opts Placement options.
[20] Fix | Delete
* @param string $content Content to inject placement into.
[21] Fix | Delete
* @param array $options {
[22] Fix | Delete
* Injection options.
[23] Fix | Delete
*
[24] Fix | Delete
* @type bool $allowEmpty Whether the tag can be empty to be counted.
[25] Fix | Delete
* @type bool $paragraph_select_from_bottom Whether to select ads from buttom.
[26] Fix | Delete
* @type string $position Position. Can be one of 'before', 'after', 'append', 'prepend'
[27] Fix | Delete
* @type number $alter_nodes Whether to alter nodes, for example to prevent injecting ads into `a` tags.
[28] Fix | Delete
* @type bool $repeat Whether to repeat the position.
[29] Fix | Delete
* @type number $paragraph_id Paragraph Id.
[30] Fix | Delete
* @type number $itemLimit If there are too few items at this level test nesting. Set to '-1` to prevent testing.
[31] Fix | Delete
* }
[32] Fix | Delete
*
[33] Fix | Delete
* @return string $content Content with injected placement.
[34] Fix | Delete
*/
[35] Fix | Delete
public static function &inject_in_content( $placement_id, $placement_opts, &$content, $options = [] ) {
[36] Fix | Delete
if ( ! extension_loaded( 'dom' ) ) {
[37] Fix | Delete
return $content;
[38] Fix | Delete
}
[39] Fix | Delete
[40] Fix | Delete
// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
[41] Fix | Delete
// phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
[42] Fix | Delete
[43] Fix | Delete
// parse arguments.
[44] Fix | Delete
$tag = isset( $placement_opts['tag'] ) ? $placement_opts['tag'] : 'p';
[45] Fix | Delete
$tag = preg_replace( '/[^a-z0-9]/i', '', $tag ); // simplify tag.
[46] Fix | Delete
/**
[47] Fix | Delete
* Store the original tag value since $tag is changed on the fly and we might want to know the original selected
[48] Fix | Delete
* options for some checks later.
[49] Fix | Delete
*/
[50] Fix | Delete
$tag_option = $tag;
[51] Fix | Delete
[52] Fix | Delete
// allow more complex xPath expression.
[53] Fix | Delete
$tag = apply_filters( 'advanced-ads-placement-content-injection-xpath', $tag, $placement_opts );
[54] Fix | Delete
[55] Fix | Delete
// get plugin options.
[56] Fix | Delete
$plugin_options = Advanced_Ads::get_instance()->options();
[57] Fix | Delete
[58] Fix | Delete
$defaults = [
[59] Fix | Delete
'allowEmpty' => false,
[60] Fix | Delete
'paragraph_select_from_bottom' => isset( $placement_opts['start_from_bottom'] ) && $placement_opts['start_from_bottom'],
[61] Fix | Delete
'position' => isset( $placement_opts['position'] ) ? $placement_opts['position'] : 'after',
[62] Fix | Delete
// only has before and after.
[63] Fix | Delete
'before' => isset( $placement_opts['position'] ) && 'before' === $placement_opts['position'],
[64] Fix | Delete
// Whether to alter nodes, for example to prevent injecting ads into `a` tags.
[65] Fix | Delete
'alter_nodes' => true,
[66] Fix | Delete
'repeat' => false,
[67] Fix | Delete
];
[68] Fix | Delete
[69] Fix | Delete
$defaults['paragraph_id'] = isset( $placement_opts['index'] ) ? $placement_opts['index'] : 1;
[70] Fix | Delete
$defaults['paragraph_id'] = max( 1, (int) $defaults['paragraph_id'] );
[71] Fix | Delete
[72] Fix | Delete
// if there are too few items at this level test nesting.
[73] Fix | Delete
$defaults['itemLimit'] = 'p' === $tag_option ? 2 : 1;
[74] Fix | Delete
[75] Fix | Delete
// trigger such a high item limit that all elements will be considered.
[76] Fix | Delete
if ( ! empty( $plugin_options['content-injection-level-disabled'] ) ) {
[77] Fix | Delete
$defaults['itemLimit'] = 1000;
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
// handle tags that are empty by definition or could be empty ("custom" option)
[81] Fix | Delete
if ( in_array( $tag_option, [ 'img', 'iframe', 'custom' ], true ) ) {
[82] Fix | Delete
$defaults['allowEmpty'] = true;
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
// allow hooks to change some options.
[86] Fix | Delete
$options = apply_filters(
[87] Fix | Delete
'advanced-ads-placement-content-injection-options',
[88] Fix | Delete
wp_parse_args( $options, $defaults ),
[89] Fix | Delete
$tag_option
[90] Fix | Delete
);
[91] Fix | Delete
[92] Fix | Delete
$wp_charset = get_bloginfo( 'charset' );
[93] Fix | Delete
// parse document as DOM (fragment - having only a part of an actual post given).
[94] Fix | Delete
[95] Fix | Delete
$content_to_load = self::get_content_to_load( $content, $wp_charset );
[96] Fix | Delete
if ( ! $content_to_load ) {
[97] Fix | Delete
return $content;
[98] Fix | Delete
}
[99] Fix | Delete
[100] Fix | Delete
$dom = new DOMDocument( '1.0', $wp_charset );
[101] Fix | Delete
// may loose some fragments or add autop-like code.
[102] Fix | Delete
$libxml_use_internal_errors = libxml_use_internal_errors( true ); // avoid notices and warnings - html is most likely malformed.
[103] Fix | Delete
[104] Fix | Delete
$success = $dom->loadHtml( '<!DOCTYPE html><html><meta http-equiv="Content-Type" content="text/html; charset=' . $wp_charset . '" /><body>' . $content_to_load );
[105] Fix | Delete
libxml_use_internal_errors( $libxml_use_internal_errors );
[106] Fix | Delete
if ( true !== $success ) {
[107] Fix | Delete
// -TODO handle cases were dom-parsing failed (at least inform user)
[108] Fix | Delete
return $content;
[109] Fix | Delete
}
[110] Fix | Delete
[111] Fix | Delete
/**
[112] Fix | Delete
* Handle advanced tags.
[113] Fix | Delete
*/
[114] Fix | Delete
switch ( $tag_option ) {
[115] Fix | Delete
case 'p':
[116] Fix | Delete
// exclude paragraphs within blockquote tags
[117] Fix | Delete
$tag = 'p[not(parent::blockquote)]';
[118] Fix | Delete
break;
[119] Fix | Delete
case 'pwithoutimg':
[120] Fix | Delete
// convert option name into correct path, exclude paragraphs within blockquote tags
[121] Fix | Delete
$tag = 'p[not(descendant::img) and not(parent::blockquote)]';
[122] Fix | Delete
break;
[123] Fix | Delete
case 'img':
[124] Fix | Delete
/*
[125] Fix | Delete
* Handle: 1) "img" tags 2) "image" block 3) "gallery" block 4) "gallery shortcode" 5) "wp_caption" shortcode
[126] Fix | Delete
* Handle the gallery created by the block or the shortcode as one image.
[127] Fix | Delete
* Prevent injection of ads next to images in tables.
[128] Fix | Delete
*/
[129] Fix | Delete
// Default shortcodes, including non-HTML5 versions.
[130] Fix | Delete
$shortcodes = "@class and (
[131] Fix | Delete
contains(concat(' ', normalize-space(@class), ' '), ' gallery-size') or
[132] Fix | Delete
contains(concat(' ', normalize-space(@class), ' '), ' wp-caption ') )";
[133] Fix | Delete
$tag = "*[self::img or self::figure or self::div[$shortcodes]]
[134] Fix | Delete
[not(ancestor::table or ancestor::figure or ancestor::div[$shortcodes])]";
[135] Fix | Delete
break;
[136] Fix | Delete
// any headline. By default h2, h3, and h4
[137] Fix | Delete
case 'headlines':
[138] Fix | Delete
$headlines = apply_filters( 'advanced-ads-headlines-for-ad-injection', [ 'h2', 'h3', 'h4' ] );
[139] Fix | Delete
[140] Fix | Delete
foreach ( $headlines as &$headline ) {
[141] Fix | Delete
$headline = 'self::' . $headline;
[142] Fix | Delete
}
[143] Fix | Delete
$tag = '*[' . implode( ' or ', $headlines ) . ']'; // /html/body/*[self::h2 or self::h3 or self::h4]
[144] Fix | Delete
break;
[145] Fix | Delete
// any HTML element that makes sense in the content
[146] Fix | Delete
case 'anyelement':
[147] Fix | Delete
$exclude = [
[148] Fix | Delete
'html',
[149] Fix | Delete
'body',
[150] Fix | Delete
'script',
[151] Fix | Delete
'style',
[152] Fix | Delete
'tr',
[153] Fix | Delete
'td',
[154] Fix | Delete
// Inline tags.
[155] Fix | Delete
'a',
[156] Fix | Delete
'abbr',
[157] Fix | Delete
'b',
[158] Fix | Delete
'bdo',
[159] Fix | Delete
'br',
[160] Fix | Delete
'button',
[161] Fix | Delete
'cite',
[162] Fix | Delete
'code',
[163] Fix | Delete
'dfn',
[164] Fix | Delete
'em',
[165] Fix | Delete
'i',
[166] Fix | Delete
'img',
[167] Fix | Delete
'kbd',
[168] Fix | Delete
'label',
[169] Fix | Delete
'option',
[170] Fix | Delete
'q',
[171] Fix | Delete
'samp',
[172] Fix | Delete
'select',
[173] Fix | Delete
'small',
[174] Fix | Delete
'span',
[175] Fix | Delete
'strong',
[176] Fix | Delete
'sub',
[177] Fix | Delete
'sup',
[178] Fix | Delete
'textarea',
[179] Fix | Delete
'time',
[180] Fix | Delete
'tt',
[181] Fix | Delete
'var',
[182] Fix | Delete
];
[183] Fix | Delete
$tag = '*[not(self::' . implode( ' or self::', $exclude ) . ')]';
[184] Fix | Delete
break;
[185] Fix | Delete
case 'custom':
[186] Fix | Delete
// get the path for the "custom" tag choice, use p as a fallback to prevent it from showing any ads if users left it empty
[187] Fix | Delete
$tag = ! empty( $placement_opts['xpath'] ) ? stripslashes( $placement_opts['xpath'] ) : 'p';
[188] Fix | Delete
break;
[189] Fix | Delete
}
[190] Fix | Delete
[191] Fix | Delete
// select positions.
[192] Fix | Delete
$xpath = new DOMXPath( $dom );
[193] Fix | Delete
[194] Fix | Delete
[195] Fix | Delete
if ( $options['itemLimit'] !== -1 ) {
[196] Fix | Delete
$items = $xpath->query( '/html/body/' . $tag );
[197] Fix | Delete
[198] Fix | Delete
if ( $items->length < $options['itemLimit'] ) {
[199] Fix | Delete
$items = $xpath->query( '/html/body/*/' . $tag );
[200] Fix | Delete
}
[201] Fix | Delete
// try third level.
[202] Fix | Delete
if ( $items->length < $options['itemLimit'] ) {
[203] Fix | Delete
$items = $xpath->query( '/html/body/*/*/' . $tag );
[204] Fix | Delete
}
[205] Fix | Delete
// try all levels as last resort.
[206] Fix | Delete
if ( $items->length < $options['itemLimit'] ) {
[207] Fix | Delete
$items = $xpath->query( '//' . $tag );
[208] Fix | Delete
}
[209] Fix | Delete
} else {
[210] Fix | Delete
$items = $xpath->query( $tag );
[211] Fix | Delete
}
[212] Fix | Delete
[213] Fix | Delete
// allow to select other elements.
[214] Fix | Delete
$items = apply_filters( 'advanced-ads-placement-content-injection-items', $items, $xpath, $tag_option );
[215] Fix | Delete
[216] Fix | Delete
// filter empty tags from items.
[217] Fix | Delete
$whitespaces = json_decode( '"\t\n\r \u00A0"' );
[218] Fix | Delete
$paragraphs = [];
[219] Fix | Delete
foreach ( $items as $item ) {
[220] Fix | Delete
if ( $options['allowEmpty'] || ( isset( $item->textContent ) && trim( $item->textContent, $whitespaces ) !== '' ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar
[221] Fix | Delete
$paragraphs[] = $item;
[222] Fix | Delete
}
[223] Fix | Delete
}
[224] Fix | Delete
[225] Fix | Delete
$ancestors_to_limit = self::get_ancestors_to_limit( $xpath );
[226] Fix | Delete
$paragraphs = self::filter_by_ancestors_to_limit( $paragraphs, $ancestors_to_limit );
[227] Fix | Delete
[228] Fix | Delete
$options['paragraph_count'] = count( $paragraphs );
[229] Fix | Delete
[230] Fix | Delete
if ( $options['paragraph_count'] >= $options['paragraph_id'] ) {
[231] Fix | Delete
$offset = $options['paragraph_select_from_bottom'] ? $options['paragraph_count'] - $options['paragraph_id'] : $options['paragraph_id'] - 1;
[232] Fix | Delete
$offsets = apply_filters( 'advanced-ads-placement-content-offsets', [ $offset ], $options, $placement_opts, $xpath, $paragraphs, $dom );
[233] Fix | Delete
$did_inject = false;
[234] Fix | Delete
[235] Fix | Delete
foreach ( $offsets as $offset ) {
[236] Fix | Delete
[237] Fix | Delete
// inject.
[238] Fix | Delete
$node = apply_filters( 'advanced-ads-placement-content-injection-node', $paragraphs[ $offset ], $tag, $options['before'] );
[239] Fix | Delete
[240] Fix | Delete
if ( $options['alter_nodes'] ) {
[241] Fix | Delete
// Prevent injection into image caption and gallery.
[242] Fix | Delete
$parent = $node;
[243] Fix | Delete
for ( $i = 0; $i < 4; $i++ ) {
[244] Fix | Delete
$parent = $parent->parentNode;
[245] Fix | Delete
if ( ! $parent instanceof DOMElement ) {
[246] Fix | Delete
break;
[247] Fix | Delete
}
[248] Fix | Delete
if ( preg_match( '/\b(wp-caption|gallery-size)\b/', $parent->getAttribute( 'class' ) ) ) {
[249] Fix | Delete
$node = $parent;
[250] Fix | Delete
break;
[251] Fix | Delete
}
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
// make sure that the ad is injected outside the link
[255] Fix | Delete
if ( 'img' === $tag_option && 'a' === $node->parentNode->tagName ) {
[256] Fix | Delete
if ( $options['before'] ) {
[257] Fix | Delete
$node->parentNode;
[258] Fix | Delete
} else {
[259] Fix | Delete
// go one level deeper if inserted after to not insert the ad into the link; probably after the paragraph
[260] Fix | Delete
$node->parentNode->parentNode;
[261] Fix | Delete
}
[262] Fix | Delete
}
[263] Fix | Delete
}
[264] Fix | Delete
[265] Fix | Delete
$ad_content = (string) Advanced_Ads_Select::get_instance()->get_ad_by_method( $placement_id, 'placement', $placement_opts );
[266] Fix | Delete
[267] Fix | Delete
if ( trim( $ad_content, $whitespaces ) === '' ) {
[268] Fix | Delete
continue;
[269] Fix | Delete
}
[270] Fix | Delete
[271] Fix | Delete
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar
[272] Fix | Delete
$ad_content = self::filter_ad_content( $ad_content, $node->tagName, $options );
[273] Fix | Delete
[274] Fix | Delete
// convert HTML to XML!
[275] Fix | Delete
$ad_dom = new DOMDocument( '1.0', $wp_charset );
[276] Fix | Delete
$libxml_use_internal_errors = libxml_use_internal_errors( true );
[277] Fix | Delete
$ad_dom->loadHtml( '<!DOCTYPE html><html><meta http-equiv="Content-Type" content="text/html; charset=' . $wp_charset . '" /><body>' . $ad_content );
[278] Fix | Delete
[279] Fix | Delete
switch ( $options['position'] ) {
[280] Fix | Delete
case 'append':
[281] Fix | Delete
$ref_node = $node;
[282] Fix | Delete
[283] Fix | Delete
foreach ( $ad_dom->getElementsByTagName( 'body' )->item( 0 )->childNodes as $importedNode ) {
[284] Fix | Delete
$importedNode = $dom->importNode( $importedNode, true );
[285] Fix | Delete
$ref_node->appendChild( $importedNode );
[286] Fix | Delete
}
[287] Fix | Delete
break;
[288] Fix | Delete
case 'prepend':
[289] Fix | Delete
$ref_node = $node;
[290] Fix | Delete
[291] Fix | Delete
foreach ( $ad_dom->getElementsByTagName( 'body' )->item( 0 )->childNodes as $importedNode ) {
[292] Fix | Delete
$importedNode = $dom->importNode( $importedNode, true );
[293] Fix | Delete
$ref_node->insertBefore( $importedNode, $ref_node->firstChild );
[294] Fix | Delete
}
[295] Fix | Delete
break;
[296] Fix | Delete
case 'before':
[297] Fix | Delete
$ref_node = $node;
[298] Fix | Delete
[299] Fix | Delete
foreach ( $ad_dom->getElementsByTagName( 'body' )->item( 0 )->childNodes as $importedNode ) {
[300] Fix | Delete
$importedNode = $dom->importNode( $importedNode, true );
[301] Fix | Delete
$ref_node->parentNode->insertBefore( $importedNode, $ref_node );
[302] Fix | Delete
}
[303] Fix | Delete
break;
[304] Fix | Delete
case 'after':
[305] Fix | Delete
default:
[306] Fix | Delete
// append before next node or as last child to body.
[307] Fix | Delete
$ref_node = $node->nextSibling;
[308] Fix | Delete
if ( isset( $ref_node ) ) {
[309] Fix | Delete
foreach ( $ad_dom->getElementsByTagName( 'body' )->item( 0 )->childNodes as $importedNode ) {
[310] Fix | Delete
$importedNode = $dom->importNode( $importedNode, true );
[311] Fix | Delete
$ref_node->parentNode->insertBefore( $importedNode, $ref_node );
[312] Fix | Delete
}
[313] Fix | Delete
} else {
[314] Fix | Delete
// append to body; -TODO using here that we only select direct children of the body tag.
[315] Fix | Delete
foreach ( $ad_dom->getElementsByTagName( 'body' )->item( 0 )->childNodes as $importedNode ) {
[316] Fix | Delete
$importedNode = $dom->importNode( $importedNode, true );
[317] Fix | Delete
$node->parentNode->appendChild( $importedNode );
[318] Fix | Delete
}
[319] Fix | Delete
}
[320] Fix | Delete
}
[321] Fix | Delete
libxml_use_internal_errors( $libxml_use_internal_errors );
[322] Fix | Delete
$did_inject = true;
[323] Fix | Delete
}
[324] Fix | Delete
[325] Fix | Delete
if ( ! $did_inject ) {
[326] Fix | Delete
return $content;
[327] Fix | Delete
}
[328] Fix | Delete
[329] Fix | Delete
$content_orig = $content;
[330] Fix | Delete
// convert to text-representation.
[331] Fix | Delete
$content = $dom->saveHTML();
[332] Fix | Delete
$content = self::prepare_output( $content, $content_orig );
[333] Fix | Delete
[334] Fix | Delete
/**
[335] Fix | Delete
* Show a warning to ad admins in the Ad Health bar in the frontend, when
[336] Fix | Delete
*
[337] Fix | Delete
* * the level limitation was not disabled
[338] Fix | Delete
* * could not inject one ad (as by use of `elseif` here)
[339] Fix | Delete
* * but there are enough elements on the site, but just in sub-containers
[340] Fix | Delete
*/
[341] Fix | Delete
} elseif ( WordPress::user_can( 'advanced_ads_manage_options' )
[342] Fix | Delete
&& $options['itemLimit'] !== -1
[343] Fix | Delete
&& empty( $plugin_options['content-injection-level-disabled'] ) ) {
[344] Fix | Delete
[345] Fix | Delete
// Check if there are more elements without limitation.
[346] Fix | Delete
$all_items = $xpath->query( '//' . $tag );
[347] Fix | Delete
[348] Fix | Delete
$paragraphs = [];
[349] Fix | Delete
foreach ( $all_items as $item ) {
[350] Fix | Delete
if ( $options['allowEmpty'] || ( isset( $item->textContent ) && trim( $item->textContent, $whitespaces ) !== '' ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar
[351] Fix | Delete
$paragraphs[] = $item;
[352] Fix | Delete
}
[353] Fix | Delete
}
[354] Fix | Delete
[355] Fix | Delete
$paragraphs = self::filter_by_ancestors_to_limit( $paragraphs, $ancestors_to_limit );
[356] Fix | Delete
if ( $options['paragraph_id'] <= count( $paragraphs ) ) {
[357] Fix | Delete
// Add a warning to ad health.
[358] Fix | Delete
add_filter( 'advanced-ads-ad-health-nodes', [ 'Advanced_Ads_In_Content_Injector', 'add_ad_health_node' ] );
[359] Fix | Delete
}
[360] Fix | Delete
}
[361] Fix | Delete
[362] Fix | Delete
// phpcs:enable
[363] Fix | Delete
[364] Fix | Delete
return $content;
[365] Fix | Delete
}
[366] Fix | Delete
[367] Fix | Delete
/**
[368] Fix | Delete
* Get content to load.
[369] Fix | Delete
*
[370] Fix | Delete
* @param string $content Original content.
[371] Fix | Delete
* @param string $wp_charset blog charset.
[372] Fix | Delete
*
[373] Fix | Delete
* @return string $content Content to load.
[374] Fix | Delete
*/
[375] Fix | Delete
private static function get_content_to_load( $content, $wp_charset ) {
[376] Fix | Delete
// Prevent removing closing tags in scripts.
[377] Fix | Delete
$content_to_load = preg_replace( '/<script.*?<\/script>/si', '<!--\0-->', $content );
[378] Fix | Delete
[379] Fix | Delete
// check which priority the wpautop filter has; might have been disabled on purpose.
[380] Fix | Delete
$wpautop_priority = has_filter( 'the_content', 'wpautop' );
[381] Fix | Delete
if ( $wpautop_priority && Advanced_Ads_Plugin::get_instance()->get_content_injection_priority() < $wpautop_priority ) {
[382] Fix | Delete
$content_to_load = wpautop( $content_to_load );
[383] Fix | Delete
}
[384] Fix | Delete
[385] Fix | Delete
return $content_to_load;
[386] Fix | Delete
}
[387] Fix | Delete
[388] Fix | Delete
/**
[389] Fix | Delete
* Filter ad content.
[390] Fix | Delete
*
[391] Fix | Delete
* @param string $ad_content Ad content.
[392] Fix | Delete
* @param string $tag_name tar before/after the content.
[393] Fix | Delete
* @param array $options Injection options.
[394] Fix | Delete
*
[395] Fix | Delete
* @return string ad content.
[396] Fix | Delete
*/
[397] Fix | Delete
private static function filter_ad_content( $ad_content, $tag_name, $options ) {
[398] Fix | Delete
// Replace `</` with `<\/` in ad content when placed within `document.write()` to prevent code from breaking.
[399] Fix | Delete
$ad_content = preg_replace( '#(document.write.+)</(.*)#', '$1<\/$2', $ad_content );
[400] Fix | Delete
[401] Fix | Delete
// Inject placeholder.
[402] Fix | Delete
$id = count( self::$ads_for_placeholders );
[403] Fix | Delete
self::$ads_for_placeholders[] = [
[404] Fix | Delete
'id' => $id,
[405] Fix | Delete
'tag' => $tag_name,
[406] Fix | Delete
'position' => $options['position'],
[407] Fix | Delete
'ad' => $ad_content,
[408] Fix | Delete
];
[409] Fix | Delete
[410] Fix | Delete
return '%advads_placeholder_' . $id . '%';
[411] Fix | Delete
}
[412] Fix | Delete
[413] Fix | Delete
/**
[414] Fix | Delete
* Prepare output.
[415] Fix | Delete
*
[416] Fix | Delete
* @param string $content Modified content.
[417] Fix | Delete
* @param string $content_orig Original content.
[418] Fix | Delete
*
[419] Fix | Delete
* @return string $content Content to output.
[420] Fix | Delete
*/
[421] Fix | Delete
private static function prepare_output( $content, $content_orig ) {
[422] Fix | Delete
$content = self::inject_ads( $content, $content_orig, self::$ads_for_placeholders );
[423] Fix | Delete
self::$ads_for_placeholders = [];
[424] Fix | Delete
[425] Fix | Delete
return $content;
[426] Fix | Delete
}
[427] Fix | Delete
[428] Fix | Delete
/**
[429] Fix | Delete
* Search for ad placeholders in the `$content` to determine positions at which to inject ads.
[430] Fix | Delete
* Given the positions, inject ads into `$content_orig.
[431] Fix | Delete
*
[432] Fix | Delete
* @param string $content Post content with injected ad placeholders.
[433] Fix | Delete
* @param string $content_orig Unmodified post content.
[434] Fix | Delete
* @param array $ads_for_placeholders Array of ads.
[435] Fix | Delete
* Each ad contains placeholder id, before or after which tag to inject the ad, the ad content.
[436] Fix | Delete
*
[437] Fix | Delete
* @return string $content
[438] Fix | Delete
*/
[439] Fix | Delete
private static function inject_ads( $content, $content_orig, $ads_for_placeholders ) {
[440] Fix | Delete
$self_closing_tags = [
[441] Fix | Delete
'area',
[442] Fix | Delete
'base',
[443] Fix | Delete
'basefont',
[444] Fix | Delete
'bgsound',
[445] Fix | Delete
'br',
[446] Fix | Delete
'col',
[447] Fix | Delete
'embed',
[448] Fix | Delete
'frame',
[449] Fix | Delete
'hr',
[450] Fix | Delete
'img',
[451] Fix | Delete
'input',
[452] Fix | Delete
'keygen',
[453] Fix | Delete
'link',
[454] Fix | Delete
'meta',
[455] Fix | Delete
'param',
[456] Fix | Delete
'source',
[457] Fix | Delete
'track',
[458] Fix | Delete
'wbr',
[459] Fix | Delete
];
[460] Fix | Delete
[461] Fix | Delete
// It is not possible to append/prepend in self closing tags.
[462] Fix | Delete
foreach ( $ads_for_placeholders as &$ad_content ) {
[463] Fix | Delete
if ( ( 'prepend' === $ad_content['position'] || 'append' === $ad_content['position'] )
[464] Fix | Delete
&& in_array( $ad_content['tag'], $self_closing_tags, true ) ) {
[465] Fix | Delete
$ad_content['position'] = 'after';
[466] Fix | Delete
}
[467] Fix | Delete
}
[468] Fix | Delete
unset( $ad_content );
[469] Fix | Delete
usort( $ads_for_placeholders, [ 'Advanced_Ads_In_Content_Injector', 'sort_ads_for_placehoders' ] );
[470] Fix | Delete
[471] Fix | Delete
// Add tags before/after which ad placehoders were injected.
[472] Fix | Delete
foreach ( $ads_for_placeholders as $ad_content ) {
[473] Fix | Delete
$tag = $ad_content['tag'];
[474] Fix | Delete
[475] Fix | Delete
switch ( $ad_content['position'] ) {
[476] Fix | Delete
case 'before':
[477] Fix | Delete
case 'prepend':
[478] Fix | Delete
$alts[] = "<{$tag}[^>]*>";
[479] Fix | Delete
break;
[480] Fix | Delete
case 'after':
[481] Fix | Delete
if ( in_array( $tag, $self_closing_tags, true ) ) {
[482] Fix | Delete
$alts[] = "<{$tag}[^>]*>";
[483] Fix | Delete
} else {
[484] Fix | Delete
$alts[] = "</{$tag}>";
[485] Fix | Delete
}
[486] Fix | Delete
break;
[487] Fix | Delete
case 'append':
[488] Fix | Delete
$alts[] = "</{$tag}>";
[489] Fix | Delete
break;
[490] Fix | Delete
}
[491] Fix | Delete
}
[492] Fix | Delete
$alts = array_unique( $alts );
[493] Fix | Delete
$tag_regexp = implode( '|', $alts );
[494] Fix | Delete
// Add ad placeholder.
[495] Fix | Delete
$alts[] = '%advads_placeholder_(?:\d+)%';
[496] Fix | Delete
$tag_and_placeholder_regexp = implode( '|', $alts );
[497] Fix | Delete
[498] Fix | Delete
preg_match_all( "#{$tag_and_placeholder_regexp}#i", $content, $tag_matches );
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function