| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sellkit\Contact_Segmentation\Rfm; |
| 4 |
|
| 5 |
use Sellkit\Database; |
| 6 |
|
| 7 |
defined( 'ABSPATH' ) || die(); |
| 8 |
|
| 9 |
/** |
| 10 |
* Class RFM score base class. |
| 11 |
* |
| 12 |
* @package Sellkit\Contact_Segmentation'Rfm |
| 13 |
* @since 1.1.0 |
| 14 |
*/ |
| 15 |
class Rfm_Score { |
| 16 |
|
| 17 |
/** |
| 18 |
* Rfm Update instance. |
| 19 |
* |
| 20 |
* @var Updater Rfm updater. |
| 21 |
* @since 1.1.0 |
| 22 |
*/ |
| 23 |
public static $rfm_updater; |
| 24 |
|
| 25 |
/** |
| 26 |
* Rfm Update instance. |
| 27 |
* |
| 28 |
* @var Updater Rfm updater. |
| 29 |
* @since 1.1.0 |
| 30 |
*/ |
| 31 |
public static $user_updater; |
| 32 |
|
| 33 |
/** |
| 34 |
* Rfm_Score constructor. |
| 35 |
* |
| 36 |
* @since 1.1.0 |
| 37 |
*/ |
| 38 |
public function __construct() { |
| 39 |
sellkit()->load_files( [ |
| 40 |
'contact-segmentation/rfm/updater', |
| 41 |
'contact-segmentation/rfm/user-updater', |
| 42 |
] ); |
| 43 |
|
| 44 |
self::$rfm_updater = new Updater(); |
| 45 |
self::$user_updater = new User_Updater(); |
| 46 |
|
| 47 |
add_action( 'sellkit_update_rfm_score', [ $this, 'update_rfm' ] ); |
| 48 |
|
| 49 |
if ( |
| 50 |
empty( sellkit_get_option( 'contact_segmentation_users_are_imported' ) ) && |
| 51 |
empty( sellkit_get_option( 'contact_segmentation_users_updating_started' ) ) |
| 52 |
) { |
| 53 |
sellkit_update_option( 'contact_segmentation_users_updating_started', true ); |
| 54 |
$this->update_rfm_users(); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Updates RFM data. |
| 60 |
* |
| 61 |
* @since 1.1.0 |
| 62 |
*/ |
| 63 |
public function update_rfm() { |
| 64 |
global $wpdb; |
| 65 |
|
| 66 |
if ( empty( sellkit_get_option( 'contact_segmentation_users_are_imported' ) ) ) { |
| 67 |
$this->update_rfm_users(); |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
$sellkit_prefix = Database::DATABASE_PREFIX; |
| 72 |
|
| 73 |
// phpcs:disable |
| 74 |
$results = $wpdb->get_results( |
| 75 |
"SELECT count(*) as total_contacts, MAX(last_order_date) as max_recency, MIN(last_order_date) as min_recency, |
| 76 |
MAX(total_spent) as max_monetary, MAX(total_order_count) as max_frequency |
| 77 |
from {$wpdb->prefix}{$sellkit_prefix}contact_segmentation" |
| 78 |
); |
| 79 |
// phpcs:enable |
| 80 |
|
| 81 |
if ( is_wp_error( $results ) ) { |
| 82 |
new \WP_Error( __( 'Somethings went wrong', 'sellkit' ) ); |
| 83 |
} |
| 84 |
|
| 85 |
if ( empty( $results[0] ) ) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
$rfm_data = (array) $results[0]; |
| 90 |
$total_contacts = ceil( $rfm_data['total_contacts'] / 100 ); |
| 91 |
|
| 92 |
for ( $i = 100; $i <= ( $total_contacts * 100 ); $i = $i + 100 ) { |
| 93 |
self::$rfm_updater->push_to_queue( |
| 94 |
[ |
| 95 |
'max_number' => $i, |
| 96 |
'max_recency' => $rfm_data['max_recency'], |
| 97 |
'min_recency' => $rfm_data['min_recency'], |
| 98 |
'max_monetary' => $rfm_data['max_monetary'], |
| 99 |
'max_frequency' => $rfm_data['max_frequency'], |
| 100 |
] |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
self::$rfm_updater->save()->dispatch(); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Updates RFM users. |
| 109 |
* |
| 110 |
* @since 1.1.0 |
| 111 |
*/ |
| 112 |
public function update_rfm_users() { |
| 113 |
$emails = get_users( [ 'fields' => [ 'user_email' ] ] ); |
| 114 |
|
| 115 |
foreach ( $emails as $email ) { |
| 116 |
self::$user_updater->push_to_queue( $email->user_email ); |
| 117 |
} |
| 118 |
|
| 119 |
self::$user_updater->save()->dispatch(); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
new Rfm_Score(); |
| 124 |
|