PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.1
6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 5.3.1 All 152 releases
mainwp / includes / core-functions.php

core-functions.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 6.1, at includes/core-functions.php

162 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Base Functions.
4 *
5 * Grab MainWP Directory and check for permissions.
6 *
7 * @package MainWP/Dashboard
8 * @author Woocommerce Authors.
9 */
10
11 // Exit if accessed directly.
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * API - Hash.
18 *
19 * @since 5.2
20 * @param string $data Message to be hashed.
21 * @return string
22 */
23 function mainwp_api_hash( $data ) {
24 return hash_hmac( 'sha256', $data, 'mainwp-api' );
25 }
26
27 /**
28 * Parses and formats a date for ISO8601/RFC3339.
29 *
30 * Required WP 4.4 or later.
31 * See https://developer.wordpress.org/reference/functions/mysql_to_rfc3339/
32 *
33 * @since 5.2
34 * @param string|null|DateTime $date Date.
35 * @param bool $utc Send false to get local/offset time.
36 * @return string|null ISO8601/RFC3339 formatted datetime.
37 */
38 function mainwp_rest_prepare_date_response( $date, $utc = true ) {
39 if ( is_numeric( $date ) ) {
40 $date = new MainWP_DateTime( "@$date", new DateTimeZone( 'UTC' ) );
41 $date->setTimezone( new DateTimeZone( wp_timezone_string() ) );
42 } elseif ( is_string( $date ) ) {
43 $date = new MainWP_DateTime( $date, new DateTimeZone( 'UTC' ) );
44 $date->setTimezone( new DateTimeZone( wp_timezone_string() ) );
45 }
46
47 if ( ! is_a( $date, 'MainWP_DateTime' ) ) {
48 return null;
49 }
50
51 // Get timestamp before changing timezone to UTC.
52 return gmdate( 'Y-m-d\TH:i:s', $utc ? $date->getTimestamp() : $date->getOffsetTimestamp() );
53 }
54
55
56
57 /**
58 * Converts a string (e.g. 'yes' or 'no') to a bool.
59 *
60 * @since 5.2
61 * @param string|bool $str String to convert. If a bool is passed it will be returned as-is.
62 * @return bool
63 */
64 function mainwp_string_to_bool( $str ) {
65 $str = $str ?? '';
66 return is_bool( $str ) ? $str : ( 'yes' === strtolower( $str ) || 1 === $str || 'true' === strtolower( $str ) || '1' === $str );
67 }
68
69 /**
70 * Encodes a value according to RFC 3986.
71 * Supports multidimensional arrays.
72 *
73 * @since 2.6.0
74 * @param string|array $value The value to encode.
75 * @return string|array Encoded values.
76 */
77 function mainwp_rest_urlencode_rfc3986( $value ) {
78 if ( is_array( $value ) ) {
79 return array_map( 'mainwp_rest_urlencode_rfc3986', $value );
80 }
81
82 return str_replace( array( '+', '%7E' ), array( ' ', '~' ), rawurlencode( $value ) );
83 }
84
85
86 /**
87 * Method mainwp_search_in_array().
88 *
89 * @param array $data The data.
90 * @param string $search_str search string.
91 * @param array $params params.
92 *
93 * @return array Search result.
94 */
95 function mainwp_search_in_array( $data, $search_str, $params = array() ) { //phpcs:ignore -- NOSONAR complex function.
96
97 if ( ! is_array( $params ) ) {
98 $params = array();
99 }
100
101 $search_in_key = isset( $params['search_in_key'] ) ? $params['search_in_key'] : true;
102 $search_in_value = isset( $params['search_in_value'] ) ? $params['search_in_value'] : true;
103 $in_sub_fields = isset( $params['in_sub_fields'] ) ? $params['in_sub_fields'] : '';
104 $use_index_result = isset( $params['use_index_result'] ) ? $params['use_index_result'] : true;
105
106 $search_str = is_string( $search_str ) ? trim( $search_str ) : '';
107
108 if ( empty( $search_str ) ) {
109 return $data;
110 }
111
112 if ( ! is_array( $data ) || empty( $data ) ) {
113 return $data;
114 }
115
116 $results = array();
117
118 foreach ( $data as $key => $value ) {
119 if ( $search_in_key && false !== stripos( $key, $search_str ) ) {
120 if ( $use_index_result ) {
121 $results[ $key ] = $value;
122 } else {
123 $results[] = $value;
124 }
125
126 continue;
127 }
128 if ( $search_in_value && is_string( $value ) && false !== stripos( $value, $search_str ) ) {
129 if ( $use_index_result ) {
130 $results[ $key ] = $value;
131 } else {
132 $results[] = $value;
133 }
134 continue;
135 }
136
137 if ( ! empty( $in_sub_fields ) ) {
138 if ( is_string( $in_sub_fields ) ) {
139 if ( is_array( $value ) && ! empty( $value[ $in_sub_fields ] ) && is_string( $value[ $in_sub_fields ] ) && false !== stripos( $value[ $in_sub_fields ], $search_str ) ) {
140 if ( $use_index_result ) {
141 $results[ $key ] = $value;
142 } else {
143 $results[] = $value;
144 }
145 }
146 } elseif ( is_array( $in_sub_fields ) ) {
147 foreach ( $in_sub_fields as $in_sub ) {
148 if ( is_array( $value ) && ! empty( $value[ $in_sub ] ) && is_string( $value[ $in_sub ] ) && false !== stripos( $value[ $in_sub ], $search_str ) ) {
149 if ( $use_index_result ) {
150 $results[ $key ] = $value;
151 } else {
152 $results[] = $value;
153 }
154 }
155 break;
156 }
157 }
158 }
159 }
160 return $results;
161 }
162