Notice
2 weeks ago
Activation_Page.php
2 weeks ago
Help_Page.php
2 weeks ago
Helpers.php
2 weeks ago
Live_Date_Preview.php
2 weeks ago
Notices.php
2 weeks ago
Activation_Page.php
335 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Shows a welcome or update message after the plugin is installed/updated. |
| 4 | */ |
| 5 | class Tribe__Admin__Activation_Page { |
| 6 | protected $args = []; |
| 7 | public $update_slug = 'update-message-'; |
| 8 | public $welcome_slug = 'welcome-message-'; |
| 9 | protected $current_context = ''; |
| 10 | |
| 11 | /** |
| 12 | * Handles the update/welcome splash screen. |
| 13 | * |
| 14 | * @param array $args { |
| 15 | * Plugin specific slugs and option names used to manage when the splash screen displays. |
| 16 | * |
| 17 | * @type string $slug |
| 18 | * @type string $version |
| 19 | * @type string $plugin_path |
| 20 | * @type string $version_history_slug |
| 21 | * @type string $update_page_title |
| 22 | * @type string $update_page_template |
| 23 | * @type string $welcome_page_title |
| 24 | * @type string $welcome_page_template |
| 25 | * } |
| 26 | */ |
| 27 | public function __construct( array $args = [] ) { |
| 28 | $this->args = wp_parse_args( $args, [ |
| 29 | 'slug' => '', |
| 30 | 'activation_transient' => '', |
| 31 | 'version' => '', |
| 32 | 'plugin_path' => '', |
| 33 | 'version_history_slug' => '', |
| 34 | 'update_page_title' => '', |
| 35 | 'update_page_template' => '', |
| 36 | 'welcome_page_title' => '', |
| 37 | 'welcome_page_template' => '', |
| 38 | ] ); |
| 39 | |
| 40 | $this->update_slug .= $this->args['slug']; |
| 41 | $this->welcome_slug .= $this->args['slug']; |
| 42 | |
| 43 | $this->hooks(); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Determines if we are currently on the Welcome page. |
| 48 | * |
| 49 | * @since 4.12.11 |
| 50 | * |
| 51 | * @return bool |
| 52 | */ |
| 53 | public function is_welcome_page() { |
| 54 | return isset( $_GET[ $this->welcome_slug ] ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Determines if we are currently on the update page. |
| 59 | * |
| 60 | * @since 4.12.11 |
| 61 | * |
| 62 | * @return bool |
| 63 | */ |
| 64 | public function is_update_page() { |
| 65 | return isset( $_GET[ $this->update_slug ] ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Listen for opportunities to show update and welcome splash pages. |
| 70 | */ |
| 71 | public function hooks() { |
| 72 | if ( |
| 73 | tribe_is_truthy( get_option( 'tribe_skip_welcome', false ) ) |
| 74 | || tribe_is_truthy( tribe_get_option( 'skip_welcome', false ) ) |
| 75 | ) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | add_action( 'admin_init', [ $this, 'maybe_redirect' ], 10, 0 ); |
| 80 | add_action( 'admin_menu', [ $this, 'register_page' ], 100, 0 ); // come in after the default page is registered |
| 81 | |
| 82 | add_action( 'update_plugin_complete_actions', [ $this, 'update_complete_actions' ], 15, 2 ); |
| 83 | add_action( 'update_bulk_plugins_complete_actions', [ $this, 'update_complete_actions' ], 15, 2 ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Filter the Default WordPress actions when updating the plugin to prevent users to be redirected if they have an |
| 88 | * specific intention of going back to the plugins page. |
| 89 | * |
| 90 | * @param array $actions The Array of links (html) |
| 91 | * @param string $plugin Which plugins are been updated |
| 92 | * @return array The filtered Links |
| 93 | */ |
| 94 | public function update_complete_actions( $actions, $plugin ) { |
| 95 | $plugins = []; |
| 96 | |
| 97 | if ( ! empty( $_GET['plugins'] ) ) { |
| 98 | $plugins = explode( ',', esc_attr( $_GET['plugins'] ) ); |
| 99 | } |
| 100 | |
| 101 | if ( ! in_array( $this->args['plugin_path'], $plugins ) ) { |
| 102 | return $actions; |
| 103 | } |
| 104 | |
| 105 | if ( isset( $actions['plugins_page'] ) ) { |
| 106 | $actions['plugins_page'] = '<a href="' . esc_url( self_admin_url( 'plugins.php?tribe-skip-welcome' ) ) . '" title="' . esc_attr__( 'Go to plugins page', 'tribe-common' ) . '" target="_parent">' . esc_html__( 'Return to Plugins page' ) . '</a>'; |
| 107 | |
| 108 | if ( ! current_user_can( 'activate_plugins' ) ) { |
| 109 | unset( $actions['plugins_page'] ); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if ( isset( $actions['updates_page'] ) ) { |
| 114 | $actions['updates_page'] = '<a href="' . esc_url( self_admin_url( 'update-core.php?tribe-skip-welcome' ) ) . '" title="' . esc_attr__( 'Go to WordPress Updates page', 'tribe-common' ) . '" target="_parent">' . esc_html__( 'Return to WordPress Updates' ) . '</a>'; |
| 115 | } |
| 116 | |
| 117 | return $actions; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Maybe redirect to the welcome page (or to the update page - though this is |
| 122 | * currently disabled). |
| 123 | */ |
| 124 | public function maybe_redirect() { |
| 125 | if ( ! empty( $_POST ) ) { |
| 126 | return; // don't interrupt anything the user's trying to do |
| 127 | } |
| 128 | |
| 129 | if ( ! is_admin() || defined( 'DOING_AJAX' ) ) { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | if ( defined( 'IFRAME_REQUEST' ) && IFRAME_REQUEST ) { |
| 134 | return; // probably the plugin update/install iframe |
| 135 | } |
| 136 | |
| 137 | if ( isset( $_GET[ $this->welcome_slug ] ) || isset( $_GET[ $this->update_slug ] ) ) { |
| 138 | return; // no infinite redirects |
| 139 | } |
| 140 | |
| 141 | if ( isset( $_GET['tribe-skip-welcome'] ) ) { |
| 142 | return; // a way to skip these checks and |
| 143 | } |
| 144 | |
| 145 | // bail if we aren't activating a plugin |
| 146 | if ( ! get_transient( $this->args['activation_transient'] ) ) { |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | delete_transient( $this->args['activation_transient'] ); |
| 151 | |
| 152 | if ( ! current_user_can( Tribe__Settings::instance()->requiredCap ) ) { |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | if ( $this->showed_update_message_for_current_version() ) { |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | // the redirect might be intercepted by another plugin, but |
| 161 | // we'll go ahead and mark it as viewed right now, just in case |
| 162 | // we end up in a redirect loop |
| 163 | // see #31088 |
| 164 | $this->log_display_of_message_page(); |
| 165 | |
| 166 | if ( $this->is_new_install() ) { |
| 167 | $this->redirect_to_welcome_page(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Have we shown the welcome/update message for the current version? |
| 173 | * |
| 174 | * @return bool |
| 175 | */ |
| 176 | protected function showed_update_message_for_current_version() { |
| 177 | $message_version_displayed = Tribe__Settings_Manager::get_option( 'last-update-message-' . $this->args['slug'] ); |
| 178 | |
| 179 | if ( empty( $message_version_displayed ) ) { |
| 180 | return false; |
| 181 | } |
| 182 | if ( version_compare( $message_version_displayed, $this->args['version'], '<' ) ) { |
| 183 | return false; |
| 184 | } |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Records the fact that we displayed the update message in relation to a specific |
| 190 | * version of the plugin (so we don't show it again until/unless they update to |
| 191 | * a higher version). |
| 192 | */ |
| 193 | protected function log_display_of_message_page() { |
| 194 | Tribe__Settings_Manager::set_option( 'last-update-message-' . $this->args['slug'], $this->args['version'] ); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * The previous_ecp_versions option will be empty or set to 0 |
| 199 | * if the current version is the first version to be installed. |
| 200 | * |
| 201 | * @return bool |
| 202 | * @see Tribe__Events__Main::maybeSetTECVersion() |
| 203 | */ |
| 204 | protected function is_new_install() { |
| 205 | $previous_versions = Tribe__Settings_Manager::get_option( $this->args['version_history_slug'] ); |
| 206 | return empty( $previous_versions ) || ( end( $previous_versions ) == '0' ); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Handles taking a user to a post-installation welcome page. |
| 211 | */ |
| 212 | protected function redirect_to_welcome_page() { |
| 213 | $url = $this->get_message_page_url( $this->welcome_slug ); |
| 214 | wp_safe_redirect( $url ); |
| 215 | exit(); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Handles taking the user to a post-update splash screen. |
| 220 | * |
| 221 | * Disused since TEC PR 88 (targeting Tribe__Events__Activation_Page, |
| 222 | * which this class was derived from). |
| 223 | * |
| 224 | * @see https://github.com/the-events-calendar/the-events-calendar/pull/88 |
| 225 | * |
| 226 | * @todo decide whether to reinstate or remove |
| 227 | */ |
| 228 | protected function redirect_to_update_page() { |
| 229 | $url = $this->get_message_page_url( $this->update_slug ); |
| 230 | wp_safe_redirect( $url ); |
| 231 | exit(); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Return the URL of the splash page. |
| 236 | * |
| 237 | * @param string $slug |
| 238 | * |
| 239 | * @return string |
| 240 | */ |
| 241 | protected function get_message_page_url( $slug ) { |
| 242 | $settings = Tribe__Settings::instance(); |
| 243 | // get the base settings page url |
| 244 | $url = apply_filters( |
| 245 | 'tribe_settings_url', |
| 246 | add_query_arg( 'page', $settings->adminSlug, admin_url( 'edit.php' ) ) |
| 247 | ); |
| 248 | $url = esc_url_raw( add_query_arg( $slug, 1, $url ) ); |
| 249 | return $url; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Dynamically registers the splash page when required. |
| 254 | */ |
| 255 | public function register_page() { |
| 256 | if ( isset( $_GET[ $this->welcome_slug ] ) ) { |
| 257 | $this->current_context = 'welcome'; |
| 258 | } elseif ( isset( $_GET[ $this->update_slug ] ) ) { |
| 259 | $this->current_context = 'update'; |
| 260 | } else { |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | $this->disable_default_settings_page(); |
| 265 | add_filter( 'admin_body_class', [ $this, 'admin_body_class' ] ); |
| 266 | add_action( Tribe__Settings::instance()->admin_page, [ $this, 'display_page' ] ); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Hooked to admin_body_class to add a class for the update or welcome page |
| 271 | * |
| 272 | * @param string $classes a space separated string of classes to be added to body |
| 273 | * |
| 274 | * @return string |
| 275 | */ |
| 276 | public function admin_body_class( $classes ) { |
| 277 | $classes .= ' tribe-' . $this->current_context; |
| 278 | return $classes; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Deactivates the regular settings screen (the splash screen will display |
| 283 | * in the Events > Settings slot instead, for this request only). |
| 284 | */ |
| 285 | protected function disable_default_settings_page() { |
| 286 | remove_action( Tribe__Settings::instance()->admin_page, [ Tribe__Settings::instance(), 'generatePage' ] ); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Prints the splash screen. |
| 291 | * |
| 292 | * @param string $context |
| 293 | * |
| 294 | * @return string|null |
| 295 | */ |
| 296 | public function display_page() { |
| 297 | if ( empty( $this->args[ $this->current_context . '_page_title' ] ) || empty( $this->args[ $this->current_context . '_page_template'] ) ) { |
| 298 | return null; |
| 299 | } |
| 300 | |
| 301 | do_action( 'tribe_settings_top' ); |
| 302 | |
| 303 | $context = isset( $_GET[ $this->welcome_slug ] ) ? 'welcome': 'update'; |
| 304 | $title = esc_html( $this->args[ $context . '_page_title'] ); |
| 305 | $html = $this->get_view( $this->args[ $context . '_page_template'] ); |
| 306 | |
| 307 | echo " |
| 308 | <div class='tribe_settings tribe_{$context}_page wrap'> |
| 309 | <h1> {$title} </h1> |
| 310 | {$html} |
| 311 | </div> |
| 312 | "; |
| 313 | |
| 314 | do_action( 'tribe_settings_bottom' ); |
| 315 | $this->log_display_of_message_page(); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Returns the output of the specified template. |
| 320 | * |
| 321 | * @param string $path |
| 322 | * |
| 323 | * @return string |
| 324 | */ |
| 325 | protected function get_view( $path ) { |
| 326 | if ( ! file_exists( $path ) ) { |
| 327 | return ''; |
| 328 | } |
| 329 | |
| 330 | ob_start(); |
| 331 | include $path; |
| 332 | return ob_get_clean(); |
| 333 | } |
| 334 | } |
| 335 |