| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly. |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* Handles loading all the field types (default and others possibly from optional addons) into the 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.0 |
| 16 |
* @version 1.8.0 |
| 17 |
*/ |
| 18 |
class Charitable_Builder_Fields { |
| 19 |
|
| 20 |
/** |
| 21 |
* Current field data. |
| 22 |
* |
| 23 |
* @since 1.8.0 |
| 24 |
* |
| 25 |
* @var array |
| 26 |
*/ |
| 27 |
public $pro_fields = array(); |
| 28 |
|
| 29 |
/** |
| 30 |
* Primary class constructor. |
| 31 |
* |
| 32 |
* @since 1.8.0 |
| 33 |
*/ |
| 34 |
public function __construct() { |
| 35 |
|
| 36 |
$this->init(); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Load and init the base field class. |
| 41 |
* |
| 42 |
* @since 1.8.0 |
| 43 |
*/ |
| 44 |
public function init() { |
| 45 |
|
| 46 |
// Parent class template. |
| 47 |
require_once Charitable()->get_path( 'includes' ) . 'admin/campaign-builder/fields/class-base.php'; |
| 48 |
|
| 49 |
// WordPress hooks. |
| 50 |
$this->hooks(); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Hooks. |
| 55 |
* |
| 56 |
* @since 1.8.0 |
| 57 |
*/ |
| 58 |
private function hooks() { |
| 59 |
|
| 60 |
// Load default fields on WP init. |
| 61 |
add_action( 'init', [ $this, 'load' ] ); |
| 62 |
|
| 63 |
add_filter( 'charitable_campaign_builder_load_fields', [ $this, 'load_pro_fields' ], 100, 2 ); |
| 64 |
|
| 65 |
add_filter( 'charitable_builder_field_button_attributes', [ $this, 'fields_attributes' ], 100, 2 ); |
| 66 |
|
| 67 |
add_filter( 'charitable_builder_design_buttons', [ $this, 'pro_field_buttons' ], 15 ); |
| 68 |
|
| 69 |
add_action( 'init', array( $this, 'setup_pro_addons' ), 999 ); // 1.8.3.5 |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Adds pro fields to the button list. |
| 74 |
* |
| 75 |
* @since 1.8.0 |
| 76 |
* |
| 77 |
* @param array $fields Field types. |
| 78 |
* |
| 79 |
* @return array Fields array. |
| 80 |
*/ |
| 81 |
public function pro_field_buttons( $fields ) { |
| 82 |
|
| 83 |
if ( false === $fields || ! is_array( $fields ) ) { |
| 84 |
return $fields; |
| 85 |
} |
| 86 |
|
| 87 |
$fields['pro']['fields'] = (array) $this->pro_fields; |
| 88 |
|
| 89 |
return $fields; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Load default field types. |
| 94 |
* |
| 95 |
* @since 1.8.0 |
| 96 |
*/ |
| 97 |
public function load() { |
| 98 |
|
| 99 |
$fields = [ |
| 100 |
'campaign-title', |
| 101 |
'campaign-description', |
| 102 |
'donate-button', |
| 103 |
'donate-amount', |
| 104 |
'progress-bar', |
| 105 |
// 'donation-options', |
| 106 |
'photo', |
| 107 |
'organizer', |
| 108 |
'html', |
| 109 |
'social-sharing', |
| 110 |
'social-links', |
| 111 |
'campaign-summary', |
| 112 |
'donate-form', |
| 113 |
'donor-wall', |
| 114 |
'shortcode', |
| 115 |
'text', |
| 116 |
]; |
| 117 |
|
| 118 |
/** |
| 119 |
* Filters array of fields to be loaded. |
| 120 |
* |
| 121 |
* @since 1.0.0 |
| 122 |
* |
| 123 |
* @param array $fields Field types. |
| 124 |
*/ |
| 125 |
$fields = (array) apply_filters( |
| 126 |
'charitable_campaign_builder_load_fields', |
| 127 |
$fields |
| 128 |
); |
| 129 |
|
| 130 |
if ( ! empty( $fields ) ) : |
| 131 |
|
| 132 |
foreach ( $fields as $key => $field_type ) { |
| 133 |
|
| 134 |
// reset the value. |
| 135 |
$file_path = false; |
| 136 |
|
| 137 |
if ( is_array( $field_type ) ) { |
| 138 |
|
| 139 |
// if this is an array, it's likely "fnacy" and added by a plugin or addon so look for the key in the array for the field type. |
| 140 |
$field_type_slug = key( $field_type ); |
| 141 |
$field_type = $field_type[ $field_type_slug ]; |
| 142 |
|
| 143 |
// where do we load the class, otherwise pick the "default" path inside core Charitable. |
| 144 |
$file = isset( $field_type['file'] ) && ! empty( $field_type['file'] ) ? esc_html( $field_type['file'] ) : false; |
| 145 |
|
| 146 |
if ( $file ) { |
| 147 |
$file = wp_slash( charitable()->get_path( 'plugin-directory' ) ) . $file; // /wp-content/plugins/charitable-videos/includes/campaign-builder/fields/class-video.php |
| 148 |
} |
| 149 |
|
| 150 |
} else { |
| 151 |
|
| 152 |
$file = charitable()->get_path( 'includes' ) . 'admin/campaign-builder/fields/class-' . $field_type . '.php'; |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
if ( false !== $file && file_exists( $file ) ) { |
| 157 |
require_once $file; |
| 158 |
} |
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
endif; |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Filters array of "pro" fields form addons to be loaded. |
| 168 |
* |
| 169 |
* @since 1.8.0 |
| 170 |
*/ |
| 171 |
public function setup_pro_addons() { |
| 172 |
|
| 173 |
$this->pro_fields = (array) apply_filters( // phpcs:ignore Charitable.PHP.ValidateHooks.InvalidHookName |
| 174 |
'charitable_load_campaign_builder_pro_fields', |
| 175 |
array( |
| 176 |
'donor-comments' => array( |
| 177 |
'order' => '250', |
| 178 |
'name' => esc_html__( 'Donor Comments', 'charitable' ), |
| 179 |
'type' => 'donor-comments', |
| 180 |
'icon' => 'fa-comment', |
| 181 |
'class' => 'pro', |
| 182 |
'addon' => 'donor-comments', |
| 183 |
), |
| 184 |
'ambassadors-team' => array( |
| 185 |
'order' => '500', |
| 186 |
'name' => esc_html__( 'Team', 'charitable' ), |
| 187 |
'type' => 'team', |
| 188 |
'icon' => 'fa-users', |
| 189 |
'class' => 'pro', |
| 190 |
'addon' => 'ambassador', |
| 191 |
), |
| 192 |
'simple-updates' => array( |
| 193 |
'order' => '750', |
| 194 |
'name' => esc_html__( 'Updates', 'charitable' ), |
| 195 |
'type' => 'simple-updates', |
| 196 |
'icon' => 'fa-bullhorn', |
| 197 |
'class' => 'pro', |
| 198 |
'addon' => 'simple-updates', |
| 199 |
), |
| 200 |
'video' => array( |
| 201 |
'order' => '1000', |
| 202 |
'name' => esc_html__( 'Video', 'charitable' ), |
| 203 |
'type' => 'video', |
| 204 |
'icon' => 'fa-film', |
| 205 |
'class' => 'pro', |
| 206 |
'addon' => 'video', |
| 207 |
), |
| 208 |
) |
| 209 |
); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Filters array of fields to be loaded. |
| 214 |
* |
| 215 |
* @since 1.8.0 |
| 216 |
* |
| 217 |
* @param array $fields Field types. |
| 218 |
*/ |
| 219 |
public function load_pro_fields( $fields = false ) { |
| 220 |
|
| 221 |
if ( false === $fields ) { |
| 222 |
$fields = array(); |
| 223 |
} |
| 224 |
|
| 225 |
foreach ( $this->pro_fields as $pro_field ) { |
| 226 |
if ( ! key_exists( $pro_field['type'], $fields ) ) { |
| 227 |
$fields[] = array( $pro_field['type'] => $pro_field ); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
return $fields; |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Adjust attributes on field buttons. |
| 237 |
* |
| 238 |
* @since 1.8.0 |
| 239 |
* |
| 240 |
* @param array $atts Button attributes. |
| 241 |
* @param array $field Button properties. |
| 242 |
* |
| 243 |
* @return array Attributes array. |
| 244 |
*/ |
| 245 |
public function fields_attributes( $atts, $field ) { |
| 246 |
|
| 247 |
if ( ! charitable_is_pro() && ! empty( $field['class'] ) && $field['class'] === 'pro' ) { |
| 248 |
$atts['class'][] = 'charitable-not-available'; |
| 249 |
} elseif ( charitable_is_pro() && ! empty( $field['class'] ) && $field['class'] === 'pro' ) { |
| 250 |
$atts['class'][] = 'charitable-not-installed'; |
| 251 |
} |
| 252 |
|
| 253 |
return $atts; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
} |
| 258 |
|
| 259 |
new Charitable_Builder_Fields(); |
| 260 |
|