= $final_week && $dismissed < $final_week; } /** * Whether the WordPress notice shows on this request: Dashboard and * Plugins only. The AjaxPress settings app shows its own banner. * * @return bool */ public function should_show(): bool { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( ! $screen || ! in_array( $screen->id, array( 'dashboard', 'plugins' ), true ) ) { return false; } return self::is_visible_for_user(); } /** * What the settings app needs to render its banner, or null to render none. * * @return array|null */ public static function app_data() { if ( ! self::is_visible_for_user() ) { return null; } $action = self::action(); return array( 'installed' => $action['installed'], 'action_url' => $action['url'], 'action_label' => $action['label'], 'steps' => $action['steps'], 'cutoff' => self::cutoff_label(), 'account_url' => self::ACCOUNT_URL, 'failed' => self::download_failed(), ); } /** * The one thing to do next: download Pro, or activate it if it is already here. * * @return array { installed: bool, url: string, label: string, steps: string } */ private static function action(): array { if ( file_exists( WP_PLUGIN_DIR . '/' . self::PRO_FILE ) ) { return array( 'installed' => true, // Built unescaped (wp_nonce_url() returns &): the settings app // uses it as a raw URL, and the notice escapes it on output. 'url' => add_query_arg( '_wpnonce', wp_create_nonce( 'activate-plugin_' . self::PRO_FILE ), self_admin_url( 'plugins.php?action=activate&plugin=' . rawurlencode( self::PRO_FILE ) ) ), 'label' => __( 'Activate AjaxPress Pro', 'ajaxpress' ), 'steps' => __( 'AjaxPress Pro is already installed. Activate it and you are done: your license and settings carry over.', 'ajaxpress' ), ); } return array( 'installed' => false, 'url' => add_query_arg( '_wpnonce', wp_create_nonce( 'ajaxpress_download_pro' ), admin_url( 'admin-post.php?action=ajaxpress_download_pro' ) ), 'label' => __( 'Download AjaxPress Pro', 'ajaxpress' ), 'steps' => __( 'Download it, then go to Plugins → Add New → Upload Plugin and activate it. Your license and settings carry over, with nothing to enter.', 'ajaxpress' ), ); } /** * Whether the page was reached from a download that could not start. * * @return bool */ private static function download_failed(): bool { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display-only flag set by our own redirect. return isset( $_GET['ajaxpress_pro_download'] ) && 'failed' === sanitize_key( wp_unslash( $_GET['ajaxpress_pro_download'] ) ); } /** * The cutoff date, in the site's date format. * * @return string */ private static function cutoff_label(): string { return date_i18n( get_option( 'date_format', 'F j, Y' ), strtotime( self::CUTOFF_DATE . ' 12:00:00 UTC' ) ); } /** * Render the notice. * * @return void */ public function maybe_render(): void { if ( ! $this->should_show() ) { return; } $action = self::action(); $dismiss = wp_nonce_url( admin_url( 'admin-post.php?action=ajaxpress_dismiss_pro_notice' ), 'ajaxpress_dismiss_pro_notice' ); ?>
self::PRO_SLUG, 'license_key' => $license['key'], 'site' => site_url(), ), AJAXPRESS_PACKEDGE_API . '/public/v1/wp/update-check' ), array( 'timeout' => 10, 'headers' => array( 'X-Public-Key' => AJAXPRESS_PACKEDGE_LICENSE_KEY ), ) ); if ( ! is_wp_error( $response ) ) { $data = json_decode( wp_remote_retrieve_body( $response ), true ); $package = is_array( $data ) && ! empty( $data['package'] ) ? (string) $data['package'] : ''; } } if ( '' === $package ) { $back = wp_get_referer() ? wp_get_referer() : admin_url(); wp_safe_redirect( add_query_arg( 'ajaxpress_pro_download', 'failed', $back ) ); exit; } // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect -- PackEdge's download host, returned by PackEdge itself. wp_redirect( $package ); exit; } /** * REST route the settings app dismisses its banner through, so the app * does not have to reload. * * @return void */ public function register_route(): void { register_rest_route( 'ajaxpress', 'pro-notice/dismiss', array( 'methods' => 'POST', 'permission_callback' => function () { return current_user_can( 'manage_options' ); }, 'callback' => function () { update_user_meta( get_current_user_id(), self::DISMISS_META, time() ); return rest_ensure_response( array( 'success' => true ) ); }, ) ); } /** * Hide the notice for this user. * * @return void */ public function dismiss(): void { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to do that.', 'ajaxpress' ), 403 ); } check_admin_referer( 'ajaxpress_dismiss_pro_notice' ); update_user_meta( get_current_user_id(), self::DISMISS_META, time() ); wp_safe_redirect( remove_query_arg( 'ajaxpress_pro_download', wp_get_referer() ? wp_get_referer() : admin_url() ) ); exit; } } new Pro_Notice(); }