block
3 years ago
services
3 years ago
widget
3 years ago
class-cookie-solution-generator.php
3 years ago
class-iubenda-amp.php
3 years ago
class-iubenda-forms.php
3 years ago
class-iubenda-list-table-forms.php
3 years ago
class-iubenda-notice.php
3 years ago
class-iubenda-settings.php
3 years ago
class-language-helper.php
3 years ago
class-privacy-policy-generator.php
3 years ago
class-product-helper.php
3 years ago
class-quick-generator-service.php
3 years ago
class-radar-service.php
3 years ago
class-service-rating.php
3 years ago
class-terms-conditions-generator.php
3 years ago
functions.php
3 years ago
functions.php
257 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Common functions. |
| 4 | * |
| 5 | * @package Iubenda |
| 6 | */ |
| 7 | |
| 8 | // Exit if accessed directly. |
| 9 | if ( ! defined( 'ABSPATH' ) ) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | if ( ! function_exists( 'iub_array_get' ) ) { |
| 14 | /** |
| 15 | * Iubenda array get |
| 16 | * |
| 17 | * @param array $array An array from which we want to retrieve some information. |
| 18 | * @param string $key A string separated by . of keys describing the path with which to retrieve information. |
| 19 | * @param mixed $default Optional. The return value if the path does not exist within the array. |
| 20 | * |
| 21 | * @return array|ArrayAccess|mixed|null |
| 22 | */ |
| 23 | function iub_array_get( $array, $key, $default = null ) { |
| 24 | if ( ! ( is_array( $array ) || $array instanceof ArrayAccess ) ) { |
| 25 | return $default instanceof Closure ? $default() : $default; |
| 26 | } |
| 27 | |
| 28 | if ( is_null( $key ) ) { |
| 29 | return $array; |
| 30 | } |
| 31 | |
| 32 | if ( array_key_exists( $key, $array ) ) { |
| 33 | return $array[ $key ]; |
| 34 | } |
| 35 | |
| 36 | if ( strpos( $key, '.' ) === false ) { |
| 37 | return $array[ $key ] ?? ( $default instanceof Closure ? $default() : $default ); |
| 38 | } |
| 39 | |
| 40 | foreach ( explode( '.', $key ) as $segment ) { |
| 41 | if ( ( is_array( $array ) || $array instanceof ArrayAccess ) && ( array_key_exists( $segment, $array ) ) ) { |
| 42 | $array = $array[ $segment ]; |
| 43 | } else { |
| 44 | return $default instanceof Closure ? $default() : $default; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return $array; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | if ( ! function_exists( 'iub_array_only' ) ) { |
| 53 | /** |
| 54 | * Return only intersects keys with the array |
| 55 | * |
| 56 | * @param array $array Array. |
| 57 | * @param array|string $keys Keys. |
| 58 | * |
| 59 | * @return array |
| 60 | */ |
| 61 | function iub_array_only( array $array, $keys ) { |
| 62 | return array_intersect_key( $array, array_flip( (array) $keys ) ); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if ( ! function_exists( '__iub_trans' ) ) { |
| 67 | /** |
| 68 | * Translate a specific string into a specific language. |
| 69 | * |
| 70 | * @param string $string specific string. |
| 71 | * @param string $locale specific language. |
| 72 | * @param string $text_domain Optional. as default iubenda. |
| 73 | * |
| 74 | * @return string|void |
| 75 | */ |
| 76 | function __iub_trans( $string, $locale, $text_domain = 'iubenda' ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore |
| 77 | |
| 78 | $mo = new MO(); |
| 79 | $mofile = IUBENDA_PLUGIN_PATH . 'languages/' . $text_domain . '-' . $locale . '.mo'; |
| 80 | if ( file_exists( $mofile ) ) { |
| 81 | $mo->import_from_file( $mofile ); |
| 82 | // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.NonSingularStringLiteralDomain |
| 83 | $string = esc_html__( $mo->translate( $string ) ); |
| 84 | } |
| 85 | |
| 86 | // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText, WordPress.WP.I18n.NonSingularStringLiteralDomain |
| 87 | return $string; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if ( ! function_exists( 'iub_verify_ajax_request' ) ) { |
| 92 | /** |
| 93 | * Custom ajax verification request with nonce check and permission check. |
| 94 | * |
| 95 | * @param int|string $action Action nonce. |
| 96 | * @param false|string $query_arg Optional. Key to check for the nonce in `$_REQUEST` (since 2.5). If false, |
| 97 | * `$_REQUEST` values will be evaluated for '_ajax_nonce', and '_wpnonce' |
| 98 | * (in that order). Default false. |
| 99 | * @param string $capability Capability name. |
| 100 | * @param bool $die Optional. Whether to die early when the nonce cannot be verified. |
| 101 | * Default true. |
| 102 | * |
| 103 | * @return void |
| 104 | */ |
| 105 | function iub_verify_ajax_request( $action, $query_arg = false, $capability = 'manage_options', $die = false ) { |
| 106 | if ( |
| 107 | ! check_ajax_referer( $action, $query_arg, $die ) || |
| 108 | ! current_user_can( apply_filters( 'iubenda_cookie_law_cap', $capability ) ) |
| 109 | ) { |
| 110 | wp_die( esc_html__( 'Sorry, you are not authorized to perform this action.' ), 403 ); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if ( ! function_exists( 'iub_verify_user_capability' ) ) { |
| 116 | /** |
| 117 | * Check if the current user have any capability or not. |
| 118 | * |
| 119 | * @param array $capabilities Capability names. |
| 120 | * |
| 121 | * @return void |
| 122 | */ |
| 123 | function iub_verify_user_capability( array $capabilities = array() ) { |
| 124 | if ( ! $capabilities ) { |
| 125 | // Set default capability. |
| 126 | $capabilities = array( 'manage_options' ); |
| 127 | } |
| 128 | |
| 129 | foreach ( $capabilities as $capability ) { |
| 130 | if ( current_user_can( apply_filters( 'iubenda_cookie_law_cap', $capability ) ) ) { |
| 131 | return; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | wp_die( esc_html__( 'Sorry, you are not authorized to perform this action.' ), 403 ); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if ( ! function_exists( 'iub_verify_postback_request' ) ) { |
| 140 | /** |
| 141 | * Custom postback verification request with nonce check and permission check. |
| 142 | * |
| 143 | * @param int|string $action Action nonce. |
| 144 | * @param false|string $query_arg Optional. Key to check for the nonce in `$_REQUEST` (since 2.5). If false, |
| 145 | * `$_REQUEST` values will be evaluated for '_ajax_nonce', and '_wpnonce' |
| 146 | * (in that order). Default false. |
| 147 | * @param string $capability Capability name. |
| 148 | * |
| 149 | * @return void |
| 150 | */ |
| 151 | function iub_verify_postback_request( $action, $query_arg = '_wpnonce', $capability = 'manage_options' ) { |
| 152 | if ( |
| 153 | ! check_admin_referer( $action, $query_arg ) || |
| 154 | ! current_user_can( apply_filters( 'iubenda_cookie_law_cap', $capability ) ) |
| 155 | ) { |
| 156 | wp_die( esc_html__( 'Sorry, you are not authorized to perform this action.' ), 403 ); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if ( ! function_exists( 'iub_get_request_parameter' ) ) { |
| 162 | /** |
| 163 | * Gets the request parameter. |
| 164 | * |
| 165 | * @param string $key The query parameter. |
| 166 | * @param string $default The default value to return if not found. |
| 167 | * |
| 168 | * @return string The request parameter. |
| 169 | */ |
| 170 | function iub_get_request_parameter( string $key, $default = '' ) { |
| 171 | // If key not exist or empty return default. |
| 172 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 173 | if ( ! isset( $_REQUEST[ $key ] ) || empty( $_REQUEST[ $key ] ) ) { |
| 174 | return $default; |
| 175 | } |
| 176 | |
| 177 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 178 | return sanitize_key( $_REQUEST[ $key ] ); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | if ( ! function_exists( 'iub_is_polylang_active' ) ) { |
| 183 | /** |
| 184 | * Check if Polylang plugin installed and activated. |
| 185 | * |
| 186 | * @return bool |
| 187 | */ |
| 188 | function iub_is_polylang_active() { |
| 189 | $result = false; |
| 190 | if ( ! function_exists( 'is_plugin_active' ) ) { |
| 191 | include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 192 | } |
| 193 | |
| 194 | $polylang_plugin_status = is_plugin_active( 'polylang/polylang.php' ) || is_plugin_active( 'polylang-pro/polylang.php' ); |
| 195 | |
| 196 | if ( $polylang_plugin_status && function_exists( 'PLL' ) ) { |
| 197 | $result = true; |
| 198 | } |
| 199 | return $result; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | if ( ! function_exists( 'iub_is_wpml_active' ) ) { |
| 204 | /** |
| 205 | * Check if WPML plugin installed and activated. |
| 206 | * |
| 207 | * @return bool |
| 208 | */ |
| 209 | function iub_is_wpml_active() { |
| 210 | $result = false; |
| 211 | if ( ! function_exists( 'is_plugin_active' ) ) { |
| 212 | include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 213 | } |
| 214 | |
| 215 | $sitepress_plugin_status = is_plugin_active( 'sitepress-multilingual-cms/sitepress.php' ) || is_plugin_active( 'wpml-multilingual-cms/sitepress.php' ); |
| 216 | |
| 217 | if ( $sitepress_plugin_status && class_exists( 'SitePress' ) ) { |
| 218 | $result = true; |
| 219 | } |
| 220 | return $result; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if ( ! function_exists( 'iub_caught_exception' ) ) { |
| 225 | /** |
| 226 | * When catching an exception, this allows us to log it if unexpected. |
| 227 | * |
| 228 | * @param Exception|Error $e The exception object. |
| 229 | */ |
| 230 | function iub_caught_exception( $e ) { |
| 231 | $message = 'Exception (' . get_class( $e ) . ') occurred during enqueuing embed code. Exception Message: ' . $e->getMessage() . ' (Code: ' . $e->getCode() . ', line ' . $e->getLine() . ' in ' . $e->getFile() . ')'; |
| 232 | |
| 233 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 234 | error_log( "Exception caught. $message." ); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | if ( ! function_exists( 'can_use_dom_document_class' ) ) { |
| 239 | |
| 240 | /** |
| 241 | * Check if Dom document not exists or there is outdated modules. |
| 242 | * |
| 243 | * @return bool |
| 244 | */ |
| 245 | function can_use_dom_document_class() { |
| 246 | if ( ! class_exists( 'DOMDocument' ) ) { |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | if ( defined( 'LIBXML_DOTTED_VERSION' ) && extension_loaded( 'libxml' ) ) { |
| 251 | return ! version_compare( LIBXML_DOTTED_VERSION, '2.7.8', '<' ); |
| 252 | } |
| 253 | |
| 254 | return true; |
| 255 | } |
| 256 | } |
| 257 |