settings = $settings; $this->pages = $pages; } /** * Register menu. * * @return void */ public function register_menu() { $this->hook_suffix = add_menu_page( __( 'QuicklyFly', 'quicklyfly' ), __( 'QuicklyFly', 'quicklyfly' ), QuicklyFly_Admin_Ajax::CAPABILITY, self::MENU_SLUG, array( $this->pages, 'render' ), QUICKLYFLY_URL . 'assets/images/quicklyfly-menu.png', 81 ); // Runs before anything is printed: the Freemius SDK's preparation for the screen being shown. add_action( 'load-' . $this->hook_suffix, array( 'QuicklyFly_License', 'prepare_screen' ) ); } /** * Sends Freemius page addresses (pricing, contact, account) to QuicklyFly's own screens. * * @return void */ public function redirect_freemius_pages() { QuicklyFly_License::redirect_freemius_pages(); } /** * Whether the current request is a QuicklyFly screen (the settings or one of its embedded screens). * * @return bool */ private function is_quicklyfly_screen() { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : ''; return self::MENU_SLUG === $page; } /** * Keeps every other plugin's, theme's and WordPress' admin notices off QuicklyFly screens, so they can * never push the layout around or cover the Upgrade, Contact us and Account screens. * Runs after the admin header is built and before WordPress prints the notices. * * @return void */ public function silence_notices() { if ( ! $this->is_quicklyfly_screen() ) { return; } remove_all_actions( 'admin_notices' ); remove_all_actions( 'all_admin_notices' ); remove_all_actions( 'network_admin_notices' ); remove_all_actions( 'user_admin_notices' ); } /** * Marks QuicklyFly screens, so the stylesheet can hide notices that arrive another way * (printed in the footer or inserted by scripts). * * @param string $classes Space separated body classes. * @return string */ public function body_class( $classes ) { if ( $this->is_quicklyfly_screen() ) { $classes .= ' quicklyfly-screen'; } return $classes; } /** * Opens the settings once, right after the plugin is activated from the Plugins screen. * Bulk activation, network admin and ajax requests are left alone. * * @return void */ public function maybe_redirect_after_activation() { global $pagenow; // Only the request WordPress sends right after activating from the Plugins screen (plugins.php?activate=true) may // redirect. Background requests (ajax, REST, cron, POST, Plugin Check and similar tools) must get their own // response untouched, and must not use up the flag either. $method = isset( $_SERVER['REQUEST_METHOD'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_METHOD'] ) ) : 'GET'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- only reads the flag WordPress adds after activation. if ( 'plugins.php' !== $pagenow || ! isset( $_GET['activate'] ) || 'GET' !== $method ) { return; } if ( wp_doing_ajax() || wp_doing_cron() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { return; } if ( ! get_transient( self::REDIRECT_TRANSIENT ) ) { return; } delete_transient( self::REDIRECT_TRANSIENT ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- only reads a core flag set by WordPress after a bulk activation. if ( is_network_admin() || isset( $_GET['activate-multi'] ) || ! current_user_can( QuicklyFly_Admin_Ajax::CAPABILITY ) ) { return; } wp_safe_redirect( admin_url( 'admin.php?page=' . self::MENU_SLUG ) ); exit; } /** * Queues the frame guard, which hides the admin chrome on ANY admin page that loads inside one of QuicklyFly's * frames (Account, Contact, Upgrade). The URL parameter is not enough: Freemius redirects (disconnect, connect, * sync) drop it, and the frame would then show the whole wp-admin. * * @return void */ private function enqueue_frame_guard() { if ( ! current_user_can( QuicklyFly_Admin_Ajax::CAPABILITY ) ) { return; } // Loaded in the head so the frame never flashes the admin chrome. wp_enqueue_script( 'quicklyfly-frame-guard', QUICKLYFLY_URL . 'assets/js/quicklyfly-frame-guard.js', array(), QUICKLYFLY_ASSET_VER, false ); wp_enqueue_style( 'quicklyfly-frame-guard', QUICKLYFLY_URL . 'assets/css/quicklyfly-frame-guard.css', array(), QUICKLYFLY_ASSET_VER ); } /** * Adds a Settings link on the Plugins screen. * * @param string[] $links Existing action links. * @return string[] */ public function action_links( $links ) { $url = admin_url( 'admin.php?page=' . self::MENU_SLUG ); array_unshift( $links, '' . esc_html__( 'Settings', 'quicklyfly' ) . '' ); return $links; } /** * Enqueue assets. * * @param string $hook_suffix Current admin page hook. * @return void */ public function enqueue_assets( $hook_suffix ) { $this->enqueue_frame_guard(); // The notice guard: a small stylesheet loaded on every QuicklyFly screen. if ( $this->is_quicklyfly_screen() ) { wp_enqueue_style( 'quicklyfly-notices', QUICKLYFLY_URL . 'assets/css/quicklyfly-notices.css', array(), QUICKLYFLY_ASSET_VER ); } // The Freemius screens (upgrade, contact, account) show without the admin menu and bar, as they sit inside the settings. // phpcs:ignore WordPress.Security.NonceVerification.Recommended $screen = isset( $_GET['quicklyfly_screen'] ) ? sanitize_key( wp_unslash( $_GET['quicklyfly_screen'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $embedded = isset( $_GET['quicklyfly_embed'] ) || '' !== $screen; if ( $embedded ) { wp_enqueue_style( 'quicklyfly-embed', QUICKLYFLY_URL . 'assets/css/quicklyfly-embed.css', array(), QUICKLYFLY_ASSET_VER ); } if ( current_user_can( QuicklyFly_Admin_Ajax::CAPABILITY ) ) { wp_enqueue_style( 'quicklyfly-admin-menu' ); } // Instant navigation never runs inside a framed Freemius screen or on the Freemius Account page. if ( ! $embedded && '' === $screen && current_user_can( QuicklyFly_Admin_Ajax::CAPABILITY ) && $this->settings->get( 'admin_ajax_navigation' ) ) { $this->enqueue_admin_nav(); } if ( $hook_suffix !== $this->hook_suffix ) { return; } wp_enqueue_style( 'quicklyfly-dashboard' ); wp_enqueue_script( 'quicklyfly-dashboard' ); wp_add_inline_script( 'quicklyfly-admin', 'window.quicklyflyAdmin = ' . wp_json_encode( $this->pages->app_data() ) . ';', 'before' ); /** * Fires once the settings screen assets are queued, so add-ons can queue theirs. */ do_action( 'quicklyfly_enqueue_admin' ); } /** * Enqueue admin nav. * * @return void */ private function enqueue_admin_nav() { /** * Filters admin URL fragments that must always load normally. * Matched against the path and query string of each link. * * @param string[] $patterns URL fragments. */ $patterns = apply_filters( 'quicklyfly_admin_bypass_patterns', array_merge( array( 'update.php', 'update-core.php', 'options.php', 'admin-post.php', 'admin-ajax.php', 'customize.php', 'plugin-install.php', 'theme-install.php', 'plugin-editor.php', 'theme-editor.php', 'export.php', 'import.php', 'post.php', 'post-new.php', 'site-editor.php', 'widgets.php', 'authorize-application.php', 'wp-login.php', 'page=quicklyfly-', 'quicklyfly_screen=', 'quicklyfly_account=', 'action=', '_wpnonce=', ), (array) $this->settings->get( 'admin_excluded', array() ) ) ); wp_enqueue_style( 'quicklyfly-admin-nav' ); wp_enqueue_script( 'quicklyfly-admin-nav' ); // The same loading indicator visitors get, while an admin screen loads. if ( 'none' !== $this->settings->get( 'loader_style' ) ) { wp_enqueue_style( 'quicklyfly-loaders' ); wp_enqueue_script( 'quicklyfly-loader' ); wp_add_inline_script( 'quicklyfly-lifecycle', 'window.quicklyflyConfig = ' . wp_json_encode( /** * Filters the loading indicator configuration used inside wp-admin. * * @param array $config Indicator configuration. */ apply_filters( 'quicklyfly_admin_loader_config', array( 'loaderStyle' => $this->settings->get( 'loader_style' ), 'loaderDelay' => (int) $this->settings->get( 'loader_delay' ), 'loaderOverlay' => (bool) $this->settings->get( 'loader_overlay' ), 'loadingLabel' => __( 'Loading', 'quicklyfly' ), 'progressColor' => $this->settings->get( 'progress_color' ), ) ) ) . ';', 'before' ); do_action( 'quicklyfly_enqueue_admin_nav' ); } wp_add_inline_script( 'quicklyfly-admin-nav', 'window.quicklyflyAdminNav = ' . wp_json_encode( array( 'adminUrl' => admin_url( '/' ), 'progressBar' => (bool) $this->settings->get( 'progress_bar' ), 'progressColor' => $this->settings->get( 'progress_color' ), 'hoverPreload' => (bool) $this->settings->get( 'admin_hover_preload' ), 'bypass' => array_values( array_filter( (array) $patterns, 'strlen' ) ), ) ) . ';', 'before' ); } }