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/clone/wp-conte.../plugins/wp-smush.../core/parser
File: class-element.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Smush\Core\Parser;
[2] Fix | Delete
[3] Fix | Delete
class Element {
[4] Fix | Delete
private $markup;
[5] Fix | Delete
[6] Fix | Delete
private $tag;
[7] Fix | Delete
[8] Fix | Delete
/**
[9] Fix | Delete
* @var Element_Attribute[]
[10] Fix | Delete
*/
[11] Fix | Delete
private $attributes;
[12] Fix | Delete
[13] Fix | Delete
/**
[14] Fix | Delete
* @var Element_CSS_Property[]
[15] Fix | Delete
*/
[16] Fix | Delete
private $css_properties;
[17] Fix | Delete
[18] Fix | Delete
private $has_updates = false;
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* @var Element_Attribute[]
[22] Fix | Delete
*/
[23] Fix | Delete
private $added_attributes = array();
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* @var Element_Attribute[]
[27] Fix | Delete
*/
[28] Fix | Delete
private $replaced_attributes = array();
[29] Fix | Delete
/**
[30] Fix | Delete
* @var Parser
[31] Fix | Delete
*/
[32] Fix | Delete
private $parser;
[33] Fix | Delete
private $postfix;
[34] Fix | Delete
/**
[35] Fix | Delete
* @var Element_Attribute[]
[36] Fix | Delete
*/
[37] Fix | Delete
private $image_attributes;
[38] Fix | Delete
[39] Fix | Delete
public function __construct( $markup, $tag, $attributes, $css_properties ) {
[40] Fix | Delete
$this->markup = $markup;
[41] Fix | Delete
$this->tag = $tag;
[42] Fix | Delete
$this->attributes = $attributes;
[43] Fix | Delete
$this->css_properties = $css_properties;
[44] Fix | Delete
$this->parser = new Parser();
[45] Fix | Delete
}
[46] Fix | Delete
[47] Fix | Delete
/**
[48] Fix | Delete
* @return mixed
[49] Fix | Delete
*/
[50] Fix | Delete
public function get_markup() {
[51] Fix | Delete
return $this->markup;
[52] Fix | Delete
}
[53] Fix | Delete
[54] Fix | Delete
/**
[55] Fix | Delete
* @return mixed
[56] Fix | Delete
*/
[57] Fix | Delete
public function get_tag() {
[58] Fix | Delete
return $this->tag;
[59] Fix | Delete
}
[60] Fix | Delete
[61] Fix | Delete
/**
[62] Fix | Delete
* @return Element_Attribute[]
[63] Fix | Delete
*/
[64] Fix | Delete
public function get_attributes() {
[65] Fix | Delete
return $this->attributes;
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
public function get_image_attributes() {
[69] Fix | Delete
if ( is_null( $this->image_attributes ) ) {
[70] Fix | Delete
$this->image_attributes = $this->prepare_image_attributes();
[71] Fix | Delete
}
[72] Fix | Delete
[73] Fix | Delete
return $this->image_attributes;
[74] Fix | Delete
}
[75] Fix | Delete
[76] Fix | Delete
private function prepare_image_attributes() {
[77] Fix | Delete
$image_attributes = array();
[78] Fix | Delete
foreach ( $this->get_image_attribute_names() as $image_attribute_name ) {
[79] Fix | Delete
$image_attribute = $this->get_attribute( $image_attribute_name );
[80] Fix | Delete
if ( $image_attribute ) {
[81] Fix | Delete
$image_attributes[] = $image_attribute;
[82] Fix | Delete
}
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
return $image_attributes;
[86] Fix | Delete
}
[87] Fix | Delete
[88] Fix | Delete
private function get_image_attribute_names() {
[89] Fix | Delete
$attribute_names = apply_filters(
[90] Fix | Delete
'wp_smush_get_image_attribute_names',
[91] Fix | Delete
/**
[92] Fix | Delete
* TODO: break down this list and move to integration classes, only keep the bare minimum here
[93] Fix | Delete
*/
[94] Fix | Delete
array(
[95] Fix | Delete
'href',
[96] Fix | Delete
'data-href',
[97] Fix | Delete
'src',
[98] Fix | Delete
'data-src',
[99] Fix | Delete
'srcset',
[100] Fix | Delete
'data-srcset',
[101] Fix | Delete
'data-thumb',
[102] Fix | Delete
'data-thumbnail',
[103] Fix | Delete
'data-back',
[104] Fix | Delete
'data-lazyload',
[105] Fix | Delete
// WP Rocket lazy loading:
[106] Fix | Delete
'data-lazy-src',
[107] Fix | Delete
'data-lazy-srcset',
[108] Fix | Delete
'data-original',
[109] Fix | Delete
// We need the following to support webp *after* lazy load.
[110] Fix | Delete
'data-bg',
[111] Fix | Delete
'data-bg-image',
[112] Fix | Delete
)
[113] Fix | Delete
);
[114] Fix | Delete
[115] Fix | Delete
$attribute_names = array_filter(
[116] Fix | Delete
(array) $attribute_names,
[117] Fix | Delete
function ( $attribute_names ) {
[118] Fix | Delete
return $attribute_names && is_string( $attribute_names );
[119] Fix | Delete
}
[120] Fix | Delete
);
[121] Fix | Delete
[122] Fix | Delete
return array_unique( $attribute_names );
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
/**
[126] Fix | Delete
* @param Element_Attribute $attribute
[127] Fix | Delete
*/
[128] Fix | Delete
public function add_attribute( $attribute ) {
[129] Fix | Delete
$this->added_attributes[ $attribute->get_name() ] = $attribute;
[130] Fix | Delete
[131] Fix | Delete
$this->set_has_updates( true );
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
public function get_attribute( $name ) {
[135] Fix | Delete
foreach ( $this->attributes as $attribute ) {
[136] Fix | Delete
if ( $attribute->get_name() === $name ) {
[137] Fix | Delete
return $attribute;
[138] Fix | Delete
}
[139] Fix | Delete
}
[140] Fix | Delete
return null;
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
/**
[144] Fix | Delete
* @param $original_name string
[145] Fix | Delete
* @param $new_attribute Element_Attribute
[146] Fix | Delete
*
[147] Fix | Delete
* @return void
[148] Fix | Delete
*/
[149] Fix | Delete
public function replace_attribute( $original_name, $new_attribute ) {
[150] Fix | Delete
$this->replaced_attributes[ $original_name ] = $new_attribute;
[151] Fix | Delete
[152] Fix | Delete
$this->set_has_updates( true );
[153] Fix | Delete
}
[154] Fix | Delete
[155] Fix | Delete
/**
[156] Fix | Delete
* @param $attribute Element_Attribute
[157] Fix | Delete
*
[158] Fix | Delete
* @return void
[159] Fix | Delete
*/
[160] Fix | Delete
public function add_or_update_attribute( $attribute ) {
[161] Fix | Delete
$attribute_exists = $this->get_attribute( $attribute->get_name() );
[162] Fix | Delete
if ( $attribute_exists ) {
[163] Fix | Delete
$this->replace_attribute( $attribute->get_name(), $attribute );
[164] Fix | Delete
} else {
[165] Fix | Delete
$this->add_attribute( $attribute );
[166] Fix | Delete
}
[167] Fix | Delete
}
[168] Fix | Delete
[169] Fix | Delete
private function set_has_updates( $has_updates ) {
[170] Fix | Delete
$this->has_updates = $has_updates;
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
/**
[174] Fix | Delete
* @return Element_CSS_Property[]
[175] Fix | Delete
*/
[176] Fix | Delete
public function get_css_properties() {
[177] Fix | Delete
return $this->css_properties;
[178] Fix | Delete
}
[179] Fix | Delete
[180] Fix | Delete
public function get_background_css_property() {
[181] Fix | Delete
foreach ( $this->get_css_properties() as $css_property ) {
[182] Fix | Delete
if ( strpos( $css_property->get_property(), 'background' ) !== false ) {
[183] Fix | Delete
return $css_property;
[184] Fix | Delete
}
[185] Fix | Delete
}
[186] Fix | Delete
return null;
[187] Fix | Delete
}
[188] Fix | Delete
[189] Fix | Delete
/**
[190] Fix | Delete
* @param Element_CSS_Property $css_property
[191] Fix | Delete
*/
[192] Fix | Delete
public function add_css_property( $css_property ) {
[193] Fix | Delete
// TODO: this won't work as of now
[194] Fix | Delete
$this->css_properties[] = $css_property;
[195] Fix | Delete
[196] Fix | Delete
$this->set_has_updates( true );
[197] Fix | Delete
}
[198] Fix | Delete
[199] Fix | Delete
public function has_updates() {
[200] Fix | Delete
$has_updates = $this->has_updates;
[201] Fix | Delete
[202] Fix | Delete
foreach ( $this->attributes as $attribute ) {
[203] Fix | Delete
$has_updates = $has_updates || $attribute->has_updates();
[204] Fix | Delete
}
[205] Fix | Delete
[206] Fix | Delete
foreach ( $this->css_properties as $css_property ) {
[207] Fix | Delete
$has_updates = $has_updates || $css_property->has_updates();
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
return $has_updates;
[211] Fix | Delete
}
[212] Fix | Delete
[213] Fix | Delete
public function get_updated_markup() {
[214] Fix | Delete
$updated = $this->get_markup();
[215] Fix | Delete
$updated = $this->update_attributes( $updated );
[216] Fix | Delete
$updated = $this->update_css_properties( $updated );
[217] Fix | Delete
$updated = $this->replace_attributes( $updated );
[218] Fix | Delete
$updated = $this->add_new_attributes( $updated );
[219] Fix | Delete
$updated = $this->add_postfix( $updated );
[220] Fix | Delete
[221] Fix | Delete
// TODO: this is a temporary way to support the old filters, remove this in the release that comes after 3.16.0
[222] Fix | Delete
$updated = apply_filters_deprecated( 'smush_cdn_image_tag', array( $updated ), '3.16.0', 'wp_smush_updated_element_markup' );
[223] Fix | Delete
$updated = apply_filters_deprecated( 'smush_cdn_bg_image_tag', array( $updated ), '3.16.0', 'wp_smush_updated_element_markup' );
[224] Fix | Delete
[225] Fix | Delete
return apply_filters( 'wp_smush_updated_element_markup', $updated );
[226] Fix | Delete
}
[227] Fix | Delete
[228] Fix | Delete
public function set_postfix( $postfix ) {
[229] Fix | Delete
$this->postfix = $postfix;
[230] Fix | Delete
}
[231] Fix | Delete
[232] Fix | Delete
private function add_postfix( $markup ) {
[233] Fix | Delete
return $markup . $this->postfix;
[234] Fix | Delete
}
[235] Fix | Delete
[236] Fix | Delete
private function replace_attributes( $markup ) {
[237] Fix | Delete
foreach ( $this->replaced_attributes as $original_attribute_name => $replaced_attribute ) {
[238] Fix | Delete
$original = $this->get_attribute( $original_attribute_name );
[239] Fix | Delete
if ( $original ) {
[240] Fix | Delete
$replaced_attribute_string = $this->change_attribute_quote_character(
[241] Fix | Delete
$replaced_attribute->get_attribute(),
[242] Fix | Delete
$this->find_quote_character( $original->get_attribute() )
[243] Fix | Delete
);
[244] Fix | Delete
[245] Fix | Delete
$markup = str_replace(
[246] Fix | Delete
$original->get_attribute(),
[247] Fix | Delete
$replaced_attribute_string,
[248] Fix | Delete
$markup
[249] Fix | Delete
);
[250] Fix | Delete
}
[251] Fix | Delete
}
[252] Fix | Delete
[253] Fix | Delete
return $markup;
[254] Fix | Delete
}
[255] Fix | Delete
[256] Fix | Delete
private function find_quote_character( $string ) {
[257] Fix | Delete
return false === strpos( $string, '"' ) ? '\'' : '"';
[258] Fix | Delete
}
[259] Fix | Delete
[260] Fix | Delete
private function change_attribute_quote_character( $full_attribute, $new_quote_character ) {
[261] Fix | Delete
$current_quote_character = $this->find_quote_character( $full_attribute );
[262] Fix | Delete
if ( $current_quote_character === $new_quote_character ) {
[263] Fix | Delete
return $full_attribute;
[264] Fix | Delete
}
[265] Fix | Delete
[266] Fix | Delete
return str_replace( $current_quote_character, $new_quote_character, $full_attribute );
[267] Fix | Delete
}
[268] Fix | Delete
[269] Fix | Delete
private function update_attributes( $markup ) {
[270] Fix | Delete
foreach ( $this->get_attributes() as $attribute ) {
[271] Fix | Delete
if ( $attribute->has_updates() ) {
[272] Fix | Delete
$markup = str_replace(
[273] Fix | Delete
$attribute->get_attribute(),
[274] Fix | Delete
$attribute->get_updated(),
[275] Fix | Delete
$markup
[276] Fix | Delete
);
[277] Fix | Delete
}
[278] Fix | Delete
}
[279] Fix | Delete
return $markup;
[280] Fix | Delete
}
[281] Fix | Delete
[282] Fix | Delete
private function update_css_properties( $markup ) {
[283] Fix | Delete
foreach ( $this->get_css_properties() as $css_property ) {
[284] Fix | Delete
if ( $css_property->has_updates() ) {
[285] Fix | Delete
$markup = str_replace(
[286] Fix | Delete
$css_property->get_full(),
[287] Fix | Delete
$css_property->get_updated(),
[288] Fix | Delete
$markup
[289] Fix | Delete
);
[290] Fix | Delete
}
[291] Fix | Delete
}
[292] Fix | Delete
return $markup;
[293] Fix | Delete
}
[294] Fix | Delete
[295] Fix | Delete
private function add_new_attributes( $markup ) {
[296] Fix | Delete
foreach ( $this->added_attributes as $added_attribute ) {
[297] Fix | Delete
$attribute_name = $added_attribute->get_name();
[298] Fix | Delete
// Remove the attribute first, important for removing any empty or invalid values before adding again
[299] Fix | Delete
$markup = $this->parser->remove_element_attribute( $markup, $attribute_name );
[300] Fix | Delete
$markup = $this->parser->add_element_attribute( $markup, $attribute_name, esc_attr( $added_attribute->get_value() ) );
[301] Fix | Delete
}
[302] Fix | Delete
[303] Fix | Delete
return $markup;
[304] Fix | Delete
}
[305] Fix | Delete
[306] Fix | Delete
public function get_attribute_value( $attribute_name ) {
[307] Fix | Delete
$attribute = $this->get_attribute( $attribute_name );
[308] Fix | Delete
return $attribute
[309] Fix | Delete
? $attribute->get_value()
[310] Fix | Delete
: '';
[311] Fix | Delete
}
[312] Fix | Delete
[313] Fix | Delete
public function append_attribute_value( $attribute_name, $appendage ) {
[314] Fix | Delete
$attribute = $this->get_attribute( $attribute_name );
[315] Fix | Delete
if ( $attribute ) {
[316] Fix | Delete
$attribute->set_value( $attribute->get_value() . " " . $appendage );
[317] Fix | Delete
} else {
[318] Fix | Delete
$this->add_attribute( new Element_Attribute( $attribute_name, $appendage ) );
[319] Fix | Delete
}
[320] Fix | Delete
}
[321] Fix | Delete
[322] Fix | Delete
public function is_image_element() {
[323] Fix | Delete
return 'img' === $this->get_tag();
[324] Fix | Delete
}
[325] Fix | Delete
}
[326] Fix | Delete
[327] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function