| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458 |
- <?php
- /**
- * Sends opt-in usage data
- * @since 2.1
- */
- final class FLBuilderUsage {
- protected static $url = 'http://stats.wpbeaverbuilder.com/';
- protected static $seconds = 604800;
- public static function init() {
- $hook = is_network_admin() ? 'network_admin_notices' : 'admin_notices';
- add_action( 'admin_init', array( 'FLBuilderUsage', 'enable_disable' ) );
- add_action( 'init', array( 'FLBuilderUsage', 'set_schedule' ) );
- add_action( $hook, array( 'FLBuilderUsage', 'render_notification' ) );
- add_action( 'admin_enqueue_scripts', array( 'FLBuilderUsage', 'scripts' ) );
- add_action( 'fl_builder_usage_event', array( 'FLBuilderUsage', 'send_stats' ) );
- add_action( 'wp_ajax_fl_usage_toggle', array( 'FLBuilderUsage', 'callback' ) );
- }
- public static function callback() {
- $enable = intval( $_POST['enable'] );
- if ( wp_verify_nonce( $_POST['_wpnonce'], 'fl-usage' ) ) {
- update_site_option( 'fl_builder_usage_enabled', $enable );
- }
- wp_die();
- }
- public static function scripts() {
- wp_enqueue_style( 'fl-builder-admin-usage', FL_BUILDER_URL . 'css/fl-builder-admin-usage.css', array(), FL_BUILDER_VERSION );
- wp_enqueue_script( 'fl-builder-admin-usage', FL_BUILDER_URL . 'js/fl-builder-admin-usage.js', array( 'jquery' ), FL_BUILDER_VERSION );
- }
- /**
- * Add scheduled event
- * @since 2.1
- */
- public static function set_schedule() {
- if ( '1' == get_site_option( 'fl_builder_usage_enabled', false ) ) {
- if ( ! wp_next_scheduled( 'fl_builder_usage_event' ) ) {
- wp_schedule_single_event( time() + self::$seconds, 'fl_builder_usage_event' );
- }
- }
- }
- /**
- * Send stats callback
- * @since 2.1
- */
- public static function send_stats() {
- if ( ! get_site_option( 'fl_builder_usage_enabled', false ) ) {
- return false;
- }
- $request = wp_remote_post( self::$url, array(
- 'body' => json_encode( self::get_data() ),
- ) );
- }
- /**
- * Enable/disable
- * @since 2.1
- */
- public static function enable_disable() {
- if ( isset( $_GET['fl_usage'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'stats_enable' ) ) {
- update_site_option( 'fl_builder_usage_enabled', $_GET['fl_usage'] );
- }
- }
- /**
- * Render admin admin notice
- * @since 2.1
- */
- public static function render_notification() {
- if ( ! self::notification_enabled() ) {
- return false;
- }
- wp_enqueue_script( 'jquery' );
- $btn = sprintf( '<div class="buttons"><span class="button button-primary enable-stats">%s</span> <span class="button disable-stats">%s</span>%s</div>',
- __( "Sure, I'll help", 'fl-builder' ),
- __( 'No, Thank You', 'fl-builder' ),
- wp_nonce_field( 'fl-usage', '_wpnonce', false )
- );
- $message = sprintf(
- __( 'Would you like to help us improve %s by sending anonymous usage data?', 'fl-builder' ),
- FLBuilderModel::get_branding()
- );
- echo '<div class="notice notice-info">';
- echo '<div class="fl-usage">';
- echo '<p>';
- printf( '%s %s', $message, $btn );
- echo '</p>';
- printf( '</div>%s</div>', FLBuilderUsage::data_demo() );
- }
- /**
- * Whether to show the stats settings in bb admin.
- */
- public static function show_settings() {
- // super admin and network settings
- if ( is_multisite() && is_super_admin() && is_network_admin() ) {
- return true;
- }
- // single site admin
- if ( ! is_multisite() && is_super_admin() ) {
- return true;
- }
- return false;
- }
- /**
- * Is notification enabled
- * @since 2.1
- * @return bool
- */
- private static function notification_enabled() {
- global $pagenow;
- $screen = get_current_screen();
- $show = false;
- if ( 'fl-builder-template' == $screen->post_type ) {
- $show = true;
- }
- if ( 'fl-theme-layout' == $screen->post_type ) {
- $show = true;
- }
- if ( 'options-general.php' == $pagenow && isset( $_GET['page'] ) && 'fl-builder-settings' == $_GET['page'] ) {
- $show = true;
- }
- if ( 'dashboard-network' == $screen->id ) {
- $show = true;
- }
- if ( '0' === get_site_option( 'fl_builder_usage_enabled' ) ) {
- $show = false;
- }
- if ( ! is_super_admin() ) {
- $show = false;
- }
- return ( $show && ! get_site_option( 'fl_builder_usage_enabled' ) ) ? true : false;
- }
- /**
- * Show a user what kind of data we are collecting.
- * @since 2.1
- * @return string
- */
- public static function data_demo() {
- $data = self::get_data( true );
- $output = '';
- $txt = '';
- $settings = array(
- 'server' => array(
- 'name' => __( 'Server Type', 'fl-builder' ),
- 'data' => $data['data']['server'],
- ),
- 'php' => array(
- 'name' => __( 'PHP Version', 'fl-builder' ),
- 'data' => $data['data']['php'],
- ),
- 'wp' => array(
- 'name' => __( 'WP Version', 'fl-builder' ),
- 'data' => $data['data']['wp'],
- ),
- 'mu' => array(
- 'name' => __( 'WP Multisite', 'fl-builder' ),
- 'data' => $data['data']['multisite'],
- ),
- 'locale' => array(
- 'name' => __( 'Locale', 'fl-builder' ),
- 'data' => $data['data']['locale'],
- ),
- 'plugins' => array(
- 'name' => __( 'Plugins Count', 'fl-builder' ),
- 'data' => $data['data']['plugins'],
- ),
- 'modules' => array(
- 'name' => __( 'Modules Used', 'fl-builder' ),
- 'data' => __( 'Which modules are used and how many times.', 'fl-builder' ),
- ),
- 'settings' => array(
- 'name' => __( 'Builder Settings', 'fl-builder' ),
- 'data' => __( 'UI theme, pinned settings etc.' ),
- ),
- );
- foreach ( $settings as $k => $data ) {
- $txt .= sprintf( '<span class="usage-demo-left">%s</span><span class="usage-demo-right">: %s</span><br />', $data['name'], $data['data'] );
- }
- $output = sprintf( '<div class="usage-demo"><a class="stats-info" href="#">%s</a><div class="stats-info-data"><p>%s</p><p><em>%s</em></p></div></div>',
- __( 'What kind of info will we collect?', 'fl-builder' ),
- $txt,
- __( 'We will never collect any private data such as IP, email addresses or usernames.', 'fl-builder' )
- );
- return $output;
- }
- /**
- * Gather stats to send
- * @since 2.1
- * @return array
- */
- public static function get_data( $demo = false ) {
- global $wp_version, $wpdb;
- if ( ! function_exists( 'get_plugins' ) ) {
- require_once ABSPATH . 'wp-admin/includes/plugin.php';
- }
- $data = array(
- 'modules' => array(),
- 'license' => array(),
- 'themer' => array(
- 'header' => 0,
- 'footer' => 0,
- 'part' => 0,
- '404' => 0,
- 'singular' => 0,
- ),
- 'pinned' => array(
- 'left' => 0,
- 'right' => 0,
- 'unpinned' => 0,
- ),
- );
- $users = count_users();
- $plugins_data = get_plugins();
- $data['plugins'] = count( $plugins_data );
- $data['plugins_active'] = 0;
- foreach ( (array) $plugins_data as $plugin_slug => $plugin ) {
- if ( is_plugin_active( $plugin_slug ) ) {
- $data['plugins_active'] ++;
- }
- }
- /**
- * Setup an array of post types to query
- */
- $post_types = get_post_types( array(
- 'public' => true,
- '_builtin' => true,
- ) );
- if ( isset( $post_types['attachment'] ) ) {
- unset( $post_types['attachment'] );
- }
- // $post_types['fl-builder-template'] = 'fl-builder-template';
- /**
- * Get a count of all posts/pages that are *not* builder enabled.
- */
- $args = array(
- 'post_type' => $post_types,
- 'post_status' => 'publish',
- 'meta_query' => array(
- 'key' => '_fl_builder_enabled',
- 'value' => '1',
- 'compare' => '!=',
- ),
- 'posts_per_page' => -1,
- );
- $query = new WP_Query( $args );
- $data['not-enabled'] = count( $query->posts );
- /**
- * Get a count of all posts pages that are using the builder.
- */
- $args = array(
- 'post_type' => $post_types,
- 'post_status' => 'publish',
- 'meta_key' => '_fl_builder_enabled',
- 'meta_value' => '1',
- 'posts_per_page' => -1,
- );
- $query = new WP_Query( $args );
- $data['enabled'] = count( $query->posts );
- /**
- * Using the array of pages/posts using builder get a list of all used modules
- */
- if ( is_array( $query->posts ) && ! empty( $query->posts ) ) {
- foreach ( $query->posts as $post ) {
- $meta = get_post_meta( $post->ID, '_fl_builder_data', true );
- foreach ( (array) $meta as $node_id => $node ) {
- if ( @isset( $node->type ) && 'module' == $node->type ) { // @codingStandardsIgnoreLine
- if ( ! isset( $data['modules'][ $node->settings->type ] ) ) {
- $data['modules'][ $node->settings->type ] = 1;
- } else {
- $data['modules'][ $node->settings->type ] ++;
- }
- }
- }
- }
- }
- // themer settings.
- $args = array(
- 'post_type' => 'fl-theme-layout',
- 'post_status' => 'publish',
- 'meta_key' => '_fl_builder_enabled',
- 'meta_value' => '1',
- 'posts_per_page' => -1,
- );
- $query = new WP_Query( $args );
- $data['themer']['total'] = count( $query->posts );
- if ( is_array( $query->posts ) && ! empty( $query->posts ) ) {
- foreach ( $query->posts as $post ) {
- $meta = get_post_meta( $post->ID );
- if ( isset( $meta['_fl_theme_layout_type'] ) ) {
- if ( ! isset( $data['themer'][ $meta['_fl_theme_layout_type'][0] ] ) ) {
- $data['themer'][ $meta['_fl_theme_layout_type'][0] ] = 1;
- } else {
- $data['themer'][ $meta['_fl_theme_layout_type'][0] ] ++;
- }
- }
- }
- }
- /**
- * Find all users that are using the builder.
- */
- $args = array(
- 'meta_key' => 'fl_builder_user_settings',
- 'meta_value' => 'null',
- 'meta_compare' => '!=',
- );
- $query = new WP_User_Query( $args );
- /**
- * Using array of users collect their builder settings, pinned, skin etc.
- */
- if ( ! empty( $query->results ) ) {
- foreach ( $query->results as $user ) {
- $meta = get_user_meta( $user->ID, 'fl_builder_user_settings', true );
- if ( isset( $meta['skin'] ) ) {
- if ( ! isset( $data['skin'][ $meta['skin'] ] ) ) {
- $data['skin'][ $meta['skin'] ] = 1;
- } else {
- $data['skin'][ $meta['skin'] ] ++;
- }
- }
- if ( isset( $meta['pinned']['position'] ) ) {
- if ( '' == $meta['pinned']['position'] ) {
- if ( ! isset( $data['pinned']['unpinned'] ) ) {
- $data['pinned']['unpinned'] = 1;
- } else {
- $data['pinned']['unpinned'] ++;
- }
- } else {
- if ( ! isset( $data['pinned'][ $meta['pinned']['position'] ] ) ) {
- $data['pinned'][ $meta['pinned']['position'] ] = 1;
- } else {
- $data['pinned'][ $meta['pinned']['position'] ] ++;
- }
- }
- }
- }
- }
- /**
- * General data
- */
- $data['server'] = $_SERVER['SERVER_SOFTWARE'];
- $data['database'] = ( ! empty( $wpdb->is_mysql ) ? $wpdb->db_version() : 'Unknown' );
- $data['multisite'] = is_multisite() ? 'Yes' : 'No';
- $data['subsites'] = is_multisite() ? get_blog_count() : '';
- $data['locale'] = get_locale();
- $data['users'] = $users['total_users'];
- $data['php'] = phpversion();
- $data['wp'] = $wp_version;
- $data['fl-builder'] = FL_BUILDER_VERSION;
- $data['fl-theme'] = ( defined( 'FL_THEME_VERSION' ) ) ? FL_THEME_VERSION : false;
- $data['fl-themer'] = ( defined( 'FL_THEME_BUILDER_VERSION' ) ) ? FL_THEME_BUILDER_VERSION : false;
- $settings_orig = FLBuilderModel::get_global_settings();
- $settings = clone $settings_orig;
- // we dont need these
- unset( $settings->js );
- unset( $settings->css );
- foreach ( $settings as $k => $setting ) {
- $data['settings'][ $k ] = $setting;
- }
- $theme = wp_get_theme();
- if ( $theme->get( 'Template' ) ) {
- $parent = wp_get_theme( $theme->get( 'Template' ) );
- $data['theme'] = $parent->get( 'Name' );
- $data['theme_child'] = $theme->get( 'Name' );
- } else {
- $data['theme'] = $theme->get( 'Name' );
- }
- if ( class_exists( 'FLUpdater' ) && false == $demo ) {
- $subscription = FLUpdater::get_subscription_info();
- if ( ! $subscription->active ) {
- $data['license'][] = 'none';
- } else {
- foreach ( (array) $subscription->subscriptions as $subscription ) {
- if ( false !== strpos( $subscription->name, 'Beaver Builder' ) ) {
- $data['license']['bb-plugin'] = $subscription->name;
- }
- if ( 'Beaver Themer Plugin' == $subscription->name ) {
- $data['license']['bb-themer'] = $subscription->name;
- }
- }
- }
- } else {
- $data['license'][] = 'none';
- }
- $output = array(
- 'id' => md5( get_bloginfo( 'url' ) . get_bloginfo( 'admin_email' ) ),
- 'data' => $data,
- );
- return $output;
- }
- }
- FLBuilderUsage::init();
|