| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly. |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* Helper functions for campaign templates (visual builder). |
| 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.4.2 |
| 16 |
* @version 1.8.4.2 |
| 17 |
*/ |
| 18 |
|
| 19 |
if ( ! function_exists( 'charitable_hex_to_rgb' ) ) { |
| 20 |
/** |
| 21 |
* Convert a hex color to an RGB array. |
| 22 |
* |
| 23 |
* @since 1.5.0 |
| 24 |
* |
| 25 |
* @param string $hex The hex color. |
| 26 |
* @return array |
| 27 |
*/ |
| 28 |
function charitable_hex_to_rgb( $hex ) { |
| 29 |
$hex = ltrim( $hex, '#' ); |
| 30 |
|
| 31 |
if ( strlen( $hex ) == 3 ) { |
| 32 |
$hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; |
| 33 |
} |
| 34 |
|
| 35 |
$int = hexdec( $hex ); |
| 36 |
|
| 37 |
return [ |
| 38 |
'r' => ( $int >> 16 ) & 255, |
| 39 |
'g' => ( $int >> 8 ) & 255, |
| 40 |
'b' => $int & 255, |
| 41 |
]; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
if ( ! function_exists( 'charitable_get_brightness' ) ) { |
| 46 |
/** |
| 47 |
* Get the brightness of an RGB color. |
| 48 |
* |
| 49 |
* @since 1.5.0 |
| 50 |
* |
| 51 |
* @param array $rgb The RGB color. |
| 52 |
* @return float |
| 53 |
*/ |
| 54 |
function charitable_get_brightness( $rgb ) { |
| 55 |
return ( $rgb['r'] * 299 + $rgb['g'] * 587 + $rgb['b'] * 114 ) / 1000; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
if ( ! function_exists( 'charitable_get_constracting_text_color' ) ) { |
| 60 |
|
| 61 |
/** |
| 62 |
* Get the contrasting text color for a given background color. |
| 63 |
* |
| 64 |
* @since 1.5.0 |
| 65 |
* |
| 66 |
* @param string $hex_color The background color. |
| 67 |
* @return string |
| 68 |
*/ |
| 69 |
function charitable_get_constracting_text_color( $hex_color ) { |
| 70 |
$rgb = charitable_hex_to_rgb( $hex_color ); |
| 71 |
$brightness = charitable_get_brightness( $rgb ); |
| 72 |
|
| 73 |
// If brightness is below 175, use white text, otherwise use black. |
| 74 |
return $brightness < 175 ? '#FFFFFF' : '#000000'; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
if ( ! function_exists( 'charitable_get_campaign_builder_asset_url' ) ) { |
| 79 |
/** |
| 80 |
* Get the correct URL for campaign builder assets. |
| 81 |
* Handles both Charitable core and Charitable Pro plugin directories. |
| 82 |
* |
| 83 |
* @since 1.8.9.2 |
| 84 |
* @version 1.8.9.4 |
| 85 |
* |
| 86 |
* @param string $asset_path Relative path from plugin assets directory. |
| 87 |
* @param bool $add_version Whether to add version parameter for cache busting. |
| 88 |
* @return string Full URL to the asset. |
| 89 |
*/ |
| 90 |
function charitable_get_campaign_builder_asset_url( $asset_path, $add_version = null ) { |
| 91 |
// Use filter to control version parameter default. |
| 92 |
if ( is_null( $add_version ) ) { |
| 93 |
/** |
| 94 |
* Filter whether to add version parameters to campaign builder asset URLs. |
| 95 |
* |
| 96 |
* @since 1.8.9.2 |
| 97 |
* @version 1.8.9.4 |
| 98 |
* |
| 99 |
* @param bool $add_version Whether to add version parameter. Default true. |
| 100 |
*/ |
| 101 |
$add_version = apply_filters( 'charitable_campaign_builder_asset_versioning', true ); |
| 102 |
} |
| 103 |
|
| 104 |
$asset_path = ltrim( $asset_path, '/' ); |
| 105 |
$url = ''; |
| 106 |
$version = ''; |
| 107 |
|
| 108 |
// Check Charitable Pro first (if it exists and has the file). |
| 109 |
if ( defined( 'CHARITABLE_PRO_VERSION' ) && defined( 'CHARITABLE_PRO_FILE' ) ) { |
| 110 |
$pro_path = plugin_dir_path( CHARITABLE_PRO_FILE ) . 'assets/' . $asset_path; |
| 111 |
if ( file_exists( $pro_path ) ) { |
| 112 |
$url = plugin_dir_url( CHARITABLE_PRO_FILE ) . 'assets/' . $asset_path; |
| 113 |
$version = CHARITABLE_PRO_VERSION; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
// Fallback to core Charitable. |
| 118 |
if ( empty( $url ) ) { |
| 119 |
$charitable_path = charitable()->get_path( 'assets' ) . $asset_path; |
| 120 |
if ( file_exists( $charitable_path ) ) { |
| 121 |
$url = charitable()->get_path( 'assets', false ) . $asset_path; |
| 122 |
$version = charitable()->get_version(); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
// Add version parameter for cache busting if enabled. |
| 127 |
if ( $add_version && ! empty( $version ) ) { |
| 128 |
$url = add_query_arg( 'ver', $version, $url ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Filter the campaign builder asset URL. |
| 133 |
* |
| 134 |
* @since 1.8.9.2 |
| 135 |
* @version 1.8.9.4 |
| 136 |
* |
| 137 |
* @param string $url The asset URL. |
| 138 |
* @param string $asset_path The original asset path. |
| 139 |
* @param bool $add_version Whether versioning was requested. |
| 140 |
*/ |
| 141 |
return apply_filters( 'charitable_campaign_builder_asset_url', $url, $asset_path, $add_version ); |
| 142 |
} |
| 143 |
} |
| 144 |
|