false === strpos( $basename, '/' ) ? basename( $basename, '.php' ) : dirname( $basename ), ); $plugin += array( 'product' => $plugin['slug'], 'name' => $plugin['slug'], 'version' => '0', 'page' => '', 'slot' => '', ); $plugin += array( 'brand' => $plugin['name'] ); $plugin['basename'] = $basename; self::$products[ $plugin['slug'] ] = $plugin; } $on = ! ( defined( 'PLUGPRESS_INSIGHT' ) && false === PLUGPRESS_INSIGHT ); if ( ! self::$products || ! apply_filters( 'plugpress_insight_enabled', $on ) ) { return; } // A copy of the site (staging, a clone, a move) starts over: a new id, // and every question asked again. $state = self::state(); if ( $state['home'] && self::home() !== $state['home'] ) { delete_option( self::OPTION ); delete_option( self::SITE_ID ); wp_clear_scheduled_hook( self::CRON ); } add_action( self::CRON, array( __CLASS__, 'weekly' ) ); if ( is_admin() ) { add_action( 'admin_init', array( __CLASS__, 'schedule' ) ); // Priority 0: before hosts that fold notices away, such as Saddle. add_action( 'admin_notices', array( __CLASS__, 'bar' ), 0 ); add_action( 'admin_footer', array( __CLASS__, 'dialog' ) ); add_action( 'admin_enqueue_scripts', array( __CLASS__, 'assets' ) ); add_action( 'wp_ajax_plugpress_insight', array( __CLASS__, 'ajax' ) ); add_filter( 'plugin_row_meta', array( __CLASS__, 'row_meta' ), 10, 2 ); } } /** * The owner's answers: site info shared (null until asked, once per site), * who clicked Allow, the brands whose card was answered, a hash of the last * weekly send, and the address all of it belongs to. * * @return array{usage:bool|null,by:int,asked:array,hash:string,home:string} */ public static function state() { $state = wp_parse_args( (array) get_option( self::OPTION, array() ), array( 'usage' => null, 'by' => 0, 'asked' => array(), 'hash' => '', 'home' => '', ) ); $state['asked'] = (array) $state['asked']; return $state; } /** * Keep the weekly update scheduled while site info is shared. */ public static function schedule() { if ( true === self::state()['usage'] && ! wp_next_scheduled( self::CRON ) ) { wp_schedule_event( time() + DAY_IN_SECONDS, 'weekly', self::CRON ); } } /** * The card, once per brand, on the Plugins screen and on a registered * plugin's own page. It asks for site info until that has an answer, then * only for the brand's news. On the plugin's page with a slot, the script * moves it into that element. */ public static function bar() { $plugin = self::product_on_screen(); $state = self::state(); if ( ! $plugin || ! current_user_can( 'manage_options' ) ) { return; } $ask = null === $state['usage']; $brand = $plugin['brand']; $user = wp_get_current_user(); $slot = self::on_own_page( $plugin ) ? $plugin['slot'] : ''; ?>
id || ! current_user_can( 'activate_plugins' ) ) { return; } $email = wp_get_current_user()->user_email; // Reason => its choice, and the question it asks ('' for none). The ids // are the server's; the order is what people see. The server still // takes short_time, which older copies show. $reasons = array( 'broke_something' => array( __( 'Something broke', 'addons-for-divi' ), __( 'What broke?', 'addons-for-divi' ) ), 'hard_to_set_up' => array( __( 'Hard to use', 'addons-for-divi' ), __( 'Where did you get stuck?', 'addons-for-divi' ) ), 'not_what_i_needed' => array( __( 'Missing a feature', 'addons-for-divi' ), __( 'What did you need?', 'addons-for-divi' ) ), 'found_another' => array( __( 'Found a better plugin', 'addons-for-divi' ), __( 'Which one?', 'addons-for-divi' ) ), 'temporary' => array( __( 'Just for now', 'addons-for-divi' ), '' ), 'other' => array( __( 'Something else', 'addons-for-divi' ), __( 'Tell us in a few words', 'addons-for-divi' ) ), ); ?> id && current_user_can( 'activate_plugins' ); if ( ! $plugins && ! self::product_on_screen() ) { return; } $file = $GLOBALS['plugpress_insight_sdk']['dir'] . '/class-plugpress-insight.php'; $version = $GLOBALS['plugpress_insight_sdk']['version']; // The handle is not the text domain, so renaming the domain leaves it. wp_enqueue_style( 'plugpress-insight-sdk', plugins_url( 'insight.css', $file ), array(), $version ); wp_enqueue_script( 'plugpress-insight-sdk', plugins_url( 'insight.js', $file ), array(), $version, true ); // Plugin file => slug and name, for the Deactivate links. $deactivate = array(); foreach ( $plugins ? self::$products : array() as $plugin ) { $deactivate[ $plugin['basename'] ] = array( $plugin['slug'], $plugin['name'] ); } $config = array( 'ajax' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'plugpress_insight' ), 'plugins' => $deactivate, ); wp_add_inline_script( 'plugpress-insight-sdk', 'var plugpressInsight = ' . wp_json_encode( $config ) . ';', 'before' ); } /** * "Stop sharing site info" under each registered plugin while it is shared. * * @param array $meta The row's links. * @param string $basename The row's plugin file. * @return array */ public static function row_meta( $meta, $basename ) { if ( in_array( $basename, wp_list_pluck( self::$products, 'basename' ), true ) && true === self::state()['usage'] && current_user_can( 'manage_options' ) ) { $meta[] = '' . esc_html__( 'Stop sharing site info', 'addons-for-divi' ) . ''; } return $meta; } /** * Every click from the script: allow, subscribe, skip, off and deactivate. */ public static function ajax() { check_ajax_referer( 'plugpress_insight' ); // phpcs:disable WordPress.Security.NonceVerification.Missing -- checked above. $do = isset( $_POST['do'] ) ? sanitize_key( $_POST['do'] ) : ''; $slug = isset( $_POST['slug'] ) ? sanitize_key( $_POST['slug'] ) : ''; $brand = isset( $_POST['product'] ) ? sanitize_key( $_POST['product'] ) : ''; $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; $reason = isset( $_POST['reason'] ) ? sanitize_key( $_POST['reason'] ) : ''; $comment = isset( $_POST['comment'] ) ? sanitize_textarea_field( wp_unslash( $_POST['comment'] ) ) : ''; // phpcs:enable $reasons = array( 'short_time', 'broke_something', 'hard_to_set_up', 'not_what_i_needed', 'found_another', 'temporary', 'other' ); $email = is_email( $email ) ? $email : ''; if ( 'deactivate' === $do ) { $plugin = isset( self::$products[ $slug ] ) ? self::$products[ $slug ] : null; if ( ! $plugin || ! in_array( $reason, $reasons, true ) || ! current_user_can( 'activate_plugins' ) ) { wp_send_json_error( null, 400 ); } self::send( 'deactivate', array( 'site' => self::site( false ), 'product' => array( 'slug' => $plugin['product'], 'version' => $plugin['version'], 'plugin' => $plugin['slug'], // Another plugin of the brand stays on. 'active' => count( wp_list_filter( self::$products, array( 'product' => $plugin['product'] ) ) ) > 1, ), 'reason' => $reason, 'comment' => mb_substr( $comment, 0, 500 ), 'email' => $email, ) ); wp_send_json_success(); } if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( null, 403 ); } if ( 'off' === $do ) { if ( true === self::state()['usage'] ) { self::send( 'optout', array( 'site' => array( 'id' => self::site_id() ), 'products' => array_keys( self::brands() ), ) ); } self::remember( array( 'usage' => false ) ); wp_clear_scheduled_hook( self::CRON ); wp_send_json_success(); } if ( ! isset( self::brands()[ $brand ] ) ) { wp_send_json_error( null, 400 ); } $news = array( 'product' => $brand, 'email' => $email, ); $answer = array( 'asked' => array( $brand => time() ) + self::state()['asked'] ); if ( 'allow' === $do ) { self::remember( $answer + array( 'usage' => true, 'by' => get_current_user_id(), ) ); self::send( 'optin', self::body() + ( $email ? array( 'news' => $news ) : array() ) ); self::schedule(); } elseif ( 'subscribe' === $do && $email ) { self::send( 'news', $news ); self::remember( $answer ); } elseif ( 'skip' === $do ) { // No thanks to site info holds for every brand; to news, for this one. self::remember( $answer + ( null === self::state()['usage'] ? array( 'usage' => false ) : array() ) ); } wp_send_json_success(); } /** * The weekly update: only while site info is shared, only when it changed. */ public static function weekly() { $state = self::state(); if ( true !== $state['usage'] ) { wp_clear_scheduled_hook( self::CRON ); return; } $body = self::body(); $hash = md5( (string) wp_json_encode( $body ) ); if ( $hash !== $state['hash'] && self::send( 'update', $body ) ) { self::remember( array( 'hash' => $hash ) ); } } /** * The opt-in and weekly body: the site, and each brand once. * * @return array */ private static function body() { $products = array(); foreach ( self::brands() as $brand => $version ) { $products[] = array( 'slug' => $brand, 'version' => $version, 'active' => true, ); } return array( 'site' => self::site( true ), 'products' => $products, ); } /** * Each brand on the site with its version: the version of its plugin of * the same slug (Saddle's for saddle), else of its first plugin. * * @return array Brand => version. */ private static function brands() { $brands = array(); foreach ( self::$products as $plugin ) { if ( ! isset( $brands[ $plugin['product'] ] ) || $plugin['slug'] === $plugin['product'] ) { $brands[ $plugin['product'] ] = $plugin['version']; } } return $brands; } /** * The site block: versions and language, plus with $full the address, * title, timezone, theme and plugins. * * @param bool $full False for a deactivation. * @return array */ private static function site( $full ) { $site = array( 'id' => self::site_id(), 'wp' => (string) get_bloginfo( 'version' ), 'php' => PHP_VERSION, 'language' => get_locale(), 'multisite' => is_multisite(), 'environment' => wp_get_environment_type(), ); if ( ! $full ) { return $site; } if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $theme = wp_get_theme(); $site['timezone'] = wp_timezone_string(); $site['url'] = home_url( '/' ); $site['title'] = get_bloginfo( 'name' ); $site['theme'] = array( 'slug' => $theme->get_stylesheet(), 'version' => (string) $theme->get( 'Version' ), ); $site['plugins'] = array(); foreach ( array_slice( get_plugins(), 0, 200, true ) as $file => $data ) { $site['plugins'][] = array( 'slug' => false === strpos( $file, '/' ) ? basename( $file, '.php' ) : dirname( $file ), 'version' => (string) $data['Version'], 'active' => is_plugin_active( $file ), ); } // The person who clicked Allow, for support. Gone if they are. $by = get_userdata( (int) self::state()['by'] ); if ( $by ) { $site['admin'] = array( 'name' => $by->display_name, 'email' => $by->user_email, ); } return $site; } /** * The site's random id, made once and shared by every PlugPress plugin. * * @return string */ private static function site_id() { $id = get_option( self::SITE_ID ); if ( ! is_string( $id ) || ! wp_is_uuid( $id, 4 ) ) { $id = wp_generate_uuid4(); update_option( self::SITE_ID, $id, false ); } return $id; } /** * The site's address as stored, without the scheme or www. The stored * option, not home_url(), which language plugins change per request. * * @return string */ private static function home() { return strtolower( untrailingslashit( (string) preg_replace( '#^https?://(www\.)?#i', '', (string) get_option( 'home' ) ) ) ); } /** * The registered plugin whose card belongs on this screen, or null: on the * Plugins screen the first brand not yet asked, on a plugin's own page * that plugin's brand if not yet asked. * * @return array|null */ private static function product_on_screen() { $screen = get_current_screen(); $plugins = $screen && 'plugins' === $screen->id; $asked = self::state()['asked']; foreach ( self::$products as $plugin ) { if ( ! isset( $asked[ $plugin['product'] ] ) && ( $plugins || self::on_own_page( $plugin ) ) ) { return $plugin; } } return null; } /** * Whether this is the plugin's own page (?page=). * * @param array $plugin A registered plugin. * @return bool */ private static function on_own_page( $plugin ) { $page = isset( $_GET['page'] ) ? sanitize_key( $_GET['page'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- which page, not an action. return $plugin['page'] && $page === $plugin['page']; } /** * Store answers over the ones already given, with the site's address. * * @param array $changes Any of usage, by, asked and hash. */ private static function remember( array $changes ) { update_option( self::OPTION, array_merge( self::state(), $changes, array( 'home' => self::home() ) ) ); } /** * One call to console.plugpress.io: 5 seconds at most, never retried. * * @param string $route optin, update, deactivate, optout or news. * @param array $body The JSON body. * @return bool Whether it answered 2xx. */ private static function send( $route, array $body ) { $url = defined( 'PLUGPRESS_INSIGHT_URL' ) ? PLUGPRESS_INSIGHT_URL : self::URL; $response = wp_remote_post( $url . $route, array( 'timeout' => 5, 'redirection' => 0, 'headers' => array( 'Content-Type' => 'application/json' ), 'body' => wp_json_encode( $body ), 'user-agent' => 'PlugPress-Insight/' . $GLOBALS['plugpress_insight_sdk']['version'], ) ); $code = (int) wp_remote_retrieve_response_code( $response ); return $code >= 200 && $code < 300; } }