PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0
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 1.8.0 All 210 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, at includes/addons/class-charitable-addons.php

174 lines 3.9 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 $filepath = $this->get_validated_addon_filepath( $addon );
112
113 /* If we cannot read the file, bounce back with an error. */
114 if ( false === $filepath ) {
115 charitable_get_deprecated()->doing_it_wrong(
116 __METHOD__,
117 sprintf( 'File %s does not exist or is not readable', $filepath ),
118 '1.0.0'
119 );
120 continue;
121 }
122
123 require_once( $filepath );
124
125 /* Call the Addon's load method */
126 call_user_func( array( $this->get_addon_class( $addon ), 'load' ) );
127 }
128
129 do_action( 'charitable_addons_loaded', $active_addons );
130 }
131
132 /**
133 * Load interface and abstract classes that addons use.
134 *
135 * @since 1.0.0
136 *
137 * @return void
138 */
139 private function load_addon_dependencies() {
140 require_once( charitable()->get_path( 'includes' ) . 'interfaces/interface-charitable-addon.php' );
141 }
142
143 /**
144 * Return the validated filepath, or false if the file path could
145 * not be validated.
146 *
147 * @since 1.5.4
148 *
149 * @param string $addon The addon slug.
150 * @return string|false
151 */
152 private function get_validated_addon_filepath( $addon ) {
153 $filepath = charitable()->get_path( 'includes' ) . "addons/{$addon}/class-{$addon}.php";
154 $valid = file_exists( $filepath ) && is_readable( $filepath );
155 return $valid ? $filepath : false;
156 }
157
158 /**
159 * Get class name of addon.
160 *
161 * @since 1.0.0
162 *
163 * @param string $addon The addon slug.
164 * @return string
165 */
166 private function get_addon_class( $addon ) {
167 $class = str_replace( '-', ' ', $addon );
168 $class = ucfirst( $class );
169 return str_replace( ' ', '_', $class );
170 }
171 }
172
173 endif;
174