| @@ -47,9 +47,9 @@ | ||
| 47 | 47 | public function init() { |
| 48 | 48 | add_action( 'wpfunnels/offer_accepted', array( $this, 'update_offer_data' ), 10, 2 ); |
| 49 | 49 | add_filter( 'wpfunnels/funnels-overview-data', array($this, 'get_upsell_data'), 10, 3 ); |
| 50 | 50 | add_filter( 'wpfunnels/stat-interval-data', array($this, 'get_upsell_stat_data'), 10, 3 ); |
| 51 | - add_filter( 'wpfunnels/top-performing-funnels-data', array($this, 'get_conversion_data'), 10 ); | |
| 51 | + add_filter( 'wpfunnels/top-performing-funnels-data', array($this, 'get_conversion_data'), 10, 3 ); | |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | |
| 55 | 55 | /** |
| @@ -202,13 +202,13 @@ | ||
| 202 | 202 | * @return mixed |
| 203 | 203 | * |
| 204 | 204 | * @since 2.4.1 |
| 205 | 205 | */ |
| 206 | - public function get_conversion_data( $funnel_data ) { | |
| 206 | + public function get_conversion_data( $funnel_data, $start_date = null, $end_date = null ) { | |
| 207 | 207 | foreach ( $funnel_data as $key => $data ) { |
| 208 | 208 | $funnel_id = $data['id']; |
| 209 | - $views = $this->get_views($funnel_id); | |
| 210 | - $conversion = $this->get_conversion($funnel_id); | |
| 209 | + $views = $this->get_views($funnel_id, $start_date, $end_date); | |
| 210 | + $conversion = $this->get_conversion($funnel_id, $start_date, $end_date); | |
| 211 | 211 | $conversion_rate = $this->get_conversion_rate( $views, $conversion ); |
| 212 | 212 | $funnel_data[$key]['views'] = $views; |
| 213 | 213 | $funnel_data[$key]['conversion'] = $conversion; |
| 214 | 214 | $funnel_data[$key]['conversion_rate'] = $conversion_rate; |
| @@ -217,8 +217,29 @@ | ||
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | |
| 220 | 220 | /** |
| 221 | + * Build a date range clause for a table's date_created column. | |
| 222 | + * | |
| 223 | + * Returns an empty clause when either bound is missing, so callers keep | |
| 224 | + * their previous unscoped behaviour instead of returning nothing. | |
| 225 | + * | |
| 226 | + * @param string $column Fully qualified date column. | |
| 227 | + * @param string $start_date | |
| 228 | + * @param string $end_date | |
| 229 | + * @return array{0:string,1:array} Clause and its bind parameters. | |
| 230 | + * | |
| 231 | + * @since 3.13.0 | |
| 232 | + */ | |
| 233 | + private function get_date_clause( $column, $start_date, $end_date ) { | |
| 234 | + if ( empty( $start_date ) || empty( $end_date ) ) { | |
| 235 | + return array( '', array() ); | |
| 236 | + } | |
| 237 | + return array( " AND $column >= %s AND $column <= %s ", array( $start_date, $end_date ) ); | |
| 238 | + } | |
| 239 | + | |
| 240 | + | |
| 241 | + /** | |
| 221 | 242 | * Retrieves the views for a specific funnel. |
| 222 | 243 | * |
| 223 | 244 | * @param int $funnel_id The ID of the funnel. |
| 224 | 245 | * @return array An array containing the views for the specified funnel. |
| @@ -224,13 +245,16 @@ | ||
| 224 | 245 | * @return array An array containing the views for the specified funnel. |
| 225 | 246 | * |
| 226 | 247 | * @since 2.4.1 |
| 227 | 248 | */ |
| 228 | - public function get_views( $funnel_id ) { | |
| 249 | + public function get_views( $funnel_id, $start_date = null, $end_date = null ) { | |
| 229 | 250 | global $wpdb; |
| 230 | 251 | $table = $wpdb->prefix.'wpfnl_analytics'; |
| 231 | - $sql = "SELECT count(id) as total from $table WHERE funnel_id=%d AND visitor_type='new'"; | |
| 232 | - $count = $wpdb->get_var($wpdb->prepare($sql, $funnel_id)); | |
| 252 | + | |
| 253 | + list( $date_clause, $date_params ) = $this->get_date_clause( 'date_created', $start_date, $end_date ); | |
| 254 | + | |
| 255 | + $sql = "SELECT count(id) as total from $table WHERE funnel_id=%d AND visitor_type='new'" . $date_clause; | |
| 256 | + $count = $wpdb->get_var($wpdb->prepare($sql, array_merge( array( $funnel_id ), $date_params ))); | |
| 233 | 257 | return $count; |
| 234 | 258 | } |
| 235 | 259 | |
| 236 | 260 | |
| @@ -241,23 +265,29 @@ | ||
| 241 | 265 | * @return mixed |
| 242 | 266 | * |
| 243 | 267 | * @since 2.4.1 |
| 244 | 268 | */ |
| 245 | - public function get_conversion($funnel_id) { | |
| 269 | + public function get_conversion($funnel_id, $start_date = null, $end_date = null) { | |
| 246 | 270 | global $wpdb; |
| 247 | 271 | $table = $wpdb->prefix.'wpfnl_stats'; |
| 272 | + | |
| 273 | + list( $date_clause, $date_params ) = $this->get_date_clause( 'o1.paid_date', $start_date, $end_date ); | |
| 274 | + | |
| 248 | 275 | $sql = "SELECT COUNT(DISTINCT o1.order_id) AS total_conversion |
| 249 | 276 | FROM $table o1 |
| 250 | 277 | LEFT JOIN $table o2 ON o1.parent_id = o2.order_id AND o1.funnel_id = o2.funnel_id |
| 251 | 278 | WHERE o2.order_id IS NULL |
| 252 | 279 | AND o1.status='completed' |
| 253 | - AND o1.funnel_id='%d'"; | |
| 254 | - $count = $wpdb->get_var($wpdb->prepare($sql, $funnel_id)); | |
| 280 | + AND o1.funnel_id='%d'" . $date_clause; | |
| 281 | + $count = $wpdb->get_var($wpdb->prepare($sql, array_merge( array( $funnel_id ), $date_params ))); | |
| 255 | 282 | |
| 256 | 283 | if ( !$count ) { |
| 257 | - $optin_entries_table = $wpdb->prefix.'wpfnl_optin_entries'; | |
| 258 | - $optin_conversion_sql = "SELECT COUNT(DISTINCT email) AS total_distinct_emails FROM $optin_entries_table WHERE funnel_id = %d;"; | |
| 259 | - $count = $wpdb->get_var($wpdb->prepare($optin_conversion_sql, $funnel_id)); | |
| 284 | + $optin_entries_table = $wpdb->prefix.'wpfnl_optin_entries'; | |
| 285 | + | |
| 286 | + list( $optin_date_clause, $optin_date_params ) = $this->get_date_clause( 'date_created', $start_date, $end_date ); | |
| 287 | + | |
| 288 | + $optin_conversion_sql = "SELECT COUNT(DISTINCT email) AS total_distinct_emails FROM $optin_entries_table WHERE funnel_id = %d" . $optin_date_clause; | |
| 289 | + $count = $wpdb->get_var($wpdb->prepare($optin_conversion_sql, array_merge( array( $funnel_id ), $optin_date_params ))); | |
| 260 | 290 | } |
| 261 | 291 | return $count; |
| 262 | 292 | } |
| 263 | 293 | |