| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) || die( 'Cheatin\' uh?' ); |
| 3 |
|
| 4 |
add_filter( 'upgrader_post_install', 'imagify_sync_theme_plugin_files_on_update', IMAGIFY_INT_MAX, 3 ); |
| 5 |
/** |
| 6 |
* Sync files after a theme or plugin has been updated. |
| 7 |
* |
| 8 |
* @since 1.7 |
| 9 |
* @author Grégory Viguier |
| 10 |
* |
| 11 |
* @param bool $response Installation response. |
| 12 |
* @param array $hook_extra Extra arguments passed to hooked filters. |
| 13 |
* @param array $result Installation result data. |
| 14 |
*/ |
| 15 |
function imagify_sync_theme_plugin_files_on_update( $response, $hook_extra, $result ) { |
| 16 |
global $wpdb; |
| 17 |
|
| 18 |
if ( ( empty( $hook_extra['plugin'] ) && empty( $hook_extra['theme'] ) ) || empty( $result['destination'] ) ) { |
| 19 |
return $response; |
| 20 |
} |
| 21 |
|
| 22 |
$folders_db = Imagify_Folders_DB::get_instance(); |
| 23 |
$files_db = Imagify_Files_DB::get_instance(); |
| 24 |
|
| 25 |
if ( ! $folders_db->can_operate() || ! $files_db->can_operate() ) { |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
if ( ! imagify_valid_key() ) { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
$user = new Imagify_User(); |
| 34 |
|
| 35 |
if ( $user->is_over_quota() ) { |
| 36 |
return; |
| 37 |
} |
| 38 |
|
| 39 |
$folder_path = trailingslashit( $result['destination'] ); |
| 40 |
|
| 41 |
if ( Imagify_Files_Scan::is_path_forbidden( $folder_path ) ) { |
| 42 |
// This theme or plugin must not be optimized. |
| 43 |
return $response; |
| 44 |
} |
| 45 |
|
| 46 |
// Get the related folder. |
| 47 |
$placeholder = Imagify_Files_Scan::add_placeholder( $folder_path ); |
| 48 |
$folder = Imagify_Folders_DB::get_instance()->get_in( 'path', $placeholder ); |
| 49 |
|
| 50 |
if ( ! $folder ) { |
| 51 |
// This theme or plugin is not in the database. |
| 52 |
return $response; |
| 53 |
} |
| 54 |
|
| 55 |
// Sync the folder files. |
| 56 |
Imagify_Custom_Folders::synchronize_files_from_folders( array( |
| 57 |
$folder['folder_id'] => array( |
| 58 |
'folder_id' => $folder['folder_id'], |
| 59 |
'path' => $placeholder, |
| 60 |
'active' => $folder['active'], |
| 61 |
'folder_path' => $folder_path, |
| 62 |
), |
| 63 |
) ); |
| 64 |
|
| 65 |
return $response; |
| 66 |
} |
| 67 |
|