'contacts', 'segments' => 'contacts', ); /** * Hook suffix of Mailpuff's page, once registered. * * @var string */ private static $hook = ''; /** * Hook suffix of Mailpuff's page ('' before admin_menu). * * @return string */ public static function page_hook() { return self::$hook; } /** * Register Mailpuff's page and the hidden editor. */ public function register_menus() { self::$hook = (string) add_menu_page( __( 'Mailpuff', 'mailpuff' ), __( 'Mailpuff', 'mailpuff' ), self::CAPABILITY, self::PAGE_SLUG, array( $this, 'render_page' ), self::menu_icon(), '58.14' ); // The app's sections as WordPress submenus. Home is the page itself; // the others are links to the same page with `tab=…` (no callback, so // WordPress prints the slug as the link). The app moves between them // without a reload and keeps the highlight in step // (src/admin/shell/wp-menu.js); `current_submenu()` marks the right // one on a fresh load. add_submenu_page( self::PAGE_SLUG, __( 'Home', 'mailpuff' ), __( 'Home', 'mailpuff' ), self::CAPABILITY, self::PAGE_SLUG, array( $this, 'render_page' ) ); foreach ( self::sections() as $tab => $label ) { add_submenu_page( self::PAGE_SLUG, $label, $label, self::CAPABILITY, self::section_slug( $tab ), '' ); } $hook = add_submenu_page( '', __( 'Email editor', 'mailpuff' ), '', self::CAPABILITY, self::EDITOR_SLUG, array( $this, 'render_editor' ) ); if ( $hook ) { add_action( 'load-' . $hook, static function () { // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- WordPress reads the screen's title from it; a hidden submenu page has none. $GLOBALS['title'] = __( 'Email editor', 'mailpuff' ); // WordPress's emoji script swaps each emoji on the page for // an image; inside the editor that drops the emoji from the // email ("With ❤️," became "With ,"). Browsers draw emoji // themselves. remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); remove_action( 'admin_print_styles', 'print_emoji_styles' ); } ); } } /** * The sections after Home, as `tab => label`, in menu order. They match * the app's own (src/admin/shell/nav.js). Tags and segments live in * Subscribers' side column, not in the menu. * * @return array */ public static function sections() { return array( 'campaigns' => __( 'Campaigns', 'mailpuff' ), 'contacts' => __( 'Subscribers', 'mailpuff' ), 'forms' => __( 'Forms', 'mailpuff' ), 'settings' => __( 'Settings', 'mailpuff' ), ); } /** * A section's submenu slug, which WordPress prints as its link. * * @param string $tab Section id. * @return string */ public static function section_slug( $tab ) { return 'admin.php?page=' . self::PAGE_SLUG . '&tab=' . $tab; } /** * Mark the open section's submenu on a fresh load of Mailpuff's page. * * @param string|null $submenu_file The submenu WordPress would mark. * @param string $parent_file The open top-level menu. * @return string|null */ public function current_submenu( $submenu_file, $parent_file ) { if ( self::PAGE_SLUG !== $parent_file ) { return $submenu_file; } // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Read-only: which menu item to highlight. if ( self::EDITOR_SLUG === sanitize_key( wp_unslash( $_GET['page'] ?? '' ) ) ) { // A campaign is written under Campaigns; the signup emails are // Settings, Emails. return self::section_slug( empty( $_GET['doc'] ) ? 'campaigns' : 'settings' ); } $tab = sanitize_key( wp_unslash( $_GET['tab'] ?? '' ) ); // phpcs:enable WordPress.Security.NonceVerification.Recommended $tab = self::MOVED[ $tab ] ?? $tab; return isset( self::sections()[ $tab ] ) ? self::section_slug( $tab ) : self::PAGE_SLUG; } /** * The email editor is a hidden page; while it is open, Mailpuff's menu * stays open, as on every other Mailpuff page. * * WordPress looks the parent up again right after this filter * (get_admin_page_parent()), finds the hidden page under no menu and * closes Mailpuff's. By now the page has been allowed and its title * set, so the hidden entry leaves the menu list here. * * @param string $parent_file The open top-level menu. * @return string */ public function editor_parent( $parent_file ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only: which menu to open. if ( self::EDITOR_SLUG !== sanitize_key( wp_unslash( $_GET['page'] ?? '' ) ) ) { return $parent_file; } remove_submenu_page( '', self::EDITOR_SLUG ); return self::PAGE_SLUG; } /** * Mailpuff's one-colour puffin, from the shared brand geometry. * WordPress recolors the single filled path to match the admin menu. * * @return string */ public static function menu_icon() { $mark = self::mark(); $svg = sprintf( '', esc_attr( $mark['viewBox'] ), esc_attr( $mark['path'] ) ); return 'data:image/svg+xml;base64,' . base64_encode( $svg ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- an inline SVG icon. } /** * The puffin's geometry, from the shared brand source. * * @return array{viewBox:string,path:string} */ private static function mark() { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- A file inside the plugin, not a URL. return json_decode( file_get_contents( MAILPUFF_PLUGIN_DIR . 'assets/brand/mark.json' ), true ); } /** * Whether Mailpuff's page is the open screen. * * @return bool */ private static function is_open() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; return $screen && '' !== self::$hook && self::$hook === $screen->id; } /** * The left of WordPress's footer on Mailpuff's page: the puffin and * Mailpuff's thank-you, in place of WordPress's. Other pages keep theirs. * The look is in {@see Mailpuff_Assets::enqueue_page()}. * * @param string $text The footer's left side. * @return string */ public function footer_text( $text ) { if ( ! self::is_open() ) { return $text; } $mark = self::mark(); return sprintf( '%3$s', esc_attr( $mark['viewBox'] ), esc_attr( $mark['path'] ), esc_html__( 'Thank you for writing with Mailpuff.', 'mailpuff' ) ); } /** * The right of WordPress's footer on Mailpuff's page: Help, Get support * and Mailpuff's version, in place of WordPress's version. Hooked after * core_update_footer(). * * @param string $text The footer's right side. * @return string */ public function footer_version( $text ) { if ( ! self::is_open() ) { return $text; } return sprintf( '%2$s%4$s %5$s%6$s', esc_url( admin_url( self::section_slug( 'help' ) ) ), esc_html__( 'Help', 'mailpuff' ), esc_url( self::SUPPORT_URL ), esc_html__( 'Get support', 'mailpuff' ), /* translators: Accessibility text. */ esc_html__( '(opens in a new tab)', 'mailpuff' ), /* translators: %s: Mailpuff's version number. */ esc_html( sprintf( __( 'Version %s', 'mailpuff' ), MAILPUFF_VERSION ) ) ); } /** The page's mount point; the admin bundle draws everything in it. */ public function render_page() { echo '
'; } /** * Render the email editor mount point. * * Inside wp-admin like every Mailpuff page: WordPress's menu and bar stay, * with Mailpuff's menu open (`editor_parent()`, `current_submenu()`); the * editor fills the rest of the window and its own top bar has the way * back. wp-admin's footer is hidden on this page only (the inline style in * {@see Mailpuff_Assets}); hiding admin notices is a rule of the editor's * own CSS, keyed on the `mailpuff-editor-screen` body class. */ public function render_editor() { // The editor renders a campaign email. Targets come in via the URL and // are surfaced as data attributes the editor reads. // phpcs:disable WordPress.Security.NonceVerification.Recommended $campaign_id = isset( $_GET['campaign_id'] ) ? absint( $_GET['campaign_id'] ) : 0; $doc = isset( $_GET['doc'] ) ? sanitize_key( wp_unslash( $_GET['doc'] ) ) : ''; // phpcs:enable WordPress.Security.NonceVerification.Recommended /** * Where the editor's Back goes for one of an add-on's own emails * (`doc=email-`), and what it says. Only an address inside * wp-admin is used. * * @param array $back { url, label }; empty for Mailpuff's own. * @param string $doc The editor's `doc`. * @param int $campaign_id The editor's campaign, 0 for a `doc`. */ $back = (array) apply_filters( 'mailpuff_editor_back', array(), $doc, $campaign_id ); $url = isset( $back['url'] ) ? esc_url_raw( (string) $back['url'] ) : ''; if ( '' !== $url && 0 !== strpos( $url, admin_url() ) ) { $url = ''; } ?>