| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' ); |
| 3 |
|
| 4 |
/** |
| 5 |
* Check if Imagify is activated on the network. |
| 6 |
* |
| 7 |
* @since 1.0 |
| 8 |
* |
| 9 |
* return bool True if Imagify is activated on the network |
| 10 |
*/ |
| 11 |
function imagify_is_active_for_network() { |
| 12 |
if ( ! function_exists( 'is_plugin_active_for_network' ) ) { |
| 13 |
require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 14 |
} |
| 15 |
return is_plugin_active_for_network( 'imagify/imagify.php' ); |
| 16 |
} |
| 17 |
|
| 18 |
/* |
| 19 |
* Get the URL related to specific admin page or action. |
| 20 |
* |
| 21 |
* @since 1.0 |
| 22 |
* |
| 23 |
* @return string The URL of the specific admin page or action |
| 24 |
*/ |
| 25 |
function get_imagify_admin_url( $action = 'options-general', $arg = array() ) { |
| 26 |
$url = ''; |
| 27 |
|
| 28 |
switch( $action ) { |
| 29 |
case 'manual-override-upload': |
| 30 |
$id = ( isset( $arg['attachment_id'] ) ) ? $arg['attachment_id'] : 0; |
| 31 |
$level = ( isset( $arg['optimization_level'] ) ) ? $arg['optimization_level'] : 0; |
| 32 |
$url = wp_nonce_url( admin_url( 'admin-post.php?action=imagify_manual_override_upload&attachment_id=' . $id . '&optimization_level=' . $level ), 'imagify-manual-override-upload' ); |
| 33 |
break; |
| 34 |
|
| 35 |
case 'manual-upload': |
| 36 |
$url = wp_nonce_url( admin_url( 'admin-post.php?action=imagify_manual_upload&attachment_id=' . $arg ), 'imagify-manual-upload' ); |
| 37 |
break; |
| 38 |
|
| 39 |
case 'restore-upload' : |
| 40 |
$url = wp_nonce_url( admin_url( 'admin-post.php?action=imagify_restore_upload&attachment_id=' . $arg ), 'imagify-restore-upload' ); |
| 41 |
break; |
| 42 |
|
| 43 |
case 'dismiss-notice': |
| 44 |
$url = wp_nonce_url( admin_url( 'admin-post.php?action=imagify_dismiss_notice¬ice=' . $arg ), 'imagify-dismiss-notice' ); |
| 45 |
break; |
| 46 |
|
| 47 |
case 'bulk-optimization': |
| 48 |
$url = admin_url( 'upload.php?page=' . IMAGIFY_SLUG . '-bulk-optimization' ); |
| 49 |
break; |
| 50 |
|
| 51 |
case 'options-general': |
| 52 |
default : |
| 53 |
$page = imagify_is_active_for_network() ? network_admin_url( 'settings.php' ) : admin_url( 'options-general.php' ); |
| 54 |
$url = $page . '?page=' . IMAGIFY_SLUG; |
| 55 |
break; |
| 56 |
} |
| 57 |
|
| 58 |
return $url; |
| 59 |
} |
| 60 |
|
| 61 |
/* |
| 62 |
* Get maximal width and height from all thumbnails. |
| 63 |
* |
| 64 |
* @since 1.1 |
| 65 |
* |
| 66 |
* @return array An array containing the max with and height. |
| 67 |
*/ |
| 68 |
function get_imagify_max_intermediate_image_size() { |
| 69 |
global $_wp_additional_image_sizes; |
| 70 |
|
| 71 |
$width = 0; |
| 72 |
$height = 0; |
| 73 |
$limit = 9999; |
| 74 |
$get_intermediate_image_sizes = get_intermediate_image_sizes(); |
| 75 |
|
| 76 |
// Create the full array with sizes and crop info |
| 77 |
foreach( $get_intermediate_image_sizes as $_size ) { |
| 78 |
if ( in_array( $_size, array( 'thumbnail', 'medium', 'large' ) ) ) { |
| 79 |
$_size_width = get_option( $_size . '_size_w' ); |
| 80 |
$_size_height = get_option( $_size . '_size_h' ); |
| 81 |
} elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) { |
| 82 |
$_size_width = $_wp_additional_image_sizes[ $_size ]['width']; |
| 83 |
$_size_height = $_wp_additional_image_sizes[ $_size ]['height']; |
| 84 |
} |
| 85 |
|
| 86 |
if ( ! isset( $_size_width, $_size_height ) ) { |
| 87 |
continue; |
| 88 |
} |
| 89 |
|
| 90 |
if ( $_size_width < $limit ) { |
| 91 |
$width = max( $width, $_size_width ); |
| 92 |
} |
| 93 |
|
| 94 |
if ( $_size_height < $limit ) { |
| 95 |
$height = max( $height, $_size_height ); |
| 96 |
} |
| 97 |
} |
| 98 |
return array( |
| 99 |
'width' => $width, |
| 100 |
'height' => $height |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Renew a dismissed Imagify notice. |
| 106 |
* |
| 107 |
* @since 1.0 |
| 108 |
* |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
function imagify_renew_notice( $notice, $user_id = 0 ) { |
| 112 |
global $current_user; |
| 113 |
$user_id = ( 0 === $user_id ) ? $current_user->ID : $user_id; |
| 114 |
$notices = get_user_meta( $user_id, '_imagify_ignore_notices', true ); |
| 115 |
|
| 116 |
if( $notices && false !== array_search( $notice, $notices ) ) { |
| 117 |
unset( $notices[array_search( $notice, $notices )] ); |
| 118 |
update_user_meta( $user_id, '_imagify_ignore_notices', $notices ); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Dismissed an Imagify notice. |
| 124 |
* |
| 125 |
* @since 1.0 |
| 126 |
* |
| 127 |
* @return void |
| 128 |
*/ |
| 129 |
function imagify_dismiss_notice( $notice, $user_id = 0 ) { |
| 130 |
global $current_user; |
| 131 |
$user_id = ( 0 === $user_id ) ? $current_user->ID : $user_id; |
| 132 |
$notices = get_user_meta( $user_id, '_imagify_ignore_notices', true ); |
| 133 |
$notices[] = $notice; |
| 134 |
$notices = array_filter( $notices ); |
| 135 |
$notices = array_unique( $notices ); |
| 136 |
|
| 137 |
update_user_meta( $user_id, '_imagify_ignore_notices', $notices ); |
| 138 |
} |