| 1 |
<?php |
| 2 |
/** |
| 3 |
* General functions made for the campaign builder. |
| 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 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Get the list of allowed tags, used in pair with wp_kses() function. |
| 20 |
* This allows getting rid of all potentially harmful HTML tags and attributes. |
| 21 |
* |
| 22 |
* @since 1.8.0 |
| 23 |
* |
| 24 |
* @return array Allowed Tags. |
| 25 |
*/ |
| 26 |
function charitable_builder_preview_get_allowed_tags() { |
| 27 |
|
| 28 |
static $allowed_tags; |
| 29 |
|
| 30 |
if ( ! empty( $allowed_tags ) ) { |
| 31 |
return $allowed_tags; |
| 32 |
} |
| 33 |
|
| 34 |
$atts = [ 'align', 'class', 'type', 'id', 'for', 'style', 'src', 'rel', 'href', 'target', 'value', 'width', 'height' ]; |
| 35 |
$tags = [ 'label', 'iframe', 'style', 'button', 'strong', 'small', 'table', 'span', 'abbr', 'code', 'pre', 'div', 'img', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ol', 'ul', 'li', 'em', 'hr', 'br', 'th', 'tr', 'td', 'p', 'a', 'b', 'i' ]; |
| 36 |
|
| 37 |
$allowed_atts = array_fill_keys( $atts, [] ); |
| 38 |
$allowed_tags = array_fill_keys( $tags, $allowed_atts ); |
| 39 |
|
| 40 |
return $allowed_tags; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Return URL to form preview page. |
| 45 |
* |
| 46 |
* @since 1.8.0 |
| 47 |
* |
| 48 |
* @param int $campaign_id Form ID. |
| 49 |
* @param bool $new_window New window flag. |
| 50 |
* @param string $post_status Post status of item. |
| 51 |
* |
| 52 |
* @return string |
| 53 |
*/ |
| 54 |
function charitable_get_campaign_preview_url( $campaign_id = false, $new_window = false, $post_status = false ) { |
| 55 |
|
| 56 |
if ( intval( $campaign_id ) === 0 ) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
if ( false === $post_status ) { |
| 61 |
$post_status = get_post_status( $campaign_id ); |
| 62 |
} |
| 63 |
|
| 64 |
if ( $post_status === 'publish' || $post_status === 'private' || $post_status === 'draft' || $post_status === 'public' ) { |
| 65 |
|
| 66 |
$url = add_query_arg( |
| 67 |
[ |
| 68 |
'p' => absint( $campaign_id ), |
| 69 |
'charitable_campaign_preview' => absint( $campaign_id ), |
| 70 |
], |
| 71 |
home_url() |
| 72 |
); |
| 73 |
|
| 74 |
if ( $new_window ) { |
| 75 |
$url = add_query_arg( |
| 76 |
[ |
| 77 |
'new_window' => 1, |
| 78 |
], |
| 79 |
$url |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
} else { |
| 84 |
|
| 85 |
$url = get_preview_post_link( $campaign_id ); |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
return $url; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Determine if we are on the campaign builder admin page |
| 94 |
* |
| 95 |
* @since 1.8.0 |
| 96 |
*/ |
| 97 |
function campaign_is_campaign_builder_admin_page() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- This is a legacy function that cannot be renamed. |
| 98 |
|
| 99 |
if ( isset( $_POST['campaign_id'] ) ) { // @codingStandardsIgnoreLine |
| 100 |
return true; |
| 101 |
} |
| 102 |
|
| 103 |
if ( ! isset( $_GET['page'] ) || $_GET['page'] !== 'charitable-campaign-builder' ) { // @codingStandardsIgnoreLine |
| 104 |
return false; |
| 105 |
} else { |
| 106 |
return true; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Get template data from an existing campaign. |
| 112 |
* |
| 113 |
* @since 1.8.0 |
| 114 |
* |
| 115 |
* @param int $campaign_id Form ID. |
| 116 |
*/ |
| 117 |
function charitable_get_template_data_from_campaign( $campaign_id = false ) { |
| 118 |
|
| 119 |
if ( false === intval( $campaign_id ) ) { |
| 120 |
return false; |
| 121 |
} |
| 122 |
|
| 123 |
$settings = get_post_meta( $campaign_id, 'campaign_settings_v2', true ); |
| 124 |
|
| 125 |
if ( ! empty( $settings ) ) { |
| 126 |
return $settings; |
| 127 |
} |
| 128 |
|
| 129 |
if ( ! empty( $settings['template_id'] ) && ! empty( $settings['template_label'] ) ) { |
| 130 |
|
| 131 |
return array( |
| 132 |
'template_id' => esc_attr( $settings['template_id'] ), |
| 133 |
esc_html( $settings['template_label'] ), |
| 134 |
); |
| 135 |
|
| 136 |
} else { |
| 137 |
|
| 138 |
return false; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Get template data from an existing campaign. |
| 144 |
* |
| 145 |
* @since 1.8.0 |
| 146 |
* |
| 147 |
* @param slug $template_id Template ID. |
| 148 |
*/ |
| 149 |
function charitable_show_field_names_by_default( $template_id = false ) { // phpcs:ignore |
| 150 |
|
| 151 |
if ( charitable_is_debug() || ( defined( 'CHARITABLE_BUILDER_SHOW_FIELD_NAMES' ) && CHARITABLE_BUILDER_SHOW_FIELD_NAMES ) ) { |
| 152 |
return apply_filters( 'charitable_builder_show_field_names_default', true, $template_id ); |
| 153 |
} |
| 154 |
|
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Check and see if preview mode is shown by default. |
| 160 |
* |
| 161 |
* @since 1.8.0 |
| 162 |
* |
| 163 |
* @param slug $template_id Template ID. |
| 164 |
*/ |
| 165 |
function charitable_show_preview_mode_by_default( $template_id = false ) { |
| 166 |
|
| 167 |
return apply_filters( 'charitable_builder_show_preview_mode_default', false, $template_id ); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Generate a tooltip. |
| 172 |
* |
| 173 |
* @since 1.8.0 |
| 174 |
* |
| 175 |
* @param string $tooltip_text The tooltip text. |
| 176 |
* @param string $extra_css Any additional CSS classes. |
| 177 |
*/ |
| 178 |
function charitable_get_tooltip_html( $tooltip_text = false, $extra_css = '' ) { |
| 179 |
|
| 180 |
if ( false === $tooltip_text ) { |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
$html = sprintf( '<span class="charitable-help-tooltip-container"><img src="' . charitable()->get_path( 'assets', false ) . 'images/icons/info.svg" alt="" class="charitable-help-tooltip ' . esc_attr( $extra_css ) . '" title="%s"></i></span>', esc_html( $tooltip_text ) ); |
| 185 |
|
| 186 |
return $html; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Get the list of allowed users who can create and/or edit campaigns. |
| 191 |
* |
| 192 |
* @since 1.8.3.2 |
| 193 |
* |
| 194 |
* @param string $by 'permissions' or 'roles'. |
| 195 |
* @param array $additional_allowed_users_id Additional users to add to the list. |
| 196 |
* |
| 197 |
* @return array Allowed Tags. |
| 198 |
*/ |
| 199 |
function charitable_get_users_as_campaign_creators( $by = 'permissions', $additional_allowed_users_id = [] ) { |
| 200 |
|
| 201 |
// Check the transient first. |
| 202 |
$allowed_users = get_transient( 'charitable_allowed_campaign_creators_by_' . $by ); |
| 203 |
|
| 204 |
if ( false !== $allowed_users ) { |
| 205 |
return $allowed_users; |
| 206 |
} |
| 207 |
|
| 208 |
$allowed_users = apply_filters( 'charitable_allowed_campaign_creators', [] ); |
| 209 |
if ( ! empty( $allowed_users ) ) { |
| 210 |
return $allowed_users; |
| 211 |
} |
| 212 |
|
| 213 |
if ( 'roles' === $by ) { |
| 214 |
|
| 215 |
$allowed_users = get_users( [ 'role__in' => [ 'administrator', 'campaign_manager' ] ] ); |
| 216 |
|
| 217 |
} elseif ( 'permissions' === $by ) { |
| 218 |
|
| 219 |
$all_users = get_users(); |
| 220 |
|
| 221 |
$users_with_permissions = []; |
| 222 |
|
| 223 |
if ( ! is_array( $allowed_users ) ) { |
| 224 |
$allowed_users = []; |
| 225 |
} |
| 226 |
|
| 227 |
foreach ( $all_users as $user ) { |
| 228 |
|
| 229 |
if ( user_can( $user->ID, 'create_campaigns' ) || user_can( $user->ID, 'edit_campaigns' ) ) { // phpcs:ignore |
| 230 |
$allowed_users[] = $user; |
| 231 |
} |
| 232 |
|
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
// If there was any additional users passed in, add them to the list. |
| 237 |
if ( $additional_allowed_users_id ) { |
| 238 |
$users = get_users( [ 'include' => $additional_allowed_users_id ] ); |
| 239 |
$allowed_users = array_merge( $users, $allowed_users ); |
| 240 |
} |
| 241 |
|
| 242 |
// Add this to a transient that expires in one hour. |
| 243 |
set_transient( 'charitable_allowed_campaign_creators_by_' . $by, $allowed_users, HOUR_IN_SECONDS ); |
| 244 |
|
| 245 |
return $allowed_users; |
| 246 |
} |
| 247 |
|