class-fl-builder-usage.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <?php
  2. /**
  3. * Sends opt-in usage data
  4. * @since 2.1
  5. */
  6. final class FLBuilderUsage {
  7. protected static $url = 'http://stats.wpbeaverbuilder.com/';
  8. protected static $seconds = 604800;
  9. public static function init() {
  10. $hook = is_network_admin() ? 'network_admin_notices' : 'admin_notices';
  11. add_action( 'admin_init', array( 'FLBuilderUsage', 'enable_disable' ) );
  12. add_action( 'init', array( 'FLBuilderUsage', 'set_schedule' ) );
  13. add_action( $hook, array( 'FLBuilderUsage', 'render_notification' ) );
  14. add_action( 'admin_enqueue_scripts', array( 'FLBuilderUsage', 'scripts' ) );
  15. add_action( 'fl_builder_usage_event', array( 'FLBuilderUsage', 'send_stats' ) );
  16. add_action( 'wp_ajax_fl_usage_toggle', array( 'FLBuilderUsage', 'callback' ) );
  17. }
  18. public static function callback() {
  19. $enable = intval( $_POST['enable'] );
  20. if ( wp_verify_nonce( $_POST['_wpnonce'], 'fl-usage' ) ) {
  21. update_site_option( 'fl_builder_usage_enabled', $enable );
  22. }
  23. wp_die();
  24. }
  25. public static function scripts() {
  26. wp_enqueue_style( 'fl-builder-admin-usage', FL_BUILDER_URL . 'css/fl-builder-admin-usage.css', array(), FL_BUILDER_VERSION );
  27. wp_enqueue_script( 'fl-builder-admin-usage', FL_BUILDER_URL . 'js/fl-builder-admin-usage.js', array( 'jquery' ), FL_BUILDER_VERSION );
  28. }
  29. /**
  30. * Add scheduled event
  31. * @since 2.1
  32. */
  33. public static function set_schedule() {
  34. if ( '1' == get_site_option( 'fl_builder_usage_enabled', false ) ) {
  35. if ( ! wp_next_scheduled( 'fl_builder_usage_event' ) ) {
  36. wp_schedule_single_event( time() + self::$seconds, 'fl_builder_usage_event' );
  37. }
  38. }
  39. }
  40. /**
  41. * Send stats callback
  42. * @since 2.1
  43. */
  44. public static function send_stats() {
  45. if ( ! get_site_option( 'fl_builder_usage_enabled', false ) ) {
  46. return false;
  47. }
  48. $request = wp_remote_post( self::$url, array(
  49. 'body' => json_encode( self::get_data() ),
  50. ) );
  51. }
  52. /**
  53. * Enable/disable
  54. * @since 2.1
  55. */
  56. public static function enable_disable() {
  57. if ( isset( $_GET['fl_usage'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'stats_enable' ) ) {
  58. update_site_option( 'fl_builder_usage_enabled', $_GET['fl_usage'] );
  59. }
  60. }
  61. /**
  62. * Render admin admin notice
  63. * @since 2.1
  64. */
  65. public static function render_notification() {
  66. if ( ! self::notification_enabled() ) {
  67. return false;
  68. }
  69. wp_enqueue_script( 'jquery' );
  70. $btn = sprintf( '<div class="buttons"><span class="button button-primary enable-stats">%s</span>&nbsp;<span class="button disable-stats">%s</span>%s</div>',
  71. __( "Sure, I'll help", 'fl-builder' ),
  72. __( 'No, Thank You', 'fl-builder' ),
  73. wp_nonce_field( 'fl-usage', '_wpnonce', false )
  74. );
  75. $message = sprintf(
  76. __( 'Would you like to help us improve %s by sending anonymous usage data?', 'fl-builder' ),
  77. FLBuilderModel::get_branding()
  78. );
  79. echo '<div class="notice notice-info">';
  80. echo '<div class="fl-usage">';
  81. echo '<p>';
  82. printf( '%s %s', $message, $btn );
  83. echo '</p>';
  84. printf( '</div>%s</div>', FLBuilderUsage::data_demo() );
  85. }
  86. /**
  87. * Whether to show the stats settings in bb admin.
  88. */
  89. public static function show_settings() {
  90. // super admin and network settings
  91. if ( is_multisite() && is_super_admin() && is_network_admin() ) {
  92. return true;
  93. }
  94. // single site admin
  95. if ( ! is_multisite() && is_super_admin() ) {
  96. return true;
  97. }
  98. return false;
  99. }
  100. /**
  101. * Is notification enabled
  102. * @since 2.1
  103. * @return bool
  104. */
  105. private static function notification_enabled() {
  106. global $pagenow;
  107. $screen = get_current_screen();
  108. $show = false;
  109. if ( 'fl-builder-template' == $screen->post_type ) {
  110. $show = true;
  111. }
  112. if ( 'fl-theme-layout' == $screen->post_type ) {
  113. $show = true;
  114. }
  115. if ( 'options-general.php' == $pagenow && isset( $_GET['page'] ) && 'fl-builder-settings' == $_GET['page'] ) {
  116. $show = true;
  117. }
  118. if ( 'dashboard-network' == $screen->id ) {
  119. $show = true;
  120. }
  121. if ( '0' === get_site_option( 'fl_builder_usage_enabled' ) ) {
  122. $show = false;
  123. }
  124. if ( ! is_super_admin() ) {
  125. $show = false;
  126. }
  127. return ( $show && ! get_site_option( 'fl_builder_usage_enabled' ) ) ? true : false;
  128. }
  129. /**
  130. * Show a user what kind of data we are collecting.
  131. * @since 2.1
  132. * @return string
  133. */
  134. public static function data_demo() {
  135. $data = self::get_data( true );
  136. $output = '';
  137. $txt = '';
  138. $settings = array(
  139. 'server' => array(
  140. 'name' => __( 'Server Type', 'fl-builder' ),
  141. 'data' => $data['data']['server'],
  142. ),
  143. 'php' => array(
  144. 'name' => __( 'PHP Version', 'fl-builder' ),
  145. 'data' => $data['data']['php'],
  146. ),
  147. 'wp' => array(
  148. 'name' => __( 'WP Version', 'fl-builder' ),
  149. 'data' => $data['data']['wp'],
  150. ),
  151. 'mu' => array(
  152. 'name' => __( 'WP Multisite', 'fl-builder' ),
  153. 'data' => $data['data']['multisite'],
  154. ),
  155. 'locale' => array(
  156. 'name' => __( 'Locale', 'fl-builder' ),
  157. 'data' => $data['data']['locale'],
  158. ),
  159. 'plugins' => array(
  160. 'name' => __( 'Plugins Count', 'fl-builder' ),
  161. 'data' => $data['data']['plugins'],
  162. ),
  163. 'modules' => array(
  164. 'name' => __( 'Modules Used', 'fl-builder' ),
  165. 'data' => __( 'Which modules are used and how many times.', 'fl-builder' ),
  166. ),
  167. 'settings' => array(
  168. 'name' => __( 'Builder Settings', 'fl-builder' ),
  169. 'data' => __( 'UI theme, pinned settings etc.' ),
  170. ),
  171. );
  172. foreach ( $settings as $k => $data ) {
  173. $txt .= sprintf( '<span class="usage-demo-left">%s</span><span class="usage-demo-right">: %s</span><br />', $data['name'], $data['data'] );
  174. }
  175. $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>',
  176. __( 'What kind of info will we collect?', 'fl-builder' ),
  177. $txt,
  178. __( 'We will never collect any private data such as IP, email addresses or usernames.', 'fl-builder' )
  179. );
  180. return $output;
  181. }
  182. /**
  183. * Gather stats to send
  184. * @since 2.1
  185. * @return array
  186. */
  187. public static function get_data( $demo = false ) {
  188. global $wp_version, $wpdb;
  189. if ( ! function_exists( 'get_plugins' ) ) {
  190. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  191. }
  192. $data = array(
  193. 'modules' => array(),
  194. 'license' => array(),
  195. 'themer' => array(
  196. 'header' => 0,
  197. 'footer' => 0,
  198. 'part' => 0,
  199. '404' => 0,
  200. 'singular' => 0,
  201. ),
  202. 'pinned' => array(
  203. 'left' => 0,
  204. 'right' => 0,
  205. 'unpinned' => 0,
  206. ),
  207. );
  208. $users = count_users();
  209. $plugins_data = get_plugins();
  210. $data['plugins'] = count( $plugins_data );
  211. $data['plugins_active'] = 0;
  212. foreach ( (array) $plugins_data as $plugin_slug => $plugin ) {
  213. if ( is_plugin_active( $plugin_slug ) ) {
  214. $data['plugins_active'] ++;
  215. }
  216. }
  217. /**
  218. * Setup an array of post types to query
  219. */
  220. $post_types = get_post_types( array(
  221. 'public' => true,
  222. '_builtin' => true,
  223. ) );
  224. if ( isset( $post_types['attachment'] ) ) {
  225. unset( $post_types['attachment'] );
  226. }
  227. // $post_types['fl-builder-template'] = 'fl-builder-template';
  228. /**
  229. * Get a count of all posts/pages that are *not* builder enabled.
  230. */
  231. $args = array(
  232. 'post_type' => $post_types,
  233. 'post_status' => 'publish',
  234. 'meta_query' => array(
  235. 'key' => '_fl_builder_enabled',
  236. 'value' => '1',
  237. 'compare' => '!=',
  238. ),
  239. 'posts_per_page' => -1,
  240. );
  241. $query = new WP_Query( $args );
  242. $data['not-enabled'] = count( $query->posts );
  243. /**
  244. * Get a count of all posts pages that are using the builder.
  245. */
  246. $args = array(
  247. 'post_type' => $post_types,
  248. 'post_status' => 'publish',
  249. 'meta_key' => '_fl_builder_enabled',
  250. 'meta_value' => '1',
  251. 'posts_per_page' => -1,
  252. );
  253. $query = new WP_Query( $args );
  254. $data['enabled'] = count( $query->posts );
  255. /**
  256. * Using the array of pages/posts using builder get a list of all used modules
  257. */
  258. if ( is_array( $query->posts ) && ! empty( $query->posts ) ) {
  259. foreach ( $query->posts as $post ) {
  260. $meta = get_post_meta( $post->ID, '_fl_builder_data', true );
  261. foreach ( (array) $meta as $node_id => $node ) {
  262. if ( @isset( $node->type ) && 'module' == $node->type ) { // @codingStandardsIgnoreLine
  263. if ( ! isset( $data['modules'][ $node->settings->type ] ) ) {
  264. $data['modules'][ $node->settings->type ] = 1;
  265. } else {
  266. $data['modules'][ $node->settings->type ] ++;
  267. }
  268. }
  269. }
  270. }
  271. }
  272. // themer settings.
  273. $args = array(
  274. 'post_type' => 'fl-theme-layout',
  275. 'post_status' => 'publish',
  276. 'meta_key' => '_fl_builder_enabled',
  277. 'meta_value' => '1',
  278. 'posts_per_page' => -1,
  279. );
  280. $query = new WP_Query( $args );
  281. $data['themer']['total'] = count( $query->posts );
  282. if ( is_array( $query->posts ) && ! empty( $query->posts ) ) {
  283. foreach ( $query->posts as $post ) {
  284. $meta = get_post_meta( $post->ID );
  285. if ( isset( $meta['_fl_theme_layout_type'] ) ) {
  286. if ( ! isset( $data['themer'][ $meta['_fl_theme_layout_type'][0] ] ) ) {
  287. $data['themer'][ $meta['_fl_theme_layout_type'][0] ] = 1;
  288. } else {
  289. $data['themer'][ $meta['_fl_theme_layout_type'][0] ] ++;
  290. }
  291. }
  292. }
  293. }
  294. /**
  295. * Find all users that are using the builder.
  296. */
  297. $args = array(
  298. 'meta_key' => 'fl_builder_user_settings',
  299. 'meta_value' => 'null',
  300. 'meta_compare' => '!=',
  301. );
  302. $query = new WP_User_Query( $args );
  303. /**
  304. * Using array of users collect their builder settings, pinned, skin etc.
  305. */
  306. if ( ! empty( $query->results ) ) {
  307. foreach ( $query->results as $user ) {
  308. $meta = get_user_meta( $user->ID, 'fl_builder_user_settings', true );
  309. if ( isset( $meta['skin'] ) ) {
  310. if ( ! isset( $data['skin'][ $meta['skin'] ] ) ) {
  311. $data['skin'][ $meta['skin'] ] = 1;
  312. } else {
  313. $data['skin'][ $meta['skin'] ] ++;
  314. }
  315. }
  316. if ( isset( $meta['pinned']['position'] ) ) {
  317. if ( '' == $meta['pinned']['position'] ) {
  318. if ( ! isset( $data['pinned']['unpinned'] ) ) {
  319. $data['pinned']['unpinned'] = 1;
  320. } else {
  321. $data['pinned']['unpinned'] ++;
  322. }
  323. } else {
  324. if ( ! isset( $data['pinned'][ $meta['pinned']['position'] ] ) ) {
  325. $data['pinned'][ $meta['pinned']['position'] ] = 1;
  326. } else {
  327. $data['pinned'][ $meta['pinned']['position'] ] ++;
  328. }
  329. }
  330. }
  331. }
  332. }
  333. /**
  334. * General data
  335. */
  336. $data['server'] = $_SERVER['SERVER_SOFTWARE'];
  337. $data['database'] = ( ! empty( $wpdb->is_mysql ) ? $wpdb->db_version() : 'Unknown' );
  338. $data['multisite'] = is_multisite() ? 'Yes' : 'No';
  339. $data['subsites'] = is_multisite() ? get_blog_count() : '';
  340. $data['locale'] = get_locale();
  341. $data['users'] = $users['total_users'];
  342. $data['php'] = phpversion();
  343. $data['wp'] = $wp_version;
  344. $data['fl-builder'] = FL_BUILDER_VERSION;
  345. $data['fl-theme'] = ( defined( 'FL_THEME_VERSION' ) ) ? FL_THEME_VERSION : false;
  346. $data['fl-themer'] = ( defined( 'FL_THEME_BUILDER_VERSION' ) ) ? FL_THEME_BUILDER_VERSION : false;
  347. $settings_orig = FLBuilderModel::get_global_settings();
  348. $settings = clone $settings_orig;
  349. // we dont need these
  350. unset( $settings->js );
  351. unset( $settings->css );
  352. foreach ( $settings as $k => $setting ) {
  353. $data['settings'][ $k ] = $setting;
  354. }
  355. $theme = wp_get_theme();
  356. if ( $theme->get( 'Template' ) ) {
  357. $parent = wp_get_theme( $theme->get( 'Template' ) );
  358. $data['theme'] = $parent->get( 'Name' );
  359. $data['theme_child'] = $theme->get( 'Name' );
  360. } else {
  361. $data['theme'] = $theme->get( 'Name' );
  362. }
  363. if ( class_exists( 'FLUpdater' ) && false == $demo ) {
  364. $subscription = FLUpdater::get_subscription_info();
  365. if ( ! $subscription->active ) {
  366. $data['license'][] = 'none';
  367. } else {
  368. foreach ( (array) $subscription->subscriptions as $subscription ) {
  369. if ( false !== strpos( $subscription->name, 'Beaver Builder' ) ) {
  370. $data['license']['bb-plugin'] = $subscription->name;
  371. }
  372. if ( 'Beaver Themer Plugin' == $subscription->name ) {
  373. $data['license']['bb-themer'] = $subscription->name;
  374. }
  375. }
  376. }
  377. } else {
  378. $data['license'][] = 'none';
  379. }
  380. $output = array(
  381. 'id' => md5( get_bloginfo( 'url' ) . get_bloginfo( 'admin_email' ) ),
  382. 'data' => $data,
  383. );
  384. return $output;
  385. }
  386. }
  387. FLBuilderUsage::init();