| @@ -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; |
| @@ -37,8 +56,12 @@ | ||
| 37 | 56 | /** |
| 38 | 57 | * Adds once weekly to the existing schedules. |
| 39 | 58 | * |
| 40 | 59 | * @since 3.06.04 |
| 60 | + * | |
| 61 | + * @param array $schedules Schedules. | |
| 62 | + * | |
| 63 | + * @return array | |
| 41 | 64 | */ |
| 42 | 65 | public static function add_schedules( $schedules = array() ) { |
| 43 | 66 | $schedules['weekly'] = array( |
| 44 | 67 | 'interval' => DAY_IN_SECONDS * 7, |
| @@ -47,8 +70,19 @@ | ||
| 47 | 70 | return $schedules; |
| 48 | 71 | } |
| 49 | 72 | |
| 50 | 73 | /** |
| 74 | + * Checks if tracking is allowed. | |
| 75 | + * | |
| 76 | + * @since 6.16.3 | |
| 77 | + * | |
| 78 | + * @return bool | |
| 79 | + */ | |
| 80 | + public static function tracking_allowed() { | |
| 81 | + return (bool) FrmAppHelper::get_settings()->tracking; | |
| 82 | + } | |
| 83 | + | |
| 84 | + /** | |
| 51 | 85 | * @since 3.06.04 |
| 52 | 86 | * |
| 53 | 87 | * @return void |
| 54 | 88 | */ |
| @@ -54,6 +88,95 @@ | ||
| 54 | 88 | */ |
| 55 | 89 | public static function send_snapshot() { |
| 56 | 90 | $usage = new FrmUsage(); |
| 57 | 91 | $usage->send_snapshot(); |
| 92 | + } | |
| 93 | + | |
| 94 | + /** | |
| 95 | + * Loads scripts. | |
| 96 | + * | |
| 97 | + * @since 6.16.1 | |
| 98 | + * | |
| 99 | + * @return void | |
| 100 | + */ | |
| 101 | + public static function load_scripts() { | |
| 102 | + if ( self::is_forms_list_page() || FrmAppHelper::is_admin_page( 'formidable-form-templates' ) || FrmAppHelper::is_admin_page() ) { | |
| 103 | + wp_enqueue_script( 'frm-usage-tracking', FrmAppHelper::plugin_url() . '/js/admin/usage-tracking.js', array( 'formidable_dom' ), FrmAppHelper::$plug_version, true ); | |
| 104 | + } | |
| 105 | + } | |
| 106 | + | |
| 107 | + /** | |
| 108 | + * Checks if is forms list page. | |
| 109 | + * | |
| 110 | + * @since 6.16.1 | |
| 111 | + * | |
| 112 | + * @return bool | |
| 113 | + */ | |
| 114 | + private static function is_forms_list_page() { | |
| 115 | + if ( ! FrmAppHelper::on_form_listing_page() ) { | |
| 116 | + return false; | |
| 117 | + } | |
| 118 | + | |
| 119 | + // Exclude Trash page. | |
| 120 | + $form_type = FrmAppHelper::simple_get( 'form_type' ); | |
| 121 | + return 'published' === $form_type; | |
| 122 | + } | |
| 123 | + | |
| 124 | + /** | |
| 125 | + * AJAX handler to track flows. | |
| 126 | + * | |
| 127 | + * @since 6.16.1 | |
| 128 | + * | |
| 129 | + * @return void | |
| 130 | + */ | |
| 131 | + public static function ajax_track_flows() { | |
| 132 | + FrmAppHelper::permission_check( 'frm_view_forms' ); | |
| 133 | + check_ajax_referer( 'frm_ajax', 'nonce' ); | |
| 134 | + | |
| 135 | + $key = FrmAppHelper::get_post_param( 'key', '', 'sanitize_text_field' ); | |
| 136 | + $value = FrmAppHelper::get_post_param( 'value', '', 'sanitize_text_field' ); | |
| 137 | + | |
| 138 | + self::update_flows_data( $key, $value ); | |
| 139 | + | |
| 140 | + wp_send_json_success(); | |
| 141 | + } | |
| 142 | + | |
| 143 | + /** | |
| 144 | + * Updates flows data. | |
| 145 | + * | |
| 146 | + * @since 6.16.1 | |
| 147 | + * | |
| 148 | + * @param string $key Flow key. | |
| 149 | + * @param string $value Flow value. | |
| 150 | + * | |
| 151 | + * @return void | |
| 152 | + */ | |
| 153 | + public static function update_flows_data( $key, $value ) { | |
| 154 | + if ( '' === $key || '' === $value ) { | |
| 155 | + return; | |
| 156 | + } | |
| 157 | + | |
| 158 | + $flows_data = self::get_flows_data(); | |
| 159 | + | |
| 160 | + if ( ! isset( $flows_data[ $key ] ) ) { | |
| 161 | + $flows_data[ $key ] = array(); | |
| 162 | + } | |
| 163 | + | |
| 164 | + if ( ! isset( $flows_data[ $key ][ $value ] ) ) { | |
| 165 | + $flows_data[ $key ][ $value ] = 0; | |
| 166 | + } | |
| 167 | + | |
| 168 | + $flows_data[ $key ][ $value ]++; | |
| 169 | + update_option( self::FLOWS_ACTION_NAME, $flows_data ); | |
| 170 | + } | |
| 171 | + | |
| 172 | + /** | |
| 173 | + * Get flows data. | |
| 174 | + * | |
| 175 | + * @since 6.16.1 | |
| 176 | + * | |
| 177 | + * @return array | |
| 178 | + */ | |
| 179 | + public static function get_flows_data() { | |
| 180 | + return get_option( self::FLOWS_ACTION_NAME, array() ); | |
| 58 | 181 | } |
| 59 | 182 | } |