| 1 |
<?php |
| 2 |
/** |
| 3 |
* Donation Factory Class |
| 4 |
* |
| 5 |
* The Charitable donation factory creating the right donation objects. |
| 6 |
* |
| 7 |
* @package Charitable/Classes/Charitable_Donation_Factory |
| 8 |
* @author David Bisset |
| 9 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 10 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 11 |
* @since 1.4.0 |
| 12 |
* @version 1.4.0 |
| 13 |
*/ |
| 14 |
|
| 15 |
// Exit if accessed directly. |
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
if ( ! class_exists( 'Charitable_Donation_Factory' ) ) : |
| 21 |
|
| 22 |
/** |
| 23 |
* Donation Factory |
| 24 |
* |
| 25 |
* @since 1.4.0 |
| 26 |
*/ |
| 27 |
class Charitable_Donation_Factory { |
| 28 |
|
| 29 |
/** |
| 30 |
* Get donation. |
| 31 |
* |
| 32 |
* @since 1.4.0 |
| 33 |
* |
| 34 |
* @param boolean|int|Charitable_Donation $donation A donation object, ID or false. |
| 35 |
* @return Charitable_Donation|boolean |
| 36 |
*/ |
| 37 |
public function get_donation( $donation = false ) { |
| 38 |
global $post; |
| 39 |
|
| 40 |
if ( false === $donation ) { |
| 41 |
$donation = $post; |
| 42 |
} elseif ( is_numeric( $donation ) ) { |
| 43 |
$donation = get_post( $donation ); |
| 44 |
} elseif ( $donation instanceof Charitable_Donation ) { |
| 45 |
$donation = get_post( $donation->id ); |
| 46 |
} |
| 47 |
|
| 48 |
if ( ! $donation || ! is_object( $donation ) ) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Filter the list of valid donation types. |
| 54 |
* |
| 55 |
* @since 1.4.7 |
| 56 |
* |
| 57 |
* @param string[] $post_types List of post types that qualify as donations. |
| 58 |
*/ |
| 59 |
$valid_post_types = apply_filters( |
| 60 |
'charitable_valid_donation_types', |
| 61 |
array( |
| 62 |
Charitable::DONATION_POST_TYPE, |
| 63 |
) |
| 64 |
); |
| 65 |
|
| 66 |
if ( ! in_array( $donation->post_type, $valid_post_types ) ) { // phpcs:ignore |
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
$classname = $this->get_donation_class( $donation ); |
| 71 |
|
| 72 |
if ( ! class_exists( $classname ) ) { |
| 73 |
$classname = 'Charitable_Donation'; |
| 74 |
} |
| 75 |
|
| 76 |
return new $classname( $donation ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Create a class name e.g. Charitable_Donation_Type_Class instead of charitable_donation_type-class. |
| 81 |
* |
| 82 |
* @since 1.4.0 |
| 83 |
* |
| 84 |
* @param string $donation_type The type of donation (e.g. 'charitable-donation'). |
| 85 |
* @return string|false The generated class name or false if invalid. |
| 86 |
*/ |
| 87 |
private function get_classname_from_donation_type( $donation_type ) { |
| 88 |
return 'Charitable_' . implode( '_', array_map( 'ucfirst', explode( '-', $donation_type ) ) ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Get the donation class name. |
| 93 |
* |
| 94 |
* @since 1.4.0 |
| 95 |
* |
| 96 |
* @param WP_Post $the_donation The donation post object. |
| 97 |
* @return string The donation class name. |
| 98 |
*/ |
| 99 |
private function get_donation_class( $the_donation ) { |
| 100 |
$donation_id = absint( $the_donation->ID ); |
| 101 |
$donation_type = $the_donation->post_type; |
| 102 |
|
| 103 |
$classname = $this->get_classname_from_donation_type( $donation_type ); |
| 104 |
|
| 105 |
// Filter classname so that the class can be overridden if extended. |
| 106 |
return apply_filters( 'charitable_donation_class', $classname, $donation_type, $donation_id ); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
endif; |
| 111 |
|