Settings
Class to handle options saving/loading
Description
Source
File: bp-integrations/learndash/core/Settings.php
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | class Settings { protected $loader ; protected $options = []; protected $optionKey = 'bp_ld_sync_settings' ; /** * Constructor * * @since BuddyBoss 1.0.0 */ public function __construct() { $this ->installDefaultSettings(); $this ->loader = new ValueLoader( $this ->options); add_action( 'bp_ld_sync/setting_updated' , [ $this , 'setGroupSyncTimestamp' ], 10, 2); } /** * Convert . seperated name to array syntax * * @since BuddyBoss 1.0.0 */ public function getName( $key = '' ) { $name = $this ->optionKey; foreach ( array_filter ( explode ( '.' , $key )) as $peice ) { $name .= "[{$peice}]" ; } return $name ; } /** * Get the option from loader * * @since BuddyBoss 1.0.0 */ public function get( $key = null, $default = null) { return $this ->loader->get( $key , $default ); } /** * Set the option to loader * * @since BuddyBoss 1.0.0 */ public function set( $key = null, $value = null) { $this ->loader->set( $key , $value ); return $this ; } /** * Presist the loader value to db * * @since BuddyBoss 1.0.0 */ public function update() { $oldOptions = $this ->options; bp_update_option( $this ->optionKey, $this ->options = $this ->loader->get()); do_action( 'bp_ld_sync/setting_updated' , $this ->options, $oldOptions ); return $this ; } /** * Set the group sync timestamp to determine if we need a full sync of simple sync * * @since BuddyBoss 1.0.0 */ public function setGroupSyncTimestamp( $options , $oldOptions ) { $new = new ValueLoader( $options ); $old = new ValueLoader( $oldOptions ); $time = time(); if ( $new ->get( 'buddypress.enabled' ) && ! $old ->get( 'buddypress.enabled' )) { bp_update_option( 'bp_ld_sync/bp_last_synced' , $time , false); } if ( $new ->get( 'learndash.enabled' ) && ! $old ->get( 'learndash.enabled' )) { bp_update_option( 'bp_ld_sync/ld_last_synced' , $time , false); } } /** * Default options * * @since BuddyBoss 1.0.0 */ public function defaultOptions() { return [ 'buddypress' => [ 'enabled' => false, 'show_in_bp_create' => true, 'show_in_bp_manage' => true, 'tab_access' => 'anyone' , 'default_auto_sync' => true, 'delete_ld_on_delete' => false, 'default_admin_sync_to' => 'admin' , 'default_mod_sync_to' => 'admin' , 'default_user_sync_to' => 'user' , ], 'learndash' => [ 'enabled' => false, 'default_auto_sync' => true, 'default_bp_privacy' => 'private' , 'default_bp_invite_status' => 'admin' , 'default_admin_sync_to' => 'admin' , 'default_user_sync_to' => 'user' , 'delete_bp_on_delete' => false, ], 'reports' => [ 'enabled' => false, 'access' => [ 'admin' , 'mod' ], 'per_page' => 20, 'cache_time' => 60 ], ]; } /** * Presist the default option into db if not exists * * @since BuddyBoss 1.0.0 */ protected function installDefaultSettings() { if (! $options = get_option( $this ->optionKey)) { $options = $this ->defaultOptions(); bp_update_option( $this ->optionKey, $options ); } $this ->options = $options ; } } |
Changelog
Version | Description |
---|---|
BuddyBoss 1.0.0 | Introduced. |
Methods
- __construct — Constructor
- defaultOptions — Default options
- get — Get the option from loader
- getName — Convert . seperated name to array syntax
- installDefaultSettings — Presist the default option into db if not exists
- set — Set the option to loader
- setGroupSyncTimestamp — Set the group sync timestamp to determine if we need a full sync of simple sync
- update — Presist the loader value to db
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.