| 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 right 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 |
* @return bool |
| 15 |
*/ |
| 16 |
function imagify_sync_theme_plugin_files_on_update( $response, $hook_extra, $result ) { |
| 17 |
global $wpdb; |
| 18 |
|
| 19 |
if ( ( empty( $hook_extra['plugin'] ) && empty( $hook_extra['theme'] ) ) || empty( $result['destination'] ) ) { |
| 20 |
return $response; |
| 21 |
} |
| 22 |
|
| 23 |
$folders_to_sync = get_site_transient( 'imagify_themes_plugins_to_sync' ); |
| 24 |
$folders_to_sync = is_array( $folders_to_sync ) ? $folders_to_sync : []; |
| 25 |
|
| 26 |
$folders_to_sync[] = $result['destination']; |
| 27 |
|
| 28 |
set_site_transient( 'imagify_themes_plugins_to_sync', $folders_to_sync, DAY_IN_SECONDS ); |
| 29 |
|
| 30 |
return $response; |
| 31 |
} |
| 32 |
|
| 33 |
add_action( 'admin_init', 'imagify_sync_theme_plugin_files_after_update' ); |
| 34 |
/** |
| 35 |
* Sync files after some themes or plugins have been updated. |
| 36 |
* |
| 37 |
* @since 1.7.1.2 |
| 38 |
* @author Grégory Viguier |
| 39 |
*/ |
| 40 |
function imagify_sync_theme_plugin_files_after_update() { |
| 41 |
global $wpdb; |
| 42 |
|
| 43 |
$folders_to_sync = get_site_transient( 'imagify_themes_plugins_to_sync' ); |
| 44 |
|
| 45 |
if ( ! $folders_to_sync || ! is_array( $folders_to_sync ) ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
delete_site_transient( 'imagify_themes_plugins_to_sync' ); |
| 50 |
|
| 51 |
$folders_db = Imagify_Folders_DB::get_instance(); |
| 52 |
$files_db = Imagify_Files_DB::get_instance(); |
| 53 |
|
| 54 |
if ( ! $folders_db->can_operate() || ! $files_db->can_operate() ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
foreach ( $folders_to_sync as $folder_path ) { |
| 59 |
$folder_path = trailingslashit( $folder_path ); |
| 60 |
|
| 61 |
if ( Imagify_Files_Scan::is_path_forbidden( $folder_path ) ) { |
| 62 |
// This theme or plugin must not be optimized. |
| 63 |
continue; |
| 64 |
} |
| 65 |
|
| 66 |
// Get the related folder. |
| 67 |
$placeholder = Imagify_Files_Scan::add_placeholder( $folder_path ); |
| 68 |
$folder = Imagify_Folders_DB::get_instance()->get_in( 'path', $placeholder ); |
| 69 |
|
| 70 |
if ( ! $folder ) { |
| 71 |
// This theme or plugin is not in the database. |
| 72 |
continue; |
| 73 |
} |
| 74 |
|
| 75 |
// Sync the folder files. |
| 76 |
Imagify_Custom_Folders::synchronize_files_from_folders( |
| 77 |
[ |
| 78 |
$folder['folder_id'] => [ |
| 79 |
'folder_id' => $folder['folder_id'], |
| 80 |
'path' => $placeholder, |
| 81 |
'active' => $folder['active'], |
| 82 |
'folder_path' => $folder_path, |
| 83 |
], |
| 84 |
] |
| 85 |
); |
| 86 |
} |
| 87 |
} |
| 88 |
|