| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' ); |
| 3 |
|
| 4 |
/** |
| 5 |
* Fix the capability for our capacity filter hook |
| 6 |
* |
| 7 |
* @since 1.0 |
| 8 |
*/ |
| 9 |
add_filter( 'option_page_capability_imagify', '_imagify_correct_capability_for_options_page' ); |
| 10 |
function _imagify_correct_capability_for_options_page( $capability ) { |
| 11 |
$cap = ( imagify_is_active_for_network() ) ? 'manage_network_options' : 'manage_options'; |
| 12 |
return apply_filters( 'imagify_capacity', $cap ); |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Tell to WordPress to be confident with our setting, we are clean! |
| 17 |
* |
| 18 |
* @since 1.0 |
| 19 |
*/ |
| 20 |
add_action( 'admin_init', '_imagify_register_setting' ); |
| 21 |
function _imagify_register_setting() |
| 22 |
{ |
| 23 |
register_setting( 'imagify', 'imagify_settings' ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Filter specific options before its value is (maybe) serialized and updated. |
| 28 |
* |
| 29 |
* @since 1.0 |
| 30 |
*/ |
| 31 |
add_filter( 'pre_update_option_' . IMAGIFY_SETTINGS_SLUG, '_imagify_pre_update_option', 10, 2 ); |
| 32 |
function _imagify_pre_update_option( $value, $old_value ) { |
| 33 |
// Store all sizes even if one of them isn't checked |
| 34 |
$value['disallowed-sizes'] = array(); |
| 35 |
|
| 36 |
if ( isset( $value['sizes'] ) ) { |
| 37 |
foreach( $value['sizes'] as $size_key => $size_value ) { |
| 38 |
if ( strpos( $size_key , '-hidden' ) ) { |
| 39 |
$key = str_replace( '-hidden', '', $size_key ); |
| 40 |
if ( ! isset( $value['sizes'][ $key ] ) ) { |
| 41 |
$value['disallowed-sizes'][ $key ] = '1'; |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
// The max width for the "Resize larger images" option can't be 0 |
| 48 |
if ( (bool) ! $value['resize_larger_w'] ) { |
| 49 |
$value['resize_larger_w'] = ''; |
| 50 |
$value['resize_larger'] = 0; |
| 51 |
} |
| 52 |
|
| 53 |
// The max width for the "Resize larger images" option can't be less than the largest thumbnail width |
| 54 |
$max_sizes = get_imagify_max_intermediate_image_size(); |
| 55 |
if ( (bool) $value['resize_larger_w'] && $value['resize_larger_w'] < $max_sizes['width'] ) { |
| 56 |
$value['resize_larger_w'] = $max_sizes['width']; |
| 57 |
} |
| 58 |
|
| 59 |
unset( $value['sizes'] ); |
| 60 |
return $value; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Used to launch some actions after saving the options |
| 65 |
* |
| 66 |
* @since 1.0 |
| 67 |
*/ |
| 68 |
add_action( 'update_option_' . IMAGIFY_SETTINGS_SLUG, '_imagify_after_save_options', 10, 2 ); |
| 69 |
add_action( 'update_site_option_' . IMAGIFY_SETTINGS_SLUG, '_imagify_after_save_options', 10, 2 ); |
| 70 |
function _imagify_after_save_options( $oldvalue, $value ) |
| 71 |
{ |
| 72 |
if ( ! ( (bool) $oldvalue && (bool) $value ) ) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
if ( ! isset( $oldvalue['api_key'] ) || $oldvalue['api_key'] != $value['api_key'] ) { |
| 77 |
$api = new Imagify(); |
| 78 |
|
| 79 |
if ( is_wp_error( $api->getUser() ) ) { |
| 80 |
imagify_renew_notice( 'wrong-api-key' ); |
| 81 |
delete_site_transient( 'imagify_check_licence_1' ); |
| 82 |
} else { |
| 83 |
imagify_dismiss_notice( 'wrong-api-key' ); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
if ( imagify_is_active_for_network() ) : |
| 90 |
|
| 91 |
/** |
| 92 |
* !options.php do not handle site options. Let's use admin-post.php for multisite installations. |
| 93 |
* |
| 94 |
* @since 1.0 |
| 95 |
*/ |
| 96 |
add_action( 'admin_post_update', '_imagify_update_site_option_on_network' ); |
| 97 |
function _imagify_update_site_option_on_network() { |
| 98 |
$option_group = IMAGIFY_SLUG; |
| 99 |
|
| 100 |
if ( ! isset( $_POST['option_page'] ) || $_POST['option_page'] !== $option_group ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
|
| 104 |
$capability = apply_filters( "option_page_capability_{$option_group}", 'manage_network_options' ); |
| 105 |
|
| 106 |
if ( ! current_user_can( $capability ) ) { |
| 107 |
wp_die( __( 'Cheatin’ uh?' ), 403 ); |
| 108 |
} |
| 109 |
|
| 110 |
check_admin_referer( $option_group . '-options' ); |
| 111 |
|
| 112 |
$whitelist_options = apply_filters( 'whitelist_options', array() ); |
| 113 |
|
| 114 |
if ( ! isset( $whitelist_options[ $option_group ] ) ) { |
| 115 |
wp_die( __( '<strong>ERROR</strong>: options page not found.' ) ); |
| 116 |
} |
| 117 |
|
| 118 |
$options = $whitelist_options[ $option_group ]; |
| 119 |
|
| 120 |
if ( $options ) { |
| 121 |
|
| 122 |
foreach ( $options as $option ) { |
| 123 |
$option = trim( $option ); |
| 124 |
$value = null; |
| 125 |
|
| 126 |
if ( isset( $_POST[ $option ] ) ) { |
| 127 |
$value = $_POST[ $option ]; |
| 128 |
if ( ! is_array( $value ) ) { |
| 129 |
$value = trim( $value ); |
| 130 |
} |
| 131 |
$value = wp_unslash( $value ); |
| 132 |
} |
| 133 |
|
| 134 |
update_site_option( $option, $value ); |
| 135 |
} |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Redirect back to the settings page that was submitted |
| 141 |
*/ |
| 142 |
$goback = add_query_arg( 'settings-updated', 'true', wp_get_referer() ); |
| 143 |
wp_redirect( $goback ); |
| 144 |
exit; |
| 145 |
} |
| 146 |
|
| 147 |
endif; |