Ability_Audit_Logger.php
1 month ago
Cookiebot_Abilities_Registrar.php
1 month ago
Cookiebot_Ability_Interface.php
1 month ago
Get_Compliance_Summary_Ability.php
1 month ago
Get_Status_Ability.php
1 month ago
Install_Ppg_Ability.php
1 month ago
Set_Cbid_Ability.php
1 month ago
Toggle_Gcm_Ability.php
1 month ago
Verify_Setup_Ability.php
1 month ago
Install_Ppg_Ability.php
130 lines
| 1 | <?php |
| 2 | // src/abilities/Install_Ppg_Ability.php |
| 3 | |
| 4 | namespace cybot\cookiebot\abilities; |
| 5 | |
| 6 | use cybot\cookiebot\settings\pages\PPG_Page; |
| 7 | |
| 8 | class Install_Ppg_Ability implements Cookiebot_Ability_Interface { |
| 9 | |
| 10 | /** |
| 11 | * @var Ability_Audit_Logger |
| 12 | */ |
| 13 | private $logger; |
| 14 | |
| 15 | /** |
| 16 | * @param Ability_Audit_Logger $logger |
| 17 | * |
| 18 | * @since 4.8.0 |
| 19 | */ |
| 20 | public function __construct( Ability_Audit_Logger $logger ) { |
| 21 | $this->logger = $logger; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @return string |
| 26 | * |
| 27 | * @since 4.8.0 |
| 28 | */ |
| 29 | public function get_name() { |
| 30 | return 'cookiebot/install-ppg'; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * @return array |
| 35 | * |
| 36 | * @since 4.8.0 |
| 37 | */ |
| 38 | public function get_args() { |
| 39 | $logger = $this->logger; |
| 40 | |
| 41 | return array( |
| 42 | 'label' => __( 'Install Privacy Policy Generator', 'cookiebot' ), |
| 43 | 'description' => __( 'Installs and activates the Privacy Policy Generator plugin (privacy-policy-usercentrics). Idempotent — returns immediately if already active. After install, direct the user to admin_url to complete policy setup.', 'cookiebot' ), |
| 44 | 'category' => 'cookiebot', |
| 45 | 'output_schema' => array( |
| 46 | 'type' => 'object', |
| 47 | 'properties' => array( |
| 48 | 'was_already_active' => array( 'type' => 'boolean', 'description' => 'True if plugin was already active.' ), |
| 49 | 'success' => array( 'type' => 'boolean', 'description' => 'Whether the operation succeeded.' ), |
| 50 | 'admin_url' => array( 'type' => 'string', 'description' => 'WP admin URL for the PPG settings page.' ), |
| 51 | ), |
| 52 | 'additionalProperties' => false, |
| 53 | ), |
| 54 | 'execute_callback' => function() use ( $logger ) { |
| 55 | if ( ! function_exists( 'is_plugin_active' ) ) { |
| 56 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 57 | } |
| 58 | |
| 59 | $admin_url = admin_url( 'admin.php?page=' . PPG_Page::ADMIN_SLUG ); |
| 60 | |
| 61 | if ( is_plugin_active( PPG_Page::PPG_PLUGIN_SLUG ) ) { |
| 62 | $logger->log( 'cookiebot/install-ppg', 'already_active', 'already_active' ); |
| 63 | return array( 'was_already_active' => true, 'success' => true, 'admin_url' => $admin_url ); |
| 64 | } |
| 65 | |
| 66 | if ( ! function_exists( 'get_plugins' ) ) { |
| 67 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 68 | } |
| 69 | |
| 70 | $plugins = get_plugins(); |
| 71 | if ( isset( $plugins[ PPG_Page::PPG_PLUGIN_SLUG ] ) ) { |
| 72 | $result = activate_plugin( PPG_Page::PPG_PLUGIN_SLUG ); |
| 73 | if ( is_wp_error( $result ) ) { |
| 74 | return new \WP_Error( 'cookiebot_ppg_install_failed', $result->get_error_message() ); |
| 75 | } |
| 76 | $logger->log( 'cookiebot/install-ppg', 'installed_inactive', 'activated' ); |
| 77 | return array( 'was_already_active' => false, 'success' => true, 'admin_url' => $admin_url ); |
| 78 | } |
| 79 | |
| 80 | require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 81 | require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php'; |
| 82 | require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 83 | |
| 84 | $api = plugins_api( |
| 85 | 'plugin_information', |
| 86 | array( |
| 87 | 'slug' => 'privacy-policy-usercentrics', |
| 88 | 'fields' => array( 'sections' => false ), |
| 89 | ) |
| 90 | ); |
| 91 | |
| 92 | if ( is_wp_error( $api ) ) { |
| 93 | return new \WP_Error( 'cookiebot_ppg_install_failed', $api->get_error_message() ); |
| 94 | } |
| 95 | |
| 96 | // Verify the API returned the expected slug and a trusted download origin. |
| 97 | if ( ! isset( $api->slug ) || 'privacy-policy-usercentrics' !== $api->slug ) { |
| 98 | return new \WP_Error( 'cookiebot_ppg_install_failed', __( 'Unexpected plugin data returned from WordPress.org.', 'cookiebot' ) ); |
| 99 | } |
| 100 | if ( ! isset( $api->download_link ) || strpos( $api->download_link, 'https://downloads.wordpress.org/' ) !== 0 ) { |
| 101 | return new \WP_Error( 'cookiebot_ppg_install_failed', __( 'Plugin download URL does not originate from WordPress.org.', 'cookiebot' ) ); |
| 102 | } |
| 103 | |
| 104 | $upgrader = new \Plugin_Upgrader( new \WP_Ajax_Upgrader_Skin() ); |
| 105 | $install = $upgrader->install( $api->download_link ); |
| 106 | |
| 107 | if ( is_wp_error( $install ) || ! $install ) { |
| 108 | return new \WP_Error( 'cookiebot_ppg_install_failed', __( 'Plugin installation failed.', 'cookiebot' ) ); |
| 109 | } |
| 110 | |
| 111 | $activate = activate_plugin( PPG_Page::PPG_PLUGIN_SLUG ); |
| 112 | if ( is_wp_error( $activate ) ) { |
| 113 | return new \WP_Error( 'cookiebot_ppg_install_failed', $activate->get_error_message() ); |
| 114 | } |
| 115 | |
| 116 | delete_transient( 'ppguc_activation_redirect' ); |
| 117 | $logger->log( 'cookiebot/install-ppg', 'not_installed', 'installed_activated' ); |
| 118 | return array( 'was_already_active' => false, 'success' => true, 'admin_url' => $admin_url ); |
| 119 | }, |
| 120 | 'permission_callback' => function() { |
| 121 | return current_user_can( 'manage_options' ) && current_user_can( 'install_plugins' ); |
| 122 | }, |
| 123 | 'meta' => array( |
| 124 | 'annotations' => array( 'readonly' => false, 'destructive' => false, 'idempotent' => true ), |
| 125 | 'show_in_rest' => true, |
| 126 | ), |
| 127 | ); |
| 128 | } |
| 129 | } |
| 130 |