| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class to load addon functionality. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Addons |
| 6 |
* @author Eric Daams |
| 7 |
* @copyright Copyright (c) 2019, Studio 164a |
| 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' ) ) { exit; } |
| 15 |
|
| 16 |
if ( ! class_exists( 'Charitable_Addons' ) ) : |
| 17 |
|
| 18 |
/** |
| 19 |
* Charitable_Addons |
| 20 |
* |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
class Charitable_Addons { |
| 24 |
|
| 25 |
/** |
| 26 |
* Load addons. This is executed before the charitable_start hook |
| 27 |
* to allow addons to hook into that. |
| 28 |
* |
| 29 |
* @since 1.0.0 |
| 30 |
* |
| 31 |
* @param Charitable $charitable |
| 32 |
* @return void |
| 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 |
* @since 1.0.0 |
| 46 |
*/ |
| 47 |
private function __construct() { |
| 48 |
add_action( 'charitable_activate_addon', array( $this, 'activate_addon' ) ); |
| 49 |
add_action( 'after_setup_theme', array( $this, 'load_addons' ) ); |
| 50 |
|
| 51 |
do_action( 'charitable_addons_start', $this ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Activate an addon. |
| 56 |
* |
| 57 |
* This is programatically called on the charitable_activate_addon hook, |
| 58 |
* triggered by a plugin. |
| 59 |
* |
| 60 |
* @since 1.0.0 |
| 61 |
* |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
public function activate_addon( $addon ) { |
| 65 |
/* This method should only be called on the charitable_activate_addon hook */ |
| 66 |
if ( 'charitable_activate_addon' !== current_filter() ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
$filepath = $this->get_validated_addon_filepath( $addon ); |
| 71 |
|
| 72 |
/* If we cannot read the file, bounce back with an error. */ |
| 73 |
if ( false === $filepath ) { |
| 74 |
charitable_get_deprecated()->doing_it_wrong( |
| 75 |
__METHOD__, |
| 76 |
sprintf( 'File %s does not exist or is not readable', $filepath ), |
| 77 |
'1.0.0' |
| 78 |
); |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
$this->load_addon_dependencies(); |
| 83 |
|
| 84 |
require_once( $filepath ); |
| 85 |
|
| 86 |
$class = $this->get_addon_class( $addon ); |
| 87 |
|
| 88 |
/* Call the Addon's activate method */ |
| 89 |
call_user_func( array( $class, 'activate' ) ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Load activated addons. |
| 94 |
* |
| 95 |
* @since 1.0.0 |
| 96 |
* |
| 97 |
* @return void |
| 98 |
*/ |
| 99 |
public function load_addons() { |
| 100 |
$active_addons = apply_filters( 'charitable_active_addons', array() ); |
| 101 |
|
| 102 |
if ( empty( $active_addons ) ) { |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
$this->load_addon_dependencies(); |
| 107 |
|
| 108 |
foreach ( $active_addons as $addon ) { |
| 109 |
$filepath = $this->get_validated_addon_filepath( $addon ); |
| 110 |
|
| 111 |
/* If we cannot read the file, bounce back with an error. */ |
| 112 |
if ( false === $filepath ) { |
| 113 |
charitable_get_deprecated()->doing_it_wrong( |
| 114 |
__METHOD__, |
| 115 |
sprintf( 'File %s does not exist or is not readable', $filepath ), |
| 116 |
'1.0.0' |
| 117 |
); |
| 118 |
continue; |
| 119 |
} |
| 120 |
|
| 121 |
require_once( $filepath ); |
| 122 |
|
| 123 |
/* Call the Addon's load method */ |
| 124 |
call_user_func( array( $this->get_addon_class( $addon ), 'load' ) ); |
| 125 |
} |
| 126 |
|
| 127 |
do_action( 'charitable_addons_loaded', $active_addons ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Load interface and abstract classes that addons use. |
| 132 |
* |
| 133 |
* @since 1.0.0 |
| 134 |
* |
| 135 |
* @return void |
| 136 |
*/ |
| 137 |
private function load_addon_dependencies() { |
| 138 |
require_once( charitable()->get_path( 'includes' ) . 'interfaces/interface-charitable-addon.php' ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Return the validated filepath, or false if the file path could |
| 143 |
* not be validated. |
| 144 |
* |
| 145 |
* @since 1.5.4 |
| 146 |
* |
| 147 |
* @param string $addon The addon slug. |
| 148 |
* @return string|false |
| 149 |
*/ |
| 150 |
private function get_validated_addon_filepath( $addon ) { |
| 151 |
$filepath = charitable()->get_path( 'includes' ) . "addons/{$addon}/class-{$addon}.php"; |
| 152 |
$valid = file_exists( $filepath ) && is_readable( $filepath ); |
| 153 |
return $valid ? $filepath : false; |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Get class name of addon. |
| 158 |
* |
| 159 |
* @since 1.0.0 |
| 160 |
* |
| 161 |
* @param string $addon The addon slug. |
| 162 |
* @return string |
| 163 |
*/ |
| 164 |
private function get_addon_class( $addon ) { |
| 165 |
$class = str_replace( '-', ' ', $addon ); |
| 166 |
$class = ucfirst( $class ); |
| 167 |
return str_replace( ' ', '_', $class ); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
endif; |
| 172 |
|