| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Intergration For WPCode. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Class_Intergrations_WPCode |
| 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.1.6 |
| 10 |
* @version 1.8.1.6 |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly. |
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
if ( ! class_exists( 'Charitable_Intergrations_WPCode' ) ) : |
| 19 |
|
| 20 |
/** |
| 21 |
* Charitable_Tools |
| 22 |
* |
| 23 |
* @final |
| 24 |
* @since 1.8.1.6 |
| 25 |
*/ |
| 26 |
class Charitable_Intergrations_WPCode { |
| 27 |
|
| 28 |
/** |
| 29 |
* The single instance of this class. |
| 30 |
* |
| 31 |
* @since 1.8.1.6 |
| 32 |
* |
| 33 |
* @var Charitable_Tools_Settings|null |
| 34 |
*/ |
| 35 |
private static $instance = null; |
| 36 |
|
| 37 |
/** |
| 38 |
* WPCode lite download URL. |
| 39 |
* |
| 40 |
* @since 1.8.1.6 |
| 41 |
* |
| 42 |
* @var string |
| 43 |
*/ |
| 44 |
public $lite_download_url = 'https://downloads.wordpress.org/plugin/insert-headers-and-footers.zip'; |
| 45 |
|
| 46 |
/** |
| 47 |
* Lite plugin slug. |
| 48 |
* |
| 49 |
* @since 1.8.1.6 |
| 50 |
* |
| 51 |
* @var string |
| 52 |
*/ |
| 53 |
public $lite_plugin_slug = 'insert-headers-and-footers/ihaf.php'; |
| 54 |
|
| 55 |
/** |
| 56 |
* WPCode lite download URL. |
| 57 |
* |
| 58 |
* @since 1.8.1.6 |
| 59 |
* |
| 60 |
* @var string |
| 61 |
*/ |
| 62 |
public $pro_plugin_slug = 'wpcode-premium/wpcode.php'; |
| 63 |
|
| 64 |
/** |
| 65 |
* Load the class. |
| 66 |
* |
| 67 |
* @since 1.8.1.6 |
| 68 |
*/ |
| 69 |
public function load() { |
| 70 |
|
| 71 |
$this->hooks(); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Hooks. |
| 76 |
* |
| 77 |
* @since 1.8.1.6 |
| 78 |
*/ |
| 79 |
private function hooks() { |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Load the WPCode snippets for our desired username or return an empty array if not available. |
| 84 |
* |
| 85 |
* @since 1.8.1.6 |
| 86 |
* |
| 87 |
* @param int|bool $limit The number of snippets to load. |
| 88 |
* @param bool $show_installed Whether to show installed snippets. |
| 89 |
* |
| 90 |
* @return array The snippets. |
| 91 |
*/ |
| 92 |
public function load_charitable_snippets( $limit = false, $show_installed = true ): array { // phpcs:ignore |
| 93 |
|
| 94 |
$snippets = $this->get_placeholder_snippets(); |
| 95 |
|
| 96 |
if ( function_exists( 'wpcode_get_library_snippets_by_username' ) ) { |
| 97 |
$snippets = wpcode_get_library_snippets_by_username( 'wpcharitable' ); |
| 98 |
} |
| 99 |
|
| 100 |
// Sort by installed. |
| 101 |
uasort( |
| 102 |
$snippets, |
| 103 |
function ( $a, $b ) { |
| 104 |
return ( $b['installed'] <=> $a['installed'] ); |
| 105 |
} |
| 106 |
); |
| 107 |
|
| 108 |
// If there's a limit, slice the array. |
| 109 |
if ( $limit ) { |
| 110 |
$snippets = array_slice( $snippets, 0, $limit ); |
| 111 |
} |
| 112 |
|
| 113 |
return $snippets; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Checks if the plugin is installed, either the lite or premium version. |
| 118 |
* |
| 119 |
* @since 1.8.1.6 |
| 120 |
* |
| 121 |
* @return bool True if the plugin is installed. |
| 122 |
*/ |
| 123 |
public function is_plugin_installed(): bool { |
| 124 |
|
| 125 |
return $this->is_pro_installed() || $this->is_lite_installed(); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Is the pro plugin installed. |
| 130 |
* |
| 131 |
* @since 1.8.1.6 |
| 132 |
* |
| 133 |
* @return bool True if the pro plugin is installed. |
| 134 |
*/ |
| 135 |
public function is_pro_installed(): bool { |
| 136 |
|
| 137 |
return array_key_exists( $this->pro_plugin_slug, get_plugins() ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Is the lite plugin installed. |
| 142 |
* |
| 143 |
* @since 1.8.1.6 |
| 144 |
* |
| 145 |
* @return bool True if the lite plugin is installed. |
| 146 |
*/ |
| 147 |
public function is_lite_installed(): bool { |
| 148 |
|
| 149 |
return array_key_exists( $this->lite_plugin_slug, get_plugins() ); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Basic check if the plugin is active by looking for the main function. |
| 154 |
* |
| 155 |
* @since 1.8.1.6 |
| 156 |
* |
| 157 |
* @return bool True if the plugin is active. |
| 158 |
*/ |
| 159 |
public function is_plugin_active(): bool { |
| 160 |
|
| 161 |
return function_exists( 'wpcode' ); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Get plugin version. |
| 166 |
* |
| 167 |
* @since 1.8.1.6 |
| 168 |
* |
| 169 |
* @return string |
| 170 |
*/ |
| 171 |
public function plugin_version(): string { |
| 172 |
|
| 173 |
if ( $this->is_pro_installed() ) { |
| 174 |
return get_plugins()[ $this->pro_plugin_slug ]['Version']; |
| 175 |
} |
| 176 |
|
| 177 |
if ( $this->is_lite_installed() ) { |
| 178 |
return get_plugins()[ $this->lite_plugin_slug ]['Version']; |
| 179 |
} |
| 180 |
|
| 181 |
return ''; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Get placeholder snippets if the WPCode snippets are not available. |
| 186 |
* |
| 187 |
* @since 1.8.1.6 |
| 188 |
* |
| 189 |
* @return array The placeholder snippets. |
| 190 |
*/ |
| 191 |
private function get_placeholder_snippets(): array { |
| 192 |
|
| 193 |
$snippet_titles = [ |
| 194 |
'Redirect To Referer After Login', |
| 195 |
'Add Custom Field to Donation Form', |
| 196 |
'Remove Donation Form Fields', |
| 197 |
'Set Custom Redirection After Registration', |
| 198 |
'Change Custom Amount Field Label', |
| 199 |
'Collect National ID Number', |
| 200 |
'Add Field Placeholders', |
| 201 |
'Remove Campaign Description', |
| 202 |
'Change Country Field To Hidden', |
| 203 |
'Add Donation Form Shortcode', |
| 204 |
'Add Checkbox To Donation Form', |
| 205 |
'Make Donor Address Required', |
| 206 |
'Use Page Template For Campaigns', |
| 207 |
'Add Accept Terms Field', |
| 208 |
'Remove Section', |
| 209 |
'Set Custom Donation Receipt Page', |
| 210 |
]; |
| 211 |
|
| 212 |
$placeholder_snippets = []; |
| 213 |
|
| 214 |
foreach ( $snippet_titles as $snippet_title ) { |
| 215 |
|
| 216 |
// Add placeholder install link so we show a button. |
| 217 |
$placeholder_snippets[] = [ |
| 218 |
'title' => $snippet_title, |
| 219 |
'install' => 'https://library.wpcode.com/', |
| 220 |
'installed' => false, |
| 221 |
'note' => 'Placeholder code snippet short description text.', |
| 222 |
]; |
| 223 |
} |
| 224 |
|
| 225 |
return $placeholder_snippets; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Enqueue assets. |
| 230 |
* |
| 231 |
* @since 1.8.1.6 |
| 232 |
*/ |
| 233 |
public function enqueue_scripts() { |
| 234 |
|
| 235 |
if ( ! class_exists( 'Charitable' ) ) { |
| 236 |
return; |
| 237 |
} |
| 238 |
|
| 239 |
$min = charitable_get_min_suffix(); |
| 240 |
$version = charitable()->get_version(); |
| 241 |
$assets_dir = charitable()->get_path( 'assets', false ); |
| 242 |
|
| 243 |
/* The following styles are only loaded on Charitable screens. */ |
| 244 |
$screen = get_current_screen(); |
| 245 |
|
| 246 |
if ( ( ! empty( $_GET['page'] ) && 'charitable-dashboard' === $_GET['page'] ) || ( ! is_null( $screen ) && 'charitable_page_charitable-tools' === $screen->base ) || ( ! empty( $_GET['tab'] ) && 'snippets' === $_GET['tab'] ) ) { // phpcs:ignore |
| 247 |
|
| 248 |
wp_enqueue_script( |
| 249 |
'listjs', |
| 250 |
$assets_dir . 'js/libraries/list.min.js', |
| 251 |
[ 'jquery' ], |
| 252 |
$version, |
| 253 |
false |
| 254 |
); |
| 255 |
|
| 256 |
wp_enqueue_script( |
| 257 |
'charitable-wpcode', |
| 258 |
$assets_dir . "js/integrations/wpcode/wpcode{$min}.js", |
| 259 |
[ 'jquery', 'listjs' ], |
| 260 |
$version, |
| 261 |
true |
| 262 |
); |
| 263 |
|
| 264 |
wp_localize_script( |
| 265 |
'charitable-wpcode', |
| 266 |
'charitableWpcodeVars', |
| 267 |
[ |
| 268 |
'installing_text' => __( 'Installing', 'charitable' ), |
| 269 |
] |
| 270 |
); |
| 271 |
|
| 272 |
wp_register_style( |
| 273 |
'charitable-integrations-wpcode', |
| 274 |
$assets_dir . 'css/integrations/wpcode' . $min . '.css', |
| 275 |
array(), |
| 276 |
$version |
| 277 |
); |
| 278 |
|
| 279 |
wp_enqueue_style( 'charitable-integrations-wpcode' ); |
| 280 |
|
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Returns and/or create the single instance of this class. |
| 286 |
* |
| 287 |
* @since 1.8.1.6 |
| 288 |
* |
| 289 |
* @return Charitable_Tools_Settings |
| 290 |
*/ |
| 291 |
public static function get_instance() { |
| 292 |
if ( is_null( self::$instance ) ) { |
| 293 |
self::$instance = new self(); |
| 294 |
} |
| 295 |
|
| 296 |
return self::$instance; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
endif; |
| 301 |
|