PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / trunk
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More vtrunk
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 +135 -10 6.3.1 → trunk View file →
@@ -3,14 +3,22 @@
3 3 die( 'You are not allowed to call this page directly.' );
4 4 }
5 5
6 6 /**
7 - * @todo Show opt-in popup after plugin activation.
8 7 * @since 3.06.04
9 8 */
10 9 class FrmUsageController {
11 10
12 11 /**
12 + * Option name of flows data.
13 + *
14 + * @since 6.16.1
15 + *
16 + * @var string
17 + */
18 + const FLOWS_ACTION_NAME = 'frm_usage_tracking_flows';
19 +
20 + /**
13 21 * Randomize the first send to prevent our servers from crashing.
14 22 *
15 23 * @since 3.06.04
16 24 *
@@ -16,17 +24,28 @@
16 24 *
17 25 * @return void
18 26 */
19 27 public static function schedule_send() {
20 - if ( wp_next_scheduled( 'formidable_send_usage' ) ) {
28 + $timestamp = wp_next_scheduled( 'formidable_send_usage' );
29 +
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 +
21 36 return;
22 37 }
23 38
39 + if ( $timestamp ) {
40 + return;
41 + }
42 +
24 43 $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 ),
44 + 'day' => random_int( 0, 6 ) * DAY_IN_SECONDS,
45 + 'hour' => random_int( 0, 23 ) * HOUR_IN_SECONDS,
46 + 'minute' => random_int( 0, 59 ) * MINUTE_IN_SECONDS,
47 + 'second' => random_int( 0, 59 ),
29 48 );
30 49
31 50 $offset = array_sum( $tracking );
32 51 $init_send = strtotime( 'next sunday' ) + $offset;
@@ -36,19 +55,36 @@
36 55
37 56 /**
38 57 * Adds once weekly to the existing schedules.
39 58 *
59 + * WordPress core registers an identical 'weekly' schedule of its own, so this adds nothing.
60 + * The 'cron_schedules' registration is gone, and with it the translated label that made
61 + * this run before the text domain loaded.
62 + *
40 63 * @since 3.06.04
64 + * @deprecated 6.35
65 + *
66 + * @param array $schedules Unused. The registered cron schedules, keyed by schedule name.
67 + *
68 + * @return array
41 69 */
42 70 public static function add_schedules( $schedules = array() ) {
43 - $schedules['weekly'] = array(
44 - 'interval' => DAY_IN_SECONDS * 7,
45 - 'display' => __( 'Once Weekly', 'formidable' ),
46 - );
71 + _deprecated_function( __METHOD__, '6.35' );
47 72 return $schedules;
48 73 }
49 74
50 75 /**
76 + * Checks if tracking is allowed.
77 + *
78 + * @since 6.16.3
79 + *
80 + * @return bool
81 + */
82 + public static function tracking_allowed() {
83 + return (bool) FrmAppHelper::get_settings()->tracking;
84 + }
85 +
86 + /**
51 87 * @since 3.06.04
52 88 *
53 89 * @return void
54 90 */
@@ -54,6 +90,95 @@
54 90 */
55 91 public static function send_snapshot() {
56 92 $usage = new FrmUsage();
57 93 $usage->send_snapshot();
94 + }
95 +
96 + /**
97 + * Loads scripts.
98 + *
99 + * @since 6.16.1
100 + *
101 + * @return void
102 + */
103 + public static function load_scripts() {
104 + if ( self::is_forms_list_page() || FrmAppHelper::is_admin_page( 'formidable-form-templates' ) || FrmAppHelper::is_admin_page() ) {
105 + wp_enqueue_script( 'frm-usage-tracking', FrmAppHelper::plugin_url() . '/js/admin/usage-tracking.js', array( 'formidable_dom' ), FrmAppHelper::$plug_version, true );
106 + }
107 + }
108 +
109 + /**
110 + * Checks if is forms list page.
111 + *
112 + * @since 6.16.1
113 + *
114 + * @return bool
115 + */
116 + private static function is_forms_list_page() {
117 + if ( ! FrmAppHelper::on_form_listing_page() ) {
118 + return false;
119 + }
120 +
121 + // Exclude Trash page.
122 + $form_type = FrmAppHelper::simple_get( 'form_type' );
123 + return 'published' === $form_type;
124 + }
125 +
126 + /**
127 + * AJAX handler to track flows.
128 + *
129 + * @since 6.16.1
130 + *
131 + * @return void
132 + */
133 + public static function ajax_track_flows() {
134 + FrmAppHelper::permission_check( 'frm_view_forms' );
135 + check_ajax_referer( 'frm_ajax', 'nonce' );
136 +
137 + $key = FrmAppHelper::get_post_param( 'key', '', 'sanitize_text_field' );
138 + $value = FrmAppHelper::get_post_param( 'value', '', 'sanitize_text_field' );
139 +
140 + self::update_flows_data( $key, $value );
141 +
142 + wp_send_json_success();
143 + }
144 +
145 + /**
146 + * Updates flows data. This increases the count of flow_data[ $key ][ $value ].
147 + *
148 + * @since 6.16.1
149 + *
150 + * @param string $key Flow key.
151 + * @param string $value Flow value.
152 + *
153 + * @return void
154 + */
155 + public static function update_flows_data( $key, $value ) {
156 + if ( '' === $key || '' === $value ) {
157 + return;
158 + }
159 +
160 + $flows_data = self::get_flows_data();
161 +
162 + if ( ! isset( $flows_data[ $key ] ) ) {
163 + $flows_data[ $key ] = array();
164 + }
165 +
166 + if ( ! isset( $flows_data[ $key ][ $value ] ) ) {
167 + $flows_data[ $key ][ $value ] = 0;
168 + }
169 +
170 + $flows_data[ $key ][ $value ]++;
171 + update_option( self::FLOWS_ACTION_NAME, $flows_data );
172 + }
173 +
174 + /**
175 + * Get flows data.
176 + *
177 + * @since 6.16.1
178 + *
179 + * @return array
180 + */
181 + public static function get_flows_data() {
182 + return get_option( self::FLOWS_ACTION_NAME, array() );
58 183 }
59 184 }