Form
5 years ago
Frontend
5 years ago
Gateways
5 years ago
ArrayDataSet.php
5 years ago
Hooks.php
5 years ago
Table.php
5 years ago
Utils.php
6 years ago
Utils.php
107 lines
| 1 | <?php |
| 2 | namespace Give\Helpers; |
| 3 | |
| 4 | /** |
| 5 | * Class Utils |
| 6 | * |
| 7 | * @package Give\Helpers |
| 8 | */ |
| 9 | class Utils { |
| 10 | /** |
| 11 | * Extract query param from URL |
| 12 | * |
| 13 | * @since 2.7.0 |
| 14 | * |
| 15 | * @param string $url |
| 16 | * @param string $queryParamName |
| 17 | * @param mixed $default |
| 18 | * |
| 19 | * @return string |
| 20 | */ |
| 21 | public static function getQueryParamFromURL( $url, $queryParamName, $default = '' ) { |
| 22 | $queryArgs = wp_parse_args( parse_url( $url, PHP_URL_QUERY ) ); |
| 23 | |
| 24 | return isset( $queryArgs[ $queryParamName ] ) ? give_clean( $queryArgs[ $queryParamName ] ) : $default; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * This function will change request url with other url. |
| 29 | * |
| 30 | * @since 2.7.0 |
| 31 | * |
| 32 | * @param string $location Requested URL. |
| 33 | * @param string $url URL. |
| 34 | * @param array $removeArgs Remove extra query params. |
| 35 | * @param array $addArgs add extra query params. |
| 36 | * |
| 37 | * @return string |
| 38 | */ |
| 39 | public static function switchRequestedURL( $location, $url, $addArgs = [], $removeArgs = [] ) { |
| 40 | $queryString = []; |
| 41 | |
| 42 | if ( $index = strpos( $location, '?' ) ) { |
| 43 | $queryString = wp_parse_args( substr( $location, strpos( $location, '?' ) + 1 ) ); |
| 44 | } |
| 45 | |
| 46 | if ( $index = strpos( $url, '?' ) ) { |
| 47 | $queryString = array_merge( $queryString, wp_parse_args( substr( $url, strpos( $url, '?' ) + 1 ) ) ); |
| 48 | } |
| 49 | |
| 50 | $url = add_query_arg( |
| 51 | $queryString, |
| 52 | $url |
| 53 | ); |
| 54 | |
| 55 | if ( $removeArgs ) { |
| 56 | foreach ( $removeArgs as $name ) { |
| 57 | $url = add_query_arg( [ $name => false ], $url ); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | if ( $addArgs ) { |
| 62 | foreach ( $addArgs as $name => $value ) { |
| 63 | $url = add_query_arg( [ $name => $value ], $url ); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return $url; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Remove giveDonationAction from URL. |
| 72 | * |
| 73 | * @since 2.7.0 |
| 74 | * @param $url |
| 75 | * |
| 76 | * @return string |
| 77 | */ |
| 78 | public static function removeDonationAction( $url ) { |
| 79 | return add_query_arg( [ 'giveDonationAction' => false ], $url ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Determines whether a plugin is active. |
| 84 | * |
| 85 | * Only plugins installed in the plugins/ folder can be active. |
| 86 | * |
| 87 | * Plugins in the mu-plugins/ folder can't be "activated," so this function will |
| 88 | * return false for those plugins. |
| 89 | * |
| 90 | * For more information on this and similar theme functions, check out |
| 91 | * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
| 92 | * Conditional Tags} article in the Theme Developer Handbook. |
| 93 | * |
| 94 | * @since 2.7.0 |
| 95 | * |
| 96 | * @param string $plugin Path to the plugin file relative to the plugins directory. |
| 97 | * @return bool True, if in the active plugins list. False, not in the list. |
| 98 | */ |
| 99 | public static function isPluginActive( $plugin ) { |
| 100 | if ( ! function_exists( 'is_plugin_active' ) ) { |
| 101 | include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 102 | } |
| 103 | |
| 104 | return is_plugin_active( $plugin ); |
| 105 | } |
| 106 | } |
| 107 |