ActivationService.php
106 lines
| 1 | <?php |
| 2 | namespace SureCart\Activation; |
| 3 | |
| 4 | /** |
| 5 | * Service for plugin activation. |
| 6 | */ |
| 7 | class ActivationService { |
| 8 | /** |
| 9 | * Holds the roles service. |
| 10 | * |
| 11 | * @var \SureCart\Permissions\RolesService |
| 12 | */ |
| 13 | protected $roles = null; |
| 14 | |
| 15 | /** |
| 16 | * Holds the roles service. |
| 17 | * |
| 18 | * @var \SureCart\WordPress\Pages\PageSeeder |
| 19 | */ |
| 20 | protected $seeder = null; |
| 21 | |
| 22 | /** |
| 23 | * Get dependencies for this service. |
| 24 | * |
| 25 | * @param \SureCart\Permissions\RolesService $roles Roles service. |
| 26 | * @param \SureCart\WordPress\Pages\PageSeeder $seeder Seeder service. |
| 27 | */ |
| 28 | public function __construct( \SureCart\Permissions\RolesService $roles, \SureCart\WordPress\Pages\PageSeeder $seeder ) { |
| 29 | $this->roles = $roles; |
| 30 | $this->seeder = $seeder; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Bootstrap. |
| 35 | * |
| 36 | * @return void |
| 37 | */ |
| 38 | public function bootstrap() { |
| 39 | register_activation_hook( SURECART_PLUGIN_FILE, [ $this, 'activate' ] ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Create roles on plugin activation. |
| 44 | * |
| 45 | * @return void |
| 46 | */ |
| 47 | public function activate() { |
| 48 | // create roles. |
| 49 | $this->roles->create(); |
| 50 | // seed pages and forms. |
| 51 | $this->seeder->seed(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Remove roles and all data. |
| 56 | * |
| 57 | * @return void |
| 58 | */ |
| 59 | public function uninstall() { |
| 60 | // remove roles. |
| 61 | $this->roles->delete(); |
| 62 | // remove pages that were automatically seeded. |
| 63 | $this->seeder->delete(); |
| 64 | // remove all forms. |
| 65 | $this->removeFormPosts(); |
| 66 | // remove all options from the options table. |
| 67 | $this->removeOptions(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Remove all posts from our post type. |
| 72 | * |
| 73 | * @return void |
| 74 | */ |
| 75 | public function removeFormPosts() { |
| 76 | // remove all form posts. |
| 77 | $form_ids = get_posts( |
| 78 | [ |
| 79 | 'post_type' => 'sc_form', |
| 80 | 'numberposts' => -1, |
| 81 | 'fields' => 'ids', |
| 82 | ] |
| 83 | ); |
| 84 | foreach ( $form_ids as $form_id ) { |
| 85 | wp_delete_post( $form_id, true ); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Remove all our options from the options table. |
| 91 | * |
| 92 | * @return void |
| 93 | */ |
| 94 | public function removeOptions() { |
| 95 | delete_option( 'ce_registered_webhook' ); |
| 96 | delete_option( 'surecart_order-confirmation_page_id' ); |
| 97 | delete_option( 'surecart_dashboard_page_id' ); |
| 98 | delete_option( 'surecart_checkout_sc_form_id' ); |
| 99 | delete_option( 'surecart_use_esm_loader' ); |
| 100 | delete_option( 'surecart_checkout_page_id' ); |
| 101 | delete_option( 'sc_webhook_signing_secret' ); |
| 102 | delete_option( 'sc_api_token' ); |
| 103 | delete_option( 'sc_uninstall' ); |
| 104 | } |
| 105 | } |
| 106 |