Last active
August 13, 2019 22:52
-
-
Save camaleaun/30e84d6a4ce05b758e0f5bdbc3b1deca to your computer and use it in GitHub Desktop.
Funcions Utils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_filter( 'themename_twbs_bootstrap_defaults', 'themename_twbs_bootstrap_defaults' ); | |
| function themename_twbs_bootstrap_defaults( $defaults ) { | |
| $defaults['version'] = '3.3.7'; | |
| $defaults['files'] = 'both'; | |
| $defaults['cdn'] = 'maxcdn'; | |
| $defaults['css_path'] = get_theme_file_uri( 'assets/css' ); | |
| $defaults['js_path'] = get_theme_file_uri( 'assets/js' ); | |
| return $defaults; | |
| } | |
| if ( ! function_exists( 'themename_register_twbs_bootstrap' ) ) { | |
| /** | |
| * Register Twitter Bootstrap. | |
| * | |
| * @version 1.0.0 | |
| * @link https://gist.github.com/camaleaun/30e84d6a4ce05b758e0f5bdbc3b1deca | |
| */ | |
| function themename_register_twbs_bootstrap( $args = array() ) { | |
| $defaults = apply_filters( | |
| 'themename_twbs_bootstrap_defaults', | |
| array( | |
| 'version' => 'latest', | |
| 'handle' => 'twbs', | |
| 'files' => 'css', | |
| 'minified' => true, | |
| 'css_path' => get_theme_file_uri( 'assets/css/vendor/bootstrap' ), | |
| 'js_path' => get_theme_file_uri( 'assets/js/vendor/bootstrap' ), | |
| 'css_option' => 'full', | |
| 'js_bundle' => true, | |
| 'js_in_footer' => true, | |
| 'popper' => 'separated', | |
| 'cdn' => 'stackpath', | |
| 'ssl' => 'https' === parse_url( home_url(), PHP_URL_SCHEME ), | |
| ); | |
| $args = wp_parse_args( | |
| $args, | |
| $defaults | |
| ); | |
| $cdn_domain = $args['cdn'] . '.bootstrapcdn.com'; | |
| $handle = $args['handle']; | |
| $ver = false; | |
| $http = new WP_Http(); | |
| $theme_ver = false; | |
| if ( 'latest' === $args['version'] ) { | |
| $theme_ver = get_option( 'themename_twbs_bootstrap_version' ); | |
| } | |
| if ( $theme_ver ) { | |
| $args['version'] = $theme_ver; | |
| } elseif ( 'latest' === $args['version'] ) { | |
| $url = 'https://api.github.com/repos/twbs/bootstrap/releases/latest'; | |
| $response = wp_remote_get( | |
| esc_url_raw( $url ), | |
| array( | |
| 'headers' => array( 'Accept: application/vnd.github.v3+json' ), | |
| ) | |
| ); | |
| $response_code = wp_remote_retrieve_response_code( $response ); | |
| if ( $http::OK === $response_code ) { | |
| $api_response = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| $args['version'] = preg_replace( '/^v/', '', $api_response['tag_name'] ); | |
| update_option( 'themename_twbs_bootstrap_version', $args['version'] ); | |
| } | |
| } | |
| if ( preg_match( '/\d\.\d\.\d/', $args['version'] ) ) { | |
| $ver = $args['version']; | |
| } | |
| if ( $ver ) { | |
| $url = "$schema$cdn_domain/bootstrap/$ver/"; | |
| $files = array( 'css', 'js' ); | |
| if ( is_string( $args['files'] ) && in_array( $args['files'], $files ) ) { | |
| $files = array( $args['files'] ); | |
| } | |
| $suffix = $args['minified'] ? '.min' : ''; | |
| foreach ( $files as $type ) { | |
| $option = ''; | |
| if ( version_compare( $ver, '4.0.0' ) >= 0 ) { | |
| if ( | |
| 'css' === $type | |
| && | |
| in_array( $args['css_option'], array( 'grid', 'reboot' ) ) | |
| ) { | |
| $option = '-' . $args['css_option']; | |
| } elseif ( 'js' === $type && $args['js_bundle'] ) { | |
| $option = '.bundle'; | |
| } | |
| } | |
| $file = "bootstrap$option$suffix.$type"; | |
| $function = 'wp_register_' . ( 'css' === $type ? 'style' : 'script' ); | |
| $src = $url . "$type/" . $file; | |
| $response = wp_remote_get( esc_url_raw( $src ) ); | |
| $response_code = wp_remote_retrieve_response_code( $response ); | |
| if ( $http::OK != $response_code ) { | |
| $path = untrailingslashit( $args[ "{$type}_path" ] ); | |
| $src = $path . '/' . $file; | |
| } | |
| $deps = array(); | |
| if ( 'js' === $type ) { | |
| $deps = array( 'jquery' ); | |
| } | |
| $last = 'css' === $type ? 'all' : (bool) $args['js_in_footer']; | |
| call_user_func( $function, $handle, $src, $deps, $ver, $last ); | |
| } | |
| } | |
| return $handle; | |
| } | |
| } | |
| /** | |
| * Remove not numbers. | |
| * | |
| * @param string $value Value. | |
| * @return string | |
| */ | |
| function themeplugin_numbers( $value ) { | |
| return preg_replace( '/\D/', '', $value ); | |
| } | |
| /** | |
| * Check lenth of numbers in CPF. | |
| * | |
| * @param string $cpf CPF number. | |
| * @return bool | |
| */ | |
| function themeplugin_maybe_cpf( $cpf ) { | |
| return strlen( themeplugin_numbers( $cpf ) ) === 11; | |
| } | |
| /** | |
| * Check validate of CPF number. | |
| * | |
| * @param string $cpf CPF number. | |
| * @return bool | |
| */ | |
| function themeplugin_is_cpf_valid( $cpf ) { | |
| // Keep only numbers. | |
| $cpf = themeplugin_numbers( $cpf ); | |
| // Ensures correct size. | |
| if ( ! themeplugin_maybe_cpf( $cpf ) ) { | |
| return false; | |
| } | |
| // Ignore false positives when only repeating single number. | |
| if ( preg_match( '/^(\d)\1+$/', $cpf ) ) { | |
| return false; | |
| } | |
| // Interacts for each check digit. | |
| for ( $n = 0; $n < 2; $n++ ) { | |
| $sum = 0; | |
| // Multiply positions by descending 10 and 11 in the second interaction, accumulating the sum. | |
| for ( $i = 10 + $n; $i > 1; $i-- ) { | |
| $sum += substr( $cpf, abs( $i - 10 - $n ), 1 ) * $i; | |
| } | |
| // Calculate module 11 for the calculated digit. | |
| $d = 11 - $sum % 11; | |
| // Case 10 replaces with 0. | |
| $d = $d <= 9 ? $d : 0; | |
| // False when not matching calculated digit with check. | |
| if ( intval( substr( $cpf, 9 + $n, 1 ) ) !== $d ) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| /** | |
| * Removes a function from filter by class name compare. | |
| * | |
| * @global array $wp_filter Stores all of the filters | |
| * | |
| * @param string $tag The filter hook to which the function to be removed is hooked. | |
| * @param callable $function_to_remove The name of the function which should be removed. | |
| * @param int $priority Optional. The priority of the function. Default 10. | |
| * @return bool Whether the function existed before it was removed. | |
| */ | |
| function themeplugin_remove_filter_object( $tag, $function_to_remove, $priority = 10 ) { | |
| global $wp_filter; | |
| $r = false; | |
| if ( is_array( $function_to_remove ) && is_object( current( $function_to_remove ) ) ) { | |
| $class_to_remove = get_class( current( $function_to_remove ) ); | |
| if ( isset( $wp_filter[ $tag ] ) ) { | |
| if ( isset( $wp_filter[ $tag ]->callbacks[ $priority ] ) ) { | |
| foreach ( $wp_filter[ $tag ]->callbacks[ $priority ] as $idx => $callback ) { | |
| if ( is_array( $callback['function'] ) && is_object( current( $callback['function'] ) ) ) { | |
| $same_class = get_class( current( $callback['function'] ) ) === $class_to_remove; | |
| if ( $same_class && $function_to_remove[1] === $callback['function'][1] ) { | |
| unset( $wp_filter[ $tag ]->callbacks[ $priority ][ $idx ] ); | |
| $r = true; | |
| } | |
| } | |
| if ( empty( $wp_filter[ $tag ]->callbacks[ $priority ] ) ) { | |
| unset( $wp_filter[ $tag ]->callbacks[ $priority ] ); | |
| } | |
| } | |
| } | |
| if ( ! $wp_filter[ $tag ]->callbacks ) { | |
| unset( $wp_filter[ $tag ] ); | |
| } | |
| } | |
| } else { | |
| $r = remove_filter( $tag, $function_to_remove, $priority ); | |
| } | |
| return $r; | |
| } | |
| /** | |
| * Custom country address format. | |
| * | |
| * @param array $replacements Default replacements. | |
| * @param array $args Arguments to replace. | |
| * | |
| * @return array | |
| */ | |
| function themeplugin_formatted_address_replacements( $replacements, $args ) { | |
| if ( class_exists( 'Extra_Checkout_Fields_For_Brazil_Front_End' ) ) { | |
| $replacements['{number}'] = isset( $args['number'] ) ? $args['number'] : ''; | |
| $replacements['{neighborhood}'] = isset( $args['neighborhood'] ) ? $args['neighborhood'] : ''; | |
| } | |
| return $replacements; | |
| } | |
| /** | |
| * Replace Extra Checkout Fields For Brazil function. | |
| */ | |
| function themeplugin_replace_formatted_address_replacements() { | |
| $extra_checkout_fields_for_brazil = new Extra_Checkout_Fields_For_Brazil_Front_End(); | |
| themeplugin_remove_filter_object( 'woocommerce_formatted_address_replacements', array( $extra_checkout_fields_for_brazil, 'formatted_address_replacements' ), 1, 2 ); | |
| add_filter( 'woocommerce_formatted_address_replacements', 'themeplugin_formatted_address_replacements', 1, 2 ); | |
| } | |
| add_action( 'plugins_loaded', 'themeplugin_replace_formatted_address_replacements', 11 ); | |
| /** | |
| * Add shop manager cabilities on create user. | |
| * | |
| * @param array $allcaps All capabilities. | |
| * @param array $caps Capabilities. | |
| * @param array $args Arguments. | |
| * @param object $user User. | |
| * @return array | |
| */ | |
| function themeplugin_user_has_cap_promote_users( $allcaps, $caps, $args, $user ) { | |
| if ( count( $user->caps ) === 1 && isset( $user->caps['shop_manager'] ) && $user->caps['shop_manager'] ) { | |
| $shop_manager_editable_roles = apply_filters( 'woocommerce_shop_manager_editable_roles', array( 'customer' ) ); | |
| if ( isset( $_POST['createuser'] ) && in_array( $_POST['role'], $shop_manager_editable_roles, true ) ) { // phpcs:ignore WordPress.Security | |
| $allcaps['promote_users'] = 1; | |
| } | |
| } | |
| return $allcaps; | |
| } | |
| add_filter( 'user_has_cap', 'themeplugin_user_has_cap_promote_users', 10, 4 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment