Additional_Page.php
1 year ago
Dashboard_Page.php
9 months ago
Embeddings_Page.php
1 year ago
Gcm_Page.php
1 year ago
General_Page.php
10 months ago
Gtm_Page.php
1 year ago
Iab_Page.php
1 year ago
Multiple_Page.php
1 year ago
PPG_Page.php
3 months ago
Settings_Page.php
1 year ago
Settings_Page_Interface.php
1 year ago
Support_Page.php
7 months ago
PPG_Page.php
167 lines
| 1 | <?php |
| 2 | |
| 3 | namespace cybot\cookiebot\settings\pages; |
| 4 | |
| 5 | use cybot\cookiebot\lib\Cookiebot_WP; |
| 6 | use function cybot\cookiebot\lib\asset_url; |
| 7 | use function cybot\cookiebot\lib\include_view; |
| 8 | |
| 9 | class PPG_Page implements Settings_Page_Interface { |
| 10 | |
| 11 | const ADMIN_SLUG = 'cookiebot_ppg'; |
| 12 | |
| 13 | const PPG_PLUGIN_SLUG = 'privacy-policy-usercentrics/privacy-policy-usercentrics.php'; |
| 14 | |
| 15 | public function menu() { |
| 16 | add_submenu_page( |
| 17 | 'cookiebot', |
| 18 | __( 'Policy Generator Plugin', 'cookiebot' ), |
| 19 | __( 'Policy Generator Plugin', 'cookiebot' ), |
| 20 | 'manage_options', |
| 21 | self::ADMIN_SLUG, |
| 22 | array( $this, 'display' ), |
| 23 | 30 |
| 24 | ); |
| 25 | } |
| 26 | |
| 27 | public function register_ajax_hooks() { |
| 28 | add_action( 'wp_ajax_cookiebot_activate_ppg', array( $this, 'ajax_activate_plugin' ) ); |
| 29 | add_action( 'wp_ajax_cookiebot_install_ppg', array( $this, 'ajax_install_plugin' ) ); |
| 30 | } |
| 31 | |
| 32 | public function ajax_activate_plugin() { |
| 33 | check_ajax_referer( 'cookiebot_ppg_nonce', 'nonce' ); |
| 34 | |
| 35 | if ( ! current_user_can( 'activate_plugins' ) ) { |
| 36 | wp_send_json_error( array( 'message' => __( 'You do not have permission to activate plugins.', 'cookiebot' ) ), 403 ); |
| 37 | } |
| 38 | |
| 39 | if ( self::is_plugin_active() ) { |
| 40 | wp_send_json_success(); |
| 41 | } |
| 42 | |
| 43 | if ( ! self::is_plugin_installed() ) { |
| 44 | wp_send_json_error( array( 'message' => __( 'Plugin is not installed.', 'cookiebot' ) ), 404 ); |
| 45 | } |
| 46 | |
| 47 | $result = activate_plugin( self::PPG_PLUGIN_SLUG ); |
| 48 | |
| 49 | if ( is_wp_error( $result ) ) { |
| 50 | wp_send_json_error( array( 'message' => $result->get_error_message() ), 500 ); |
| 51 | } |
| 52 | |
| 53 | // Prevent PPG's own activation redirect from stripping our ppg_ref query param. |
| 54 | delete_transient( 'ppguc_activation_redirect' ); |
| 55 | |
| 56 | wp_send_json_success(); |
| 57 | } |
| 58 | |
| 59 | public function ajax_install_plugin() { |
| 60 | check_ajax_referer( 'cookiebot_ppg_nonce', 'nonce' ); |
| 61 | |
| 62 | if ( ! current_user_can( 'install_plugins' ) ) { |
| 63 | wp_send_json_error( array( 'message' => __( 'You do not have permission to install plugins.', 'cookiebot' ) ), 403 ); |
| 64 | } |
| 65 | |
| 66 | if ( self::is_plugin_installed() ) { |
| 67 | wp_send_json_success(); |
| 68 | } |
| 69 | |
| 70 | require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 71 | require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 72 | |
| 73 | $api = plugins_api( |
| 74 | 'plugin_information', |
| 75 | array( |
| 76 | 'slug' => 'privacy-policy-usercentrics', |
| 77 | 'fields' => array( 'sections' => false ), |
| 78 | ) |
| 79 | ); |
| 80 | |
| 81 | if ( is_wp_error( $api ) ) { |
| 82 | wp_send_json_error( array( 'message' => $api->get_error_message() ), 500 ); |
| 83 | } |
| 84 | |
| 85 | $upgrader = new \Plugin_Upgrader( new \WP_Ajax_Upgrader_Skin() ); |
| 86 | $result = $upgrader->install( $api->download_link ); |
| 87 | |
| 88 | if ( is_wp_error( $result ) || ! $result ) { |
| 89 | wp_send_json_error( array( 'message' => __( 'Plugin installation failed.', 'cookiebot' ) ), 500 ); |
| 90 | } |
| 91 | |
| 92 | wp_send_json_success(); |
| 93 | } |
| 94 | |
| 95 | public function display() { |
| 96 | wp_enqueue_style( |
| 97 | 'cookiebot-ppg-page-css', |
| 98 | asset_url( 'css/backend/ppg_page.css' ), |
| 99 | array(), |
| 100 | Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION |
| 101 | ); |
| 102 | |
| 103 | $is_installed = self::is_plugin_installed(); |
| 104 | $is_active = self::is_plugin_active(); |
| 105 | |
| 106 | $ppg_redirect_url = add_query_arg( |
| 107 | array( |
| 108 | 'page' => 'privacy-policy-usercentrics', |
| 109 | 'ppg_ref' => 'content-distribution', |
| 110 | ), |
| 111 | admin_url( 'admin.php' ) |
| 112 | ); |
| 113 | |
| 114 | if ( ! $is_active ) { |
| 115 | wp_enqueue_script( |
| 116 | 'cookiebot-ppg-page-js', |
| 117 | asset_url( 'js/backend/ppg-page.js' ), |
| 118 | array(), |
| 119 | Cookiebot_WP::COOKIEBOT_PLUGIN_VERSION, |
| 120 | true |
| 121 | ); |
| 122 | |
| 123 | wp_localize_script( |
| 124 | 'cookiebot-ppg-page-js', |
| 125 | 'cookiebot_ppg', |
| 126 | array( |
| 127 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 128 | 'nonce' => wp_create_nonce( 'cookiebot_ppg_nonce' ), |
| 129 | 'redirect_url' => $ppg_redirect_url, |
| 130 | 'i18n' => array( |
| 131 | 'installing' => __( 'Installing...', 'cookiebot' ), |
| 132 | 'install' => __( 'Install Now', 'cookiebot' ), |
| 133 | 'activating' => __( 'Activating...', 'cookiebot' ), |
| 134 | 'activate' => __( 'Activate', 'cookiebot' ), |
| 135 | ), |
| 136 | ) |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | $args = array( |
| 141 | 'hero_image' => asset_url( 'img/ppg-hero.png' ), |
| 142 | 'is_installed' => $is_installed, |
| 143 | 'is_active' => $is_active, |
| 144 | 'ppg_redirect_url' => $ppg_redirect_url, |
| 145 | ); |
| 146 | |
| 147 | include_view( 'admin/common/ppg-page.php', $args ); |
| 148 | } |
| 149 | |
| 150 | private static function is_plugin_installed() { |
| 151 | if ( ! function_exists( 'get_plugins' ) ) { |
| 152 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 153 | } |
| 154 | |
| 155 | $plugins = get_plugins(); |
| 156 | return isset( $plugins[ self::PPG_PLUGIN_SLUG ] ); |
| 157 | } |
| 158 | |
| 159 | private static function is_plugin_active() { |
| 160 | if ( ! function_exists( 'is_plugin_active' ) ) { |
| 161 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 162 | } |
| 163 | |
| 164 | return is_plugin_active( self::PPG_PLUGIN_SLUG ); |
| 165 | } |
| 166 | } |
| 167 |