| 1 |
<?php |
| 2 |
|
| 3 |
use Imagify\User\User; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); |
| 6 |
|
| 7 |
if ( class_exists( 'C_NextGEN_Bootstrap' ) && class_exists( 'Mixin' ) && get_site_option( 'ngg_options' ) ) : |
| 8 |
|
| 9 |
/** |
| 10 |
* Create the Imagify table needed for NGG compatibility. |
| 11 |
* |
| 12 |
* @since 1.5 |
| 13 |
* @since 1.7 Deprecated. |
| 14 |
* @author Jonathan Buttigieg |
| 15 |
* @deprecated |
| 16 |
*/ |
| 17 |
function _imagify_create_ngg_table() { |
| 18 |
_deprecated_function( __FUNCTION__ . '()', '1.7', '\\Imagify\\ThirdParty\\NGG\\DB::get_instance()->maybe_upgrade_table()' ); |
| 19 |
|
| 20 |
\Imagify\ThirdParty\NGG\DB::get_instance()->maybe_upgrade_table(); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Update all Imagify stats for NGG Bulk Optimization. |
| 25 |
* |
| 26 |
* @since 1.5 |
| 27 |
* @since 1.7 Deprecated. |
| 28 |
* @author Jonathan Buttigieg |
| 29 |
* @deprecated |
| 30 |
*/ |
| 31 |
function _imagify_ngg_update_bulk_stats() { |
| 32 |
_deprecated_function( __FUNCTION__ . '()', '1.7', 'imagify_ngg_bulk_page_data()' ); |
| 33 |
|
| 34 |
if ( empty( $_GET['page'] ) || imagify_get_ngg_bulk_screen_slug() !== $_GET['page'] ) { // WPCS: CSRF ok. |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
add_filter( 'imagify_count_attachments' , 'imagify_ngg_count_attachments' ); |
| 39 |
add_filter( 'imagify_count_optimized_attachments' , 'imagify_ngg_count_optimized_attachments' ); |
| 40 |
add_filter( 'imagify_count_error_attachments' , 'imagify_ngg_count_error_attachments' ); |
| 41 |
add_filter( 'imagify_count_unoptimized_attachments' , 'imagify_ngg_count_unoptimized_attachments' ); |
| 42 |
add_filter( 'imagify_percent_optimized_attachments' , 'imagify_ngg_percent_optimized_attachments' ); |
| 43 |
add_filter( 'imagify_count_saving_data' , 'imagify_ngg_count_saving_data', 8 ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Prepare the data that goes back with the Heartbeat API. |
| 48 |
* |
| 49 |
* @since 1.5 |
| 50 |
* @since 1.7.1 Deprecated. |
| 51 |
* @deprecated |
| 52 |
* |
| 53 |
* @param array $response The Heartbeat response. |
| 54 |
* @param array $data The $_POST data sent. |
| 55 |
* @return array |
| 56 |
*/ |
| 57 |
function _imagify_ngg_heartbeat_received( $response, $data ) { |
| 58 |
_deprecated_function( __FUNCTION__ . '()', '1.7.1' ); |
| 59 |
|
| 60 |
if ( ! isset( $data['imagify_heartbeat'] ) || 'update_ngg_bulk_data' !== $data['imagify_heartbeat'] ) { |
| 61 |
return $response; |
| 62 |
} |
| 63 |
|
| 64 |
add_filter( 'imagify_count_saving_data', 'imagify_ngg_count_saving_data', 8 ); |
| 65 |
$saving_data = imagify_count_saving_data(); |
| 66 |
$user = new User(); |
| 67 |
|
| 68 |
$response['imagify_bulk_data'] = array( |
| 69 |
// User account. |
| 70 |
'unconsumed_quota' => is_wp_error( $user ) ? 0 : $user->get_percent_unconsumed_quota(), |
| 71 |
// Global chart. |
| 72 |
'optimized_attachments_percent' => imagify_ngg_percent_optimized_attachments(), |
| 73 |
'unoptimized_attachments' => imagify_ngg_count_unoptimized_attachments(), |
| 74 |
'optimized_attachments' => imagify_ngg_count_optimized_attachments(), |
| 75 |
'errors_attachments' => imagify_ngg_count_error_attachments(), |
| 76 |
// Stats block. |
| 77 |
'already_optimized_attachments' => number_format_i18n( $saving_data['count'] ), |
| 78 |
'original_human' => imagify_size_format( $saving_data['original_size'], 1 ), |
| 79 |
'optimized_human' => imagify_size_format( $saving_data['optimized_size'], 1 ), |
| 80 |
'optimized_percent' => $saving_data['percent'], |
| 81 |
); |
| 82 |
|
| 83 |
return $response; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Filter the current user capability to operate Imagify. |
| 88 |
* |
| 89 |
* @since 1.6.11 |
| 90 |
* @since 1.9 Deprecated. |
| 91 |
* @see imagify_get_capacity() |
| 92 |
* @author Grégory Viguier |
| 93 |
* @deprecated |
| 94 |
* |
| 95 |
* @param bool $user_can Tell if the current user has the required capacity to operate Imagify. |
| 96 |
* @param string $capacity The user capacity. |
| 97 |
* @param string $describer Capacity describer. See imagify_get_capacity() for possible values. Can also be a "real" user capacity. |
| 98 |
* @param int $post_id A post ID (a gallery ID for NGG). |
| 99 |
* @return bool |
| 100 |
*/ |
| 101 |
function imagify_ngg_current_user_can( $user_can, $capacity, $describer, $post_id ) { |
| 102 |
static $user_can_per_gallery = array(); |
| 103 |
|
| 104 |
_deprecated_function( __FUNCTION__ . '()', '1.9' ); |
| 105 |
|
| 106 |
if ( ! $user_can || ! $post_id || 'NextGEN Manage gallery' !== $capacity ) { |
| 107 |
return $user_can; |
| 108 |
} |
| 109 |
|
| 110 |
$image = nggdb::find_image( $post_id ); |
| 111 |
|
| 112 |
if ( isset( $user_can_per_gallery[ $image->galleryid ] ) ) { |
| 113 |
return $user_can_per_gallery[ $image->galleryid ]; |
| 114 |
} |
| 115 |
|
| 116 |
$gallery_mapper = C_Gallery_Mapper::get_instance(); |
| 117 |
$gallery = $gallery_mapper->find( $image->galleryid, false ); |
| 118 |
|
| 119 |
if ( get_current_user_id() === $gallery->author || current_user_can( 'NextGEN Manage others gallery' ) ) { |
| 120 |
// The user created this gallery or can edit others galleries. |
| 121 |
$user_can_per_gallery[ $image->galleryid ] = true; |
| 122 |
return $user_can_per_gallery[ $image->galleryid ]; |
| 123 |
} |
| 124 |
|
| 125 |
// The user can't edit this gallery. |
| 126 |
$user_can_per_gallery[ $image->galleryid ] = false; |
| 127 |
return $user_can_per_gallery[ $image->galleryid ]; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Get user capacity to operate Imagify within NGG galleries. |
| 132 |
* It is meant to be used to filter 'imagify_capacity'. |
| 133 |
* |
| 134 |
* @since 1.6.11 |
| 135 |
* @since 1.9 Deprecated. |
| 136 |
* @see imagify_get_capacity() |
| 137 |
* @author Grégory Viguier |
| 138 |
* @deprecated |
| 139 |
* |
| 140 |
* @param string $capacity The user capacity. |
| 141 |
* @param string $describer Capacity describer. See imagify_get_capacity() for possible values. Can also be a "real" user capacity. |
| 142 |
* @return string |
| 143 |
*/ |
| 144 |
function imagify_get_ngg_capacity( $capacity = 'edit_post', $describer = 'manual-optimize' ) { |
| 145 |
if ( 'manual-optimize' === $describer ) { |
| 146 |
return 'NextGEN Manage gallery'; |
| 147 |
} |
| 148 |
|
| 149 |
return $capacity; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Dispatch the optimization process. |
| 154 |
* |
| 155 |
* @since 1.8 |
| 156 |
* @since 1.9 Deprecated. |
| 157 |
* @author Grégory Viguier |
| 158 |
* @deprecated |
| 159 |
*/ |
| 160 |
function imagify_ngg_dispatch_dynamic_thumbnail_background_process() { |
| 161 |
_deprecated_function( __FUNCTION__ . '()', '1.9' ); |
| 162 |
|
| 163 |
Imagify_NGG_Dynamic_Thumbnails_Background_Process::get_instance()->save()->dispatch(); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* On manual optimization, manual re-optimization, and manual restoration, filter the user capacity to operate Imagify within NGG. |
| 168 |
* |
| 169 |
* @since 1.6.11 |
| 170 |
* @since 1.9 Deprecated. |
| 171 |
* @author Grégory Viguier |
| 172 |
* @deprecated |
| 173 |
*/ |
| 174 |
function _do_admin_post_imagify_ngg_user_capacity() { |
| 175 |
_deprecated_function( __FUNCTION__ . '()', '1.9' ); |
| 176 |
|
| 177 |
if ( ! empty( $_GET['context'] ) && 'NGG' === $_GET['context'] ) { // WPCS: CSRF ok. |
| 178 |
add_filter( 'imagify_capacity', 'imagify_get_ngg_capacity', 10, 2 ); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Get all unoptimized attachment ids. |
| 184 |
* |
| 185 |
* @since 1.0 |
| 186 |
* @since 1.9 Deprecated |
| 187 |
* @author Jonathan Buttigieg |
| 188 |
* @deprecated |
| 189 |
*/ |
| 190 |
function _do_wp_ajax_imagify_ngg_get_unoptimized_attachment_ids() { |
| 191 |
_deprecated_function( __FUNCTION__ . '()', '1.9', '\\Imagify\\ThirdParty\\NGG\\AdminAjaxPost::get_instance()->get_media_ids()' ); |
| 192 |
|
| 193 |
\Imagify\ThirdParty\NGG\AdminAjaxPost::get_instance()->get_media_ids(); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Provide custom folder type data. |
| 198 |
* |
| 199 |
* @since 1.7 |
| 200 |
* @since 1.9 Deprecated |
| 201 |
* @author Grégory Viguier |
| 202 |
* @deprecated |
| 203 |
* |
| 204 |
* @param array $data An array with keys corresponding to cell classes, and values formatted with HTML. |
| 205 |
* @param string $context A context. |
| 206 |
* @return array |
| 207 |
*/ |
| 208 |
function imagify_ngg_get_folder_type_data( $data, $context ) { |
| 209 |
_deprecated_function( __FUNCTION__ . '()', '1.9' ); |
| 210 |
|
| 211 |
if ( 'ngg' !== $context ) { |
| 212 |
return $data; |
| 213 |
} |
| 214 |
|
| 215 |
// Already filtered in imagify_ngg_bulk_page_data(). |
| 216 |
$total_saving_data = imagify_count_saving_data(); |
| 217 |
|
| 218 |
return [ |
| 219 |
'images-optimized' => imagify_ngg_count_optimized_attachments(), |
| 220 |
'errors' => imagify_ngg_count_error_attachments(), |
| 221 |
'optimized' => $total_saving_data['optimized_size'], |
| 222 |
'original' => $total_saving_data['original_size'], |
| 223 |
'errors_url' => admin_url( 'admin.php?page=nggallery-manage-gallery' ), |
| 224 |
]; |
| 225 |
} |
| 226 |
|
| 227 |
endif; |
| 228 |
|
| 229 |
if ( function_exists( 'wr2x_delete_attachment' ) ) : |
| 230 |
|
| 231 |
/** |
| 232 |
* Remove all retina versions if they exist. |
| 233 |
* |
| 234 |
* @since 1.0 |
| 235 |
* @since 1.8 Deprecated. |
| 236 |
* @deprecated |
| 237 |
* |
| 238 |
* @param int $attachment_id An attachment ID. |
| 239 |
*/ |
| 240 |
function _imagify_wr2x_delete_attachment_on_restore( $attachment_id ) { |
| 241 |
_deprecated_function( __FUNCTION__ . '()', '1.8' ); |
| 242 |
|
| 243 |
wr2x_delete_attachment( $attachment_id ); |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Regenerate all retina versions. |
| 248 |
* |
| 249 |
* @since 1.0 |
| 250 |
* @since 1.8 Deprecated. |
| 251 |
* @deprecated |
| 252 |
* |
| 253 |
* @param int $attachment_id An attachment ID. |
| 254 |
*/ |
| 255 |
function _imagify_wr2x_generate_images_on_restore( $attachment_id ) { |
| 256 |
_deprecated_function( __FUNCTION__ . '()', '1.8' ); |
| 257 |
|
| 258 |
wr2x_delete_attachment( $attachment_id ); |
| 259 |
wr2x_generate_images( wp_get_attachment_metadata( $attachment_id ) ); |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Filter the optimization data of each thumbnail. |
| 264 |
* |
| 265 |
* @since 1.0 |
| 266 |
* @since 1.8 Deprecated. |
| 267 |
* @deprecated |
| 268 |
* |
| 269 |
* @param array $data The statistics data. |
| 270 |
* @param object $response The API response. |
| 271 |
* @param int $id The attachment ID. |
| 272 |
* @param string $path The attachment path. |
| 273 |
* @param string $url The attachment URL. |
| 274 |
* @param string $size_key The attachment size key. |
| 275 |
* @param bool $optimization_level The optimization level. |
| 276 |
* @return array $data The new optimization data. |
| 277 |
*/ |
| 278 |
function _imagify_optimize_wr2x( $data, $response, $id, $path, $url, $size_key, $optimization_level ) { |
| 279 |
_deprecated_function( __FUNCTION__ . '()', '1.8', 'Imagify_WP_Retina_2x::optimize_retina_version()' ); |
| 280 |
|
| 281 |
/** |
| 282 |
* Allow to optimize the retina version generated by WP Retina x2. |
| 283 |
* |
| 284 |
* @since 1.0 |
| 285 |
* |
| 286 |
* @param bool $do_retina True will force the optimization. |
| 287 |
*/ |
| 288 |
$do_retina = apply_filters( 'do_imagify_optimize_retina', true ); |
| 289 |
$retina_path = wr2x_get_retina( $path ); |
| 290 |
|
| 291 |
if ( empty( $retina_path ) || ! $do_retina ) { |
| 292 |
return $data; |
| 293 |
} |
| 294 |
|
| 295 |
$response = do_imagify( $retina_path, array( |
| 296 |
'backup' => false, |
| 297 |
'optimization_level' => $optimization_level, |
| 298 |
'context' => 'wp-retina', |
| 299 |
) ); |
| 300 |
$attachment = get_imagify_attachment( 'wp', $id, 'imagify_fill_thumbnail_data' ); |
| 301 |
|
| 302 |
return $attachment->fill_data( $data, $response, $size_key . '@2x' ); |
| 303 |
} |
| 304 |
|
| 305 |
endif; |
| 306 |
|
| 307 |
if ( defined( 'WP_ROCKET_VERSION' ) ) : |
| 308 |
|
| 309 |
/** |
| 310 |
* Don't load Imagify CSS & JS files on WP Rocket options screen to avoid conflict with older version of SweetAlert. |
| 311 |
* Since 1.6.10 they should be enqueued only if one of our notices displays here. |
| 312 |
* |
| 313 |
* @since 1.6.9.1 |
| 314 |
* @since 1.6.10 Use the new class Imagify_Assets. |
| 315 |
* @since 1.9.3 Deprecated. |
| 316 |
* @author Jonathan Buttigieg |
| 317 |
* @author Grégory Viguier |
| 318 |
* @deprecated |
| 319 |
*/ |
| 320 |
function imagify_dequeue_sweetalert_wprocket() { |
| 321 |
_deprecated_function( __FUNCTION__ . '()', '1.9.3', '\\Imagify\\ThirdParty\\WPRocket\\Main::dequeue_sweetalert()' ); |
| 322 |
|
| 323 |
if ( ! defined( 'WP_ROCKET_PLUGIN_SLUG' ) ) { |
| 324 |
return; |
| 325 |
} |
| 326 |
|
| 327 |
if ( ! imagify_is_screen( 'settings_page_' . WP_ROCKET_PLUGIN_SLUG ) && ! imagify_is_screen( 'settings_page_' . WP_ROCKET_PLUGIN_SLUG . '-network' ) ) { |
| 328 |
return; |
| 329 |
} |
| 330 |
|
| 331 |
Imagify_Assets::get_instance()->dequeue_script( array( 'sweetalert-core', 'sweetalert', 'notices' ) ); |
| 332 |
} |
| 333 |
|
| 334 |
endif; |
| 335 |
|