| 1 |
<?php |
| 2 |
/** |
| 3 |
* Base class management panel. |
| 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.9.1, 1.8.9.1 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Builder_Panel' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Base panel class. |
| 21 |
* |
| 22 |
* @since 1.8.0 |
| 23 |
*/ |
| 24 |
abstract class Charitable_Builder_Panel { |
| 25 |
|
| 26 |
/** |
| 27 |
* Full name of the panel. |
| 28 |
* |
| 29 |
* @since 1.8.0 |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
public $name; |
| 34 |
|
| 35 |
/** |
| 36 |
* Slug. |
| 37 |
* |
| 38 |
* @since 1.8.0 |
| 39 |
* |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
public $slug; |
| 43 |
|
| 44 |
/** |
| 45 |
* Font Awesome Icon used for the editor button, eg "fa-list". |
| 46 |
* |
| 47 |
* @since 1.8.0 |
| 48 |
* |
| 49 |
* @var mixed |
| 50 |
*/ |
| 51 |
public $icon = false; |
| 52 |
|
| 53 |
/** |
| 54 |
* Priority order the field button should show inside the "Add Fields" tab. |
| 55 |
* |
| 56 |
* @since 1.8.0 |
| 57 |
* |
| 58 |
* @var int |
| 59 |
*/ |
| 60 |
public $order = 50; |
| 61 |
|
| 62 |
/** |
| 63 |
* If panel contains a sidebar element or is full width. |
| 64 |
* |
| 65 |
* @since 1.8.0 |
| 66 |
* |
| 67 |
* @var bool |
| 68 |
*/ |
| 69 |
public $sidebar = false; |
| 70 |
|
| 71 |
/** |
| 72 |
* If panel is acting as a button instead of showing a panel UI |
| 73 |
* |
| 74 |
* @since 1.8.0 |
| 75 |
* |
| 76 |
* @var bool |
| 77 |
*/ |
| 78 |
public $button = false; |
| 79 |
|
| 80 |
/** |
| 81 |
* If panel is acting as a button, what's the url. |
| 82 |
* |
| 83 |
* @since 1.8.0 |
| 84 |
* |
| 85 |
* @var string |
| 86 |
*/ |
| 87 |
public $url = false; |
| 88 |
|
| 89 |
/** |
| 90 |
* Contain campaign object if we have one. |
| 91 |
* |
| 92 |
* @since 1.8.0 |
| 93 |
* |
| 94 |
* @var object |
| 95 |
*/ |
| 96 |
public $campaign; |
| 97 |
|
| 98 |
/** |
| 99 |
* Contain array of the form data (post_content). |
| 100 |
* |
| 101 |
* @since 1.8.0 |
| 102 |
* |
| 103 |
* @var array |
| 104 |
*/ |
| 105 |
public $campaign_data = array(); |
| 106 |
|
| 107 |
/** |
| 108 |
* Class instance. |
| 109 |
* |
| 110 |
* @since 1.8.0 |
| 111 |
* |
| 112 |
* @var static |
| 113 |
*/ |
| 114 |
private static $instance; |
| 115 |
|
| 116 |
/** |
| 117 |
* Primary class constructor. |
| 118 |
* |
| 119 |
* @since 1.8.0 |
| 120 |
*/ |
| 121 |
public function __construct() { |
| 122 |
|
| 123 |
// Force revision false for now, and this is a potential placeholder for future features. |
| 124 |
$revision = false; |
| 125 |
|
| 126 |
// Bootstrap. |
| 127 |
$this->init(); |
| 128 |
|
| 129 |
// Load panel specific enqueues. |
| 130 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueues' ], 15 ); |
| 131 |
|
| 132 |
// Primary panel button. |
| 133 |
add_action( 'charitable_builder_panel_buttons', [ $this, 'button' ], $this->order, 2 ); |
| 134 |
|
| 135 |
// Output. |
| 136 |
add_action( 'charitable_builder_panels', [ $this, 'panel_output' ], $this->order, 2 ); |
| 137 |
|
| 138 |
$this->get_campaign_settings(); |
| 139 |
|
| 140 |
// Save instance. |
| 141 |
self::$instance = $this; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Get class instance. |
| 146 |
* |
| 147 |
* @since 1.8.0 |
| 148 |
* |
| 149 |
* @return static |
| 150 |
*/ |
| 151 |
public static function instance() { |
| 152 |
|
| 153 |
if ( self::$instance === null || ! self::$instance instanceof static ) { |
| 154 |
self::$instance = new static(); |
| 155 |
} |
| 156 |
|
| 157 |
return self::$instance; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Get campaign settings and set to the class var. |
| 162 |
* |
| 163 |
* @since 1.8.0 |
| 164 |
* @version 1.8.2 add maybe_unserialize |
| 165 |
*/ |
| 166 |
public function get_campaign_settings() { |
| 167 |
|
| 168 |
// was a campaign ID passed (then we are editting an existin campaign form). |
| 169 |
if ( ! empty( $_GET['campaign_id'] ) ) { // phpcs:ignore |
| 170 |
$campaign_id = (int) $_GET['campaign_id']; // phpcs:ignore |
| 171 |
|
| 172 |
$this->campaign_data = get_post_meta( $campaign_id, 'campaign_settings_v2', true ); |
| 173 |
|
| 174 |
if ( empty( $this->campaign_data ) || false === $this->campaign_data ) { |
| 175 |
$this->campaign_data = $this->get_default_settings(); |
| 176 |
} |
| 177 |
|
| 178 |
if ( is_string( $this->campaign_data ) ) { |
| 179 |
$this->campaign_data = maybe_unserialize( $this->campaign_data ); |
| 180 |
} |
| 181 |
|
| 182 |
$this->campaign_data['id'] = $this->campaign_data['campaign_id'] = $campaign_id; // this should be fixed at some point. |
| 183 |
|
| 184 |
} else { |
| 185 |
|
| 186 |
$this->campaign_data = $this->get_default_settings(); |
| 187 |
|
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Get default settings and set to the class var. |
| 193 |
* |
| 194 |
* @since 1.8.0 |
| 195 |
*/ |
| 196 |
public function get_default_settings() { |
| 197 |
|
| 198 |
if ( ! is_array( $this->campaign_data ) ) { |
| 199 |
$this->campaign_data = array(); |
| 200 |
} |
| 201 |
|
| 202 |
$this->campaign_data['id'] = $this->campaign_data['campaign_id'] = 0; |
| 203 |
|
| 204 |
$campaign_name = isset( $_POST['campaign_name'] ) && '' !== trim( $_POST['campaign_name'] ) ? esc_html( $_POST['campaign_name'] ) : esc_html__( 'New Campaign', 'charitable' ); // phpcs:ignore |
| 205 |
|
| 206 |
$this->campaign_data['tabs']['campaign']['title'] = 'Story'; |
| 207 |
$this->campaign_data['tabs']['campaign']['desc'] = 'Write Your Campaign\'s Story Here'; |
| 208 |
|
| 209 |
$this->campaign_data['title'] = $campaign_name; |
| 210 |
$this->campaign_data['desc'] = false; // 'test description'; |
| 211 |
|
| 212 |
$this->campaign_data['layout']['advanced']['tab_style'] = 'small'; |
| 213 |
$this->campaign_data['layout']['advanced']['tab_size'] = 'medium'; |
| 214 |
|
| 215 |
return $this->campaign_data; |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* All systems go. Used by children. |
| 220 |
* |
| 221 |
* @since 1.8.0 |
| 222 |
*/ |
| 223 |
public function init() { |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Enqueue assets for the builder. Used by children. |
| 228 |
* |
| 229 |
* @since 1.8.0 |
| 230 |
*/ |
| 231 |
public function enqueues() { |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Primary panel button in the left panel navigation. |
| 236 |
* |
| 237 |
* @since 1.8.0 |
| 238 |
* @version 1.8.9.1 |
| 239 |
* |
| 240 |
* @param mixed $campaign Current campaign object. |
| 241 |
* @param string $view The current view. |
| 242 |
*/ |
| 243 |
public function button( $campaign, $view ) { |
| 244 |
|
| 245 |
$active = $view === $this->slug ? 'active' : ''; |
| 246 |
$license = 'pro'; |
| 247 |
$upgrade_class = false; |
| 248 |
$upgrade_params = false; |
| 249 |
$button_linkable = ( $this->button && $this->url ) ? 'type="button"' : false; |
| 250 |
$data_panel = $button_linkable ? '' : 'data-panel="' . esc_attr( $this->slug ) . '"'; |
| 251 |
|
| 252 |
?> |
| 253 |
|
| 254 |
<?php |
| 255 |
if ( $button_linkable ) : |
| 256 |
?> |
| 257 |
<a target="_blank" href="<?php echo esc_url( $this->url ); ?>"><?php endif; ?> |
| 258 |
|
| 259 |
<button <?php echo esc_attr( $button_linkable ); ?> class="charitable-panel-<?php echo esc_attr( $this->slug ); ?>-button <?php echo esc_attr( $upgrade_class ); ?> <?php echo esc_attr( $active ); ?>" <?php echo $data_panel; ?> <?php echo esc_attr( $upgrade_params ); ?>> |
| 260 |
<?php if ( $this->icon ) : ?> |
| 261 |
<img class="topbar_icon" src="<?php echo esc_url( charitable()->get_path( 'assets', false ) . 'images/icons/' . $this->icon ); ?>" /> |
| 262 |
<?php endif; ?> |
| 263 |
<span><?php echo esc_html( $this->name ); ?></span> |
| 264 |
</button> |
| 265 |
|
| 266 |
<?php |
| 267 |
if ( $button_linkable ) : |
| 268 |
?> |
| 269 |
</a><?php endif; ?> |
| 270 |
<?php |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Output the contents of the panel. |
| 275 |
* |
| 276 |
* @since 1.8.0 |
| 277 |
* |
| 278 |
* @param object $campaign Current campaign object. |
| 279 |
* @param string $view Active Campaign Builder view (panel). |
| 280 |
*/ |
| 281 |
public function panel_output( $campaign, $view = 'design' ) { |
| 282 |
|
| 283 |
$wrap = $this->sidebar ? 'charitable-panel-sidebar-content' : 'charitable-panel-full-content'; |
| 284 |
$classes = [ 'charitable-panel' ]; |
| 285 |
$field_slug_id = $this->slug; |
| 286 |
|
| 287 |
if ( in_array( $this->slug, [ 'design', 'settings', 'fields', 'revisions' ], true ) ) { |
| 288 |
$classes[] = 'charitable-panel-fields'; |
| 289 |
} |
| 290 |
|
| 291 |
if ( in_array( $this->slug, [ 'design', 'revisions' ], true ) ) { |
| 292 |
$classes[] = 'charitable-panel-design'; |
| 293 |
$field_slug_id = 'design'; |
| 294 |
} |
| 295 |
|
| 296 |
if ( in_array( $this->slug, [ 'settings', 'revisions' ], true ) ) { |
| 297 |
$classes[] = 'charitable-panel-settings'; |
| 298 |
} |
| 299 |
|
| 300 |
if ( in_array( $this->slug, [ 'template', 'revisions' ], true ) ) { |
| 301 |
$classes[] = 'charitable-panel-template'; |
| 302 |
} |
| 303 |
|
| 304 |
if ( $view == $this->slug ) { |
| 305 |
$classes[] = 'active'; |
| 306 |
} |
| 307 |
|
| 308 |
printf( '<div class="%s" id="charitable-panel-%s">', charitable_sanitize_classes( $classes, true ), esc_attr( $field_slug_id ) ); // phpcs:ignore |
| 309 |
|
| 310 |
printf( '<div class="%s">', esc_attr( $wrap ) ); |
| 311 |
|
| 312 |
if ( true === $this->sidebar ) { |
| 313 |
|
| 314 |
if ( $this->slug === 'design' ) { |
| 315 |
echo '<div class="charitable-panel-sidebar-toggle"><div class="charitable-panel-sidebar-toggle-vertical-line"></div><div class="charitable-panel-sidebar-toggle-icon"><i class="fa fa-angle-left"></i></div></div>'; |
| 316 |
} |
| 317 |
|
| 318 |
echo '<div class="charitable-panel-sidebar">'; |
| 319 |
|
| 320 |
do_action( 'charitable_builder_before_panel_sidebar', $this->campaign, $this->slug ); |
| 321 |
|
| 322 |
$this->panel_sidebar(); |
| 323 |
|
| 324 |
do_action( 'charitable_builder_after_panel_sidebar', $this->campaign, $this->slug ); |
| 325 |
|
| 326 |
echo '</div>'; |
| 327 |
|
| 328 |
} |
| 329 |
|
| 330 |
echo '<div class="charitable-panel-content-wrap">'; |
| 331 |
|
| 332 |
echo '<div class="charitable-panel-content">'; |
| 333 |
|
| 334 |
do_action( 'charitable_builder_before_panel_content', $this->campaign, $this->slug ); |
| 335 |
|
| 336 |
$this->panel_content(); |
| 337 |
|
| 338 |
do_action( 'charitable_builder_after_panel_content', $this->campaign, $this->slug ); |
| 339 |
|
| 340 |
echo '</div>'; |
| 341 |
|
| 342 |
echo '</div>'; |
| 343 |
|
| 344 |
echo '</div>'; |
| 345 |
|
| 346 |
echo '</div>'; |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Output the panel's sidebar if we have one. |
| 351 |
* |
| 352 |
* @since 1.8.0 |
| 353 |
*/ |
| 354 |
public function panel_sidebar() { |
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
* Output panel sidebar sections. |
| 359 |
* |
| 360 |
* @since 1.8.0 |
| 361 |
* |
| 362 |
* @param string $name Sidebar section name. |
| 363 |
* @param string $slug Sidebar section slug. |
| 364 |
* @param string $icon Sidebar section icon. |
| 365 |
*/ |
| 366 |
public function panel_sidebar_section( $name, $slug, $icon = '' ) { |
| 367 |
|
| 368 |
$default_classes = [ |
| 369 |
'charitable-panel-sidebar-section', |
| 370 |
'charitable-panel-sidebar-section-' . $slug, |
| 371 |
]; |
| 372 |
|
| 373 |
if ( $slug === 'default' ) { |
| 374 |
$default_classes[] = 'default'; |
| 375 |
} |
| 376 |
|
| 377 |
if ( ! empty( $icon ) ) { |
| 378 |
$default_classes[] = 'icon'; |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Allow adding custom CSS classes to a sidebar section in the Campaign Builder. |
| 383 |
* |
| 384 |
* @since 1.8.0 |
| 385 |
* |
| 386 |
* @param array $classes Sidebar section classes. |
| 387 |
* @param string $name Sidebar section name. |
| 388 |
* @param string $slug Sidebar section slug. |
| 389 |
* @param string $icon Sidebar section icon. |
| 390 |
*/ |
| 391 |
$classes = (array) apply_filters( 'charitable_builder_panel_sidebar_section_classes', [], $name, $slug, $icon ); |
| 392 |
$classes = array_merge( $default_classes, $classes ); |
| 393 |
|
| 394 |
echo '<a href="#" class="' . esc_attr( charitable_sanitize_classes( $classes, true ) ) . '" data-section="' . esc_attr( $slug ) . '">'; |
| 395 |
|
| 396 |
if ( ! empty( $icon ) ) { |
| 397 |
echo '<img src="' . esc_url( $icon ) . '">'; |
| 398 |
} |
| 399 |
|
| 400 |
echo esc_html( $name ); |
| 401 |
|
| 402 |
echo '<i class="fa fa-angle-right charitable-toggle-arrow"></i>'; |
| 403 |
|
| 404 |
echo '</a>'; |
| 405 |
} |
| 406 |
|
| 407 |
/** |
| 408 |
* Output the panel's primary content. |
| 409 |
* |
| 410 |
* @since 1.8.0 |
| 411 |
*/ |
| 412 |
public function panel_content() { |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
endif; |
| 417 |
|