| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin Filters |
| 4 |
* |
| 5 |
* @package Give |
| 6 |
* @subpackage Admin/Filters |
| 7 |
* @copyright Copyright (c) 2016, GiveWP |
| 8 |
* @license https://opensource.org/licenses/gpl-license GNU Public License |
| 9 |
* @since 1.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Sanitize number of decimals setting field. |
| 19 |
* |
| 20 |
* 1. User can only set absolute integer value as number of decimals. |
| 21 |
* 2. number_decimals setting will be zero if no decimal separator defined |
| 22 |
* |
| 23 |
* @since 1.8 |
| 24 |
* @used-by Give_Plugin_Settings::give_settings() |
| 25 |
* |
| 26 |
* @param string $value |
| 27 |
* |
| 28 |
* @return mixed |
| 29 |
*/ |
| 30 |
function __give_sanitize_number_decimals_setting_field( $value ) { |
| 31 |
$value_changed = false; |
| 32 |
$show_notice = false; |
| 33 |
$old_value = $value; |
| 34 |
|
| 35 |
if ( isset( $_POST['decimal_separator'] ) ) { |
| 36 |
$value = ! empty( $_POST['decimal_separator'] ) ? $value : 0; |
| 37 |
$value_changed = true; |
| 38 |
} |
| 39 |
|
| 40 |
if ( $value_changed && ( $old_value !== $value ) ) { |
| 41 |
Give_Admin_Settings::add_error( 'give-number-decimal', __( 'The \'Number of Decimals\' option has been automatically set to zero because the \'Decimal Separator\' is not set.', 'give' ) ); |
| 42 |
} |
| 43 |
|
| 44 |
$value = absint( $value ); |
| 45 |
$is_currency_set_to_bitcoin = ( 'BTC' === give_get_option( 'currency' ) && ! isset( $_POST['currency'] ) ) || 'BTC' === $_POST['currency']; |
| 46 |
|
| 47 |
if ( $is_currency_set_to_bitcoin && 8 < $value ) { |
| 48 |
$value = 8; |
| 49 |
$show_notice = true; |
| 50 |
} elseif ( ! $is_currency_set_to_bitcoin && 6 <= $value ) { |
| 51 |
$value = 5; |
| 52 |
$show_notice = true; |
| 53 |
} |
| 54 |
|
| 55 |
if ( $show_notice ) { |
| 56 |
Give_Admin_Settings::add_error( |
| 57 |
'give-number-decimal', |
| 58 |
sprintf( |
| 59 |
__( 'The \'Number of Decimals\' option has been automatically set to %s because you entered a number higher than the maximum allowed.', 'give' ), |
| 60 |
$value |
| 61 |
) |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
return absint( $value ); |
| 66 |
} |
| 67 |
|
| 68 |
add_filter( 'give_admin_settings_sanitize_option_number_decimals', '__give_sanitize_number_decimals_setting_field', 10 ); |
| 69 |
|
| 70 |
|
| 71 |
/** |
| 72 |
* Sanitize number of decimals setting field. |
| 73 |
* |
| 74 |
* 1. User can only set absolute integer value as number of decimals. |
| 75 |
* 2. number_decimals setting will be zero if no decimal separator defined |
| 76 |
* |
| 77 |
* @since 1.8 |
| 78 |
* @used-by Give_Plugin_Settings::give_settings() |
| 79 |
* |
| 80 |
* @param string $value |
| 81 |
* |
| 82 |
* @return mixed |
| 83 |
*/ |
| 84 |
function __give_validate_decimal_separator_setting_field( $value ) { |
| 85 |
$thousand_separator = isset( $_POST['thousands_separator'] ) ? give_clean( $_POST['thousands_separator'] ) : ''; |
| 86 |
$decimal_separator = isset( $_POST['decimal_separator'] ) ? give_clean( $_POST['decimal_separator'] ) : ''; |
| 87 |
|
| 88 |
if ( $decimal_separator === $thousand_separator ) { |
| 89 |
$value = ''; |
| 90 |
$_POST['number_decimals'] = 0; |
| 91 |
Give_Admin_Settings::add_error( 'give-decimal-separator', __( 'The \'Decimal Separator\' option has automatically been set to empty because it can not be equal to the \'Thousand Separator\'', 'give' ) ); |
| 92 |
} |
| 93 |
|
| 94 |
return $value; |
| 95 |
} |
| 96 |
|
| 97 |
add_filter( 'give_admin_settings_sanitize_option_decimal_separator', '__give_validate_decimal_separator_setting_field', 10 ); |
| 98 |
|
| 99 |
/** |
| 100 |
* Change $delimiter text to symbol. |
| 101 |
* |
| 102 |
* @since 1.8.14 |
| 103 |
* |
| 104 |
* @param string $delimiter |
| 105 |
* |
| 106 |
* @return string $delimiter. |
| 107 |
*/ |
| 108 |
function __give_import_delimiter_set_callback( $delimiter ) { |
| 109 |
$delimite_type = array( |
| 110 |
'csv' => ',', |
| 111 |
'tab-separated-values' => "\t", |
| 112 |
); |
| 113 |
|
| 114 |
return ( array_key_exists( $delimiter, $delimite_type ) ? $delimite_type[ $delimiter ] : ',' ); |
| 115 |
} |
| 116 |
|
| 117 |
add_filter( 'give_import_delimiter_set', '__give_import_delimiter_set_callback', 10 ); |
| 118 |
|
| 119 |
/** |
| 120 |
* Give unset the page id from the core setting data from the json files. |
| 121 |
* |
| 122 |
* @since 1.8.17 |
| 123 |
* |
| 124 |
* @param array $json_to_array Data from json file |
| 125 |
* @param string $type |
| 126 |
* |
| 127 |
* @return array $json_to_array |
| 128 |
*/ |
| 129 |
function give_import_core_settings_merge_pages( $json_to_array, $type ) { |
| 130 |
if ( 'merge' === $type ) { |
| 131 |
unset( $json_to_array['success_page'] ); |
| 132 |
unset( $json_to_array['failure_page'] ); |
| 133 |
unset( $json_to_array['history_page'] ); |
| 134 |
} |
| 135 |
|
| 136 |
return $json_to_array; |
| 137 |
} |
| 138 |
|
| 139 |
add_filter( 'give_import_core_settings_data', 'give_import_core_settings_merge_pages', 11, 2 ); |
| 140 |
|
| 141 |
/** |
| 142 |
* Give check the image size from the core setting data from the json files. |
| 143 |
* |
| 144 |
* @since 1.8.17 |
| 145 |
* |
| 146 |
* @param $json_to_array |
| 147 |
* @param string $type |
| 148 |
* |
| 149 |
* @return array $json_to_array |
| 150 |
*/ |
| 151 |
function give_import_core_settings_merge_image_size( $json_to_array, $type ) { |
| 152 |
if ( 'merge' === $type ) { |
| 153 |
// Featured image sizes import under Display Options > Post Types > Featured Image Size. |
| 154 |
if ( |
| 155 |
! empty( $json_to_array['form_featured_img'] ) |
| 156 |
&& ! empty( $json_to_array['featured_image_size'] ) |
| 157 |
&& give_is_setting_enabled( $json_to_array['form_featured_img'] ) |
| 158 |
) { |
| 159 |
$images_sizes = get_intermediate_image_sizes(); |
| 160 |
|
| 161 |
if ( ! in_array( $json_to_array['featured_image_size'], $images_sizes, true ) ) { |
| 162 |
unset( $json_to_array['featured_image_size'] ); |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
return $json_to_array; |
| 168 |
} |
| 169 |
|
| 170 |
add_filter( 'give_import_core_settings_data', 'give_import_core_settings_merge_image_size', 12, 2 ); |
| 171 |
|
| 172 |
/** |
| 173 |
* Give upload the image logo from the core setting data from the json files. |
| 174 |
* |
| 175 |
* @since 1.8.17 |
| 176 |
* |
| 177 |
* @param $json_to_array |
| 178 |
* @param string $type |
| 179 |
* |
| 180 |
* @return array $json_to_array |
| 181 |
*/ |
| 182 |
function give_import_core_settings_merge_upload_image( $json_to_array, $type ) { |
| 183 |
if ( 'merge' === $type ) { |
| 184 |
// Emails > Email Settings > Logo. |
| 185 |
if ( ! empty( $json_to_array['email_logo'] ) ) { |
| 186 |
|
| 187 |
// Need to require these files. |
| 188 |
if ( ! function_exists( 'media_handle_upload' ) ) { |
| 189 |
require_once ABSPATH . 'wp-admin/includes/image.php'; |
| 190 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 191 |
require_once ABSPATH . 'wp-admin/includes/media.php'; |
| 192 |
} |
| 193 |
|
| 194 |
$url = $json_to_array['email_logo']; |
| 195 |
$new_url = media_sideload_image( $url, 0, null, 'src' ); |
| 196 |
if ( ! is_wp_error( $new_url ) ) { |
| 197 |
$json_to_array['email_logo'] = $new_url; |
| 198 |
} else { |
| 199 |
unset( $json_to_array['email_logo'] ); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
return $json_to_array; |
| 205 |
} |
| 206 |
|
| 207 |
add_filter( 'give_import_core_settings_data', 'give_import_core_settings_merge_upload_image', 13, 2 ); |
| 208 |
|
| 209 |
/** |
| 210 |
* Give unset the license key from the core setting data from the json files. |
| 211 |
* |
| 212 |
* @since 1.8.17 |
| 213 |
* |
| 214 |
* @param array $json_to_array Data from json file |
| 215 |
* @param string $type |
| 216 |
* |
| 217 |
* @return array $json_to_array |
| 218 |
*/ |
| 219 |
function give_import_core_settings_merge_license_key( $json_to_array, $type ) { |
| 220 |
if ( 'merge' === $type ) { |
| 221 |
foreach ( $json_to_array as $key => $value ) { |
| 222 |
$is_license_key = strpos( '_license_key', $key ); |
| 223 |
if ( ! empty( $is_license_key ) ) { |
| 224 |
unset( $json_to_array[ $key ] ); |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
return $json_to_array; |
| 230 |
} |
| 231 |
|
| 232 |
add_filter( 'give_import_core_settings_data', 'give_import_core_settings_merge_license_key', 14, 2 ); |
| 233 |
|
| 234 |
/** |
| 235 |
* Give merge the json data and setting data. |
| 236 |
* |
| 237 |
* @since 1.8.17 |
| 238 |
* |
| 239 |
* @param $json_to_array |
| 240 |
* @param $type |
| 241 |
* @param $host_give_options |
| 242 |
* |
| 243 |
* @return array $json_to_array |
| 244 |
*/ |
| 245 |
function give_import_core_settings_merge_data( $json_to_array, $type, $host_give_options ) { |
| 246 |
if ( 'merge' === $type ) { |
| 247 |
$json_to_array_merge = array_merge( $host_give_options, $json_to_array ); |
| 248 |
$json_to_array = $json_to_array_merge; |
| 249 |
} |
| 250 |
|
| 251 |
return $json_to_array; |
| 252 |
} |
| 253 |
|
| 254 |
add_filter( 'give_import_core_settings_data', 'give_import_core_settings_merge_data', 1000, 3 ); |
| 255 |
|
| 256 |
/** |
| 257 |
* Backward Compatibility - Cleanup User Roles. |
| 258 |
* |
| 259 |
* @param array $caps List of capabilities. |
| 260 |
* |
| 261 |
* @since 1.8.17 |
| 262 |
* |
| 263 |
* @return mixed |
| 264 |
*/ |
| 265 |
function give_bc_1817_cleanup_user_roles( $caps ) { |
| 266 |
|
| 267 |
if ( |
| 268 |
! give_has_upgrade_completed( 'v1817_cleanup_user_roles' ) && |
| 269 |
! isset( $caps['view_give_payments'] ) |
| 270 |
) { |
| 271 |
give_v1817_process_cleanup_user_roles(); |
| 272 |
} |
| 273 |
|
| 274 |
return $caps; |
| 275 |
} |
| 276 |
|
| 277 |
add_filter( 'user_has_cap', 'give_bc_1817_cleanup_user_roles' ); |
| 278 |
|