PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.17
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.17
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
← All changes | classes/controllers/FrmUsageController.php +125 -5 6.36.17 View file →
@@ -9,8 +9,17 @@
9 9 */
10 10 class FrmUsageController {
11 11
12 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 + /**
13 22 * Randomize the first send to prevent our servers from crashing.
14 23 *
15 24 * @since 3.06.04
16 25 *
@@ -16,17 +25,26 @@
16 25 *
17 26 * @return void
18 27 */
19 28 public static function schedule_send() {
20 - if ( wp_next_scheduled( 'formidable_send_usage' ) ) {
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 + }
21 35 return;
22 36 }
23 37
38 + if ( $timestamp ) {
39 + return;
40 + }
41 +
24 42 $tracking = array(
25 - 'day' => rand( 0, 6 ) * DAY_IN_SECONDS,
26 - 'hour' => rand( 0, 23 ) * HOUR_IN_SECONDS,
27 - 'minute' => rand( 0, 59 ) * MINUTE_IN_SECONDS,
28 - 'second' => rand( 0, 59 ),
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 ),
29 47 );
30 48
31 49 $offset = array_sum( $tracking );
32 50 $init_send = strtotime( 'next sunday' ) + $offset;
@@ -47,8 +65,20 @@
47 65 return $schedules;
48 66 }
49 67
50 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 + /**
51 81 * @since 3.06.04
52 82 *
53 83 * @return void
54 84 */
@@ -54,6 +84,96 @@
54 84 */
55 85 public static function send_snapshot() {
56 86 $usage = new FrmUsage();
57 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 + if ( ! FrmAppHelper::is_admin_page() ) {
110 + return false;
111 + }
112 +
113 + // Check Trash page.
114 + $form_type = FrmAppHelper::simple_get( 'form_type' );
115 + if ( $form_type && 'published' !== $form_type ) {
116 + return false;
117 + }
118 +
119 + // Check edit or settings page.
120 + return ! FrmAppHelper::simple_get( 'frm_action' );
121 + }
122 +
123 + /**
124 + * AJAX handler to track flows.
125 + *
126 + * @since 6.16.1
127 + */
128 + public static function ajax_track_flows() {
129 + FrmAppHelper::permission_check( 'frm_view_forms' );
130 + check_ajax_referer( 'frm_ajax', 'nonce' );
131 +
132 + $key = FrmAppHelper::get_post_param( 'key', '', 'sanitize_text_field' );
133 + $value = FrmAppHelper::get_post_param( 'value', '', 'sanitize_text_field' );
134 +
135 + self::update_flows_data( $key, $value );
136 +
137 + wp_send_json_success();
138 + }
139 +
140 + /**
141 + * Updates flows data.
142 + *
143 + * @since 6.16.1
144 + *
145 + * @param string $key Flow key.
146 + * @param string $value Flow value.
147 + *
148 + * @return void
149 + */
150 + public static function update_flows_data( $key, $value ) {
151 + $flows_data = self::get_flows_data();
152 +
153 + if ( '' === $key || '' === $value ) {
154 + return;
155 + }
156 +
157 + if ( ! isset( $flows_data[ $key ] ) ) {
158 + $flows_data[ $key ] = array();
159 + }
160 +
161 + if ( ! isset( $flows_data[ $key ][ $value ] ) ) {
162 + $flows_data[ $key ][ $value ] = 0;
163 + }
164 +
165 + $flows_data[ $key ][ $value ]++;
166 + update_option( self::FLOWS_ACTION_NAME, $flows_data );
167 + }
168 +
169 + /**
170 + * Get flows data.
171 + *
172 + * @since 6.16.1
173 + *
174 + * @return array
175 + */
176 + public static function get_flows_data() {
177 + return get_option( self::FLOWS_ACTION_NAME, array() );
58 178 }
59 179 }