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