Settings.php
327 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonorDashboards\Admin; |
| 4 | |
| 5 | /** |
| 6 | * @since 2.10.0 |
| 7 | */ |
| 8 | class Settings |
| 9 | { |
| 10 | |
| 11 | /** |
| 12 | * Register settings related to Donor Profiles |
| 13 | * |
| 14 | * @since 2.10.0 |
| 15 | * |
| 16 | * @param array $settings |
| 17 | * |
| 18 | * @return array |
| 19 | * |
| 20 | */ |
| 21 | public function register($settings) |
| 22 | { |
| 23 | $donorDashboardSettings = [ |
| 24 | $this->getDonorDashboardPageSetting(), |
| 25 | $this->donorDashboardPageIsPublished() ? $this->getOverrideLegacyDonationManagementPagesSetting() : null, |
| 26 | ]; |
| 27 | |
| 28 | return give_settings_array_insert( |
| 29 | $settings, |
| 30 | 'history_page', |
| 31 | $donorDashboardSettings |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Return true if donor profile page is defined and published, false if not |
| 37 | * |
| 38 | * @since 2.10.0 |
| 39 | * @return boolean |
| 40 | * |
| 41 | */ |
| 42 | protected function donorDashboardPageIsPublished() |
| 43 | { |
| 44 | $donorDashboardPageId = ! empty(give_get_option('donor_dashboard_page')) ? give_get_option( |
| 45 | 'donor_dashboard_page' |
| 46 | ) : null; |
| 47 | |
| 48 | return $donorDashboardPageId && get_post_status($donorDashboardPageId) === 'publish'; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Return CMB2 compatible array used to render/control donor profile page setting |
| 53 | * |
| 54 | * @since 2.10.0 |
| 55 | * @return array |
| 56 | * |
| 57 | */ |
| 58 | protected function getDonorDashboardPageSetting() |
| 59 | { |
| 60 | $generateDonorDashboardPageUrl = add_query_arg( |
| 61 | [ |
| 62 | 'give-generate-donor-dashboard-page' => '1', |
| 63 | ], |
| 64 | admin_url('edit.php') |
| 65 | ); |
| 66 | |
| 67 | $generateDonorDashboardPageDesc = $this->donorDashboardPageIsPublished() ? '' : sprintf( |
| 68 | __(' Need helping setting one up? <a href="%s">Generate a new Donor Dashboard page.</a>', 'give'), |
| 69 | $generateDonorDashboardPageUrl |
| 70 | ); |
| 71 | |
| 72 | return [ |
| 73 | 'name' => __('Donor Dashboard Page', 'give'), |
| 74 | 'desc' => __( |
| 75 | 'This is the page where donors can manage their information, review history and more -- all in one place. The Donor Dashboard block or <code>[give_donor_dashboard]</code> shortcode should be on this page. ', |
| 76 | 'give' |
| 77 | ) . $generateDonorDashboardPageDesc, |
| 78 | 'id' => 'donor_dashboard_page', |
| 79 | 'type' => 'select', |
| 80 | 'class' => 'give-select give-select-chosen', |
| 81 | 'options' => give_cmb2_get_post_options( |
| 82 | [ |
| 83 | 'post_type' => 'page', |
| 84 | 'numberposts' => 30, |
| 85 | ] |
| 86 | ), |
| 87 | 'attributes' => [ |
| 88 | 'data-search-type' => 'pages', |
| 89 | 'data-placeholder' => esc_html__('Choose a page', 'give'), |
| 90 | ], |
| 91 | ]; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Return CMB2 compatible array used to render/control override legacy donation management pages setting |
| 96 | * |
| 97 | * @since 2.10.0 |
| 98 | * @return array |
| 99 | * |
| 100 | */ |
| 101 | protected function getOverrideLegacyDonationManagementPagesSetting() |
| 102 | { |
| 103 | return [ |
| 104 | 'name' => esc_html__('Override Legacy Donation Management Pages', 'give'), |
| 105 | 'desc' => esc_html__( |
| 106 | 'Use the Donor Dashboard instead of the legacy donation management pages (Donation History, Edit Profile, Subscriptions, etc).', |
| 107 | 'give' |
| 108 | ), |
| 109 | 'id' => 'override_legacy_donation_management_pages', |
| 110 | 'wrapper_class' => 'override-legacy-donation-management-pages', |
| 111 | 'type' => 'radio_inline', |
| 112 | 'default' => 'enabled', |
| 113 | 'options' => [ |
| 114 | 'enabled' => esc_html__('Enabled', 'give'), |
| 115 | 'disabled' => esc_html__('Disabled', 'give'), |
| 116 | ], |
| 117 | ]; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Generate donor profile page, and update site setting to use it |
| 122 | * |
| 123 | * @since 2.10.0 |
| 124 | * @return void |
| 125 | * |
| 126 | */ |
| 127 | public function generateDonorDashboardPage() |
| 128 | { |
| 129 | // Check if a Donor Dashboard page has already been created |
| 130 | |
| 131 | if ( ! empty(give_get_option('donor_dashboard_page'))) { |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | $content = $this->getDonorDashboardPageContent(); |
| 136 | |
| 137 | $pageId = wp_insert_post( |
| 138 | [ |
| 139 | 'comment_status' => 'close', |
| 140 | 'ping_status' => 'close', |
| 141 | 'post_author' => 1, |
| 142 | 'post_title' => __('Donor Dashboard', 'give'), |
| 143 | 'post_status' => 'publish', |
| 144 | 'post_content' => $content, |
| 145 | 'post_type' => 'page', |
| 146 | ] |
| 147 | ); |
| 148 | |
| 149 | if ($pageId) { |
| 150 | give_update_option('donor_dashboard_page', $pageId); |
| 151 | |
| 152 | give_update_option('override_legacy_donation_management_pages', 'enabled'); |
| 153 | |
| 154 | $overrideSettingsMap = [ |
| 155 | 'history_page', |
| 156 | 'subscriptions_page', |
| 157 | ]; |
| 158 | |
| 159 | foreach ($overrideSettingsMap as $setting) { |
| 160 | if (give_get_option($setting) !== $pageId) { |
| 161 | give_update_option($setting, $pageId); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Get default content for donor profile page, based on format (block vs shortcode) |
| 169 | * |
| 170 | * @since 2.10.0 |
| 171 | * |
| 172 | * @param string $format |
| 173 | * |
| 174 | * @return string |
| 175 | * |
| 176 | */ |
| 177 | protected function getDonorDashboardPageContent() |
| 178 | { |
| 179 | if ($this->shouldGenerateWithBlock()) { |
| 180 | return get_comment_delimited_block_content( |
| 181 | 'give/donor-dashboard', |
| 182 | [ |
| 183 | 'align' => 'wide', |
| 184 | ], |
| 185 | null |
| 186 | ); |
| 187 | } else { |
| 188 | return '[give_donor_dashboard]'; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Determine whether the Donor Dashboard page should be generated with a block |
| 194 | * |
| 195 | * @since 2.10.0 |
| 196 | * @return bool |
| 197 | * |
| 198 | */ |
| 199 | private function shouldGenerateWithBlock() |
| 200 | { |
| 201 | $usingBlocks = $this->isBlockEditorActive() || $this->isGutenbergEditorActive() ? true : false; |
| 202 | $usingClassicEditor = $this->isClassicEditorActive(); |
| 203 | $usingDisableGutenberg = $this->isDisableGutenbergActive(); |
| 204 | |
| 205 | if ($usingClassicEditor === false && $usingDisableGutenberg === false && $usingBlocks) { |
| 206 | return true; |
| 207 | } |
| 208 | |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Determine whether the Gutenberg editor is active |
| 214 | * |
| 215 | * @since 2.10.0 |
| 216 | * @return bool |
| 217 | * |
| 218 | */ |
| 219 | private function isGutenbergEditorActive() |
| 220 | { |
| 221 | if (has_filter('replace_editor', 'gutenberg_init')) { |
| 222 | // Gutenberg is installed and activated. |
| 223 | return true; |
| 224 | } |
| 225 | |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Determine whether block editor is active |
| 231 | * |
| 232 | * @since 2.10.0 |
| 233 | * @return bool |
| 234 | * |
| 235 | */ |
| 236 | private function isBlockEditorActive() |
| 237 | { |
| 238 | if (version_compare($GLOBALS['wp_version'], '5.0-beta', '>')) { |
| 239 | // Block editor. |
| 240 | return true; |
| 241 | } |
| 242 | |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Determine whether the Classic editor is active |
| 248 | * |
| 249 | * @since 2.10.0 |
| 250 | * @return bool |
| 251 | * |
| 252 | */ |
| 253 | private function isClassicEditorActive() |
| 254 | { |
| 255 | include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 256 | |
| 257 | if (is_plugin_active('classic-editor/classic-editor.php') && (get_option( |
| 258 | 'classic-editor-replace' |
| 259 | ) !== 'no-replace')) { |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Determine whether Disable Gutenberg plugin is active |
| 268 | * |
| 269 | * @since 2.10.0 |
| 270 | * @return bool |
| 271 | * |
| 272 | */ |
| 273 | private function isDisableGutenbergActive() |
| 274 | { |
| 275 | include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 276 | |
| 277 | if (is_plugin_active('disable-gutenberg/disable-gutenberg.php')) { |
| 278 | return true; |
| 279 | } |
| 280 | |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Filter and override legacy donation management page settings |
| 286 | * |
| 287 | * @since 2.10.0 |
| 288 | * |
| 289 | * @param array $settings |
| 290 | * |
| 291 | * @return array |
| 292 | * |
| 293 | */ |
| 294 | public function overrideLegacyDonationManagementPageSettings($settings) |
| 295 | { |
| 296 | // Only override settings if the override legacy donation management pages setting is enabled |
| 297 | if ($this->donorDashboardPageIsPublished() && give_is_setting_enabled( |
| 298 | give_get_option('override_legacy_donation_management_pages', 'enabled') |
| 299 | )) { |
| 300 | $pageId = give_get_option('donor_dashboard_page'); |
| 301 | |
| 302 | $overrideSettingsMap = [ |
| 303 | 'history_page', |
| 304 | 'subscriptions_page', |
| 305 | ]; |
| 306 | |
| 307 | foreach ($overrideSettingsMap as $setting) { |
| 308 | if (give_get_option($setting) !== $pageId) { |
| 309 | give_update_option($setting, $pageId); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | // Hide settings that are overridden by Donor Profile setting |
| 314 | $key = 0; |
| 315 | foreach ($settings as $setting) { |
| 316 | if (in_array($setting['id'], $overrideSettingsMap)) { |
| 317 | unset($settings[$key]); |
| 318 | } |
| 319 | $key++; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | return $settings; |
| 324 | } |
| 325 | |
| 326 | } |
| 327 |