PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.19
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.19
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / controllers / FrmUsageController.php

FrmUsageController.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.19, at classes/controllers/FrmUsageController.php

169 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 /**
7 * @todo Show opt-in popup after plugin activation.
8 * @since 3.06.04
9 */
10 class FrmUsageController {
11
12 /**
13 * Option name of flows data.
14 *
15 * @since 6.16.1
16 *
17 * @var string
18 */
19 const FLOWS_ACTION_NAME = 'frm_usage_tracking_flows';
20
21 /**
22 * Randomize the first send to prevent our servers from crashing.
23 *
24 * @since 3.06.04
25 *
26 * @return void
27 */
28 public static function schedule_send() {
29 $timestamp = wp_next_scheduled( 'formidable_send_usage' );
30 if ( ! self::tracking_allowed() ) {
31 if ( $timestamp ) {
32 // Remove the scheduled event if it's not allowed and it's scheduled.
33 wp_unschedule_event( $timestamp, 'formidable_send_usage' );
34 }
35 return;
36 }
37
38 if ( $timestamp ) {
39 return;
40 }
41
42 $tracking = array(
43 'day' => rand( 0, 6 ) * DAY_IN_SECONDS,
44 'hour' => rand( 0, 23 ) * HOUR_IN_SECONDS,
45 'minute' => rand( 0, 59 ) * MINUTE_IN_SECONDS,
46 'second' => rand( 0, 59 ),
47 );
48
49 $offset = array_sum( $tracking );
50 $init_send = strtotime( 'next sunday' ) + $offset;
51
52 wp_schedule_event( $init_send, 'weekly', 'formidable_send_usage' );
53 }
54
55 /**
56 * Adds once weekly to the existing schedules.
57 *
58 * @since 3.06.04
59 */
60 public static function add_schedules( $schedules = array() ) {
61 $schedules['weekly'] = array(
62 'interval' => DAY_IN_SECONDS * 7,
63 'display' => __( 'Once Weekly', 'formidable' ),
64 );
65 return $schedules;
66 }
67
68 /**
69 * Checks if tracking is allowed.
70 *
71 * @since 6.16.3
72 *
73 * @return bool
74 */
75 public static function tracking_allowed() {
76 $settings = FrmAppHelper::get_settings();
77 return $settings->tracking;
78 }
79
80 /**
81 * @since 3.06.04
82 *
83 * @return void
84 */
85 public static function send_snapshot() {
86 $usage = new FrmUsage();
87 $usage->send_snapshot();
88 }
89
90 /**
91 * Loads scripts.
92 *
93 * @since 6.16.1
94 */
95 public static function load_scripts() {
96 if ( self::is_forms_list_page() || FrmAppHelper::is_admin_page( 'formidable-form-templates' ) ) {
97 wp_enqueue_script( 'frm-usage-tracking', FrmAppHelper::plugin_url() . '/js/admin/usage-tracking.js', array( 'formidable_dom' ), FrmAppHelper::$plug_version, true );
98 }
99 }
100
101 /**
102 * Checks if is forms list page.
103 *
104 * @since 6.16.1
105 *
106 * @return bool
107 */
108 private static function is_forms_list_page() {
109 return FrmAppHelper::is_admin_list_page();
110 }
111
112 /**
113 * AJAX handler to track flows.
114 *
115 * @since 6.16.1
116 */
117 public static function ajax_track_flows() {
118 FrmAppHelper::permission_check( 'frm_view_forms' );
119 check_ajax_referer( 'frm_ajax', 'nonce' );
120
121 $key = FrmAppHelper::get_post_param( 'key', '', 'sanitize_text_field' );
122 $value = FrmAppHelper::get_post_param( 'value', '', 'sanitize_text_field' );
123
124 self::update_flows_data( $key, $value );
125
126 wp_send_json_success();
127 }
128
129 /**
130 * Updates flows data.
131 *
132 * @since 6.16.1
133 *
134 * @param string $key Flow key.
135 * @param string $value Flow value.
136 *
137 * @return void
138 */
139 public static function update_flows_data( $key, $value ) {
140 $flows_data = self::get_flows_data();
141
142 if ( '' === $key || '' === $value ) {
143 return;
144 }
145
146 if ( ! isset( $flows_data[ $key ] ) ) {
147 $flows_data[ $key ] = array();
148 }
149
150 if ( ! isset( $flows_data[ $key ][ $value ] ) ) {
151 $flows_data[ $key ][ $value ] = 0;
152 }
153
154 $flows_data[ $key ][ $value ]++;
155 update_option( self::FLOWS_ACTION_NAME, $flows_data );
156 }
157
158 /**
159 * Get flows data.
160 *
161 * @since 6.16.1
162 *
163 * @return array
164 */
165 public static function get_flows_data() {
166 return get_option( self::FLOWS_ACTION_NAME, array() );
167 }
168 }
169