PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.8.0
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.8.0
1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 1.8.0 All 210 releases
charitable / includes / admin / campaign-builder / util.php

util.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.8.0, at includes/admin/campaign-builder/util.php

207 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Util functions.
4 *
5 * @package Charitable
6 * @author David Bisset
7 * @copyright Copyright (c) 2023, WP Charitable LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.8.0
10 * @version 1.8.0
11 * @phpcs:disable Universal.Arrays.DisallowShortArraySyntax.Found
12 */
13
14 /**
15 * Get sanitized post title or "no title" placeholder.
16 *
17 * The placeholder is prepended with post ID.
18 *
19 * @since 1.8.0
20 *
21 * @param WP_Post|object $post Post object.
22 *
23 * @return string Post title.
24 */
25 function charitable_get_post_title( $post ) {
26
27 /* translators: %d - a post ID. */
28 return charitable_is_empty_string( trim( $post->post_title ) ) ? sprintf( __( '#%d (no title)', 'charitable' ), absint( $post->ID ) ) : $post->post_title;
29 }
30
31 /**
32 * Check if a string is empty.
33 *
34 * @since 1.8.0
35 *
36 * @param string $the_string String to test.
37 *
38 * @return bool
39 */
40 function charitable_is_empty_string( $the_string ) {
41
42 return is_string( $the_string ) && $the_string === '';
43 }
44
45 /**
46 * Changes array of items into string of items, separated by comma and sql-escaped.
47 *
48 * @see https://coderwall.com/p/zepnaw
49 *
50 * @since 1.8.0
51 *
52 * @param mixed|array $items Item(s) to be joined into string.
53 * @param string $format Can be %s or %d.
54 *
55 * @return string Items separated by comma and sql-escaped.
56 */
57 function charitable_wpdb_prepare_in( $items, $format = '%s' ) {
58
59 global $wpdb;
60
61 $items = (array) $items;
62 $how_many = count( $items );
63
64 if ( $how_many === 0 ) {
65 return '';
66 }
67
68 $placeholders = array_fill( 0, $how_many, $format );
69 $prepared_format = implode( ',', $placeholders );
70
71 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
72 return $wpdb->prepare( $prepared_format, $items );
73 }
74
75 /**
76 * Settings select field callback.
77 *
78 * @since 1.8.0
79 *
80 * @param array $args Arguments.
81 *
82 * @return string
83 */
84 function charitable_settings_select_callback( $args ) {
85
86 $default = isset( $args['default'] ) ? esc_html( $args['default'] ) : '';
87 $value = isset( $args['id'] ) ? esc_html( $args['id'] ) : $default; // charitable_setting( $args['id'], $default );.
88 $id = sanitize_key( $args['id'] );
89 $select_name = $id;
90 $class = ! empty( $args['choicesjs'] ) ? 'choicesjs-select' : '';
91 $choices = ! empty( $args['choicesjs'] ) ? true : false;
92 $data = isset( $args['data'] ) ? (array) $args['data'] : [];
93 $attr = isset( $args['attr'] ) ? (array) $args['attr'] : [];
94
95 if ( $choices && ! empty( $args['search'] ) ) {
96 $data['search'] = 'true';
97 }
98
99 if ( ! empty( $args['placeholder'] ) ) {
100 $data['placeholder'] = $args['placeholder'];
101 }
102
103 if ( $choices && ! empty( $args['multiple'] ) ) {
104 $attr[] = 'multiple';
105 $select_name = $id . '[]';
106 }
107
108 foreach ( $data as $name => $val ) {
109 $data[ $name ] = 'data-' . sanitize_html_class( $name ) . '="' . esc_attr( $val ) . '"';
110 }
111
112 $data = implode( ' ', $data );
113 $attr = implode( ' ', array_map( 'sanitize_html_class', $attr ) );
114
115 $output = $choices ? '<span class="choicesjs-select-wrap">' : '';
116 $output .= '<select id="campaign-setting-' . $id . '" name="' . $select_name . '" class="' . $class . '"' . $data . $attr . '>';
117
118 foreach ( $args['options'] as $option => $name ) {
119 if ( empty( $args['selected'] ) ) {
120 $selected = selected( $value, $option, false );
121 } else {
122 $selected = is_array( $args['selected'] ) && in_array( $option, $args['selected'], true ) ? 'selected' : '';
123 }
124 $output .= '<option value="' . esc_attr( $option ) . '" ' . $selected . '>' . esc_html( $name ) . '</option>';
125 }
126
127 $output .= '</select>';
128 $output .= $choices ? '</span>' : '';
129
130 if ( ! empty( $args['desc'] ) ) {
131 $output .= '<p class="desc">' . wp_kses_post( $args['desc'] ) . '</p>';
132 }
133
134 return $output;
135 }
136
137 /**
138 * Check if Gutenberg is active.
139 *
140 * @since 1.8.0
141 *
142 * @return bool True if Gutenberg is active.
143 */
144 function charitable_is_gutenberg_active() {
145
146 $gutenberg = false;
147 $block_editor = false;
148
149 if ( has_filter( 'replace_editor', 'gutenberg_init' ) ) {
150 // Gutenberg is installed and activated.
151 $gutenberg = true;
152 }
153
154 if ( version_compare( $GLOBALS['wp_version'], '5.0-beta', '>' ) ) {
155 // Block editor.
156 $block_editor = true;
157 }
158
159 if ( ! $gutenberg && ! $block_editor ) {
160 return false;
161 }
162
163 include_once ABSPATH . 'wp-admin/includes/plugin.php';
164
165 if ( is_plugin_active( 'disable-gutenberg/disable-gutenberg.php' ) ) {
166 return ! disable_gutenberg();
167 }
168
169 if ( is_plugin_active( 'classic-editor/classic-editor.php' ) ) {
170 return get_option( 'classic-editor-replace' ) === 'block';
171 }
172
173 return true;
174 }
175
176 /**
177 * Is the string a unix time stamp.
178 *
179 * @since 1.8.0
180 *
181 * @param string $timestamp A string that could be a date.
182 *
183 * @return bool True if it's likely a unix time stamp.
184 */
185 function charitable_is_valid_timestamp( $timestamp = '' ) {
186
187 return ( (string) (int) $timestamp === $timestamp )
188 && ( $timestamp <= PHP_INT_MAX )
189 && ( $timestamp >= ~PHP_INT_MAX );
190 }
191
192 /**
193 * Is the string a unix time stamp.
194 *
195 * @since 1.8.0
196 *
197 * @param string $date The string to check.
198 * @param string $format Any date format.
199 *
200 * @return bool True if it's the date with matching format.
201 */
202 function charitable_validate_date( $date = '', $format = 'Y-m-d' ) {
203
204 $d = DateTime::createFromFormat( $format, $date );
205 return $d && $d->format( $format ) === $date;
206 }
207