| 1 |
<?php |
| 2 |
/** |
| 3 |
* Campaign v2 shortcode model. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Campaign_Shortcode |
| 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 |
if ( ! class_exists( 'Charitable_Campaign_Shortcode' ) ) : |
| 19 |
|
| 20 |
/** |
| 21 |
* Campaign Model |
| 22 |
*/ |
| 23 |
class Charitable_Campaign_Shortcode { |
| 24 |
|
| 25 |
/** |
| 26 |
* Campaign data. |
| 27 |
* |
| 28 |
* @var array $campaign_data The campaign data. |
| 29 |
*/ |
| 30 |
private $campaign_data; |
| 31 |
|
| 32 |
/** |
| 33 |
* Display the shortcode output. This is the callback method for the campaigns shortcode. |
| 34 |
* |
| 35 |
* @since 1.8.0 |
| 36 |
* |
| 37 |
* @param array $atts The user-defined shortcode attributes. |
| 38 |
* @return string |
| 39 |
*/ |
| 40 |
public static function display( $atts ) { |
| 41 |
|
| 42 |
$default = array( |
| 43 |
'id' => '', |
| 44 |
); |
| 45 |
|
| 46 |
$args = shortcode_atts( $default, $atts, 'campaign' ); |
| 47 |
|
| 48 |
// Get the campaign data from the ID that should be passed along in the shortcode. |
| 49 |
$args['campaign'] = self::get_campaign( $args ); |
| 50 |
$args['campaign_data'] = self::get_campaign_data( $args ); |
| 51 |
|
| 52 |
// Get the template_id, which should be a part of the campaign data, fall back to the default if you have to. |
| 53 |
$args['template_id'] = ! empty( $args['campaign_data']['template_id'] ) ? esc_attr( $args['campaign_data']['template_id'] ) : charitable_campaign_builder_default_template(); |
| 54 |
|
| 55 |
$args['template'] = self::get_template( $args['template_id'] ); |
| 56 |
|
| 57 |
self::prefix_add_footer_styles( $args['template_id'], $args['campaign_data'] ); |
| 58 |
|
| 59 |
/** |
| 60 |
* Replace the default template with your own. |
| 61 |
* |
| 62 |
* If you replace the template with your own, it needs to be an instance of Charitable_Template. |
| 63 |
* |
| 64 |
* @since 1.0.0 |
| 65 |
* |
| 66 |
* @param false|Charitable_Template The template. If false (the default), we will use our own template. |
| 67 |
* @param array $args All the parsed arguments. |
| 68 |
*/ |
| 69 |
$template = apply_filters( 'charitable_campaign_shortcode_template', false, $args ); |
| 70 |
|
| 71 |
/* Fall back to default Charitable_Template if no template returned or if template was not object of 'Charitable_Template' class. */ |
| 72 |
if ( ! is_object( $template ) || ! is_a( $template, 'Charitable_Template' ) ) { |
| 73 |
$template = new Charitable_Template( 'campaign/builder/content.php', false ); |
| 74 |
} |
| 75 |
|
| 76 |
if ( ! $template->template_file_exists() ) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Modify the view arguments that are passed to the campaigns shortcode template. |
| 82 |
* |
| 83 |
* @since 1.0.0 |
| 84 |
* |
| 85 |
* @param array $view_args The arguments to pass. |
| 86 |
* @param array $args All the parsed arguments. |
| 87 |
*/ |
| 88 |
$view_args = apply_filters( 'charitable_campaign_shortcode_view_args', $args, $template ); |
| 89 |
|
| 90 |
$template->set_view_args( $view_args ); |
| 91 |
|
| 92 |
ob_start(); |
| 93 |
|
| 94 |
$template->render(); |
| 95 |
|
| 96 |
$html = ob_get_clean(); |
| 97 |
|
| 98 |
// If the $args['elementor'] is true, then replace the campaign-loop with the elementor campaign-loop. |
| 99 |
if ( isset( $args['elementor'] ) && $args['elementor'] ) { |
| 100 |
$html = str_replace( 'campaign-loop', 'elementor-campaign-loop', $html ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Customize the output of the shortcode. |
| 105 |
* |
| 106 |
* @since 1.0.0 |
| 107 |
* |
| 108 |
* @param string $content The content to be displayed. |
| 109 |
* @param array $args All the parsed arguments. |
| 110 |
* @return string |
| 111 |
*/ |
| 112 |
return apply_filters( 'charitable_campaign_shortcode', $html, $args ); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Return campaigns to display in the campaigns shortcode. |
| 117 |
* |
| 118 |
* @since 1.0.0 |
| 119 |
* |
| 120 |
* @param array $args The query arguments to be used to retrieve campaigns. |
| 121 |
* @return WP_Query |
| 122 |
*/ |
| 123 |
public static function get_campaign( $args ) { |
| 124 |
|
| 125 |
$campaign = charitable_get_campaign( $args['id'] ); |
| 126 |
|
| 127 |
return $campaign; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Get campaign data. |
| 132 |
* |
| 133 |
* @since 1.0.0 |
| 134 |
* |
| 135 |
* @param array $args The query arguments to be used to retrieve campaigns. |
| 136 |
* @return WP_Query |
| 137 |
*/ |
| 138 |
public static function get_campaign_data( $args ) { |
| 139 |
|
| 140 |
$settings = get_post_meta( $args['id'], 'campaign_settings_v2', true ); |
| 141 |
|
| 142 |
if ( ! empty( $settings ) ) { |
| 143 |
return $settings; |
| 144 |
} |
| 145 |
|
| 146 |
return $args; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Return the template metadata to help display in the campaigns shortcode. |
| 151 |
* |
| 152 |
* @since 1.0.0 |
| 153 |
* |
| 154 |
* @param string $template_id The template ID. |
| 155 |
* @return array|false |
| 156 |
*/ |
| 157 |
public static function get_template_data( $template_id ) { |
| 158 |
|
| 159 |
$settings = get_post_meta( $template_id, 'campaign_settings_v2', true ); |
| 160 |
|
| 161 |
if ( ! empty( $settings ) ) { |
| 162 |
return $settings; |
| 163 |
} |
| 164 |
|
| 165 |
return false; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Load template specific CSS in the footer. |
| 170 |
* |
| 171 |
* @since 1.8.0 |
| 172 |
* @version 1.8.1 Added $template_id blank check. |
| 173 |
* @version 1.8.2 removed $min from frotend styles. |
| 174 |
* |
| 175 |
* @param string $template_id Template slug. |
| 176 |
* @param array $campaign_data Saved campaign data. |
| 177 |
*/ |
| 178 |
public static function prefix_add_footer_styles( $template_id = 'basic', $campaign_data = false ) { |
| 179 |
|
| 180 |
if ( '' === $template_id ) { |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
$min = charitable_get_min_suffix(); |
| 185 |
$version = charitable()->get_version(); |
| 186 |
$assets_dir = charitable()->get_path( 'assets', false ); |
| 187 |
|
| 188 |
// Pass along the theme color overides to the theme. Start by defining the base/default theme values as stored in settings. |
| 189 |
$query_args = array( |
| 190 |
'p' => ! empty( $campaign_data['color_base_primary'] ) ? charitable_sanitize_hex( $campaign_data['color_base_primary'], false ) : false, |
| 191 |
's' => ! empty( $campaign_data['color_base_secondary'] ) ? charitable_sanitize_hex( $campaign_data['color_base_secondary'], false ) : false, |
| 192 |
't' => ! empty( $campaign_data['color_base_tertiary'] ) ? charitable_sanitize_hex( $campaign_data['color_base_tertiary'], false ) : false, |
| 193 |
'b' => ! empty( $campaign_data['color_base_button'] ) ? charitable_sanitize_hex( $campaign_data['color_base_button'], false ) : false, |
| 194 |
); |
| 195 |
|
| 196 |
// Now check for overrides in the settings. |
| 197 |
$layout_theme_settings = isset( $campaign_data['layout']['advanced'] ) ? $campaign_data['layout']['advanced'] : array(); |
| 198 |
|
| 199 |
$query_args['p'] = ! empty( $layout_theme_settings['theme_color_primary'] ) ? charitable_sanitize_hex( $layout_theme_settings['theme_color_primary'], false ) : $query_args['p']; |
| 200 |
$query_args['s'] = ! empty( $layout_theme_settings['theme_color_secondary'] ) ? charitable_sanitize_hex( $layout_theme_settings['theme_color_secondary'], false ) : $query_args['s']; |
| 201 |
$query_args['t'] = ! empty( $layout_theme_settings['theme_color_tertiary'] ) ? charitable_sanitize_hex( $layout_theme_settings['theme_color_tertiary'], false ) : $query_args['t']; |
| 202 |
$query_args['b'] = ! empty( $layout_theme_settings['theme_color_button'] ) ? charitable_sanitize_hex( $layout_theme_settings['theme_color_button'], false ) : $query_args['b']; |
| 203 |
|
| 204 |
$template_frontend_css_url = add_query_arg( |
| 205 |
apply_filters( |
| 206 |
'charitable_builder_template_frontend_styles_' . $template_id, |
| 207 |
$query_args, |
| 208 |
$template_id, |
| 209 |
$campaign_data |
| 210 |
), |
| 211 |
rest_url( 'charitable/v1/campaign-css/' . $template_id ) |
| 212 |
); |
| 213 |
|
| 214 |
wp_enqueue_style( |
| 215 |
'charitable-campaign-theme-base', |
| 216 |
apply_filters( |
| 217 |
'charitable_builder_template_frontend_base_styles_' . $template_id, |
| 218 |
$assets_dir . 'css/campaign-builder/themes/frontend/base' . $min . '.css', |
| 219 |
$template_id, |
| 220 |
$campaign_data |
| 221 |
), |
| 222 |
array(), |
| 223 |
$version |
| 224 |
); |
| 225 |
|
| 226 |
wp_enqueue_style( |
| 227 |
'charitable-campaign-theme-' . $template_id, |
| 228 |
$template_frontend_css_url, |
| 229 |
array( 'charitable-campaign-theme-base' ), |
| 230 |
false // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NoExplicitVersion |
| 231 |
); |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Load template classes. |
| 236 |
* |
| 237 |
* @since 1.8.0 |
| 238 |
* |
| 239 |
* @param string $template_id The template ID. |
| 240 |
* @return array|false |
| 241 |
*/ |
| 242 |
public static function get_template( $template_id = 'basic' ) { |
| 243 |
|
| 244 |
/** |
| 245 |
* Allows developers to disable loading of some builder panels. |
| 246 |
* |
| 247 |
* @since 1.8.0 |
| 248 |
* |
| 249 |
* @param array $panels Panels slugs array. |
| 250 |
*/ |
| 251 |
$classes = apply_filters( |
| 252 |
'charitable_builder_template_classes_', |
| 253 |
array( |
| 254 |
'templates', |
| 255 |
) |
| 256 |
); |
| 257 |
|
| 258 |
foreach ( $classes as $class ) { |
| 259 |
$class = sanitize_file_name( $class ); |
| 260 |
$file = require_once charitable()->get_path( 'includes' ) . "admin/campaign-builder/templates/class-{$class}.php"; |
| 261 |
|
| 262 |
if ( file_exists( $file ) ) { |
| 263 |
require_once $file; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
$builder_template = new Charitable_Campaign_Builder_Templates(); |
| 268 |
$templates_data = $builder_template->get_templates_data( $template_id ); |
| 269 |
$template_data = isset( $templates_data['templates'][ $template_id ] ) ? $templates_data['templates'][ $template_id ] : false; |
| 270 |
|
| 271 |
return $template_data; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
endif; |
| 276 |
|