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