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