| 1 |
<?php |
| 2 |
/** |
| 3 |
* Main initialization class |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace FDSUS; |
| 7 |
|
| 8 |
if (!defined('ABSPATH')) exit; // Exit if accessed directly |
| 9 |
|
| 10 |
use FDSUS\Lib\Dls\Notice; |
| 11 |
use FDSUS\Model\Settings; |
| 12 |
use wpdb; |
| 13 |
use FDSUS\Id as Id; |
| 14 |
use FDSUS\Model\Data; |
| 15 |
use FDSUS\Model\Sheet as SheetModel; |
| 16 |
use FDSUS\Model\DbUpdate; |
| 17 |
|
| 18 |
/** |
| 19 |
* Autoloader |
| 20 |
* |
| 21 |
* @param string $className |
| 22 |
*/ |
| 23 |
if (!function_exists('\FDSUS\fdsusAutoloader')): |
| 24 |
function fdsusAutoloader($className) |
| 25 |
{ |
| 26 |
$classesDir = ''; |
| 27 |
$classFile = ''; |
| 28 |
|
| 29 |
if (0 === strpos($className, __NAMESPACE__ . '\\') && !in_array($className, array(__NAMESPACE__ . '\Main'))) { |
| 30 |
$classesDir = realpath(plugin_dir_path(__FILE__)) . DIRECTORY_SEPARATOR; |
| 31 |
$classFile = str_replace(__NAMESPACE__ . '/', '', str_replace('\\', '/', $className)) . '.php'; |
| 32 |
$classFile = strtolower(preg_replace('/\B([A-Z])/', '-$1', $classFile)); // add hyphen before uppercase classes then convert to all lowercase |
| 33 |
} |
| 34 |
|
| 35 |
if ($classesDir && $classFile) { |
| 36 |
if (!file_exists($classesDir . $classFile)) { |
| 37 |
error_log('Autoloader tried to load a file that did not exist... ' . $classesDir . $classFile); |
| 38 |
return; |
| 39 |
} |
| 40 |
require_once $classesDir . $classFile; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
spl_autoload_register('\FDSUS\fdsusAutoloader'); |
| 45 |
endif; |
| 46 |
|
| 47 |
if (!function_exists('\FDSUS\fdsusIsFallbackPlugin')): |
| 48 |
function fdsusIsFallbackPlugin() |
| 49 |
{ |
| 50 |
return dirname(__FILE__) !== dirname(WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . Id::getPluginBasename('free')); |
| 51 |
} |
| 52 |
endif; |
| 53 |
|
| 54 |
Notice::instance(array('frontendFilter' => Id::PREFIX . '_notices')); |
| 55 |
Settings::instance(); |
| 56 |
|
| 57 |
if ( |
| 58 |
( |
| 59 |
empty(Id::version('pro')) |
| 60 |
|| fdsusIsFallbackPlugin() |
| 61 |
|| (!empty(Id::version('free')) && !empty(Id::version('pro')) |
| 62 |
&& version_compare(Id::version('free'), Id::version('pro'), '==') |
| 63 |
&& !class_exists('\FDSUS\Main') |
| 64 |
) |
| 65 |
) |
| 66 |
&& !class_exists('\FDSUS\Main') |
| 67 |
): |
| 68 |
|
| 69 |
class Main { |
| 70 |
|
| 71 |
/** @var wpdb */ |
| 72 |
public $wpdb; |
| 73 |
|
| 74 |
/** @var Data */ |
| 75 |
public $data; |
| 76 |
|
| 77 |
/** @var DbUpdate */ |
| 78 |
private $dbUpdate; |
| 79 |
|
| 80 |
public $prefix; |
| 81 |
public $detailed_errors = false; |
| 82 |
|
| 83 |
public function __construct() |
| 84 |
{ |
| 85 |
/** |
| 86 |
* Disables the migration process for the plugin |
| 87 |
* |
| 88 |
* @var bool |
| 89 |
* |
| 90 |
* @api |
| 91 |
* @since 2.2.7 |
| 92 |
*/ |
| 93 |
if (!defined('FDSUS_DISABLE_MIGRATE_2_0_to_2_1')) { |
| 94 |
define('FDSUS_DISABLE_MIGRATE_2_0_to_2_1', false); |
| 95 |
} |
| 96 |
|
| 97 |
global $wpdb; |
| 98 |
$this->wpdb = $wpdb; |
| 99 |
$this->data = new Data(); |
| 100 |
$this->dbUpdate = new DbUpdate(); |
| 101 |
new \FDSUS\Controller\Sheet(); |
| 102 |
new \FDSUS\Controller\Task(); |
| 103 |
new \FDSUS\Controller\Signup(); |
| 104 |
new \FDSUS\Controller\Capabilities(); |
| 105 |
new \FDSUS\Controller\Privacy(); |
| 106 |
new \FDSUS\Controller\Ajax(); |
| 107 |
new \FDSUS\Controller\Block(__DIR__); |
| 108 |
new \FDSUS\Controller\MailCustomization(); |
| 109 |
new \FDSUS\Controller\Cache(); |
| 110 |
new \FDSUS\Controller\Captcha(); |
| 111 |
if (is_admin() || defined('FD_UNIT_TEST')) { // TODO keep? or move all non-admin specific things like save actions into proper controller |
| 112 |
new \FDSUS\Controller\Admin(); |
| 113 |
new \FDSUS\Controller\Admin\Dashboard(); |
| 114 |
new \FDSUS\Controller\Admin\Settings(); |
| 115 |
new \FDSUS\Controller\Admin\Help(); |
| 116 |
new \FDSUS\Controller\Admin\ManageSignups(); |
| 117 |
new \FDSUS\Controller\Admin\EditSheet(); |
| 118 |
new \FDSUS\Controller\Admin\EditSignupPage(); |
| 119 |
new \FDSUS\Controller\Admin\Export(); |
| 120 |
new \FDSUS\Controller\Admin\SiteHealth(); |
| 121 |
} else { |
| 122 |
new \FDSUS\Controller\Scode\SignUpForm(); |
| 123 |
new \FDSUS\Controller\Scode\SignUpSheet(); |
| 124 |
new \FDSUS\Controller\Scode\UserSignUps(); |
| 125 |
new \FDSUS\Controller\Scode\SignUpSpotCount(); |
| 126 |
} |
| 127 |
$this->prefix = Id::PREFIX; |
| 128 |
|
| 129 |
if (Id::DEBUG_DISPLAY || get_option('dls_sus_detailed_errors') === 'true') { |
| 130 |
$this->detailed_errors = true; |
| 131 |
$this->data->detailed_errors = true; |
| 132 |
} |
| 133 |
|
| 134 |
register_activation_hook(Id::getPluginBasename('free'), array(&$this, 'activate')); |
| 135 |
register_deactivation_hook(Id::getPluginBasename('free'), array(&$this, 'deactivate')); |
| 136 |
|
| 137 |
add_action('wp_enqueue_scripts', array(&$this, 'add_css_and_js_to_frontend')); |
| 138 |
if (is_admin()) { |
| 139 |
add_action('admin_enqueue_scripts', array(&$this, 'enqueueAdminScripts'), 10); |
| 140 |
} |
| 141 |
add_action('init', array(&$this, 'setDefaultOptions'), 0); |
| 142 |
add_action('init', array(&$this, 'flushIfNeeded'), 0); |
| 143 |
|
| 144 |
add_filter('rewrite_rules_array', array(&$this, 'add_rewrite_rules')); |
| 145 |
add_filter('get_the_archive_title', array(&$this, 'modify_archive_title')); |
| 146 |
|
| 147 |
if (Id::IS_LOCAL) { |
| 148 |
add_filter('edd_sl_api_request_verify_ssl', '__return_false'); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Set default option values |
| 154 |
*/ |
| 155 |
public function setDefaultOptions() |
| 156 |
{ |
| 157 |
if (get_option('dls_sus_sheet_slug') === false) { |
| 158 |
update_option('dls_sus_sheet_slug', SheetModel::getBaseSlug()); |
| 159 |
} |
| 160 |
|
| 161 |
$sheetSlug = get_option('dls_sus_sheet_slug'); |
| 162 |
if (empty($sheetSlug)) { |
| 163 |
add_option('dls_sus_sheet_slug', 'sheet', null, 'yes'); |
| 164 |
} |
| 165 |
|
| 166 |
if (get_option('dls_sus_hide_address') === false) { |
| 167 |
add_option('dls_sus_hide_address', 'true', null, 'yes'); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Action that runs during setDefaultOptions() |
| 172 |
*/ |
| 173 |
do_action('fdsus_set_default_options'); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Register plugin css and js files |
| 178 |
*/ |
| 179 |
function add_css_and_js_to_frontend() |
| 180 |
{ |
| 181 |
// Pull pro or free |
| 182 |
$pluginPath = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . (Id::getPluginBasename()); |
| 183 |
|
| 184 |
wp_enqueue_script('jquery'); |
| 185 |
|
| 186 |
if (Settings::isEmailValidationEnabled()) { |
| 187 |
wp_register_script( |
| 188 |
Id::PREFIX . '-mailcheck', |
| 189 |
plugins_url('js/mailcheck.min.js', __FILE__), |
| 190 |
array(), |
| 191 |
'1.1.2' |
| 192 |
); |
| 193 |
} |
| 194 |
|
| 195 |
wp_register_style( |
| 196 |
Id::PREFIX . '-style', |
| 197 |
plugins_url('css/style.css', $pluginPath), |
| 198 |
array(), |
| 199 |
Id::version() |
| 200 |
); |
| 201 |
|
| 202 |
$mainSusDeps = array(); |
| 203 |
|
| 204 |
if (Settings::isEmailValidationEnabled()) { |
| 205 |
$mainSusDeps[] = 'dlssus-mailcheck'; |
| 206 |
} |
| 207 |
|
| 208 |
wp_register_script( |
| 209 |
'dlssus-js', |
| 210 |
plugins_url('js/dist/main.min.js', $pluginPath), |
| 211 |
$mainSusDeps, |
| 212 |
Id::version() |
| 213 |
); |
| 214 |
|
| 215 |
$inlineScriptArray = array( |
| 216 |
'dlssus_validate_email' => array('disable' => !Settings::isEmailValidationEnabled()), |
| 217 |
'dls_sus_recaptcha_version' => esc_js(Settings::getRecaptchaVersion()), // TODO CAPTCHA - move to Captcha class |
| 218 |
); |
| 219 |
|
| 220 |
/** |
| 221 |
* Filter for wp_add_inline_script FDSUS constant array value |
| 222 |
* |
| 223 |
* @param array $inlineScriptArray |
| 224 |
* @since 2.2.12 |
| 225 |
*/ |
| 226 |
$inlineScriptArray = apply_filters('fdsus_add_inline_script_array', $inlineScriptArray); |
| 227 |
|
| 228 |
wp_add_inline_script( |
| 229 |
'dlssus-js', 'const FDSUS = ' . json_encode($inlineScriptArray), 'before' |
| 230 |
); |
| 231 |
|
| 232 |
// Enqueue to sheet page |
| 233 |
if (get_post_type() == SheetModel::POST_TYPE) { |
| 234 |
wp_enqueue_script('jquery'); |
| 235 |
wp_enqueue_style(Id::PREFIX . '-style'); |
| 236 |
if (Settings::isEmailValidationEnabled()) { |
| 237 |
wp_enqueue_script(Id::PREFIX . '-mailcheck'); |
| 238 |
} |
| 239 |
wp_enqueue_script('dlssus-js'); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Add rewrite for new query vars |
| 245 |
* |
| 246 |
* @param array $aRules |
| 247 |
* |
| 248 |
* @return array |
| 249 |
*/ |
| 250 |
function add_rewrite_rules($aRules) |
| 251 |
{ |
| 252 |
$sheetSlug = SheetModel::getBaseSlug(); |
| 253 |
$new1 = array($sheetSlug . '/(.+?)/(.+?)/?$' => 'index.php?dlssus_sheet=$matches[1]&dlssus_task=$matches[2]'); |
| 254 |
$aRules = $new1 + $aRules; |
| 255 |
|
| 256 |
return $aRules; |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Modify archive title |
| 261 |
* |
| 262 |
* @param $title |
| 263 |
* |
| 264 |
* @return mixed|string |
| 265 |
*/ |
| 266 |
public function modify_archive_title($title) |
| 267 |
{ |
| 268 |
if (get_post_type() == SheetModel::POST_TYPE) { |
| 269 |
$title = str_replace('Archives: ', '', $title); |
| 270 |
} |
| 271 |
|
| 272 |
return $title; |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Check if we need to flush rewrites (like if slug was changed in Settings) |
| 277 |
*/ |
| 278 |
public function flushIfNeeded() |
| 279 |
{ |
| 280 |
if (get_transient(Id::PREFIX . '_flush_rewrite_rules')) { |
| 281 |
flush_rewrite_rules(); |
| 282 |
delete_transient(Id::PREFIX . '_flush_rewrite_rules'); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Activate the plugin |
| 288 |
*/ |
| 289 |
public function activate() |
| 290 |
{ |
| 291 |
$this->dbUpdate->check(); |
| 292 |
set_transient(Id::PREFIX . '_flush_rewrite_rules', true); |
| 293 |
|
| 294 |
/** |
| 295 |
* Action that runs on plugin activation |
| 296 |
*/ |
| 297 |
do_action('fdsus_activate'); |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Deactivate the plugin |
| 302 |
*/ |
| 303 |
public function deactivate() |
| 304 |
{ |
| 305 |
set_transient(Id::PREFIX . '_flush_rewrite_rules', true); |
| 306 |
|
| 307 |
// Crons |
| 308 |
wp_clear_scheduled_hook('fdsus_dbupdate_action'); |
| 309 |
|
| 310 |
/** |
| 311 |
* Action that runs on plugin deactivation |
| 312 |
*/ |
| 313 |
do_action('fdsus_deactivate'); |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Enqueue admin scripts and styles |
| 318 |
*/ |
| 319 |
public function enqueueAdminScripts(): void |
| 320 |
{ |
| 321 |
$stylePath = plugins_url('css/admin.css', __FILE__); |
| 322 |
|
| 323 |
// Register stylesheet |
| 324 |
wp_register_style( |
| 325 |
'fdsus-admin', |
| 326 |
$stylePath, |
| 327 |
array(), |
| 328 |
Id::version('free') |
| 329 |
); |
| 330 |
} |
| 331 |
|
| 332 |
} |
| 333 |
|
| 334 |
$fdsus = new Main(); |
| 335 |
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'template-tags.php'; |
| 336 |
|
| 337 |
endif; |
| 338 |
|