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

162 lines 3.7 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 * @version 1.0.0
7 * @author Eric Daams
8 * @copyright Copyright (c) 2015, Studio 164a
9 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
10 */
11
12 // Exit if accessed directly
13 if ( ! defined( 'ABSPATH' ) ) exit;
14
15 if ( ! class_exists( 'Charitable_Addons' ) ) :
16
17 /**
18 * Charitable_Addons
19 *
20 * @since 1.0.0
21 */
22 class Charitable_Addons {
23
24 /**
25 * Load addons. This is executed before the charitable_start hook
26 * to allow addons to hook into that.
27 *
28 * @param Charitable $charitable
29 * @return void
30 * @access public
31 * @static
32 * @since 1.0.0
33 */
34 public static function load( Charitable $charitable ) {
35 if ( $charitable->started() ) {
36 return;
37 }
38
39 new Charitable_Addons();
40 }
41
42 /**
43 * Create class object.
44 *
45 * @access private
46 * @since 1.0.0
47 */
48 private function __construct() {
49 add_action( 'charitable_activate_addon', array( $this, 'activate_addon' ) );
50 add_action( 'after_setup_theme', array( $this, 'load_addons' ) );
51
52 do_action( 'charitable_addons_start', $this );
53 }
54
55 /**
56 * Activate an addon.
57 *
58 * This is programatically called on the charitable_activate_addon hook,
59 * triggered by a plugin.
60 *
61 * @return void
62 * @access public
63 * @since 1.0.0
64 */
65 public function activate_addon( $addon ) {
66 /* This method should only be called on the charitable_activate_addon hook */
67 if ( 'charitable_activate_addon' !== current_filter() ) {
68 return;
69 }
70
71 $filepath = $this->get_addon_filepath( $addon );
72
73 /* If we cannot read the file, bounce back with an error. */
74 if ( ! file_exists( $filepath ) || ! is_readable( $filepath ) ) {
75 // _doing_it_wrong( __METHOD__, sprintf( 'File %s does not exist or is not readable', $filepath ), '1.0.0' );
76 return;
77 }
78
79 $this->load_addon_dependencies();
80
81 require_once( $filepath );
82
83 $class = $this->get_addon_class( $addon );
84
85 /* Call the Addon's activate method */
86 call_user_func( array( $class, 'activate' ) );
87 }
88
89 /**
90 * Load activated addons.
91 *
92 * @return void
93 * @access public
94 * @since 1.0.0
95 */
96 public function load_addons() {
97 $active_addons = apply_filters( 'charitable_active_addons', array() );
98
99 if ( empty( $active_addons ) ) {
100 return;
101 }
102
103 $this->load_addon_dependencies();
104
105 foreach ( $active_addons as $addon ) {
106
107 $filepath = $this->get_addon_filepath( $addon );
108
109 if ( ! file_exists( $filepath ) || ! is_readable( $filepath ) ) {
110 // _doing_it_wrong( __METHOD__, sprintf( 'File %s does not exist or is not readable', $filepath ), '1.0.0' );
111 continue;
112 }
113
114 require_once( $filepath );
115
116 /* Call the Addon's load method */
117 call_user_func( array( $this->get_addon_class( $addon ), 'load' ) );
118 }
119
120 do_action( 'charitable_addons_loaded', $active_addons );
121 }
122
123 /**
124 * Load interface and abstract classes that addons use.
125 *
126 * @return void
127 * @access private
128 * @since 1.0.0
129 */
130 private function load_addon_dependencies() {
131 require_once( charitable()->get_path( 'includes' ) . 'addons/interface-charitable-addon.php' );
132 }
133
134 /**
135 * Return the filepath to the given addon.
136 *
137 * @param string $addon
138 * @return string
139 * @access private
140 * @since 1.0.0
141 */
142 private function get_addon_filepath( $addon ) {
143 return charitable()->get_path( 'includes' ) . "addons/{$addon}/class-{$addon}.php";
144 }
145
146 /**
147 * Get class name of addon.
148 *
149 * @param string $addon
150 * @return string
151 * @access private
152 * @since 1.0.0
153 */
154 private function get_addon_class( $addon ) {
155 $class = str_replace( '-', ' ', $addon );
156 $class = ucfirst( $class );
157 $class = str_replace( ' ', '_', $class );
158 return $class;
159 }
160 }
161
162 endif; // End class_exists check