PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.7
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.7
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 1.7.0.9 All 211 releases
charitable / includes / addons / class-charitable-addons.php

class-charitable-addons.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.7.0.7, at includes/addons/class-charitable-addons.php

180 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class to load addon functionality.
4 *
5 * @package Charitable/Classes/Charitable_Addons
6 * @author David Bisset
7 * @copyright Copyright (c) 2022, WP Charitable LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.0.0
10 * @version 1.0.0
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 if ( ! class_exists( 'Charitable_Addons' ) ) :
19
20 /**
21 * Charitable_Addons
22 *
23 * @since 1.0.0
24 */
25 class Charitable_Addons {
26
27 /**
28 * Load addons. This is executed before the charitable_start hook
29 * to allow addons to hook into that.
30 *
31 * @since 1.0.0
32 *
33 * @param Charitable $charitable
34 * @return void
35 */
36 public static function load( Charitable $charitable ) {
37 if ( $charitable->started() ) {
38 return;
39 }
40
41 new Charitable_Addons();
42 }
43
44 /**
45 * Create class object.
46 *
47 * @since 1.0.0
48 */
49 private function __construct() {
50 add_action( 'charitable_activate_addon', array( $this, 'activate_addon' ) );
51 add_action( 'after_setup_theme', array( $this, 'load_addons' ) );
52
53 do_action( 'charitable_addons_start', $this );
54 }
55
56 /**
57 * Activate an addon.
58 *
59 * This is programatically called on the charitable_activate_addon hook,
60 * triggered by a plugin.
61 *
62 * @since 1.0.0
63 *
64 * @return void
65 */
66 public function activate_addon( $addon ) {
67 /* This method should only be called on the charitable_activate_addon hook */
68 if ( 'charitable_activate_addon' !== current_filter() ) {
69 return;
70 }
71
72 $filepath = $this->get_validated_addon_filepath( $addon );
73
74 /* If we cannot read the file, bounce back with an error. */
75 if ( false === $filepath ) {
76 charitable_get_deprecated()->doing_it_wrong(
77 __METHOD__,
78 sprintf( 'File %s does not exist or is not readable', $filepath ),
79 '1.0.0'
80 );
81 return;
82 }
83
84 $this->load_addon_dependencies();
85
86 require_once( $filepath );
87
88 $class = $this->get_addon_class( $addon );
89
90 /* Call the Addon's activate method */
91 call_user_func( array( $class, 'activate' ) );
92 }
93
94 /**
95 * Load activated addons.
96 *
97 * @since 1.0.0
98 *
99 * @return void
100 */
101 public function load_addons() {
102 $active_addons = apply_filters( 'charitable_active_addons', array() );
103
104 if ( empty( $active_addons ) ) {
105 return;
106 }
107
108 $this->load_addon_dependencies();
109
110 foreach ( $active_addons as $addon ) {
111
112 //todo: resolve an unknown PHP Notice.
113 if ( $addon === 'charitable-user-dashboard' ) {
114 continue;
115 }
116
117 $filepath = $this->get_validated_addon_filepath( $addon );
118
119 /* If we cannot read the file, bounce back with an error. */
120 if ( false === $filepath ) {
121 charitable_get_deprecated()->doing_it_wrong(
122 __METHOD__,
123 sprintf( 'File %s does not exist or is not readable', $filepath ),
124 '1.0.0'
125 );
126 continue;
127 }
128
129 require_once( $filepath );
130
131 /* Call the Addon's load method */
132 call_user_func( array( $this->get_addon_class( $addon ), 'load' ) );
133 }
134
135 do_action( 'charitable_addons_loaded', $active_addons );
136 }
137
138 /**
139 * Load interface and abstract classes that addons use.
140 *
141 * @since 1.0.0
142 *
143 * @return void
144 */
145 private function load_addon_dependencies() {
146 require_once( charitable()->get_path( 'includes' ) . 'interfaces/interface-charitable-addon.php' );
147 }
148
149 /**
150 * Return the validated filepath, or false if the file path could
151 * not be validated.
152 *
153 * @since 1.5.4
154 *
155 * @param string $addon The addon slug.
156 * @return string|false
157 */
158 private function get_validated_addon_filepath( $addon ) {
159 $filepath = charitable()->get_path( 'includes' ) . "addons/{$addon}/class-{$addon}.php";
160 $valid = file_exists( $filepath ) && is_readable( $filepath );
161 return $valid ? $filepath : false;
162 }
163
164 /**
165 * Get class name of addon.
166 *
167 * @since 1.0.0
168 *
169 * @param string $addon The addon slug.
170 * @return string
171 */
172 private function get_addon_class( $addon ) {
173 $class = str_replace( '-', ' ', $addon );
174 $class = ucfirst( $class );
175 return str_replace( ' ', '_', $class );
176 }
177 }
178
179 endif;
180