setup_otter_notice(); } /** * Setup the Otter Blocks notice. * * @return void */ public function setup_otter_notice() { add_action( 'admin_notices', array( $this, 'render_welcome_notice' ), 0 ); add_action( 'wp_ajax_raft_dismiss_welcome_notice', array( $this, 'remove_welcome_notice' ) ); add_action( 'wp_ajax_raft_set_otter_ref', array( $this, 'set_otter_ref' ) ); } /** * Render the welcome notice. * * @return void */ public function render_welcome_notice() { if ( ! $this->should_show_welcome_notice() ) { return; } $otter_status = $this->get_otter_status(); Assets_Manager::enqueue_style( Assets_Manager::ASSETS_SLUGS['welcome-notice'], 'welcome-notice' ); Assets_Manager::enqueue_script( Assets_Manager::ASSETS_SLUGS['welcome-notice'], 'welcome-notice', true, array(), array( 'nonce' => wp_create_nonce( 'raft-dismiss-welcome-notice' ), 'otterRefNonce' => wp_create_nonce( 'raft-set-otter-ref' ), 'ajaxUrl' => esc_url( admin_url( 'admin-ajax.php' ) ), 'otterStatus' => $otter_status, 'activationUrl' => esc_url( add_query_arg( array( 'plugin_status' => 'all', 'paged' => '1', 'action' => 'activate', 'plugin' => rawurlencode( 'otter-blocks/otter-blocks.php' ), '_wpnonce' => wp_create_nonce( 'activate-plugin_otter-blocks/otter-blocks.php' ), ), admin_url( 'plugins.php' ) ) ), 'activating' => __( 'Activating', 'raft' ) . '…', 'installing' => __( 'Installing', 'raft' ) . '…', 'done' => __( 'Done', 'raft' ), ) ); $notice_html = '
'; $notice_html .= ''; $notice_html .= '
'; $notice_html .= '' . __( 'Otter Blocks preview', 'raft' ) . ''; $notice_html .= '
'; $notice_html .= '

'; /* translators: %s: Otter Blocks */ $notice_html .= sprintf( __( 'Power up your website building experience with %s!', 'raft' ), 'Otter Blocks' ); $notice_html .= '

'; $notice_html .= '

' . __( 'Otter is a Gutenberg Blocks page builder plugin that adds extra functionality to the WordPress Block Editor (also known as Gutenberg) for a better page building experience without the need for traditional page builders.', 'raft' ) . '

'; $notice_html .= '
'; /* translators: %s: Otter Blocks */ $notice_html .= ''; $notice_html .= ''; $notice_html .= '' . __( 'Learn More', 'raft' ) . ''; $notice_html .= ''; $notice_html .= ''; $notice_html .= '
'; $notice_html .= '
'; $notice_html .= '
'; $notice_html .= '
'; echo wp_kses_post( $notice_html ); } /** * Dismiss the welcome notice. * * @return void */ public function remove_welcome_notice() { if ( ! isset( $_POST['nonce'] ) ) { return; } if ( ! wp_verify_nonce( sanitize_text_field( $_POST['nonce'] ), 'raft-dismiss-welcome-notice' ) ) { return; } update_option( Constants::CACHE_KEYS['dismissed-welcome-notice'], 'yes' ); wp_die(); } /** * Update Otter reference key. * * @return void */ public function set_otter_ref() { if ( empty( $_POST['nonce'] ) ) { return; } if ( ! wp_verify_nonce( sanitize_text_field( $_POST['nonce'] ), 'raft-set-otter-ref' ) ) { return; } update_option( self::OTTER_REF, 'raft' ); wp_send_json_success(); } /** * Should we show the welcome notice? * * @return bool */ private function should_show_welcome_notice(): bool { // Already using Otter. if ( is_plugin_active( 'otter-blocks/otter-blocks.php' ) ) { return false; } // Notice was dismissed. if ( get_option( Constants::CACHE_KEYS['dismissed-welcome-notice'], 'no' ) === 'yes' ) { return false; } $screen = get_current_screen(); // Only show in dashboard/themes. if ( ! in_array( $screen->id, array( 'dashboard', 'themes' ) ) ) { return false; } // AJAX actions. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { return false; } // Don't show in network admin. if ( is_network_admin() ) { return false; } // User can't dismiss. We don't show it. if ( ! current_user_can( 'manage_options' ) ) { return false; } // User can't install plugins. We don't show it. if ( ! current_user_can( 'install_plugins' ) ) { return false; } // Block editor context. if ( $screen->is_block_editor() ) { return false; } // Dismiss after one week from activation. $activated_time = get_option( 'raft_install' ); if ( ! empty( $activated_time ) && time() - intval( $activated_time ) > WEEK_IN_SECONDS ) { update_option( Constants::CACHE_KEYS['dismissed-welcome-notice'], 'yes' ); return false; } return true; } /** * Get the Otter Blocks plugin status. * * @return string */ private function get_otter_status(): string { $status = 'not-installed'; if ( file_exists( ABSPATH . 'wp-content/plugins/otter-blocks/otter-blocks.php' ) ) { return 'installed'; } return $status; } }