Plugin Directory

source: wp-members/trunk/includes/vendor/rocketgeek-utilities/includes/utilities.php

Last change on this file was 3199979, checked in by cbutlerjr, 5 months ago

3.5.0 beta rc 2 release

File size: 9.6 KB
Line 
1<?php
2/**
3 * This file is part of the RocketGeek Utility Functions library.
4 *
5 * This library is open source and Apache-2.0 licensed. I hope you find it
6 * useful for your project(s). Attribution is appreciated ;-)
7 *
8 * @package    RocketGeek_Utilities
9 * @subpackage RocketGeek_Utilities_Utilities
10 * @version    1.0.3
11 *
12 * @link       https://github.com/rocketgeek/rocketgeek-utilities/
13 * @author     Chad Butler <https://butlerblog.com>
14 * @author     RocketGeek <https://rocketgeek.com>
15 * @copyright  Copyright (c) 2024 Chad Butler
16 * @license    Apache-2.0
17 *
18 * Copyright [2024] Chad Butler, RocketGeek
19 *
20 * Licensed under the Apache License, Version 2.0 (the "License");
21 * you may not use this file except in compliance with the License.
22 * You may obtain a copy of the License at
23 *
24 *     https://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS,
28 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 * See the License for the specific language governing permissions and
30 * limitations under the License.
31 */
32
33if ( ! function_exists( 'rktgk_force_ssl' ) ):
34/**
35 * Forces a URL to be secure (ssl).
36 *
37 * @since 1.0.0
38 *
39 * @param  string $url URL to be make secure.
40 * @return string      The secure URL.
41 */
42function rktgk_force_ssl( $url ) {
43        return ( is_ssl() ) ? preg_replace( "/^http:/i", "https:", $url ) : $url;
44}
45endif;
46
47if ( ! function_exists( 'rktgk_get_suffix' ) ):
48/**
49 * Determines whether to use a .min suffix for a script/style file.
50 *
51 * @since 1.0.0
52 *
53 * @param boolean $echo
54 */
55function rktgk_get_suffix( $echo = false ) {
56        $suffix = ( defined( 'SCRIPT_DEBUG' ) && true === SCRIPT_DEBUG ) ? '' : '.min';
57        if ( true === $echo ) {
58                echo $suffix;
59                return;
60        } else {
61                return $suffix;
62        }
63}
64endif;
65
66if ( ! function_exists( 'rktgk_maybe_unserialize' ) ):
67/**
68 * Better unserialization than WP's maybe_unserialize().
69 *
70 * Sanitizes array output before returning. If the unserialized result is an
71 * array, then it runs the result through wpmem_sanitize_array(), which
72 * sanitizes each individual array element.
73 *
74 * @since 1.0.0
75 *
76 * @param  mixed  $original
77 * @return mixed  $original
78 */
79function rktgk_maybe_unserialize( $original ) {
80        if ( is_serialized( $original ) ) { // don't attempt to unserialize data that wasn't serialized going in
81                $original = unserialize( $original );
82        }
83        return ( is_array( $original ) ) ? wpmem_sanitize_array( $original ) : $original;
84}
85endif;
86
87if ( ! function_exists( 'rktgk_maybe_wpautop' ) ):
88/**
89 * Run wpautop on content. Defaults to true.
90 *
91 * Useful for shortcodes that don't autop on their own.
92 * Toggle boolean can be passed as a string without pre-filtering
93 * since it runs rktgk_str_to_bool().
94 *
95 * @since 1.0.0
96 *
97 * @param  string  $content
98 * @param  mixed   $do_autop
99 * @return string  $content either autop'ed or not.
100 */
101function rktgk_maybe_wpautop( $content, $do_autop = true ) {
102        return ( true === rktgk_str_to_bool( $do_autop ) ) ? wpautop( $content ) : $content;
103}
104endif;
105
106if ( ! function_exists( 'rktgk_do_shortcode' ) ):
107/**
108 * Call a shortcode function by tag name.
109 *
110 * Use this function for directly calling a shortcode without using do_shortcode.
111 * do_shortcode() runs an extensive regex that goes through every shortcode in
112 * the WP global $shortcode_tags. That's a lot of processing wasted if all you
113 * want to do is run a specific shortcode/function. Yes, you could run the callback
114 * directly, but what if that callback is in a class instance method? This utlitiy
115 * allows you to run a shortcode function directly, regardless of whether it is
116 * a direct function or in a class. It comes from an article by J.D. Grimes on this
117 * subject and I've provided a link to that article.
118 *
119 * @author J.D. Grimes
120 * @link https://codesymphony.co/dont-do_shortcode/
121 *
122 * @since 1.0.0
123 *
124 * @param string $tag     The shortcode whose function to call.
125 * @param array  $atts    The attributes to pass to the shortcode function. Optional.
126 * @param array  $content The shortcode's content. Default is null (none).
127 *
128 * @return string|bool False on failure, the result of the shortcode on success.
129 */
130function rktgk_do_shortcode( $tag, array $atts = array(), $content = null ) {
131 
132        global $shortcode_tags;
133
134        if ( ! isset( $shortcode_tags[ $tag ] ) ) {
135                return false;
136        }
137
138        return call_user_func( $shortcode_tags[ $tag ], $atts, $content, $tag );
139}
140endif;
141
142if ( ! function_exists( 'rktgk_is_woo_active' ) ):
143/**
144 * Checks if WooCommerce is active.
145 *
146 * @link https://woocommerce.com/document/query-whether-woocommerce-is-activated/
147 *
148 * @since 1.0.0
149 *
150 * @return boolean
151 */
152function rktgk_is_woo_active() {
153        return ( class_exists( 'woocommerce' ) ) ? true : false;
154}
155endif;
156
157if ( ! function_exists( 'rktgk_get_user_ip' ) ):
158/**
159 * Get user IP address.
160 *
161 * From Pippin.
162 * @link https://gist.github.com/pippinsplugins/9641841
163 *
164 * @since 1.0.0
165 *
166 * @return string $ip.
167 */
168function rktgk_get_user_ip() {
169        if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
170                //check ip from share internet
171                $ip = $_SERVER['HTTP_CLIENT_IP'];
172        } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
173                //to check ip is pass from proxy
174                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
175        } else {
176                $ip = $_SERVER['REMOTE_ADDR'];
177        }
178
179        $sanitized_ip = sanitize_text_field( $ip );
180
181        /**
182         * Filter the IP result.
183         *
184         * @since 1.0.0
185         *
186         * @param string $sanitized_ip
187         */
188        return apply_filters( 'rktgk_get_user_ip', $sanitized_ip );
189}
190endif;
191
192if ( ! function_exists( 'rktgk_get_server_var' ) ) :
193/**
194 *  A getter for $_SERVER vars
195 *
196 * @since 1.0.2
197 *
198 * @param  string  $server_var
199 * @return string
200 */
201function rktgk_get_server_var( $server_var ) {
202        return ( isset( $_SERVER[ $server_var ] ) ) ? $_SERVER[ $server_var ] : '';
203}
204endif;
205
206if ( ! function_exists( 'rktgk_get_script_uri' ) ) :
207function rktgk_get_script_uri( $var = false ) {
208        $url = rktgk_get_server_var( 'SCRIPT_URI' );
209        if ( '' != $url ) {
210                return $url;
211        } else {
212                $url = rktgk_get_server_var( 'REQUEST_URI' );
213                if ( '' != $url ) {
214                        return $url;
215                } else {
216                        return rktgk_get_server_var( 'SCRIPT_NAME' );
217                }
218        }
219}
220endif;
221
222if ( ! function_exists( 'rktgk_build_html_tag' ) ) :
223/**
224 * Builds an HTML tag from provided attributes.
225 *
226 * @since 1.0.2
227 *
228 * @param  array  $args {
229 *     An array of attributes to build the html tag.
230 *
231 *     @type string  $tag              HTML tag to build.
232 *     @type array   $attributes|$atts Array of attributes of the tag, keyed as the attribute name.
233 *     @type string  $content          Content inside the wrapped tag (omit for self-closing tags).
234 * }
235 * @param boolean $echo
236 */
237function rktgk_build_html_tag( $args, $echo = false ) {
238       
239        // A list of self-closing tags (so $content is not used).
240        $self_closing_tags = array( 'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr' );
241
242        // Check for attributes and allow for shorthand "atts"
243        if ( isset( $args['attributes'] ) ) {
244                $attributes = $args['attributes'];
245        } elseif ( isset( $args['atts'] ) ) {
246                $attributes = $args['atts'];
247        } else {
248                $attributes = false;
249        }
250
251        // Assemble tag and attributes.
252        $tag = '<' . esc_attr( $args['tag'] );
253        if ( false != $attributes ) {
254                foreach ( $attributes as $attribute => $value ) {
255                        // Sanitize classes.
256                        $value = ( 'class' == $attribute || 'id' == $attribute ) ? rktgk_sanitize_class( $value ) : $value;
257
258                        // Escape urls and remaining attributes.
259                        $esc_value = ( 'href' == $attribute ) ? esc_url( $value ) : esc_attr( $value );
260                       
261                        // Continue tag assembly.
262                        $tag .= ' ' . esc_attr( $attribute ) . '="' . $esc_value . '"';
263                }
264        }
265
266        // If tag is self closing.
267        if ( in_array( $args['tag'], $self_closing_tags ) ) {
268                $tag .= ' />';
269        } else {
270                // If tag is a wrapped tag.
271                $tag .= '>' . esc_html( $args['content'] ) . '</' . esc_attr( $args['tag'] ) . '>';
272        }
273
274        if ( $echo ) {
275                echo $tag;
276        } else {
277                return $tag;
278        }
279}
280endif;
281
282if ( ! function_exists( 'rktgk_wc_checkout_fields' ) ):
283function rktgk_wc_checkout_fields() {
284        return array(
285                // Billing checkout fields.
286                'billing_first_name',
287                'billing_last_name',
288                'billing_company',
289                'billing_address_1',
290                'billing_address_2',
291                'billing_city',
292                'billing_postcode',
293                'billing_country',
294                'billing_state',
295                'billing_email',
296                'billing_phone',
297
298                // Shipping checkout fields.
299                'shipping_first_name',
300                'shipping_last_name',
301                'shipping_company',
302                'shipping_address_1',
303                'shipping_address_2',
304                'shipping_city',
305                'shipping_postcode',
306                'shipping_country',
307                'shipping_state',
308
309                // Account checkout fields.
310                'account_username',
311                'account_passoword',
312                'account_password-2',
313
314                // Order checkout fields.
315                'order_comments',
316        );
317}
318endif;
319
320if ( ! function_exists( 'rktgk_plugin_pathinfo' ) ) :
321function rktgk_plugin_pathinfo( $magic_file ) {
322        $path_parts      = pathinfo( $magic_file );
323        $plugin_dir_path = plugin_dir_path( $magic_file );
324        $basename        = basename( $plugin_dir_path );
325
326        $path_info = array( 
327                'magic_file'    => $magic_file,
328                'path'          => $plugin_dir_path,
329                'url'           => plugin_dir_url ( $magic_file ),
330                'wp_name'       => trailingslashit( $basename ) . $path_parts['basename'],
331                'basename'      => $basename,
332                'slug'          => $path_parts['filename'],
333        );
334
335        return apply_filters( 'rktgk_plugin_pathinfo', $path_info, $magic_file );
336}
337endif;
Note: See TracBrowser for help on using the repository browser.