get( 'Name' ); * return $meta; * }, 10, 2 ); * * CHANGELOG * --------- * 1.3.0 Optional `list` argument in register(): the newsletter list the plugin's signups go to. * Defaults to `general`, so plugins that do not pass it behave exactly as before. * 1.2.0 Optional phone field, honeypot, `acowebs_feedback_optin_meta` filter. * * REQUIREMENTS: WordPress 5.0+, PHP 7.1+. * * @package Acowebs_Feedback */ // phpcs:disable WordPress.Files.FileName.InvalidClassFileName, Generic.Files.OneObjectStructurePerFile if ( ! defined( 'ABSPATH' ) ) { exit; // Do not load directly. } /* * Public API base URL (no trailing slash). Must never change after plugins ship. * Sites can override it with the `acowebs_feedback_api_url` filter. */ if ( ! defined( 'ACOWEBS_FEEDBACK_API' ) ) { define( 'ACOWEBS_FEEDBACK_API', 'https://audience.acowebs.com' ); } if ( ! class_exists( 'Acowebs_Feedback_Optin' ) ) { /** * Newsletter opt-in admin notice. */ class Acowebs_Feedback_Optin { /** * Version of this helper. Bump when you change it. */ const VERSION = '1.3.0'; /** * User-meta key set when the user subscribed or dismissed the notice. */ const META_KEY = 'acowebs_optin_done'; /** * Nonce action shared by the notice and both AJAX handlers. */ const NONCE = 'acowebs_optin'; /** * List used when a plugin does not register its own. */ const DEFAULT_LIST = 'general'; /** * Registered plugins, keyed by slug. * * @var array */ private static $plugins = array(); /** * Whether the hooks have been added. * * @var bool */ private static $hooked = false; /** * Registers a plugin that may show the notice. Call this once from your plugin, early * (at load time, not only on admin_init), so the AJAX handlers can find it. * * @param array $args { * @type string $plugin Canonical plugin name, for example 'Product Table'. * @type string $edition 'free' or 'pro'. * @type string $slug Unique plugin slug, for example 'product-table-for-woocommerce'. * @type string[] $page_slugs Values of ?page= for the plugin's own admin pages. * @type string $prefix Your plugin prefix, e.g. 'wcpa'. Keeps the notice's ids unique. * @type string $display Name shown in the notice text. Defaults to $plugin. * @type string $list Newsletter list the signups go to. Defaults to 'general'. (1.3.0) * @type string $kit_dir Absolute path to your copy of the UI kit folder (with trailing slash). * @type string $kit_url URL of that folder (with trailing slash), for the stylesheet. * } * @return void */ public static function register( array $args ): void { $args = wp_parse_args( $args, array( 'plugin' => '', 'edition' => 'free', 'slug' => '', 'page_slugs' => array(), 'prefix' => '', 'display' => '', 'list' => self::DEFAULT_LIST, 'kit_dir' => '', 'kit_url' => '', ) ); if ( '' === $args['plugin'] || '' === $args['slug'] ) { return; } $args['edition'] = 'pro' === strtolower( (string) $args['edition'] ) ? 'pro' : 'free'; $args['page_slugs'] = array_filter( array_map( 'sanitize_key', (array) $args['page_slugs'] ) ); $args['prefix'] = sanitize_key( '' !== $args['prefix'] ? $args['prefix'] : $args['slug'] ); $args['display'] = '' !== $args['display'] ? $args['display'] : $args['plugin']; $args['list'] = trim( sanitize_text_field( (string) $args['list'] ) ); $args['list'] = '' !== $args['list'] ? $args['list'] : self::DEFAULT_LIST; self::$plugins[ sanitize_key( $args['slug'] ) ] = $args; if ( ! self::$hooked ) { self::$hooked = true; add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_styles' ) ); add_action( 'admin_notices', array( __CLASS__, 'render_notice' ) ); add_action( 'wp_ajax_acowebs_optin_subscribe', array( __CLASS__, 'ajax_subscribe' ) ); add_action( 'wp_ajax_acowebs_optin_dismiss', array( __CLASS__, 'ajax_dismiss' ) ); } } /** * The API base URL, passed through the `acowebs_feedback_api_url` filter. * * @return string URL without a trailing slash. */ public static function api_url() { // Same filter as Acowebs_Feedback_Client::api_url(). return untrailingslashit( (string) apply_filters( 'acowebs_feedback_api_url', ACOWEBS_FEEDBACK_API ) ); } /** * Returns the registered plugin that owns the current admin screen, or null. * The dashboard counts for every registered plugin: the first one registered is used. * * @return array|null */ private static function current_plugin() { if ( ! function_exists( 'get_current_screen' ) ) { return null; } $screen = get_current_screen(); if ( ! $screen ) { return null; } if ( 'dashboard' === $screen->id ) { return empty( self::$plugins ) ? null : reset( self::$plugins ); } foreach ( self::$plugins as $plugin ) { foreach ( $plugin['page_slugs'] as $page ) { // Top-level pages are "toplevel_page_{slug}", submenu pages "{parent}_page_{slug}". if ( substr( $screen->id, -strlen( '_page_' . $page ) ) === '_page_' . $page ) { return $plugin; } } } return null; } /** * The registered plugin whose notice should show on this screen for this user, or null. * * @return array|null */ private static function plugin_to_show() { if ( ! current_user_can( 'manage_options' ) ) { return null; } if ( get_user_meta( get_current_user_id(), self::META_KEY, true ) ) { return null; } $plugin = self::current_plugin(); if ( ! $plugin || '' === $plugin['kit_dir'] || ! is_readable( $plugin['kit_dir'] . 'newsletter-notice.html' ) ) { return null; } return $plugin; } /** * Loads the UI kit stylesheet on screens where the notice shows. * * @return void */ public static function enqueue_styles(): void { $plugin = self::plugin_to_show(); if ( $plugin && '' !== $plugin['kit_url'] ) { // One handle for every Acowebs plugin: the stylesheet is shared and loaded once. wp_enqueue_style( 'acowebs-feedback-ui', $plugin['kit_url'] . 'acowebs-feedback-ui.css', array(), self::VERSION ); } } /** * Prints the notice: the UI kit markup with its placeholders filled in (escaped). * * @return void */ public static function render_notice(): void { $plugin = self::plugin_to_show(); if ( ! $plugin ) { return; } $user = wp_get_current_user(); $markup = (string) file_get_contents( $plugin['kit_dir'] . 'newsletter-notice.html' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents // Drop the kit's leading comment and the preview-only "Hidden: ..." line. $markup = preg_replace( '/^\s*\s*/s', '', $markup ); $markup = preg_replace( '#
\s*#s', '', $markup ); $markup = strtr( $markup, array( 'value="{Plugin name}"' => 'value="' . esc_attr( $plugin['plugin'] ) . '"', '{Plugin name}' => esc_html( $plugin['display'] ), '{prefix}' => esc_attr( $plugin['prefix'] ), 'value="admin@example.com"' => 'value="' . esc_attr( $user->user_email ) . '"', 'name="list" value="general"' => 'name="list" value="' . esc_attr( self::plugin_list( $plugin ) ) . '"', 'name="edition" value="free"' => 'name="edition" value="' . esc_attr( $plugin['edition'] ) . '"', 'value="https://shop.example.com"' => 'value="' . esc_url( home_url() ) . '"', 'value="Example Shop"' => 'value="' . esc_attr( get_bloginfo( 'name' ) ) . '"', ) ); $config = array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( self::NONCE ), 'slug' => sanitize_key( $plugin['slug'] ), 'rootId' => 'acofb-' . $plugin['prefix'] . '-newsletter', 'i18n' => array( 'sending' => __( 'Subscribing…', 'acowebs-feedback' ), 'error' => __( 'Sorry, we could not subscribe you right now. Please try again later.', 'acowebs-feedback' ), ), ); // .notice lets WordPress place it with the other admin notices; the kit card does the styling. echo '