| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
class FrmSettingsController { |
| 7 |
|
| 8 |
/** |
| 9 |
* Payments sections are removed from the top level and added to a payments section. |
| 10 |
* |
| 11 |
* @since 6.22.1 |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
private static $removed_payments_sections = array(); |
| 16 |
|
| 17 |
public static function menu() { |
| 18 |
// Make sure admins can see the menu items |
| 19 |
FrmAppHelper::force_capability( 'frm_change_settings' ); |
| 20 |
|
| 21 |
add_submenu_page( 'formidable', 'Formidable | ' . __( 'Global Settings', 'formidable' ), __( 'Global Settings', 'formidable' ), 'frm_change_settings', 'formidable-settings', 'FrmSettingsController::route' ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Include license box template on demand. |
| 26 |
* |
| 27 |
* @return void |
| 28 |
*/ |
| 29 |
public static function license_box() { |
| 30 |
if ( ! current_user_can( 'activate_plugins' ) ) { |
| 31 |
return; |
| 32 |
} |
| 33 |
include FrmAppHelper::plugin_path() . '/classes/views/frm-settings/license_box.php'; |
| 34 |
} |
| 35 |
|
| 36 |
public static function display_form( $errors = array(), $message = '' ) { |
| 37 |
global $frm_vars; |
| 38 |
|
| 39 |
$frm_settings = FrmAppHelper::get_settings(); |
| 40 |
|
| 41 |
$uploads = wp_upload_dir(); |
| 42 |
$target_path = $uploads['basedir'] . '/formidable/css'; |
| 43 |
|
| 44 |
$sections = self::get_settings_tabs(); |
| 45 |
$current = FrmAppHelper::simple_get( 't', 'sanitize_title', 'general_settings' ); |
| 46 |
|
| 47 |
if ( in_array( $current, array( 'stripe_settings', 'square_settings', 'authorize_net_settings', 'paypal_settings' ), true ) ) { |
| 48 |
$current = 'payments_settings'; |
| 49 |
} |
| 50 |
|
| 51 |
require FrmAppHelper::plugin_path() . '/classes/views/frm-settings/form.php'; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Get sections to use for Global Settings. |
| 56 |
* |
| 57 |
* @return array<array> |
| 58 |
*/ |
| 59 |
private static function get_settings_tabs() { |
| 60 |
$sections = array( |
| 61 |
'general' => array( |
| 62 |
'class' => __CLASS__, |
| 63 |
'function' => 'general_settings', |
| 64 |
'name' => __( 'General Settings', 'formidable' ), |
| 65 |
'icon' => 'frm_icon_font frm_settings_icon', |
| 66 |
), |
| 67 |
'messages' => array( |
| 68 |
'class' => __CLASS__, |
| 69 |
'function' => 'message_settings', |
| 70 |
'name' => __( 'Message Defaults', 'formidable' ), |
| 71 |
'icon' => 'frm_icon_font frm_stamp_icon', |
| 72 |
), |
| 73 |
'permissions' => array( |
| 74 |
'class' => __CLASS__, |
| 75 |
'function' => 'permission_settings', |
| 76 |
'name' => __( 'Permissions', 'formidable' ), |
| 77 |
'icon' => 'frm_icon_font frm_lock_icon', |
| 78 |
), |
| 79 |
'payments' => array( |
| 80 |
'name' => __( 'Payments', 'formidable' ), |
| 81 |
'icon' => 'frm_icon_font frm_simple_cc_icon', |
| 82 |
'class' => __CLASS__, |
| 83 |
'function' => 'payments_settings', |
| 84 |
), |
| 85 |
'custom_css' => array( |
| 86 |
'class' => 'FrmStylesController', |
| 87 |
'function' => 'custom_css', |
| 88 |
'name' => __( 'Custom CSS', 'formidable' ), |
| 89 |
'icon' => 'frm_icon_font frm_code_icon', |
| 90 |
), |
| 91 |
'manage_styles' => array( |
| 92 |
'class' => 'FrmStylesController', |
| 93 |
'function' => 'manage', |
| 94 |
'name' => __( 'Manage Styles', 'formidable' ), |
| 95 |
'icon' => 'frm_icon_font frm_pallet_icon', |
| 96 |
), |
| 97 |
'captcha' => array( |
| 98 |
'class' => __CLASS__, |
| 99 |
'function' => 'captcha_settings', |
| 100 |
'name' => __( 'Captcha/Spam', 'formidable' ), |
| 101 |
'icon' => 'frm_icon_font frm_shield_check_icon', |
| 102 |
), |
| 103 |
'white_label' => array( |
| 104 |
'name' => __( 'White Labeling', 'formidable' ), |
| 105 |
'icon' => 'frm_icon_font frm_ghost_icon', |
| 106 |
'html_class' => 'frm_show_upgrade_tab frm_noallow', |
| 107 |
'data' => array( |
| 108 |
'medium' => 'white-label', |
| 109 |
'upgrade' => __( 'White labeling options', 'formidable' ), |
| 110 |
'screenshot' => 'white-label.png', |
| 111 |
), |
| 112 |
), |
| 113 |
'inbox' => array( |
| 114 |
'name' => __( 'Inbox', 'formidable' ), |
| 115 |
'icon' => 'frm_icon_font frm_email_icon', |
| 116 |
'html_class' => 'frm_show_upgrade_tab frm_noallow', |
| 117 |
'data' => array( |
| 118 |
'medium' => 'inbox-settings', |
| 119 |
'upgrade' => __( 'Inbox settings', 'formidable' ), |
| 120 |
'screenshot' => 'inbox.png', |
| 121 |
), |
| 122 |
), |
| 123 |
); |
| 124 |
|
| 125 |
if ( apply_filters( 'frm_include_addon_page', false ) ) { |
| 126 |
// If no addons need a license, skip this page |
| 127 |
$show_licenses = false; |
| 128 |
$installed_addons = apply_filters( 'frm_installed_addons', array() ); |
| 129 |
foreach ( $installed_addons as $installed_addon ) { |
| 130 |
if ( ! $installed_addon->is_parent_licence && $installed_addon->plugin_name != 'Formidable Pro' && $installed_addon->needs_license ) { |
| 131 |
$show_licenses = true; |
| 132 |
break; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
if ( $show_licenses ) { |
| 137 |
$sections['licenses'] = array( |
| 138 |
'class' => 'FrmAddonsController', |
| 139 |
'function' => 'license_settings', |
| 140 |
'name' => __( 'Plugin Licenses', 'formidable' ), |
| 141 |
'icon' => 'frmfont frm_key_icon', |
| 142 |
'ajax' => true, |
| 143 |
); |
| 144 |
} |
| 145 |
}//end if |
| 146 |
|
| 147 |
/** |
| 148 |
* @param array<array> $sections |
| 149 |
*/ |
| 150 |
$sections = apply_filters( 'frm_add_settings_section', $sections ); |
| 151 |
self::remove_payments_sections( $sections ); |
| 152 |
|
| 153 |
$sections['misc'] = array( |
| 154 |
'name' => __( 'Miscellaneous', 'formidable' ), |
| 155 |
'icon' => 'frm_icon_font frm_shuffle_icon', |
| 156 |
'class' => __CLASS__, |
| 157 |
'function' => 'misc_settings', |
| 158 |
); |
| 159 |
|
| 160 |
foreach ( $sections as $key => $section ) { |
| 161 |
$original = $section; |
| 162 |
$defaults = array( |
| 163 |
'html_class' => '', |
| 164 |
'name' => ucfirst( $key ), |
| 165 |
'icon' => 'frm_icon_font frm_settings_icon', |
| 166 |
'anchor' => $key . '_settings', |
| 167 |
'data' => array(), |
| 168 |
); |
| 169 |
|
| 170 |
$section = array_merge( $defaults, $section ); |
| 171 |
|
| 172 |
if ( isset( $section['ajax'] ) && ! isset( $section['data']['frmajax'] ) ) { |
| 173 |
$section['data']['frmajax'] = $section['ajax']; |
| 174 |
} |
| 175 |
|
| 176 |
// For reverse compatibility. |
| 177 |
if ( ! isset( $section['function'] ) && ( ! is_array( $original ) || ! isset( $original['name'] ) ) ) { |
| 178 |
$section['function'] = $original; |
| 179 |
} |
| 180 |
|
| 181 |
$sections[ $key ] = $section; |
| 182 |
}//end foreach |
| 183 |
|
| 184 |
return $sections; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Remove the payments sections (PayPal, Square, Stripe, Authorize.Net) |
| 189 |
* and show them all on the payments section in separate tabs. |
| 190 |
* |
| 191 |
* @since 6.22.1 |
| 192 |
* |
| 193 |
* @param array $sections |
| 194 |
* @return void |
| 195 |
*/ |
| 196 |
private static function remove_payments_sections( &$sections ) { |
| 197 |
$payment_section_keys = array( 'paypal', 'square', 'stripe', 'authorize_net' ); |
| 198 |
|
| 199 |
foreach ( $sections as $key => $section ) { |
| 200 |
if ( in_array( $key, $payment_section_keys, true ) ) { |
| 201 |
self::$removed_payments_sections[ $key ] = $section; |
| 202 |
unset( $sections[ $key ] ); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
uksort( self::$removed_payments_sections, array( __CLASS__, 'payment_sections_sort_callback' ) ); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Sort the payments sections (PayPal, Square, Stripe, Authorize.Net) |
| 211 |
* |
| 212 |
* @since 6.22.1 |
| 213 |
* |
| 214 |
* @param string $a |
| 215 |
* @param string $b |
| 216 |
* @return int |
| 217 |
*/ |
| 218 |
private static function payment_sections_sort_callback( $a, $b ) { |
| 219 |
$order = array( 'stripe', 'square', 'paypal', 'authorize_net' ); |
| 220 |
$first_key = array_search( $a, $order ); |
| 221 |
$second_key = array_search( $b, $order ); |
| 222 |
if ( false === $first_key || false === $second_key ) { |
| 223 |
return 0; |
| 224 |
} |
| 225 |
return $first_key - $second_key; |
| 226 |
} |
| 227 |
|
| 228 |
public static function load_settings_tab() { |
| 229 |
FrmAppHelper::permission_check( 'frm_change_settings' ); |
| 230 |
check_ajax_referer( 'frm_ajax', 'nonce' ); |
| 231 |
|
| 232 |
$section = FrmAppHelper::get_post_param( 'tab', '', 'sanitize_text_field' ); |
| 233 |
$sections = self::get_settings_tabs(); |
| 234 |
if ( ! isset( $sections[ $section ] ) ) { |
| 235 |
wp_die(); |
| 236 |
} |
| 237 |
|
| 238 |
$section = $sections[ $section ]; |
| 239 |
|
| 240 |
if ( isset( $section['class'] ) ) { |
| 241 |
call_user_func( array( $section['class'], $section['function'] ) ); |
| 242 |
} else { |
| 243 |
call_user_func( ( isset( $section['function'] ) ? $section['function'] : $section ) ); |
| 244 |
} |
| 245 |
wp_die(); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Render the general global settings section. |
| 250 |
* |
| 251 |
* @since 4.0 |
| 252 |
* |
| 253 |
* @return void |
| 254 |
*/ |
| 255 |
public static function general_settings() { |
| 256 |
$frm_settings = FrmAppHelper::get_settings(); |
| 257 |
$uploads = wp_upload_dir(); |
| 258 |
$target_path = $uploads['basedir'] . '/formidable/css'; |
| 259 |
|
| 260 |
include FrmAppHelper::plugin_path() . '/classes/views/frm-settings/general.php'; |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Render the global currency selector if Pro is up to date. |
| 265 |
* |
| 266 |
* @param FrmSettings $frm_settings |
| 267 |
* @param string $more_html |
| 268 |
* @return void |
| 269 |
*/ |
| 270 |
public static function maybe_render_currency_selector( $frm_settings, $more_html ) { |
| 271 |
if ( is_callable( 'FrmProSettingsController::add_currency_settings' ) ) { |
| 272 |
FrmProSettingsController::add_currency_settings(); |
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
$currencies = FrmCurrencyHelper::get_currencies(); |
| 277 |
include FrmAppHelper::plugin_path() . '/classes/views/frm-settings/_currency.php'; |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* @since 4.0 |
| 282 |
*/ |
| 283 |
public static function message_settings() { |
| 284 |
$frm_settings = FrmAppHelper::get_settings(); |
| 285 |
|
| 286 |
include FrmAppHelper::plugin_path() . '/classes/views/frm-settings/messages.php'; |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* @since 4.0 |
| 291 |
*/ |
| 292 |
public static function captcha_settings() { |
| 293 |
$frm_settings = FrmAppHelper::get_settings(); |
| 294 |
$captcha_lang = FrmAppHelper::locales( 'captcha' ); |
| 295 |
|
| 296 |
include FrmAppHelper::plugin_path() . '/classes/views/frm-settings/captcha/captcha.php'; |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* @since 4.0 |
| 301 |
*/ |
| 302 |
public static function permission_settings() { |
| 303 |
$frm_settings = FrmAppHelper::get_settings(); |
| 304 |
$frm_roles = FrmAppHelper::frm_capabilities(); |
| 305 |
|
| 306 |
include FrmAppHelper::plugin_path() . '/classes/views/frm-settings/permissions.php'; |
| 307 |
} |
| 308 |
|
| 309 |
public static function payments_settings() { |
| 310 |
$payment_sections = self::$removed_payments_sections; |
| 311 |
|
| 312 |
$tab = FrmAppHelper::simple_get( 't', 'sanitize_title', 'general_settings' ); |
| 313 |
if ( $tab && in_array( $tab, array( 'stripe_settings', 'square_settings', 'authorize_net_settings', 'paypal_settings' ), true ) ) { |
| 314 |
$tab = str_replace( '_settings', '', $tab ); |
| 315 |
} else { |
| 316 |
$tab = 'stripe'; |
| 317 |
} |
| 318 |
|
| 319 |
include FrmAppHelper::plugin_path() . '/classes/views/frm-settings/payments.php'; |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* @since 4.0 |
| 324 |
*/ |
| 325 |
public static function misc_settings() { |
| 326 |
$frm_settings = FrmAppHelper::get_settings(); |
| 327 |
|
| 328 |
include FrmAppHelper::plugin_path() . '/classes/views/frm-settings/misc.php'; |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Save form data submitted from the Global settings page. |
| 333 |
* |
| 334 |
* @param bool|string $stop_load |
| 335 |
* |
| 336 |
* @return void |
| 337 |
*/ |
| 338 |
public static function process_form( $stop_load = false ) { |
| 339 |
global $frm_vars; |
| 340 |
|
| 341 |
$frm_settings = FrmAppHelper::get_settings(); |
| 342 |
$process_form = FrmAppHelper::get_post_param( 'process_form', '', 'sanitize_text_field' ); |
| 343 |
|
| 344 |
if ( ! wp_verify_nonce( $process_form, 'process_form_nonce' ) ) { |
| 345 |
$error_args = array( |
| 346 |
'title' => __( 'Verification failed', 'formidable' ), |
| 347 |
'body' => $frm_settings->admin_permission, |
| 348 |
'cancel_text' => __( 'Cancel', 'formidable' ), |
| 349 |
); |
| 350 |
FrmAppController::show_error_modal( $error_args ); |
| 351 |
return; |
| 352 |
} |
| 353 |
|
| 354 |
$errors = array(); |
| 355 |
$message = ''; |
| 356 |
|
| 357 |
if ( empty( $frm_vars['settings_routed'] ) ) { |
| 358 |
$errors = $frm_settings->validate( $_POST, array() ); |
| 359 |
|
| 360 |
$frm_settings->update( wp_unslash( $_POST ) ); |
| 361 |
|
| 362 |
if ( ! $errors ) { |
| 363 |
$frm_settings->store(); |
| 364 |
$message = __( 'Settings Saved', 'formidable' ); |
| 365 |
} |
| 366 |
} else { |
| 367 |
$message = __( 'Settings Saved', 'formidable' ); |
| 368 |
} |
| 369 |
|
| 370 |
if ( $stop_load === 'stop_load' ) { |
| 371 |
$frm_vars['settings_routed'] = true; |
| 372 |
return; |
| 373 |
} |
| 374 |
|
| 375 |
self::display_form( $errors, $message ); |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* Include the Update button on the global settings page. |
| 380 |
* |
| 381 |
* @since 4.0.02 |
| 382 |
*/ |
| 383 |
public static function save_button() { |
| 384 |
echo '<input class="button-primary frm-button-primary" type="submit" |
| 385 |
value="' . esc_attr__( 'Update', 'formidable' ) . '"/>'; |
| 386 |
} |
| 387 |
|
| 388 |
public static function route( $stop_load = false ) { |
| 389 |
$action = isset( $_REQUEST['frm_action'] ) ? 'frm_action' : 'action'; |
| 390 |
$action = FrmAppHelper::get_param( $action, '', 'get', 'sanitize_title' ); |
| 391 |
FrmAppHelper::include_svg(); |
| 392 |
|
| 393 |
if ( $action === 'process-form' ) { |
| 394 |
self::process_form( $stop_load ); |
| 395 |
} elseif ( $stop_load != 'stop_load' ) { |
| 396 |
self::display_form(); |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
/** |
| 401 |
* Add CTA to the bottom on the plugin settings pages. |
| 402 |
* |
| 403 |
* @since 3.04.02 |
| 404 |
*/ |
| 405 |
public static function settings_cta( $view ) { |
| 406 |
if ( get_option( 'frm_lite_settings_upgrade', false ) ) { |
| 407 |
return; |
| 408 |
} |
| 409 |
|
| 410 |
include FrmAppHelper::plugin_path() . '/classes/views/frm-settings/settings_cta.php'; |
| 411 |
} |
| 412 |
|
| 413 |
/** |
| 414 |
* Dismiss upgrade notice at the bottom on the plugin settings pages. |
| 415 |
* |
| 416 |
* @since 3.04.02 |
| 417 |
*/ |
| 418 |
public static function settings_cta_dismiss() { |
| 419 |
check_ajax_referer( 'frm_ajax', 'nonce' ); |
| 420 |
FrmAppHelper::permission_check( 'frm_change_settings' ); |
| 421 |
|
| 422 |
update_option( 'frm_lite_settings_upgrade', time(), 'no' ); |
| 423 |
|
| 424 |
wp_send_json_success(); |
| 425 |
} |
| 426 |
|
| 427 |
/** |
| 428 |
* Autocomplete page admin ajax endpoint |
| 429 |
* |
| 430 |
* @since 4.03.06 |
| 431 |
*/ |
| 432 |
public static function page_search() { |
| 433 |
FrmAppHelper::permission_check( 'frm_edit_forms' ); |
| 434 |
check_ajax_referer( 'frm_ajax', 'nonce' ); |
| 435 |
|
| 436 |
global $wpdb; |
| 437 |
|
| 438 |
$term = FrmAppHelper::get_param( 'term', '', 'get', 'sanitize_text_field' ); |
| 439 |
$post_type = FrmAppHelper::get_param( 'post_type', 'page', 'get', 'sanitize_text_field' ); |
| 440 |
|
| 441 |
$where = array( |
| 442 |
'post_status' => 'publish', |
| 443 |
'post_type' => $post_type, |
| 444 |
'post_title LIKE' => $term, |
| 445 |
); |
| 446 |
|
| 447 |
$atts = array( |
| 448 |
'limit' => 25, |
| 449 |
'order_by' => 'post_title', |
| 450 |
); |
| 451 |
|
| 452 |
$pages = FrmDb::get_results( $wpdb->posts, $where, 'ID, post_title', $atts ); |
| 453 |
|
| 454 |
$results = array(); |
| 455 |
foreach ( $pages as $page ) { |
| 456 |
$results[] = array( |
| 457 |
'value' => $page->ID, |
| 458 |
'label' => $page->post_title, |
| 459 |
); |
| 460 |
} |
| 461 |
|
| 462 |
wp_send_json( $results ); |
| 463 |
} |
| 464 |
} |
| 465 |
|