Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop anton #443

Merged
merged 5 commits into from
Feb 21, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 additions & 3 deletions lib/classes/class-utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -602,8 +602,55 @@ function_exists('is_wp_version_compatible') &&
return false;
}

}
/**
* Useful when there is a need to do things depending on a call stack.
* Returns true if any of the conditions met. Returns false otherwise.
*
* @param $callstack array Result of debug_backtrace function.
* @param $conditions array CallStack fingerprint with `stack_level` integer.
*
* Example:
* array(
* array(
* 'stack_level' => 4,
* 'function' => '__construct',
* 'class' => 'ET_Core_PageResource'
* ),
* array(
* 'stack_level' => 4,
* 'function' => 'get_cache_filename',
* 'class' => 'ET_Builder_Element'
* )
* )
*
* @return bool
*/
public static function isCallStackMatches( $callstack, $conditions ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool

if ( !is_array( $conditions ) ) {
$conditions = array( $conditions );
}

}
foreach( $conditions as $condition ) {
$condition['stack_level'] = $condition['stack_level'] ? $condition['stack_level'] : 0;

$levelData = $callstack[$condition['stack_level']];

unset( $condition['stack_level'] );

}
$levelMatches = false;
foreach( $condition as $key => $value ) {
if ( $levelData[ $key ] === $value ) {
$levelMatches = true;
} else {
$levelMatches = false;
}
}

if ( $levelMatches ) return true;
}

return false;
}
}
}
}
3 changes: 1 addition & 2 deletions lib/classes/compatibility/buddypress.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public function module_init($sm){
add_action('groups_avatar_uploaded', array($this, 'avatar_uploaded'), 10, 3);

add_filter('bp_core_fetch_avatar', array($this, 'bp_core_fetch_avatar'), 10, 3);
add_filter('bp_core_fetch_avatar_url', array($this, 'bp_core_fetch_avatar_url'), 10, 3);
add_filter('bp_core_pre_delete_existing_avatar', array($this, 'delete_existing_avatar'), 10, 2);
add_filter('bp_attachments_pre_get_attachment', array($this, 'bp_attachments_pre_get_attachment'), 10, 2);

Expand Down Expand Up @@ -68,7 +67,7 @@ public function avatar_uploaded($item_id, $type, $r){
*/
public function bp_core_fetch_avatar($image_html){
try {
preg_match("/src=(?:'|\")(.*?)(?:'|\")/", $image_html, $image_url);
preg_match("/src=(?:'|\")(http.*?)(?:'|\")/", $image_html, $image_url);
if(!empty($image_url[1])){
$gs_image_url = $this->bp_core_fetch_avatar_url($image_url[1]);
$image_html = str_replace($image_url[1], $gs_image_url, $image_html);
Expand Down
45 changes: 43 additions & 2 deletions lib/classes/compatibility/divi.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,32 @@ class Divi extends ICompatibility {
protected $id = 'divi';
protected $title = 'Divi';
protected $constant = 'WP_STATELESS_COMPATIBILITY_DIVI';
protected $description = 'Ensures compatibility with Divi Builder Export.';
protected $description = 'Ensures compatibility with Divi theme.';
protected $theme_name = 'Divi';

/**
* Cache Busting call stack conditions to disable.
* Fixing the issue with multiple cache files being created on each page load.
* @see https://github.com/wpCloud/wp-stateless/issues/430
* @var array
*/
private $cache_busting_disable_conditions = array(
array(
'stack_level' => 4,
'function' => '__construct',
'class' => 'ET_Core_PageResource'
),
array(
'stack_level' => 4,
'function' => 'get_cache_filename',
'class' => 'ET_Builder_Element'
)
);

/**
* Initialize compatibility module
* @param $sm
*/
public function module_init($sm){
// exclude randomize_filename from export
if(
Expand All @@ -29,7 +52,25 @@ public function module_init($sm){
) {
remove_filter( 'sanitize_file_name', array( "wpCloud\StatelessMedia\Utility", 'randomize_filename' ), 10 );
}


// maybe disable the filter
add_filter('sanitize_file_name', array( $this, 'sanitize_file_name' ), 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could have used stateless_skip_cache_busting filter instead. Returning true will short-circuit the randomize_filename function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll check that filter. But don't understand now what exactly will short-circuit. Can you give an example?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see now what you mean short-circuit. Will implement using that filter, thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alimuzzaman I think there is some bug in this line https://github.com/wpCloud/wp-stateless/blob/a93638abe0d2ae5013e0c7f6f7155e8e101c0c33/lib/classes/class-utility.php#L80

It should return $filename but not $return. If you return $return then the result of the randomize_filename function will be 1 (casted from true)

Can you confirm this?

Copy link
Contributor

@alimuzzaman alimuzzaman Feb 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, for the confusion. you should return unmodified $filename from the filter, not true. Then $return will become $filename.
I wrote like that so you can modify it from the filter if needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, for the confusion. you should return unmodified $filename from the filter, not true. Then $return will become $filename.
I wrote like that so you can modify it from the filter.

Ok, makes sense. Better to keep it as is.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add documentation in stateless_skip_cache_busting filter.

}

/**
* Check if `sanitize_file_name` filter was called in a place where we don't need our custom filter.
* Remove our 10-priority filter if condition met.
* @param $filename
* @return mixed
*/
public function sanitize_file_name( $filename ) {
$callstack = debug_backtrace( DEBUG_BACKTRACE_PROVIDE_OBJECT, 5 );

if ( Utility::isCallStackMatches( $callstack, $this->cache_busting_disable_conditions ) ) {
remove_filter( 'sanitize_file_name', array( "wpCloud\StatelessMedia\Utility", 'randomize_filename' ), 10 );
}

return $filename;
}

}
Expand Down