PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.11
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.11
1.8.13.1 1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 All 212 releases
charitable / includes / donations / class-charitable-donation-factory.php

class-charitable-donation-factory.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.7.0.11, at includes/donations/class-charitable-donation-factory.php

112 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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) 2022, 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 ) ) {
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 /**
81 * Create a class name e.g. Charitable_Donation_Type_Class instead of chartiable_donation_type-class.
82 *
83 * @since 1.4.0
84 *
85 * @param string $donation_type
86 * @return string|false
87 */
88 private function get_classname_from_donation_type( $donation_type ) {
89 return 'Charitable_' . implode( '_', array_map( 'ucfirst', explode( '-', $donation_type ) ) );
90 }
91
92 /**
93 * Get the product class name.
94 *
95 * @since 1.4.0
96 *
97 * @param WP_Post $the_donation
98 * @return string
99 */
100 private function get_donation_class( $the_donation ) {
101 $donation_id = absint( $the_donation->ID );
102 $donation_type = $the_donation->post_type;
103
104 $classname = $this->get_classname_from_donation_type( $donation_type );
105
106 // Filter classname so that the class can be overridden if extended.
107 return apply_filters( 'charitable_donation_class', $classname, $donation_type, $donation_id );
108 }
109 }
110
111 endif;
112