Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*
*/
// This file must not accessed directly.
if (!defined('ABSPATH')) {
exit;
}
/**
* Define CiviCRM_For_WordPress_Admin_Page_Options Class.
*
* @since 5.34
*/
class CiviCRM_For_WordPress_Admin_Page_Options {
/**
* @var object
* Plugin object reference.
* @since 5.34
* @access public
*/
public $civi;
/**
* @var object
* Admin object reference.
* @since 5.34
* @access public
*/
public $admin;
/**
* @var object
* Admin page slug.
* @since 5.34
* @access public
*/
public $slug = 'civi_options';
/**
* Instance constructor.
*
* @since 5.34
*/
public function __construct() {
// Bail if CiviCRM is not installed.
if (!CIVICRM_INSTALLED) {
return;
}
// Store reference to CiviCRM plugin object.
$this->civi = civi_wp();
// Store reference to admin object.
$this->admin = civi_wp()->admin;
// Wait for admin class to register hooks.
add_action('civicrm/admin/hooks/registered', [$this, 'register_hooks']);
}
/**
* Register hooks.
*
* @since 5.34
*/
public function register_hooks() {
// Add items to the CiviCRM admin menu.
add_action('admin_menu', [$this, 'add_menu_items'], 9);
// Add our meta boxes.
add_action('civicrm/page/options/add_meta_boxes', [$this, 'meta_boxes_options_add']);
// Add AJAX handlers.
add_action('wp_ajax_civicrm_basepage', [$this, 'ajax_save_basepage']);
add_action('wp_ajax_civicrm_shortcode', [$this, 'ajax_save_shortcode']);
add_action('wp_ajax_civicrm_theme_compatibility', [$this, 'ajax_save_theme_compatibility']);
add_action('wp_ajax_civicrm_email_sync', [$this, 'ajax_save_email_sync']);
add_action('wp_ajax_civicrm_refresh_permissions', [$this, 'ajax_refresh_permissions']);
add_action('wp_ajax_civicrm_clear_caches', [$this, 'ajax_clear_caches']);
}
/**
* Get the capability required to access the Settings Page.
*
* @since 5.37
*/
public function access_capability() {
/**
* Return default capability but allow overrides.
*
* @since 5.37
*
* @param str The default access capability.
*/
return apply_filters('civicrm/admin/settings/cap', 'manage_options');
}
/**
* Adds CiviCRM sub-menu items to WordPress admin menu.
*
* @since 5.34
*/
public function add_menu_items() {
if (!$this->civi->initialize()) {
return;
}
// Get access capability.
$capability = $this->access_capability();
// Add Settings submenu item.
$options_page = add_submenu_page(
'CiviCRM',
__('CiviCRM Settings for WordPress', 'civicrm'),
__('Settings', 'civicrm'),
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
$this->slug,
[$this, 'page_options']
);
// Register our form submit hander.
add_action('load-' . $options_page, [$this, 'form_submitted']);
// Add resources prior to page load.
add_action('admin_head-' . $options_page, [$this, 'admin_head']);
add_action('admin_print_styles-' . $options_page, [$this, 'admin_css']);
}
/**
* Enqueue scripts on the pages that need them.
*
* @since 5.34
*/
public function admin_head() {
// Enqueue WordPress scripts.
wp_enqueue_script('common');
wp_enqueue_script('jquery-ui-sortable');
wp_enqueue_script('dashboard');
// Enqueue Javascript.
wp_enqueue_script(
'civicrm-options-script',
CIVICRM_PLUGIN_URL . 'assets/js/civicrm.options.js',
['jquery'],
);
// Init settings and localisation array.
$vars = [
'settings' => [
'ajax_url' => admin_url('admin-ajax.php'),
],
'localisation' => [
'saving' => __('Saving...', 'civicrm'),
'saved' => __('Saved', 'civicrm'),
'refresh' => __('Refresh', 'civicrm'),
'refreshing' => __('Refreshing...', 'civicrm'),
'refreshed' => __('Refreshed', 'civicrm'),
'cache' => __('Clear Caches', 'civicrm'),
'clearing' => __('Clearing...', 'civicrm'),
'cleared' => __('Cleared', 'civicrm'),
],
];
// Localise the WordPress way.
wp_localize_script(
'civicrm-options-script',
'CiviCRM_Options_Vars',
$vars
);
}
/**
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
* Enqueue stylesheet on this page.
*
* @since 5.34
*/
public function admin_css() {
// Enqueue common CSS.
wp_enqueue_style(
'civicrm-admin-styles',
CIVICRM_PLUGIN_URL . 'assets/css/civicrm.admin.css',
NULL,
CIVICRM_PLUGIN_VERSION,
'all'
);
}
// ---------------------------------------------------------------------------
// Page Loader
// ---------------------------------------------------------------------------
/**
* Render the CiviCRM Settings page.
*
* @since 5.34
*/
public function page_options() {
// Get the current screen object.
$screen = get_current_screen();
/**
* Allow meta boxes to be added to this screen.
*
* The Screen ID to use is: "civicrm_page_cwps_settings".
*
* Used internally by:
*
* - self::meta_boxes_options_add()
*
* @since 5.34
*
* @param str $screen_id The ID of the current screen.
*/
do_action('civicrm/page/options/add_meta_boxes', $screen->id);
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Get the column CSS class.
$columns = absint($screen->get_columns());
$columns_css = '';
if ($columns) {
$columns_css = " columns-$columns";
}
// Include template file.
include CIVICRM_PLUGIN_DIR . 'assets/templates/pages/page.options.php';
}
/**
* Get the URL for the form action.
*
* @since 5.34
*
* @return string $target_url The URL for the admin form action.
*/
public function page_submit_url_get() {
// Our array of arguments.
$args = [
'page' => $this->slug,
];
// Sanitise admin page url.
$target_url = add_query_arg($args, admin_url('admin.php'));
// --<
return $target_url;
}
// ---------------------------------------------------------------------------
// Meta Box Loaders
// ---------------------------------------------------------------------------
/**
*
* @since 5.34
*
* @param str $screen_id The Admin Page Screen ID.
*/
public function meta_boxes_options_add($screen_id) {
// Define valid Screen IDs.
$screen_ids = [
'civicrm_page_' . $this->slug,
];
// Bail if not the Screen ID we want.
if (!in_array($screen_id, $screen_ids)) {
return;
}
// Bail if user cannot access the Settings Page.
$capability = $this->access_capability();
if (!current_user_can($capability)) {
return;
}
// Init data.
$data = [];
// Create "WordPress Base Page" metabox.
add_meta_box(
'civicrm_options_basepage',
__('WordPress Base Page', 'civicrm'),
[$this, 'meta_box_options_basepage_render'],
$screen_id,
'normal',
'core',
$data
);
// Create "Shortcode Display Mode" metabox.
add_meta_box(
'civicrm_options_shortcode',
__('Shortcode Display Mode', 'civicrm'),
[$this, 'meta_box_options_shortcode_render'],
$screen_id,
'normal',
'core',
$data
);
// Create "Shortcode Theme Compatibility" metabox.
add_meta_box(
'civicrm_options_theme',
__('Shortcode Theme Compatibility', 'civicrm'),
[$this, 'meta_box_options_theme_render'],
$screen_id,
'normal',
'core',
$data
);
// Create "Email Sync" metabox.
add_meta_box(
'civicrm_options_email',
__('Contact Email to User Email Sync', 'civicrm'),
[$this, 'meta_box_options_email_render'],
$screen_id,
'normal',
'core',
$data
);
// Create "Clear Cache" metabox.
add_meta_box(
'civicrm_options_cache',
__('Clear Caches', 'civicrm'),
[$this, 'meta_box_options_cache_render'],
$screen_id,
// Create "Permissions and Capabilities" metabox.
add_meta_box(
'civicrm_options_permissions',
__('Permissions and Capabilities', 'civicrm'),
[$this, 'meta_box_options_permissions_render'],
$screen_id,
'side',
'core',
$data
);
// Create "Useful Links" metabox.
add_meta_box(
'civicrm_options_emailinks',
__('Useful Links', 'civicrm'),
[$this, 'meta_box_options_links_render'],
$screen_id,
'side',
'core',
$data
);
}
// ---------------------------------------------------------------------------
// Meta Box Renderers
// ---------------------------------------------------------------------------
/**
* Render "WordPress Base Page" meta box.
*
* @since 5.34
*
* @param mixed $unused Unused param.
* @param array $metabox Array containing id, title, callback, and args elements.
*/
public function meta_box_options_basepage_render($unused, $metabox) {
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// Get the setting.
$basepage_slug = civicrm_api3('Setting', 'getvalue', [
'name' => 'wpBasePage',
'group' => 'CiviCRM Preferences',
]);
// Did we get a value?
if (!empty($basepage_slug)) {
// Define the query for our Base Page.
$args = [
'post_type' => 'page',
'name' => strtolower($basepage_slug),
'post_status' => 'publish',
'posts_per_page' => 1,
];
// Do the query.
$pages = get_posts($args);
}
// Default error message.
$message = __('Could not find the WordPress Base Page.', 'civicrm');
// Find the Base Page object.
$basepage = NULL;
if (!empty($pages) && is_array($pages)) {
$basepage = array_pop($pages);
}
// Define the params for the Pages dropdown.
$params = [
'post_type' => 'page',
'sort_column' => 'menu_order, post_title',
'show_option_none' => __('- Select a Base Page -', 'civicrm'),
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
];
// If the Base Page is set, add its ID.
if ($basepage instanceof WP_Post) {
$params['selected'] = $basepage->ID;
}
// Determine whether the notice should be hidden.
$hidden = '';
if ($basepage instanceof WP_Post) {
$hidden = ' display: none;';
}
// Set AJAX submit button options.
$options_ajax = [
'style' => 'float: right;',
'data-security' => esc_attr(wp_create_nonce('civicrm_basepage')),
'disabled' => NULL,
];
// Set POST submit button options.
$options_post = [
'style' => 'float: right;',
];
/**
* Filters the Base Page POST submit button attributes.
*
* @since 5.34
*
* @param array $options_post The existing button attributes.
*/
$options_post = apply_filters('civicrm/metabox/basepage/submit/options', $options_post);
// Include template file.
include CIVICRM_PLUGIN_DIR . 'assets/templates/metaboxes/metabox.options.basepage.php';
}
/**
* Render "Shortcode" meta box.
*
* @since 5.44
*
* @param mixed $unused Unused param.
* @param array $metabox Array containing id, title, callback, and args elements.
*/
public function meta_box_options_shortcode_render($unused, $metabox) {
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
if (!$this->civi->initialize()) {
return;
}
// Get the Shortcode Mode setting.
$shortcode_mode = $this->civi->admin->get_shortcode_mode();
// Set selected attributes.
$selected_legacy = $shortcode_mode === 'legacy' ? 'selected="selected"' : '';
$selected_modern = $shortcode_mode === 'modern' ? 'selected="selected"' : '';
// Set AJAX submit button options.
$options_ajax = [
'style' => 'float: right;',
'data-security' => esc_attr(wp_create_nonce('civicrm_shortcode')),
'disabled' => NULL,
];
// Set POST submit button options.
$options_post = [
'style' => 'float: right;',
];
/**
* Filters the Shortcode POST submit button attributes.
*
* @since 5.44
*
* @param array $options_post The existing button attributes.
*/
$options_post = apply_filters('civicrm/metabox/shortcode/submit/options', $options_post);
// Include template file.
include CIVICRM_PLUGIN_DIR . 'assets/templates/metaboxes/metabox.options.shortcode.php';
}
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/**
* Render "Theme Compatibility" meta box.
*
* @since 5.80
*
* @param mixed $unused Unused param.
* @param array $metabox Array containing id, title, callback, and args elements.
*/
public function meta_box_options_theme_render($unused, $metabox) {
if (!$this->civi->initialize()) {
return;
}
// Get the Shortcode Theme Compatibility setting.
$theme_compatibility_mode = $this->civi->admin->get_theme_compatibility_mode();
// Set selected attributes.
$selected_loop = $theme_compatibility_mode === 'loop' ? 'selected="selected"' : '';
$selected_filter = $theme_compatibility_mode === 'filter' ? 'selected="selected"' : '';
// Set AJAX submit button options.
$options_ajax = [
'style' => 'float: right;',
'data-security' => esc_attr(wp_create_nonce('civicrm_theme')),
'disabled' => NULL,
];
// Set POST submit button options.
$options_post = [
'style' => 'float: right;',
];
/**
* Filters the Theme Compatibility POST submit button attributes.
*
* @since 5.44
*
* @param array $options_post The existing button attributes.
*/
$options_post = apply_filters('civicrm/metabox/theme/submit/options', $options_post);
// Include template file.
include CIVICRM_PLUGIN_DIR . 'assets/templates/metaboxes/metabox.options.theme.php';
}
/**
* Render "Contact Email to User Email Sync" meta box.
*
* @since 5.34
*
* @param mixed $unused Unused param.
* @param array $metabox Array containing id, title, callback, and args elements.
*/
public function meta_box_options_email_render($unused, $metabox) {
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
if (!$this->civi->initialize()) {
return;
}
// Get the setting.
$email_sync_select = civicrm_api3('Setting', 'getvalue', [
'name' => 'syncCMSEmail',
'group' => 'CiviCRM Preferences',
]);
// Set selected attributes.
$selected_yes = $email_sync_select ? 'selected="selected"' : '';
$selected_no = $email_sync_select ? '' : 'selected="selected"';
// Set AJAX submit button options.
$options_ajax = [
'style' => 'float: right;',
'data-security' => esc_attr(wp_create_nonce('civicrm_email_sync')),
'disabled' => NULL,
];
// Set POST submit button options.
$options_post = [
'style' => 'float: right;',
];
/**
* Filters the Email Sync POST submit button attributes.
*
* @since 5.34
*
* @param array $options_post The existing button attributes.
*/
$options_post = apply_filters('civicrm/metabox/email_sync/submit/options', $options_post);
// Include template file.
include CIVICRM_PLUGIN_DIR . 'assets/templates/metaboxes/metabox.options.email.php';
}
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/**
* Render "Permissions" meta box.
*
* @since 5.52
*
* @param mixed $unused Unused param.
* @param array $metabox Array containing id, title, callback, and args elements.
*/
public function meta_box_options_permissions_render($unused, $metabox) {
// Get the custom role.
$custom_role = $this->civi->users->has_custom_role();
// Set selected attributes.
$selected_disable = empty($custom_role) ? 'selected="selected"' : '';
$selected_enable = !empty($custom_role) ? 'selected="selected"' : '';
// Set submit button options.
$options = [
'style' => 'float: right;',
'data-security' => esc_attr(wp_create_nonce('civicrm_refresh_permissions')),
];
// Include template file.
include CIVICRM_PLUGIN_DIR . 'assets/templates/metaboxes/metabox.options.permissions.php';
}
/**
* Render "Clear Cache" meta box.
*
* @since 5.34
*
* @param mixed $unused Unused param.
* @param array $metabox Array containing id, title, callback, and args elements.
*/
public function meta_box_options_cache_render($unused, $metabox) {
// Set submit button options.
$options = [
'style' => 'float: right;',
'data-security' => esc_attr(wp_create_nonce('civicrm_clear_caches')),
];
// Include template file.
include CIVICRM_PLUGIN_DIR . 'assets/templates/metaboxes/metabox.options.cache.php';
}
/**
* Render "Useful Links" meta box.
*
* @since 5.34
*
* @param mixed $unused Unused param.
* @param array $metabox Array containing id, title, callback, and args elements.
*/
public function meta_box_options_links_render($unused, $metabox) {
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
if (!$this->civi->initialize()) {
return;
}
// Construct an array of admin links.
$admin_links = [
'urls' => [
'url' => $this->civi->admin->get_admin_link('civicrm/admin/setting/url', 'reset=1'),
'text' => __('Settings - Resource URLs', 'civicrm'),
],
'uploads' => [
'url' => $this->civi->admin->get_admin_link('civicrm/admin/setting/path', 'reset=1'),
'text' => __('Settings - Upload Directories', 'civicrm'),
],
'permissions' => [
'url' => $this->civi->admin->get_admin_link('civicrm/admin/access/wp-permissions', 'reset=1'),
'text' => __('WordPress Access Control', 'civicrm'),
],
'extensions' => [
'url' => $this->civi->admin->get_admin_link('civicrm/admin/extensions', 'reset=1'),
'text' => __('CiviCRM Extensions', 'civicrm'),
],
];
/**
* Filters the admin links array.
*
* @since 5.34
*
* @param array $admin_links The default array of admin links.
*/
$admin_links = apply_filters('civicrm/metabox/links/admin', $admin_links);
// Construct an array of maintenance links.
$maintenance_links = [
'menu' => [
'url' => $this->civi->admin->get_admin_link('civicrm/menu/rebuild', 'reset=1'),
'text' => __('Rebuild the CiviCRM menu', 'civicrm'),
],
'triggers' => [
'url' => $this->civi->admin->get_admin_link('civicrm/menu/rebuild', 'reset=1&triggerRebuild=1'),
'text' => __('Rebuild the CiviCRM database triggers', 'civicrm'),
],
'upgrade' => [
'url' => $this->civi->admin->get_admin_link('civicrm/upgrade', 'reset=1'),
'text' => __('Upgrade CiviCRM', 'civicrm'),
'description' => __('Please note: you need to update the CiviCRM plugin directory first.', 'civicrm'),
],
];
/**
* Filters the maintenance links array.
*
* @since 5.34
*
* @param array $maintenance_links The default array of admin links.
*/
$maintenance_links = apply_filters('civicrm/metabox/links/maintenance', $maintenance_links);
// Include template file.
include CIVICRM_PLUGIN_DIR . 'assets/templates/metaboxes/metabox.options.links.php';
}
// ---------------------------------------------------------------------------
// Form Handlers
// ---------------------------------------------------------------------------
/**
* Perform actions when the form has been submitted.
*
* @since 5.34
*/
public function form_submitted() {
// Nonce is irrelevant at this stage.
// phpcs:disable WordPress.Security.NonceVerification.Recommended
// phpcs:disable WordPress.Security.NonceVerification.Missing
if (!empty($_POST['civicrm_basepage_post_submit'])) {
// Save Base Page.
$this->form_nonce_check();
$this->form_save_basepage();
$this->form_redirect();
}
elseif (!empty($_POST['civicrm_shortcode_post_submit'])) {
// Save Shortcode Mode.
$this->form_nonce_check();
$this->form_save_shortcode();
$this->form_redirect();
}
elseif (!empty($_POST['civicrm_theme_post_submit'])) {
// Save Shortcode Mode.
$this->form_nonce_check();
$this->form_save_theme_compatibility();
$this->form_redirect();
}
elseif (!empty($_POST['civicrm_email_post_submit'])) {
// Save Email Sync.
$this->form_nonce_check();
$this->form_save_email_sync();
$this->form_redirect();
}
elseif (!empty($_POST['civicrm_permissions_submit'])) {
// Refresh permissions.
$this->form_nonce_check();
$this->civi->users->refresh_capabilities();
$this->form_redirect();
}
elseif (!empty($_POST['civicrm_cache_submit'])) {
// Clear caches.
$this->form_nonce_check();
$this->civi->admin->clear_caches();
$this->form_redirect();
}
// phpcs:enable WordPress.Security.NonceVerification.Recommended
// phpcs:enable WordPress.Security.NonceVerification.Missing
// Nonce is checked in self::form_nonce_check().
// phpcs:disable WordPress.Security.NonceVerification.Missing
$post_id = empty($_POST['page_id']) ? 0 : (int) sanitize_text_field(wp_unslash($_POST['page_id']));
// phpcs:enable WordPress.Security.NonceVerification.Missing
if ($post_id === 0) {
return;
}
// Bail if we don't find a post object.
$post = get_post($post_id);
if (!($post instanceof WP_Post)) {
return;
}
// Save the setting.
civicrm_api3('Setting', 'create', [
'wpBasePage' => $post->post_name,
]);
}
/**
* Save the CiviCRM Shortcode Mode Setting.
*
* @since 5.44
*/
// Nonce is checked in self::form_nonce_check().
// phpcs:disable WordPress.Security.NonceVerification.Missing
$chosen = isset($_POST['shortcode_mode']) ? sanitize_text_field(wp_unslash($_POST['shortcode_mode'])) : 0;
// phpcs:enable WordPress.Security.NonceVerification.Missing
if ($chosen === 0 || !in_array($chosen, $this->civi->admin->get_shortcode_modes())) {
return;
}
// Save the setting.
update_option('shortcode_mode', $chosen);
}
/**
* Save the CiviCRM Shortcode Theme Compatibility Mode Setting.
*
* @since 5.80
*/
private function form_save_theme_compatibility() {
// Bail if there is no valid chosen value.
// Nonce is checked in self::form_nonce_check().
// phpcs:disable WordPress.Security.NonceVerification.Missing
$chosen = isset($_POST['theme_compatibility_mode']) ? sanitize_text_field(wp_unslash($_POST['theme_compatibility_mode'])) : 0;
// phpcs:enable WordPress.Security.NonceVerification.Missing
if ($chosen === 0 || !in_array($chosen, $this->civi->admin->get_theme_compatibility_modes())) {
return;
}
// Save the setting.
update_option('theme_compatibility_mode', $chosen);
}
/**
* Save the CiviCRM Email Sync Setting.
*
* @since 5.34
*/
// Nonce is checked in self::form_nonce_check().
// phpcs:disable WordPress.Security.NonceVerification.Missing
$chosen = isset($_POST['sync_email']) ? sanitize_text_field(wp_unslash($_POST['sync_email'])) : 0;
// phpcs:enable WordPress.Security.NonceVerification.Missing
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
if ($chosen === 0) {
return;
}
// Setting is actually a boolean.
$sync_email = $chosen === 'no' ? FALSE : TRUE;
// Save the setting.
civicrm_api3('Setting', 'create', [
'syncCMSEmail' => $sync_email,
]);
}
/**
* Check the nonce.
*
* @since 5.34
*/
private function form_nonce_check() {
// Do we trust the source of the data?
check_admin_referer('civicrm_options_form_action', 'civicrm_options_form_nonce');
}
/**
* Redirect to the Settings page with an extra param.
*
* @since 5.34
*/
private function form_redirect() {
// Our array of arguments.
$args = [
'page' => $this->slug,
'settings-updated' => 'true',
];
// Redirect to our admin page.
wp_safe_redirect(add_query_arg($args, admin_url('admin.php')));
}
// ---------------------------------------------------------------------------
// AJAX Handlers
// ---------------------------------------------------------------------------
/**
*
* @since 5.34
*/
public function ajax_save_basepage() {
// Default response.
$data = [
'section' => 'basepage',
'result' => '',
'notice' => __('Unable to save the WordPress Base Page.', 'civicrm'),
'message' => __('Please select a Page from the drop-down for CiviCRM to use as its Base Page. If CiviCRM was able to create one automatically, there should be one with the title "CiviCRM". If not, please select another suitable WordPress Page.', 'civicrm'),
'saved' => FALSE,
];
// Since this is an AJAX request, check security.
$result = check_ajax_referer('civicrm_basepage', FALSE, FALSE);
if ($result === FALSE) {
$data['notice'] = __('Authentication failed. Unable to save the WordPress Base Page.', 'civicrm');
wp_send_json($data);
}
// Bail if there's no valid Post ID.
$post_id = empty($_POST['value']) ? 0 : (int) sanitize_text_field(wp_unslash($_POST['value']));
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
if ($post_id === 0) {
$data['notice'] = __('No Page ID detected. Unable to save the WordPress Base Page.', 'civicrm');
wp_send_json($data);
}
// Bail if we don't find a post object.
$post = get_post($post_id);
if (!($post instanceof WP_Post)) {
$data['notice'] = __('Could not find selected Page. Unable to save the WordPress Base Page.', 'civicrm');
wp_send_json($data);
}
// Save the setting.
civicrm_api3('Setting', 'create', [
'wpBasePage' => $post->post_name,
]);
// Retrieve the setting in case hook callbacks have altered it.
// TODO: find out why this *doesn't* change when hooks *do* change it.
$actual = civicrm_api3('Setting', 'getvalue', [
'name' => 'wpBasePage',
'group' => 'CiviCRM Preferences',
]);
// Query for our Base Page.
$pages = get_posts([
'post_type' => 'page',
'name' => strtolower($actual),
'post_status' => 'publish',
'posts_per_page' => 1,
]);
// Bail if the Base Page was not found.
if (empty($pages) || !is_array($pages)) {
$data['notice'] = __('Could not get data for the selected Page.', 'civicrm');
wp_send_json($data);
}
// Grab what should be the only item.
$basepage = array_pop($pages);
// Data response.
$data = [
'section' => 'basepage',
'result' => $basepage->ID,
'message' => __('It appears that your Base Page has been set. Looking good.', 'civicrm'),
'saved' => TRUE,
];
// Return the data.
wp_send_json($data);
}
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
/**
* Save the CiviCRM Shortcode Mode Setting.
*
* @since 5.44
*/
public function ajax_save_shortcode() {
// Default response.
$data = [
'section' => 'shortcode',
'message' => __('Could not save the selected setting.', 'civicrm'),
'saved' => FALSE,
];
// Since this is an AJAX request, check security.
$result = check_ajax_referer('civicrm_shortcode', FALSE, FALSE);
if ($result === FALSE) {
$data['notice'] = __('Authentication failed. Could not save the selected setting.', 'civicrm');
wp_send_json($data);
}
// Bail if there is no valid chosen value.
$chosen = isset($_POST['value']) ? sanitize_text_field(wp_unslash($_POST['value'])) : 0;
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
if ($chosen === 0 || !in_array($chosen, $this->civi->admin->get_shortcode_modes())) {
$data['notice'] = __('Unrecognised parameter. Could not save the selected setting.', 'civicrm');
wp_send_json($data);
}
// Set the Shortcode Mode setting.
$this->civi->admin->set_shortcode_mode($chosen);
// Data response.
$data = [
'section' => 'shortcode',
'result' => $chosen,
'message' => __('Setting saved.', 'civicrm'),
'saved' => TRUE,
];
// Return the data.
wp_send_json($data);
}
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
/**
* Save the CiviCRM Shortcode Theme Compatibility Mode Setting.
*
* @since 5.80
*/
public function ajax_save_theme_compatibility() {
// Default response.
$data = [
'section' => 'theme',
'message' => __('Could not save the selected setting.', 'civicrm'),
'saved' => FALSE,
];
// Since this is an AJAX request, check security.
$result = check_ajax_referer('civicrm_theme', FALSE, FALSE);
if ($result === FALSE) {
$data['notice'] = __('Authentication failed. Could not save the selected setting.', 'civicrm');
wp_send_json($data);
}
// Bail if there is no valid chosen value.
$chosen = isset($_POST['value']) ? sanitize_text_field(wp_unslash($_POST['value'])) : 0;
if ($chosen === 0 || !in_array($chosen, $this->civi->admin->get_theme_compatibility_modes())) {
$data['notice'] = __('Unrecognised parameter. Could not save the selected setting.', 'civicrm');
wp_send_json($data);
}
// Set the Shortcode Theme Compatibility Mode setting.
$this->civi->admin->set_theme_compatibility_mode($chosen);
// Data response.
$data = [
'section' => 'theme',
'result' => $chosen,
'message' => __('Setting saved.', 'civicrm'),
'saved' => TRUE,
];
// Return the data.
wp_send_json($data);
}
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
/**
* Save the CiviCRM Email Sync Setting.
*
* @since 5.34
*/
public function ajax_save_email_sync() {
// Default response.
$data = [
'section' => 'email_sync',
'message' => __('Could not save the selected setting.', 'civicrm'),
'saved' => FALSE,
];
// Since this is an AJAX request, check security.
$result = check_ajax_referer('civicrm_email_sync', FALSE, FALSE);
if ($result === FALSE) {
$data['notice'] = __('Authentication failed. Could not save the selected setting.', 'civicrm');
wp_send_json($data);
}
// Bail if there is no valid chosen value.
$chosen = isset($_POST['value']) ? sanitize_text_field(wp_unslash($_POST['value'])) : 0;
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
if ($chosen === 0) {
$data['notice'] = __('Unrecognised parameter. Could not save the selected setting.', 'civicrm');
wp_send_json($data);
}
// Setting is actually a boolean.
$sync_email = $chosen === 'no' ? FALSE : TRUE;
// Save the setting.
civicrm_api3('Setting', 'create', [
'syncCMSEmail' => $sync_email,
]);
// Retrieve the setting in case hook callbacks have altered it.
// TODO: find out why this *doesn't* change when hooks *do* change it.
$actual = civicrm_api3('Setting', 'getvalue', [
'name' => 'syncCMSEmail',
'group' => 'CiviCRM Preferences',
]);
// Data response.
$data = [
'section' => 'email_sync',
'result' => $actual ? 'yes' : 'no',
'message' => __('Setting saved.', 'civicrm'),
'saved' => TRUE,
];
// Return the data.
wp_send_json($data);
}
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
/**
* Refresh the CiviCRM permissions.
*
* @since 5.52
*/
public function ajax_refresh_permissions() {
// Default response.
$data = [
'section' => 'refresh_permissions',
'notice' => __('Could not refresh the CiviCRM permissions.', 'civicrm'),
'saved' => FALSE,
];
// Since this is an AJAX request, check security.
$result = check_ajax_referer('civicrm_refresh_permissions', FALSE, FALSE);
if ($result === FALSE) {
$data['notice'] = __('Authentication failed. Could not refresh the CiviCRM permissions.', 'civicrm');
wp_send_json($data);
}
// Bail if there is no valid chosen value.
$chosen = isset($_POST['value']) ? sanitize_text_field(wp_unslash($_POST['value'])) : 0;
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
if ($chosen === 0 || !in_array($chosen, ['enable', 'disable'])) {
$data['notice'] = __('Unrecognised parameter. Could not refresh the CiviCRM permissions.', 'civicrm');
wp_send_json($data);
}
// Always refresh the CiviCRM permissions.
$this->civi->users->refresh_capabilities();
// Have we enabled the custom role?
if ($chosen === 'enable') {
// Create the role if it doesn't exist.
if (!$this->civi->users->has_custom_role()) {
$this->civi->users->create_custom_role();
}
// Refresh the custom role's permissions.
$this->civi->users->refresh_custom_role_capabilities();
}
// Have we disabled the custom role?
if ($chosen === 'disable') {
// Delete the role if it exists.
if ($this->civi->users->has_custom_role()) {
$this->civi->users->delete_custom_role();
}
}
// Data response.
$data = [
'section' => 'refresh_permissions',
'notice' => __('CiviCRM permissions refreshed.', 'civicrm'),
'saved' => TRUE,
];
// Return the data.
wp_send_json($data);
}
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
/**
* Clear the CiviCRM caches.
*
* @since 5.34
*/
public function ajax_clear_caches() {
// Default response.
$data = [
'section' => 'clear_caches',
'notice' => __('Could not clear the CiviCRM caches.', 'civicrm'),
'saved' => FALSE,
];
// Since this is an AJAX request, check security.
$result = check_ajax_referer('civicrm_clear_caches', FALSE, FALSE);
if ($result === FALSE) {
$data['notice'] = __('Authentication failed. Could not clear the CiviCRM caches.', 'civicrm');
wp_send_json($data);
}
// Bail if there is no valid value.
$chosen = isset($_POST['value']) ? (int) sanitize_text_field(wp_unslash($_POST['value'])) : 0;
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
if ($chosen !== 1) {
$data['notice'] = __('Unrecognised parameter. Could not clear the CiviCRM caches.', 'civicrm');
wp_send_json($data);
}
// Go ahead and clear the caches.
$this->civi->admin->clear_caches();
// Data response.
$data = [
'section' => 'clear_caches',
'notice' => __('CiviCRM caches cleared.', 'civicrm'),
'saved' => TRUE,
];
// Return the data.
wp_send_json($data);
}
}