← All changes
|
includes/campaigns/class-charitable-campaign-processor.php
+128
-43
1.6.7
→
1.8.13
View file →
| @@ -2,17 +2,19 @@ | ||
| 2 | 2 | /** |
| 3 | 3 | * Class that is responsible for creating campaigns. |
| 4 | 4 | * |
| 5 | 5 | * @package Charitable/Classes/Charitable_Campaign_Processor |
| 6 | - * @author Eric Daams | |
| 7 | - * @copyright Copyright (c) 2018, Studio 164a | |
| 6 | + * @author David Bisset | |
| 7 | + * @copyright Copyright (c) 2023, WP Charitable LLC | |
| 8 | 8 | * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 | 9 | * @since 1.5.9 |
| 10 | - * @version 1.5.10 | |
| 10 | + * @version 1.6.54 | |
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | 13 | // Exit if accessed directly. |
| 14 | -if ( ! defined( 'ABSPATH' ) ) { exit; } | |
| 14 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 15 | + exit; | |
| 16 | +} | |
| 15 | 17 | |
| 16 | 18 | if ( ! class_exists( 'Charitable_Campaign_Processor' ) ) : |
| 17 | 19 | |
| 18 | 20 | /** |
| @@ -58,8 +60,17 @@ | ||
| 58 | 60 | */ |
| 59 | 61 | protected $meta_keys; |
| 60 | 62 | |
| 61 | 63 | /** |
| 64 | + * Campaign id. | |
| 65 | + * | |
| 66 | + * @since 1.5.9 | |
| 67 | + * | |
| 68 | + * @var array | |
| 69 | + */ | |
| 70 | + public $campaign_id; | |
| 71 | + | |
| 72 | + /** | |
| 62 | 73 | * Create class object. |
| 63 | 74 | * |
| 64 | 75 | * @since 1.5.9 |
| 65 | 76 | * |
| @@ -80,10 +91,12 @@ | ||
| 80 | 91 | $this->meta_keys = is_array( $meta_keys ) ? $meta_keys : array( |
| 81 | 92 | 'goal', |
| 82 | 93 | 'end_date', |
| 83 | 94 | 'suggested_donations', |
| 95 | + 'suggested_donations_default', | |
| 84 | 96 | 'allow_custom_donations', |
| 85 | 97 | 'description', |
| 98 | + 'minimum_donation_amount', | |
| 86 | 99 | ); |
| 87 | 100 | |
| 88 | 101 | $this->parse_args(); |
| 89 | 102 | } |
| @@ -130,9 +143,10 @@ | ||
| 130 | 143 | /** |
| 131 | 144 | * If a class sanitization method exists for this key, call it. |
| 132 | 145 | */ |
| 133 | 146 | if ( $this->use_sanitizer( $key ) ) { |
| 134 | - $value = call_user_func( array( $this, 'sanitize_' . $key ), $value ); | |
| 147 | + $sanitizer_method = 'sanitize_' . $key; | |
| 148 | + $value = $this->$sanitizer_method( $value ); | |
| 135 | 149 | } |
| 136 | 150 | |
| 137 | 151 | /** |
| 138 | 152 | * Filter a campaign argument when it is set. |
| @@ -152,10 +166,15 @@ | ||
| 152 | 166 | * |
| 153 | 167 | * @return int The campaign ID, or 0 if the campaign was not created. |
| 154 | 168 | */ |
| 155 | 169 | public function save() { |
| 170 | + | |
| 156 | 171 | $this->campaign_id = $this->save_core(); |
| 157 | 172 | |
| 173 | + if ( ! $this->campaign_id || is_wp_error( $this->campaign_id ) ) { | |
| 174 | + return 0; | |
| 175 | + } | |
| 176 | + | |
| 158 | 177 | $this->save_meta(); |
| 159 | 178 | $this->save_taxonomies(); |
| 160 | 179 | |
| 161 | 180 | return $this->campaign_id; |
| @@ -164,10 +183,11 @@ | ||
| 164 | 183 | /** |
| 165 | 184 | * Save the core data. |
| 166 | 185 | * |
| 167 | 186 | * @since 1.5.9 |
| 187 | + * @since 1.8.1 Added two action hooks. | |
| 168 | 188 | * |
| 169 | - * @return int | |
| 189 | + * @return int|WP_Error The donation ID if succesful. The value 0 or WP_Error on failure. | |
| 170 | 190 | */ |
| 171 | 191 | public function save_core() { |
| 172 | 192 | $data = array_reduce( $this->core_keys, array( $this, 'get_core_data_value' ) ); |
| 173 | 193 | $data['post_type'] = Charitable::CAMPAIGN_POST_TYPE; |
| @@ -185,12 +205,39 @@ | ||
| 185 | 205 | /** |
| 186 | 206 | * Update or insert the campaign. |
| 187 | 207 | */ |
| 188 | 208 | if ( $data['ID'] ) { |
| 189 | - return wp_update_post( $data ); | |
| 209 | + | |
| 210 | + // The returned value can be either the campaign_id or 0 on fail. | |
| 211 | + $campaign_id = wp_update_post( $data ); | |
| 212 | + | |
| 213 | + /** | |
| 214 | + * Peform an action after the campaign has been updated. | |
| 215 | + * | |
| 216 | + * @since 1.8.1 | |
| 217 | + * | |
| 218 | + * @param array $data Core data. | |
| 219 | + * @param Charitable_Campaign_Processor $processor This processor object. | |
| 220 | + */ | |
| 221 | + do_action( 'charitable_campaign_processor_save_core', 'update', $campaign_id, $data, $this ); | |
| 222 | + | |
| 223 | + return $campaign_id; | |
| 190 | 224 | } |
| 191 | 225 | |
| 192 | - return wp_insert_post( $data ); | |
| 226 | + // The returned value can be either the campaign_id or 0 on fail. | |
| 227 | + $campaign_id = wp_insert_post( $data ); | |
| 228 | + | |
| 229 | + /** | |
| 230 | + * Peform an action after the campaign has been updated. | |
| 231 | + * | |
| 232 | + * @since 1.8.1 | |
| 233 | + * | |
| 234 | + * @param array $data Core data. | |
| 235 | + * @param Charitable_Campaign_Processor $processor This processor object. | |
| 236 | + */ | |
| 237 | + do_action( 'charitable_campaign_processor_save_core', 'insert', $campaign_id, $data, $this ); | |
| 238 | + | |
| 239 | + return $campaign_id; | |
| 193 | 240 | } |
| 194 | 241 | |
| 195 | 242 | /** |
| 196 | 243 | * Save the campaign meta data. |
| @@ -195,10 +242,11 @@ | ||
| 195 | 242 | /** |
| 196 | 243 | * Save the campaign meta data. |
| 197 | 244 | * |
| 198 | 245 | * @since 1.5.9 |
| 246 | + * @since 1.6.54 Return an array, instead of true. | |
| 199 | 247 | * |
| 200 | - * @return boolean | |
| 248 | + * @return array The meta data saved, in key=>value format. | |
| 201 | 249 | */ |
| 202 | 250 | public function save_meta() { |
| 203 | 251 | /** |
| 204 | 252 | * Filter the campaign meta. |
| @@ -207,11 +255,14 @@ | ||
| 207 | 255 | * |
| 208 | 256 | * @param array $meta Meta data. |
| 209 | 257 | * @param Charitable_Campaign_Processor $processor This processor object. |
| 210 | 258 | */ |
| 259 | + | |
| 211 | 260 | $meta = apply_filters( 'charitable_campaign_processor_meta_data', charitable_array_subset( $this->args, $this->meta_keys ), $this ); |
| 212 | 261 | |
| 213 | - return array_walk( $meta, array( $this, 'save_meta_field' ) ); | |
| 262 | + array_walk( $meta, array( $this, 'save_meta_field' ) ); | |
| 263 | + | |
| 264 | + return $meta; | |
| 214 | 265 | } |
| 215 | 266 | |
| 216 | 267 | /** |
| 217 | 268 | * Returns a truncated key. |
| @@ -228,10 +279,11 @@ | ||
| 228 | 279 | /** |
| 229 | 280 | * Save the campaign meta data. |
| 230 | 281 | * |
| 231 | 282 | * @since 1.5.9 |
| 283 | + * @since 1.6.54 Return an array, instead of void. | |
| 232 | 284 | * |
| 233 | - * @return void | |
| 285 | + * @return array Taxonomy data that was saved. | |
| 234 | 286 | */ |
| 235 | 287 | public function save_taxonomies() { |
| 236 | 288 | /** |
| 237 | 289 | * Filter the campaign taxonomy data. |
| @@ -240,16 +292,24 @@ | ||
| 240 | 292 | * |
| 241 | 293 | * @param array $data Taxonomy data. |
| 242 | 294 | * @param Charitable_Campaign_Processor $processor This processor object. |
| 243 | 295 | */ |
| 244 | - $data = apply_filters( 'charitable_campaign_processor_taxonomy_data', array( | |
| 245 | - 'campaign_category' => $this->get( 'categories' ), | |
| 246 | - 'campaign_tag' => $this->get( 'tags' ), | |
| 247 | - ), $this ); | |
| 296 | + $data = apply_filters( | |
| 297 | + 'charitable_campaign_processor_taxonomy_data', | |
| 298 | + array( | |
| 299 | + 'campaign_category' => $this->get( 'categories' ), | |
| 300 | + 'campaign_tag' => $this->get( 'tags' ), | |
| 301 | + ), | |
| 302 | + $this | |
| 303 | + ); | |
| 248 | 304 | |
| 249 | 305 | foreach ( $data as $taxonomy => $terms ) { |
| 250 | - wp_set_object_terms( $this->campaign_id, $terms, $taxonomy, false ); | |
| 306 | + if ( false !== $terms ) { | |
| 307 | + wp_set_object_terms( $this->campaign_id, $terms, $taxonomy, false ); | |
| 308 | + } | |
| 251 | 309 | } |
| 310 | + | |
| 311 | + return $data; | |
| 252 | 312 | } |
| 253 | 313 | |
| 254 | 314 | /** |
| 255 | 315 | * Checks whether this is a new campaign being created, |
| @@ -277,24 +337,29 @@ | ||
| 277 | 337 | * @since 1.5.9 |
| 278 | 338 | * |
| 279 | 339 | * @param array $args The list of default arguments. |
| 280 | 340 | */ |
| 281 | - return apply_filters( 'charitable_campaign_processor_default_args', array( | |
| 282 | - 'ID' => 0, | |
| 283 | - 'title' => '', | |
| 284 | - 'content' => '', | |
| 285 | - 'creator' => get_current_user_id(), | |
| 286 | - 'status' => 'publish', | |
| 287 | - 'parent' => 0, | |
| 288 | - 'image' => 0, | |
| 289 | - 'description' => '', | |
| 290 | - 'goal' => 0, | |
| 291 | - 'end_date' => 0, | |
| 292 | - 'suggested_donations' => array(), | |
| 293 | - 'allow_custom_donations' => 1, | |
| 294 | - 'categories' => array(), | |
| 295 | - 'tags' => array(), | |
| 296 | - ) ); | |
| 341 | + return apply_filters( | |
| 342 | + 'charitable_campaign_processor_default_args', | |
| 343 | + array( | |
| 344 | + 'ID' => 0, | |
| 345 | + 'title' => '', | |
| 346 | + 'content' => '', | |
| 347 | + 'creator' => get_current_user_id(), | |
| 348 | + 'status' => 'publish', | |
| 349 | + 'parent' => 0, | |
| 350 | + 'image' => 0, | |
| 351 | + 'description' => '', | |
| 352 | + 'goal' => 0, | |
| 353 | + 'end_date' => 0, | |
| 354 | + 'suggested_donations' => array(), | |
| 355 | + 'suggested_donations_default' => 0, | |
| 356 | + 'allow_custom_donations' => 1, | |
| 357 | + 'categories' => array(), | |
| 358 | + 'tags' => array(), | |
| 359 | + 'minimum_donation_amount' => 0, | |
| 360 | + ) | |
| 361 | + ); | |
| 297 | 362 | } |
| 298 | 363 | |
| 299 | 364 | /** |
| 300 | 365 | * Parse submitted values with the default values. |
| @@ -402,8 +467,24 @@ | ||
| 402 | 467 | return Charitable_Campaign::sanitize_campaign_suggested_donations( $value ); |
| 403 | 468 | } |
| 404 | 469 | |
| 405 | 470 | /** |
| 471 | + * Sanitize the suggested donations default | |
| 472 | + * | |
| 473 | + * @since 1.5.9 | |
| 474 | + * | |
| 475 | + * @param arrat $value Current value of suggested_donations. | |
| 476 | + * @return array | |
| 477 | + */ | |
| 478 | + protected function sanitize_suggested_donations_default( $value ) { | |
| 479 | + if ( $this->should_skip_sanitizer_for_key( 'suggested_donations_default', array( 'Charitable_Campaign', 'sanitize_campaign_suggested_donations' ) ) ) { | |
| 480 | + return $value; | |
| 481 | + } | |
| 482 | + | |
| 483 | + return Charitable_Campaign::sanitize_campaign_suggested_donations_default( $value ); | |
| 484 | + } | |
| 485 | + | |
| 486 | + /** | |
| 406 | 487 | * Sanitize the custom donations checkbox. |
| 407 | 488 | * |
| 408 | 489 | * @since 1.5.9 |
| 409 | 490 | * |
| @@ -414,11 +495,9 @@ | ||
| 414 | 495 | if ( $this->should_skip_sanitizer_for_key( 'allow_custom_donations', array( 'Charitable_Campaign', 'sanitize_custom_donations' ) ) ) { |
| 415 | 496 | return $value; |
| 416 | 497 | } |
| 417 | 498 | |
| 418 | - return Charitable_Campaign::sanitize_custom_donations( $value, array( | |
| 419 | - '_campaign_suggested_donations' => $this->get_suggested_donations_value(), | |
| 420 | - ) ); | |
| 499 | + return Charitable_Campaign::sanitize_custom_donations( $value, $this->input ); | |
| 421 | 500 | } |
| 422 | 501 | |
| 423 | 502 | /** |
| 424 | 503 | * Return the input value of the suggested donations, or the existing suggested |
| @@ -471,8 +550,9 @@ | ||
| 471 | 550 | |
| 472 | 551 | if ( ! $this->is_mysql_date_format( $value ) ) { |
| 473 | 552 | charitable_get_deprecated()->doing_it_wrong( |
| 474 | 553 | __METHOD__, |
| 554 | + // Translators: %s is the date passed. | |
| 475 | 555 | sprintf( __( 'Campaign processor requires date in YYYY-MM-DD format. Date passed as %s. This message was added in Charitable version 1.5.9.', 'charitable' ), $value ), |
| 476 | 556 | null |
| 477 | 557 | ); |
| 478 | 558 | |
| @@ -515,12 +595,14 @@ | ||
| 515 | 595 | $parent = get_post_field( 'post_parent', $value ); |
| 516 | 596 | |
| 517 | 597 | /* If the image is unattached to any post, attach it to the campaign. */ |
| 518 | 598 | if ( 0 === $parent ) { |
| 519 | - wp_update_post( array( | |
| 520 | - 'ID' => $value, | |
| 521 | - 'post_parent' => $this->campaign_id, | |
| 522 | - ) ); | |
| 599 | + wp_update_post( | |
| 600 | + array( | |
| 601 | + 'ID' => $value, | |
| 602 | + 'post_parent' => $this->campaign_id, | |
| 603 | + ) | |
| 604 | + ); | |
| 523 | 605 | } |
| 524 | 606 | } |
| 525 | 607 | |
| 526 | 608 | /** |
| @@ -552,18 +634,19 @@ | ||
| 552 | 634 | * @param mixed $value The value of the meta field. |
| 553 | 635 | * @param string $key The key of the meta field. |
| 554 | 636 | * @return void |
| 555 | 637 | */ |
| 556 | - protected function save_meta_field( $value, $key ) { | |
| 638 | + public function save_meta_field( $value, $key ) { | |
| 557 | 639 | update_post_meta( $this->campaign_id, $this->get_meta_key( $key ), $value ); |
| 558 | 640 | |
| 559 | 641 | if ( method_exists( $this, 'post_process_' . $key ) ) { |
| 560 | - call_user_func( array( $this, 'post_process_' . $key ), $value ); | |
| 642 | + $post_process_method = 'post_process_' . $key; | |
| 643 | + $this->$post_process_method( $value ); | |
| 561 | 644 | } |
| 562 | 645 | } |
| 563 | 646 | |
| 564 | 647 | /** |
| 565 | - * Returns a sanitized meta key. | |
| 648 | + * Returns a sanitized meta key. | |
| 566 | 649 | * |
| 567 | 650 | * @since 1.5.9 |
| 568 | 651 | * |
| 569 | 652 | * @param string $key The key to be sanitized. |
| @@ -627,9 +710,11 @@ | ||
| 627 | 710 | $method = is_array( $callback ) ? implode( '::', $callback ) : $method; |
| 628 | 711 | |
| 629 | 712 | charitable_get_deprecated()->doing_it_wrong( |
| 630 | 713 | $tag, |
| 631 | - sprintf( __( '%s is deprecated as a sanitizer on the %s hook. To prevent sanitization, use add_filter( \'charitable_campaign_processor_disable_sanitizer_%s\', \'__return_true\' ) instead.', 'charitable' ), | |
| 714 | + sprintf( | |
| 715 | + // Translators: %1$s is the method, %2$s is the tag, %3$s is the key. | |
| 716 | + __( '%1$s is deprecated as a sanitizer on the %2$s hook. To prevent sanitization, use add_filter( \'charitable_campaign_processor_disable_sanitizer_%3$s\', \'__return_true\' ) instead.', 'charitable' ), | |
| 632 | 717 | $method, |
| 633 | 718 | $tag, |
| 634 | 719 | $key |
| 635 | 720 | ), |