| 1 |
<?php |
| 2 |
/** |
| 3 |
* The class that defines a subpanel for the settings area for the campaign builder. |
| 4 |
* |
| 5 |
* @package Charitable/Admin/Charitable_Campaign_Meta_Boxes |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.8.0 |
| 10 |
* @version. 1.8.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Builder_Panel_Settings_Addons' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* General subpanel for Settings Panel for campaign builder. |
| 21 |
* |
| 22 |
* @since 1.8.0 |
| 23 |
*/ |
| 24 |
class Charitable_Builder_Panel_Settings_Addons { |
| 25 |
|
| 26 |
/** |
| 27 |
* Slug. |
| 28 |
* |
| 29 |
* @since 1.8.0 |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
private $addon_array = array(); |
| 34 |
|
| 35 |
/** |
| 36 |
* Get things going. Add action hooks for the sidebar menu and the panel itself. |
| 37 |
* |
| 38 |
* @since 1.8.0 |
| 39 |
*/ |
| 40 |
public function __construct() { |
| 41 |
|
| 42 |
$this->init(); |
| 43 |
|
| 44 |
add_action( 'charitable_campaign_builder_settings_sidebar', array( $this, 'sidebar_tab' ), 55 ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Init some things. |
| 49 |
* |
| 50 |
* @since 1.8.0 |
| 51 |
*/ |
| 52 |
public function init() { |
| 53 |
|
| 54 |
$this->addon_array = apply_filters( |
| 55 |
'charitable_campaign_builder_settings_addons_not_available', |
| 56 |
array( |
| 57 |
'ambassadors' => array( |
| 58 |
'label' => esc_html__( 'Ambassadors & Peer to Peer Fundraising', 'charitable' ), |
| 59 |
'addon-slug' => 'charitable-ambassadors', |
| 60 |
'plan-to-upgrade-to' => 'pro', |
| 61 |
), |
| 62 |
'recurring-donations' => array( |
| 63 |
'label' => esc_html__( 'Recurring Donations', 'charitable' ), |
| 64 |
'addon-slug' => 'charitable-recurring', |
| 65 |
'plan-to-upgrade-to' => 'plus', |
| 66 |
), |
| 67 |
'fee-relief' => array( |
| 68 |
'label' => esc_html__( 'Fee Relief', 'charitable' ), |
| 69 |
'addon-slug' => 'charitable-fee-relief', |
| 70 |
'plan-to-upgrade-to' => 'plus', |
| 71 |
), |
| 72 |
'simple-updates' => array( |
| 73 |
'label' => esc_html__( 'Simple Updates', 'charitable' ), |
| 74 |
'addon-slug' => 'charitable-simple-updates', |
| 75 |
'plan-to-upgrade-to' => 'plus', |
| 76 |
), |
| 77 |
'geolocation' => array( |
| 78 |
'label' => esc_html__( 'Geolocation', 'charitable' ), |
| 79 |
'addon-slug' => 'charitable-geolocation', |
| 80 |
'plan-to-upgrade-to' => 'plus', |
| 81 |
), |
| 82 |
) |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Sort the addon array "on the fly" so that items without "file" stay at the bottom. |
| 88 |
* |
| 89 |
* @since 1.8.0 |
| 90 |
* @param array $addon_array The array of addons. |
| 91 |
*/ |
| 92 |
private function sort_addon_array( $addon_array ) { |
| 93 |
|
| 94 |
$with_file = []; |
| 95 |
$without_file = []; |
| 96 |
|
| 97 |
foreach ( $addon_array as $key => $item ) { |
| 98 |
if ( isset( $item['file'] ) ) { |
| 99 |
$with_file[ $key ] = $item; |
| 100 |
} else { |
| 101 |
$without_file[ $key ] = $item; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
return $with_file + $without_file; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Retrieve the plugin basename from the plugin slug. |
| 110 |
* |
| 111 |
* @since 1.7.0 |
| 112 |
* |
| 113 |
* @param string $slug The plugin slug. |
| 114 |
* @return string The plugin basename if found, else the plugin slug. |
| 115 |
*/ |
| 116 |
public function get_plugin_basename_from_slug( $slug ) { |
| 117 |
|
| 118 |
$keys = array_keys( get_plugins() ); |
| 119 |
|
| 120 |
foreach ( $keys as $key ) { |
| 121 |
if ( preg_match( '|^' . $slug . '|', $key ) ) { |
| 122 |
return $key; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return $slug; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Generate sidebar html. |
| 131 |
* |
| 132 |
* @since 1.8.0 |
| 133 |
* @since 1.8.6.2 - Tweak recurring add-on + not available logic. |
| 134 |
*/ |
| 135 |
public function sidebar_tab() { |
| 136 |
|
| 137 |
if ( ! is_array( $this->addon_array ) || empty( $this->addon_array ) ) : |
| 138 |
return; |
| 139 |
endif; |
| 140 |
|
| 141 |
// grab the list of active plugins only once, don't need it in the loop. |
| 142 |
$active_plugins = apply_filters( 'active_plugins', get_option( 'active_plugins' ) ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- 'active_plugins' is a WordPress core hook. |
| 143 |
|
| 144 |
// sort array. |
| 145 |
$addon_array = $this->sort_addon_array( ( $this->addon_array ) ); |
| 146 |
|
| 147 |
if ( empty( $addon_array ) ) { |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
$installed_plugins = get_plugins(); |
| 152 |
|
| 153 |
// get license stuff once. |
| 154 |
$charitable_addons = get_transient( '_charitable_addons' ); |
| 155 |
|
| 156 |
// Get addons data from transient or perform API query if no transient. |
| 157 |
if ( false === $charitable_addons ) { |
| 158 |
$charitable_addons = charitable_get_addons_data_from_server(); |
| 159 |
} |
| 160 |
|
| 161 |
$current_plan_slug = charitable_get_license_slug_from_plan_id(); |
| 162 |
|
| 163 |
foreach ( $addon_array as $slug => $addon_info ) { |
| 164 |
|
| 165 |
$data_path = false; // 'charitable-conversational-forms/charitable-conversational-forms.php'; |
| 166 |
$slug = esc_attr( $slug ); |
| 167 |
$license = ''; |
| 168 |
$addon_slug = esc_attr( $addon_info['addon-slug'] ); |
| 169 |
$label = esc_html( $addon_info['label'] ); |
| 170 |
$valid_plans = array(); // populate with all the valid plan types for this addon. |
| 171 |
|
| 172 |
if ( ! empty( $charitable_addons ) ) : |
| 173 |
foreach ( $charitable_addons as $charitable_addon ) { |
| 174 |
if ( ! empty( $charitable_addon['slug'] ) && strpos( $charitable_addon['slug'], $addon_slug ) !== false ) { |
| 175 |
$charitable_addon_info = (array) $charitable_addon; |
| 176 |
break; |
| 177 |
} |
| 178 |
} |
| 179 |
if ( ! empty( $charitable_addon_info['license'] ) && ( in_array( 'agency', $charitable_addon_info['license'], true ) || in_array( 'elite', $charitable_addon_info['license'], true ) ) ) { |
| 180 |
$license = 'pro'; |
| 181 |
} elseif ( ! empty( $charitable_addon_info['license'] ) ) { |
| 182 |
$license = reset( $charitable_addon_info['license'] ); |
| 183 |
} |
| 184 |
|
| 185 |
endif; |
| 186 |
|
| 187 |
switch ( strtolower( $current_plan_slug ) ) { |
| 188 |
case 'pro': |
| 189 |
$valid_plans = array( 'basic', 'plus', 'pro', 'agency', 'elite' ); |
| 190 |
break; |
| 191 |
case 'plus': |
| 192 |
$valid_plans = array( 'basic', 'plus' ); |
| 193 |
break; |
| 194 |
case 'basic': |
| 195 |
$valid_plans = array( 'basic' ); |
| 196 |
break; |
| 197 |
default: |
| 198 |
$valid_plans = array(); // empty. |
| 199 |
break; |
| 200 |
} |
| 201 |
|
| 202 |
// determine css classes. |
| 203 |
$classes = array( |
| 204 |
'charitable-panel-sidebar-section', |
| 205 |
'charitable-panel-sidebar-section-' . $slug, |
| 206 |
); |
| 207 |
|
| 208 |
// Determine the plugin basename. |
| 209 |
$plugin_basename = $this->get_plugin_basename_from_slug( $addon_info['addon-slug'] ); |
| 210 |
|
| 211 |
// Setup an attribute for a url to download the addon, in case we need it. |
| 212 |
$install_url = false; |
| 213 |
|
| 214 |
// If the addon requires an upgrade of the current (valid) plan. |
| 215 |
if ( ! in_array( $license, $valid_plans, true ) ) { |
| 216 |
$classes[] = 'charitable-not-available'; |
| 217 |
// Determine if the addon is installed, active, or needs to be installed based on license and other factors. |
| 218 |
} elseif ( ! charitable_is_pro() && in_array( $addon_slug . '/' . $addon_slug . '.php', $active_plugins, true ) ) { |
| 219 |
// if pro isn't active but it's in the plugin directory, that's a special case. |
| 220 |
$classes[] = 'charitable-addon-installed-charitable-lite charitable-addon-' . $slug; |
| 221 |
} elseif ( charitable_is_pro() && in_array( $addon_slug . '/' . $addon_slug . '.php', $active_plugins, true ) ) { |
| 222 |
// if paid version is installed but somehow user has the addon installed, that's a special case. |
| 223 |
$classes[] = 'charitable-addon-installed-charitable-pro charitable-addon-' . $slug; |
| 224 |
|
| 225 |
} elseif ( ! charitable_is_pro() && ! in_array( $addon_slug . '/' . $addon_slug . '.php', $active_plugins, true ) ) { |
| 226 |
|
| 227 |
$classes[] = 'charitable-not-available'; |
| 228 |
|
| 229 |
} elseif ( ! empty( $valid_plans ) && ! in_array( $current_plan_slug, $valid_plans, true ) ) { // charitable version is a non-lite paid "pro" version but assign the correct class that either warrants an upgrade, install, or is installed. |
| 230 |
|
| 231 |
// if pro is active and the plan isn't high enough, add the CSS class. |
| 232 |
$classes[] = 'charitable-need-upgrade'; |
| 233 |
|
| 234 |
// Set the license to the required plan level for upgrade, not the user's current plan. |
| 235 |
if ( ! empty( $addon_info['plan-to-upgrade-to'] ) ) { |
| 236 |
$license = $addon_info['plan-to-upgrade-to']; |
| 237 |
} |
| 238 |
} elseif ( isset( $installed_plugins[ $plugin_basename ] ) && ! is_plugin_active( $plugin_basename ) ) { |
| 239 |
|
| 240 |
// if the plugin exists (installed, visible in the plugin list in WP admin) but not activated. |
| 241 |
$classes[] = 'charitable-not-activated'; |
| 242 |
|
| 243 |
} elseif ( ! in_array( 'charitable-' . $slug . '/charitable-' . $slug . '.php', $active_plugins, true ) ) { |
| 244 |
|
| 245 |
// if pro is active AND the plugin is NOT activated, then that's another CSS class to encourage an install. |
| 246 |
$classes[] = 'charitable-not-installed'; |
| 247 |
|
| 248 |
$install_url = ! empty( $charitable_addon_info['download_link'] ) ? esc_url( $charitable_addon_info['download_link'] ) : false; |
| 249 |
|
| 250 |
// If the install url is not a valid URL, then set it to false. |
| 251 |
if ( ! filter_var( $install_url, FILTER_VALIDATE_URL ) ) { |
| 252 |
$install_url = false; |
| 253 |
} |
| 254 |
} else { |
| 255 |
|
| 256 |
// where do we load the class, this has to be supplied by the addon. |
| 257 |
$file = isset( $addon_info['file'] ) && ! empty( $addon_info['file'] ) ? esc_html( $addon_info['file'] ) : false; |
| 258 |
|
| 259 |
if ( $file ) { |
| 260 |
|
| 261 |
$file = wp_slash( charitable()->get_path( 'plugin-directory' ) ) . $file; |
| 262 |
|
| 263 |
if ( file_exists( $file ) ) { |
| 264 |
$classes[] = 'charitable-addon-installed charitable-addon-loaded'; |
| 265 |
require_once $file; |
| 266 |
} else { |
| 267 |
$classes[] = 'charitable-addon-installed charitable-addon-file-nonexist'; |
| 268 |
} |
| 269 |
|
| 270 |
} else { |
| 271 |
|
| 272 |
$classes[] = 'charitable-addon-installed charitable-addon-file-missing'; |
| 273 |
|
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
$classes = implode( ' ', $classes ); |
| 278 |
|
| 279 |
echo '<a href="#" class="' . esc_attr( $classes ) . '" data-plugin-url="' . esc_attr( $addon_slug ) . '/' . esc_attr( $addon_slug ) . '.php' . '" data-name="' . esc_html( $label ) . ' addon" data-slug="' . esc_attr( $slug ) . '" data-section="' . esc_attr( $slug ) . '" data-action="" data-path="' . esc_attr( $data_path ) . '" data-video="" data-install="' . esc_url( $install_url ) . '" data-license="' . esc_attr( $license ) . '" data-utm-content="' . urlencode( $label ) . '"> ' . esc_html( $label ) . ' <i class="fa fa-angle-right charitable-toggle-arrow"></i></a>'; |
| 280 |
|
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
new Charitable_Builder_Panel_Settings_Addons(); |
| 286 |
|
| 287 |
endif; |
| 288 |
|