name. * * @return array */ private static function plugin_family(): array { return array( 'all-in-one-utilities/all-in-one-utilities.php' => 'All-in-One Utilities', 'all-in-one-utilities-pro/all-in-one-utilities-pro.php' => 'All-in-One Utilities Pro', 'all-in-one-utilities-woo-addon/all-in-one-utilities-woo-addon.php' => 'All-in-One Utilities Woo Add-on', 'all-in-one-utilities-tools/all-in-one-utilities-tools.php' => 'All-in-One Utilities Tools', 'all-in-one-utilities-chat/all-in-one-utilities-chat.php' => 'All-in-One Utilities Chat', ); } /** * Hook registration. * * @return void */ public function register(): void { add_action( 'admin_menu', array( $this, 'menu' ), 20 ); add_action( 'admin_post_' . self::ACTION, array( $this, 'handle' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'assets' ) ); } /** * The submenu. * * @return void */ public function menu(): void { add_submenu_page( Menu::SLUG, __( 'Request Setup', 'all-in-one-utilities' ), __( 'Request Setup', 'all-in-one-utilities' ), 'manage_options', self::PAGE, array( $this, 'render' ) ); } /** * The tag input's script, on this page only. * * @param string $hook_suffix Current admin page. * @return void */ public function assets( string $hook_suffix ): void { if ( false === strpos( $hook_suffix, self::PAGE ) ) { return; } wp_enqueue_script( 'aiou-setup-request', AIOU_URL . 'assets/js/setup-request.js', array(), Menu::asset_version( 'assets/js/setup-request.js' ), true ); wp_localize_script( 'aiou-setup-request', 'aiouSetupRequest', array( 'placeholder' => __( 'Type to add a plugin…', 'all-in-one-utilities' ), /* translators: %s: plugin name */ 'remove' => __( 'Remove %s', 'all-in-one-utilities' ), 'none' => __( 'No more plugins to add.', 'all-in-one-utilities' ), 'noMatch' => __( 'No installed plugin matches.', 'all-in-one-utilities' ), 'required' => __( 'Choose at least one plugin to configure.', 'all-in-one-utilities' ), ) ); } /** * The family plugins installed on this site. * * @return array Keyed by a short id. */ public static function installed(): array { if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $plugins = get_plugins(); $found = array(); foreach ( self::plugin_family() as $plugin_file => $name ) { if ( isset( $plugins[ $plugin_file ] ) ) { $found[ sanitize_key( dirname( $plugin_file ) ) ] = array( 'name' => $name, 'version' => (string) $plugins[ $plugin_file ]['Version'], 'active' => is_plugin_active( $plugin_file ), ); } } return $found; } /** * The site details sent with a request -- shown on the page first, so * the admin sees exactly what is shared. No passwords, keys or content. * * @return array Label => value. */ public static function site_details(): array { global $wp_version; $theme = wp_get_theme(); $plugins = array(); foreach ( self::installed() as $plugin ) { $plugins[] = $plugin['name'] . ' ' . $plugin['version'] . ( $plugin['active'] ? '' : ' (' . __( 'inactive', 'all-in-one-utilities' ) . ')' ); } $details = array( __( 'Site name', 'all-in-one-utilities' ) => wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), __( 'Site address', 'all-in-one-utilities' ) => home_url(), __( 'WordPress', 'all-in-one-utilities' ) => (string) $wp_version . ( is_multisite() ? ' (' . __( 'multisite', 'all-in-one-utilities' ) . ')' : '' ), __( 'PHP', 'all-in-one-utilities' ) => PHP_VERSION, __( 'Language', 'all-in-one-utilities' ) => get_locale(), __( 'Theme', 'all-in-one-utilities' ) => $theme->get( 'Name' ) . ' ' . $theme->get( 'Version' ), __( 'WooCommerce', 'all-in-one-utilities' ) => defined( 'WC_VERSION' ) ? (string) WC_VERSION : __( 'not active', 'all-in-one-utilities' ), __( 'Active plugins', 'all-in-one-utilities' ) => (string) count( (array) get_option( 'active_plugins', array() ) ), __( 'Installed plugins', 'all-in-one-utilities' ) => implode( ', ', $plugins ), ); return $details; } /** * Render the page. * * @return void */ public function render(): void { $installed = self::installed(); $user = wp_get_current_user(); $notice = get_transient( 'aiou_setup_notice_' . get_current_user_id() ); $draft = is_array( $notice ) && ! empty( $notice['draft'] ) ? (array) $notice['draft'] : array(); $draft = wp_parse_args( $draft, array( 'name' => $user->display_name, 'email' => $user->user_email, 'plugins' => array(), 'message' => '', 'consent' => false, ) ); delete_transient( 'aiou_setup_notice_' . get_current_user_id() ); AdminShell::page_start( array( 'title' => __( 'Request Setup', 'all-in-one-utilities' ), 'description' => __( 'Finding the setup hard? Ask us to install and configure the plugins for you. We reply with an estimate of the hours needed, and start only once you approve it.', 'all-in-one-utilities' ), 'class' => 'aiou-setup', ) ); ?>

' . esc_html( SupportRequest::support_email() ) . '' ); ?>

403 ) ); } check_admin_referer( self::ACTION ); $installed = self::installed(); $name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : ''; $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; $message = isset( $_POST['message'] ) ? sanitize_textarea_field( wp_unslash( $_POST['message'] ) ) : ''; $consent = ! empty( $_POST['consent'] ); $plugins = isset( $_POST['plugins'] ) ? array_map( 'sanitize_key', (array) wp_unslash( $_POST['plugins'] ) ) : array(); $plugins = array_values( array_intersect( $plugins, array_keys( $installed ) ) ); // Only plugins installed here. $draft = array( 'name' => $name, 'email' => $email, 'plugins' => $plugins, 'message' => $message, 'consent' => $consent, ); if ( '' === $name ) { $this->back( 'error', __( 'Enter your name.', 'all-in-one-utilities' ), $draft ); } if ( ! is_email( $email ) ) { $this->back( 'error', __( 'Enter a valid email address for the reply.', 'all-in-one-utilities' ), $draft ); } if ( ! $plugins ) { $this->back( 'error', __( 'Choose at least one plugin to configure.', 'all-in-one-utilities' ), $draft ); } if ( '' === trim( $message ) ) { $this->back( 'error', __( 'Describe what you need set up.', 'all-in-one-utilities' ), $draft ); } if ( mb_strlen( $message ) > self::MAX_MESSAGE ) { /* translators: %d: maximum number of characters */ $this->back( 'error', sprintf( __( 'The message is too long (%d characters at most).', 'all-in-one-utilities' ), self::MAX_MESSAGE ), $draft ); } if ( ! $consent ) { $this->back( 'error', __( 'Tick the box to agree to share the site details, so we can estimate the work.', 'all-in-one-utilities' ), $draft ); } $lock = 'aiou_setup_sent_' . get_current_user_id(); if ( get_transient( $lock ) ) { $this->back( 'error', __( 'A request was just sent. Please wait a minute before sending another.', 'all-in-one-utilities' ), $draft ); } $names = array(); foreach ( $plugins as $id ) { $names[] = $installed[ $id ]['name']; } $details = array(); foreach ( self::site_details() as $label => $value ) { $details[] = $label . ': ' . $value; } $site = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ); $body = implode( "\n", array( 'Setup request', '', 'From: ' . $name . ' <' . $email . '>', 'Plugins to configure: ' . implode( ', ', $names ), '', 'What they need:', $message, '', '-- ', 'Site details (shared with the requester\'s consent):', implode( "\n", $details ), ) ); $sent = wp_mail( SupportRequest::support_email(), '[' . $site . '] Setup request: ' . implode( ', ', $names ), $body, array( 'Reply-To: ' . $name . ' <' . $email . '>' ) ); if ( ! $sent ) { $this->back( 'error', __( 'The request could not be sent. Check that this site can send email (see the Email Log), then try again.', 'all-in-one-utilities' ), $draft ); } set_transient( $lock, 1, self::COOLDOWN ); $this->back( 'success', sprintf( /* translators: %s: the requester's email address */ __( 'Request sent. We\'ll reply to %s with an estimate of the hours needed; nothing starts until you approve it.', 'all-in-one-utilities' ), $email ) ); } /** * Store a one-time notice (and the draft, after an error) and go back. * * @param string $type 'success' or 'error'. * @param string $message Notice text. * @param array $draft What was typed, to show again. * @return never */ private function back( string $type, string $message, array $draft = array() ) { set_transient( 'aiou_setup_notice_' . get_current_user_id(), array( 'type' => $type, 'message' => $message, 'draft' => $draft, ), 5 * MINUTE_IN_SECONDS ); wp_safe_redirect( admin_url( 'admin.php?page=' . self::PAGE . '#aiou-setup-notice' ) ); exit; } }